mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
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>
This commit is contained in:
1 parent
c5edeb9776
commit
d4e397a900
6 files changed
+83
-9
No files matched your search
@@ -1,7 +1,9 @@
|
||||
#include "sharedarraybuffer_metadata.h"
|
||||
|
||||
#include "base_object-inl.h"
|
||||
#include "memory_tracker-inl.h"
|
||||
#include "node_errors.h"
|
||||
#include "node_worker.h"
|
||||
#include "util-inl.h"
|
||||
|
||||
#include <utility>
|
||||
@@ -91,8 +93,16 @@ SharedArrayBufferMetadata::ForSharedArrayBuffer(
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// If the SharedArrayBuffer is coming from a Worker, we need to make sure
|
||||
// that the corresponding ArrayBuffer::Allocator lives at least as long as
|
||||
// the SharedArrayBuffer itself.
|
||||
worker::Worker* w = env->worker_context();
|
||||
std::shared_ptr<v8::ArrayBuffer::Allocator> allocator =
|
||||
w != nullptr ? w->array_buffer_allocator() : nullptr;
|
||||
|
||||
SharedArrayBuffer::Contents contents = source->Externalize();
|
||||
SharedArrayBufferMetadataReference r(new SharedArrayBufferMetadata(contents));
|
||||
SharedArrayBufferMetadataReference r(
|
||||
new SharedArrayBufferMetadata(contents, allocator));
|
||||
if (r->AssignToSharedArrayBuffer(env, context, source).IsNothing())
|
||||
return nullptr;
|
||||
return r;
|
||||
@@ -114,8 +124,9 @@ Maybe<bool> SharedArrayBufferMetadata::AssignToSharedArrayBuffer(
|
||||
}
|
||||
|
||||
SharedArrayBufferMetadata::SharedArrayBufferMetadata(
|
||||
const SharedArrayBuffer::Contents& contents)
|
||||
: contents_(contents) { }
|
||||
const SharedArrayBuffer::Contents& contents,
|
||||
std::shared_ptr<v8::ArrayBuffer::Allocator> allocator)
|
||||
: contents_(contents), allocator_(allocator) { }
|
||||
|
||||
SharedArrayBufferMetadata::~SharedArrayBufferMetadata() {
|
||||
contents_.Deleter()(contents_.Data(),
|
||||
|
||||
Reference in new issue
Block a user