diff options
author | Anna Henningsen <anna@addaleax.net> | 2019-08-18 00:05:48 +0200 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-08-20 00:14:10 +0200 |
commit | 119c4ccf0ef2addc84fd638004a631f55c7aefc9 (patch) | |
tree | 03e2889956067200c5d84285a16288707a415ab3 /test | |
parent | bdf07f4317ce738bbe56c5b91aaebf35d15052a8 (diff) | |
download | android-node-v8-119c4ccf0ef2addc84fd638004a631f55c7aefc9.tar.gz android-node-v8-119c4ccf0ef2addc84fd638004a631f55c7aefc9.tar.bz2 android-node-v8-119c4ccf0ef2addc84fd638004a631f55c7aefc9.zip |
worker: fix crash when SharedArrayBuffer outlives creating thread
Keep a reference to the `ArrayBuffer::Allocator` alive for at least
as long as a `SharedArrayBuffer` allocated by it lives.
Refs: https://github.com/nodejs/node/pull/28788
Fixes: https://github.com/nodejs/node/issues/28777
Fixes: https://github.com/nodejs/node/issues/28773
PR-URL: https://github.com/nodejs/node/pull/29190
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/parallel/test-worker-arraybuffer-zerofill.js | 33 | ||||
-rw-r--r-- | test/parallel/test-worker-sharedarraybuffer-from-worker-thread.js | 22 |
2 files changed, 55 insertions, 0 deletions
diff --git a/test/parallel/test-worker-arraybuffer-zerofill.js b/test/parallel/test-worker-arraybuffer-zerofill.js new file mode 100644 index 0000000000..3dcf4c006e --- /dev/null +++ b/test/parallel/test-worker-arraybuffer-zerofill.js @@ -0,0 +1,33 @@ +'use strict'; +require('../common'); +const assert = require('assert'); +const { Worker } = require('worker_threads'); + +// Make sure that allocating uninitialized ArrayBuffers in one thread does not +// affect the zero-initialization in other threads. + +const w = new Worker(` +const { parentPort } = require('worker_threads'); + +function post() { + const uint32array = new Uint32Array(64); + parentPort.postMessage(uint32array.reduce((a, b) => a + b)); +} + +setInterval(post, 0); +`, { eval: true }); + +function allocBuffers() { + Buffer.allocUnsafe(32 * 1024 * 1024); +} + +const interval = setInterval(allocBuffers, 0); + +let messages = 0; +w.on('message', (sum) => { + assert.strictEqual(sum, 0); + if (messages++ === 100) { + clearInterval(interval); + w.terminate(); + } +}); diff --git a/test/parallel/test-worker-sharedarraybuffer-from-worker-thread.js b/test/parallel/test-worker-sharedarraybuffer-from-worker-thread.js new file mode 100644 index 0000000000..60e8a5d52a --- /dev/null +++ b/test/parallel/test-worker-sharedarraybuffer-from-worker-thread.js @@ -0,0 +1,22 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const { Worker } = require('worker_threads'); + +// Regression test for https://github.com/nodejs/node/issues/28777 +// Make sure that SharedArrayBuffers created in Worker threads are accessible +// after the creating thread ended. + +const w = new Worker(` +const { parentPort } = require('worker_threads'); +const sharedArrayBuffer = new SharedArrayBuffer(4); +parentPort.postMessage(sharedArrayBuffer); +`, { eval: true }); + +let sharedArrayBuffer; +w.once('message', common.mustCall((message) => sharedArrayBuffer = message)); +w.once('exit', common.mustCall(() => { + const uint8array = new Uint8Array(sharedArrayBuffer); + uint8array[0] = 42; + assert.deepStrictEqual(uint8array, new Uint8Array([42, 0, 0, 0])); +})); |