diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js index 80092bf74e1676..6401532c6d9b38 100644 --- a/lib/internal/bootstrap/pre_execution.js +++ b/lib/internal/bootstrap/pre_execution.js @@ -16,7 +16,6 @@ const { getEmbedderOptions, refreshOptions, } = require('internal/options'); -const { reconnectZeroFillToggle } = require('internal/buffer'); const { defineOperation, emitExperimentalWarning, @@ -30,12 +29,6 @@ const assert = require('internal/assert'); function prepareMainThreadExecution(expandArgv1 = false, initialzeModules = true) { - refreshRuntimeOptions(); - - // TODO(joyeecheung): this is also necessary for workers when they deserialize - // this toggle from the snapshot. - reconnectZeroFillToggle(); - // Patch the process object with legacy properties and normalizations patchProcessObject(expandArgv1); setupTraceCategoryState(); diff --git a/lib/internal/buffer.js b/lib/internal/buffer.js index bd38cf48a7fc6e..1aa071cdc071dc 100644 --- a/lib/internal/buffer.js +++ b/lib/internal/buffer.js @@ -30,7 +30,7 @@ const { hexWrite, ucs2Write, utf8Write, - getZeroFillToggle + setZeroFillToggle } = internalBinding('buffer'); const { untransferable_object_private_symbol, @@ -1055,24 +1055,15 @@ function markAsUntransferable(obj) { // in C++. // |zeroFill| can be undefined when running inside an isolate where we // do not own the ArrayBuffer allocator. Zero fill is always on in that case. -let zeroFill = getZeroFillToggle(); function createUnsafeBuffer(size) { - zeroFill[0] = 0; + setZeroFillToggle(false); try { return new FastBuffer(size); } finally { - zeroFill[0] = 1; + setZeroFillToggle(true) } } -// The connection between the JS land zero fill toggle and the -// C++ one in the NodeArrayBufferAllocator gets lost if the toggle -// is deserialized from the snapshot, because V8 owns the underlying -// memory of this toggle. This resets the connection. -function reconnectZeroFillToggle() { - zeroFill = getZeroFillToggle(); -} - module.exports = { FastBuffer, addBufferPrototypeMethods, @@ -1080,5 +1071,4 @@ module.exports = { createUnsafeBuffer, readUInt16BE, readUInt32BE, - reconnectZeroFillToggle }; diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 5b2186feb8c707..d091b14a0e155e 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -1186,33 +1186,14 @@ void SetBufferPrototype(const FunctionCallbackInfo& args) { env->set_buffer_prototype_object(proto); } -void GetZeroFillToggle(const FunctionCallbackInfo& args) { +void SetZeroFillToggle(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); NodeArrayBufferAllocator* allocator = env->isolate_data()->node_allocator(); Local ab; - // It can be a nullptr when running inside an isolate where we - // do not own the ArrayBuffer allocator. - if (allocator == nullptr) { - // Create a dummy Uint32Array - the JS land can only toggle the C++ land - // setting when the allocator uses our toggle. With this the toggle in JS - // land results in no-ops. - ab = ArrayBuffer::New(env->isolate(), sizeof(uint32_t)); - } else { + if (allocator != nullptr) { uint32_t* zero_fill_field = allocator->zero_fill_field(); - std::unique_ptr backing = - ArrayBuffer::NewBackingStore(zero_fill_field, - sizeof(*zero_fill_field), - [](void*, size_t, void*) {}, - nullptr); - ab = ArrayBuffer::New(env->isolate(), std::move(backing)); + *zero_fill_field = args[0]->BooleanValue(env->isolate()); } - - ab->SetPrivate( - env->context(), - env->untransferable_object_private_symbol(), - True(env->isolate())).Check(); - - args.GetReturnValue().Set(Uint32Array::New(ab, 0, 1)); } void DetachArrayBuffer(const FunctionCallbackInfo& args) { @@ -1321,7 +1302,7 @@ void Initialize(Local target, env->SetMethod(target, "ucs2Write", StringWrite); env->SetMethod(target, "utf8Write", StringWrite); - env->SetMethod(target, "getZeroFillToggle", GetZeroFillToggle); + env->SetMethod(target, "setZeroFillToggle", SetZeroFillToggle); } } // anonymous namespace @@ -1361,7 +1342,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { registry->Register(StringWrite); registry->Register(StringWrite); registry->Register(StringWrite); - registry->Register(GetZeroFillToggle); + registry->Register(SetZeroFillToggle); registry->Register(DetachArrayBuffer); registry->Register(CopyArrayBuffer);