Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: simplify toggling the buffer zero-fill flag #43537

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const {
getEmbedderOptions,
refreshOptions,
} = require('internal/options');
const { reconnectZeroFillToggle } = require('internal/buffer');
const {
defineOperation,
emitExperimentalWarning,
Expand All @@ -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();
Expand Down
16 changes: 3 additions & 13 deletions lib/internal/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const {
hexWrite,
ucs2Write,
utf8Write,
getZeroFillToggle
setZeroFillToggle
} = internalBinding('buffer');
const {
untransferable_object_private_symbol,
Expand Down Expand Up @@ -1055,30 +1055,20 @@ 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,
markAsUntransferable,
createUnsafeBuffer,
readUInt16BE,
readUInt32BE,
reconnectZeroFillToggle
};
29 changes: 5 additions & 24 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1186,33 +1186,14 @@ void SetBufferPrototype(const FunctionCallbackInfo<Value>& args) {
env->set_buffer_prototype_object(proto);
}

void GetZeroFillToggle(const FunctionCallbackInfo<Value>& args) {
void SetZeroFillToggle(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
NodeArrayBufferAllocator* allocator = env->isolate_data()->node_allocator();
Local<ArrayBuffer> 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<BackingStore> 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<Value>& args) {
Expand Down Expand Up @@ -1321,7 +1302,7 @@ void Initialize(Local<Object> target,
env->SetMethod(target, "ucs2Write", StringWrite<UCS2>);
env->SetMethod(target, "utf8Write", StringWrite<UTF8>);

env->SetMethod(target, "getZeroFillToggle", GetZeroFillToggle);
env->SetMethod(target, "setZeroFillToggle", SetZeroFillToggle);
}

} // anonymous namespace
Expand Down Expand Up @@ -1361,7 +1342,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(StringWrite<HEX>);
registry->Register(StringWrite<UCS2>);
registry->Register(StringWrite<UTF8>);
registry->Register(GetZeroFillToggle);
registry->Register(SetZeroFillToggle);

registry->Register(DetachArrayBuffer);
registry->Register(CopyArrayBuffer);
Expand Down