Skip to content

Commit f35b26d

Browse files
committed
fixup! support native IPC serialization in V8 sandbox
Allocate ValueSerializer buffers as V8 backing stores and retain the original backing store when adopting the released buffer. This preserves the shared allocator lifetime when serialized buffers are transferred from a worker and outlive its isolate.
1 parent b8f475c commit f35b26d

1 file changed

Lines changed: 86 additions & 36 deletions

File tree

‎patches/node/support_v8_sandboxed_pointers.patch‎

Lines changed: 86 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,10 @@ index 00fcd4b6afccce47ff21c4447d9cd60f25c11835..8516615bb73813ddc96eb5ab34028874
320320

321321
diff --git a/src/node_serdes_utils.h b/src/node_serdes_utils.h
322322
new file mode 100644
323-
index 0000000000000000000000000000000000000000..18bbbeb18c6b5fdb826745aafe84f003690643f0
323+
index 0000000000000000000000000000000000000000..44bc180778a23ce611b570c91242a1b10c28a058
324324
--- /dev/null
325325
+++ b/src/node_serdes_utils.h
326-
@@ -0,0 +1,94 @@
326+
@@ -0,0 +1,100 @@
327327
+#ifndef SRC_NODE_SERDES_UTILS_H_
328328
+#define SRC_NODE_SERDES_UTILS_H_
329329
+
@@ -343,73 +343,79 @@ index 0000000000000000000000000000000000000000..18bbbeb18c6b5fdb826745aafe84f003
343343
+ public:
344344
+ explicit SerializerDelegate(v8::Isolate* isolate,
345345
+ size_t minimum_buffer_capacity = 0)
346-
+ : allocator_(isolate->GetArrayBufferAllocator()),
346+
+ : isolate_(isolate),
347347
+ minimum_buffer_capacity_(minimum_buffer_capacity) {}
348348
+
349349
+ void* ReallocateBufferMemory(void* old_buffer,
350350
+ size_t requested_size,
351351
+ size_t* actual_size) override {
352352
+ const size_t new_capacity =
353353
+ std::max(minimum_buffer_capacity_, requested_size);
354-
+ void* new_buffer = old_buffer == nullptr
355-
+ ? allocator_->Allocate(new_capacity)
356-
+ : allocator_->AllocateUninitialized(new_capacity);
357-
+ if (new_buffer == nullptr) return nullptr;
354+
+ const auto initialization_mode =
355+
+ old_buffer == nullptr
356+
+ ? v8::BackingStoreInitializationMode::kZeroInitialized
357+
+ : v8::BackingStoreInitializationMode::kUninitialized;
358+
+ std::unique_ptr<v8::BackingStore> new_backing_store =
359+
+ v8::ArrayBuffer::NewBackingStore(
360+
+ isolate_,
361+
+ new_capacity,
362+
+ initialization_mode,
363+
+ v8::BackingStoreOnFailureMode::kReturnNull);
364+
+ if (new_backing_store == nullptr) return nullptr;
358365
+
359366
+ if (old_buffer != nullptr) {
360-
+ const size_t bytes_to_copy =
361-
+ std::min(buffer_capacity_, new_capacity);
362-
+ memcpy(new_buffer,
367+
+ CHECK_NOT_NULL(backing_store_);
368+
+ CHECK_EQ(old_buffer, backing_store_->Data());
369+
+ memcpy(new_backing_store->Data(),
363370
+ old_buffer,
364-
+ bytes_to_copy);
365-
+ if (new_capacity > bytes_to_copy) {
366-
+ memset(static_cast<uint8_t*>(new_buffer) + bytes_to_copy,
371+
+ std::min(backing_store_->ByteLength(), new_capacity));
372+
+ if (new_capacity > backing_store_->ByteLength()) {
373+
+ memset(static_cast<uint8_t*>(new_backing_store->Data()) +
374+
+ backing_store_->ByteLength(),
367375
+ 0,
368-
+ new_capacity - bytes_to_copy);
376+
+ new_capacity - backing_store_->ByteLength());
369377
+ }
370-
+ allocator_->Free(old_buffer, buffer_capacity_);
371378
+ }
372379
+
373-
+ buffer_capacity_ = new_capacity;
380+
+ backing_store_ = std::move(new_backing_store);
374381
+ *actual_size = new_capacity;
375-
+ return new_buffer;
382+
+ return backing_store_->Data();
376383
+ }
377384
+
378385
+ void FreeBufferMemory(void* buffer) override {
379-
+ const size_t capacity = std::exchange(buffer_capacity_, 0);
380-
+ allocator_->Free(buffer, capacity);
386+
+ CHECK_NOT_NULL(backing_store_);
387+
+ CHECK_EQ(buffer, backing_store_->Data());
388+
+ backing_store_.reset();
381389
+ }
382390
+
383391
+ v8::Local<v8::ArrayBuffer> AdoptReleasedBuffer(
384392
+ v8::Isolate* isolate,
385393
+ std::pair<uint8_t*, size_t> buffer) {
386-
+ auto deleter_data = std::make_unique<BackingStoreDeleterData>(
387-
+ BackingStoreDeleterData{
388-
+ allocator_, std::exchange(buffer_capacity_, 0)});
394+
+ CHECK_EQ(isolate, isolate_);
395+
+ if (buffer.first == nullptr) {
396+
+ CHECK_EQ(buffer.second, 0);
397+
+ CHECK_NULL(backing_store_);
398+
+ return v8::ArrayBuffer::New(isolate, 0);
399+
+ }
400+
+
401+
+ CHECK_NOT_NULL(backing_store_);
402+
+ CHECK_EQ(buffer.first, backing_store_->Data());
389403
+ auto backing_store = v8::ArrayBuffer::NewBackingStore(
390404
+ buffer.first,
391405
+ buffer.second,
392406
+ [](void* data, size_t, void* deleter_data) {
393-
+ std::unique_ptr<BackingStoreDeleterData> state(
394-
+ static_cast<BackingStoreDeleterData*>(deleter_data));
395-
+ if (data != nullptr) {
396-
+ state->allocator->Free(data, state->capacity);
397-
+ }
407+
+ std::unique_ptr<v8::BackingStore> allocation(
408+
+ static_cast<v8::BackingStore*>(deleter_data));
409+
+ CHECK_EQ(data, allocation->Data());
398410
+ },
399-
+ deleter_data.get());
400-
+ deleter_data.release();
411+
+ backing_store_.release());
401412
+ return v8::ArrayBuffer::New(isolate, std::move(backing_store));
402413
+ }
403414
+
404415
+ private:
405-
+ struct BackingStoreDeleterData {
406-
+ v8::ArrayBuffer::Allocator* allocator;
407-
+ size_t capacity;
408-
+ };
409-
+
410-
+ v8::ArrayBuffer::Allocator* allocator_;
416+
+ v8::Isolate* isolate_;
411417
+ const size_t minimum_buffer_capacity_;
412-
+ size_t buffer_capacity_ = 0;
418+
+ std::unique_ptr<v8::BackingStore> backing_store_;
413419
+};
414420
+
415421
+} // namespace serdes
@@ -452,3 +458,47 @@ index 5e01f64a5039235447101d0d8cfbdba3da4ddbc1..49c457cae2b3da603a548874f63dbdf9
452458
auto ab = ArrayBuffer::New(isolate, std::move(bs));
453459
v8::Local<Uint8Array> u8 = v8::Uint8Array::New(ab, 0, 1);
454460

461+
diff --git a/test/parallel/test-worker-sharedarraybuffer-from-worker-thread.js b/test/parallel/test-worker-sharedarraybuffer-from-worker-thread.js
462+
index ce8410f6dd3d7531b12db2af5608085015a4bf07..e081a8efbaa62e240a27c26a7ec93c83f4e43da1 100644
463+
--- a/test/parallel/test-worker-sharedarraybuffer-from-worker-thread.js
464+
+++ b/test/parallel/test-worker-sharedarraybuffer-from-worker-thread.js
465+
@@ -3,6 +3,7 @@
466+
const common = require('../common');
467+
const assert = require('assert');
468+
const { Worker } = require('worker_threads');
469+
+const v8 = require('v8');
470+
471+
// Regression test for https://github.com/nodejs/node/issues/28777
472+
// Make sure that SharedArrayBuffers and transferred ArrayBuffers created in
473+
@@ -26,3 +27,31 @@ for (const ctor of ['ArrayBuffer', 'SharedArrayBuffer']) {
474+
assert.deepStrictEqual(uint8array, new Uint8Array([42, 0, 0, 0]));
475+
}));
476+
}
477+
+
478+
+// Serialized buffers transferred from workers must retain their allocator.
479+
+{
480+
+ const expected = { payload: 'x'.repeat(1024 * 1024) };
481+
+ const worker = new Worker(`
482+
+ const assert = require('assert');
483+
+ const v8 = require('v8');
484+
+ const { parentPort } = require('worker_threads');
485+
+
486+
+ const serialized =
487+
+ v8.serialize({ payload: 'x'.repeat(1024 * 1024) });
488+
+
489+
+ parentPort.postMessage(serialized, [serialized.buffer]);
490+
+ assert.strictEqual(serialized.byteLength, 0);
491+
+ `, { eval: true });
492+
+
493+
+ let serialized;
494+
+
495+
+ worker.once('message', common.mustCall((message) => {
496+
+ serialized = message;
497+
+ }));
498+
+
499+
+ worker.once('exit', common.mustCall((code) => {
500+
+ assert.strictEqual(code, 0);
501+
+ assert(serialized);
502+
+ assert.deepStrictEqual(v8.deserialize(serialized), expected);
503+
+ }));
504+
+}

0 commit comments

Comments
 (0)