From 2717fe6ce0a35faafac885a80fefba8fb8df6846 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sat, 7 Aug 2021 14:04:03 +0530 Subject: [PATCH] [v16.x backport] src: return Maybe from InitializeContextRuntime() Signed-off-by: Darshan Sen PR-URL: https://github.com/nodejs/node/pull/39695 Backport-PR-URL: https://github.com/nodejs/node/pull/39834 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- src/api/environment.cc | 123 ++++++++++++++++++++++++++++---------- src/node_contextify.cc | 4 +- src/node_internals.h | 2 +- src/node_main_instance.cc | 2 +- 4 files changed, 95 insertions(+), 36 deletions(-) diff --git a/src/api/environment.cc b/src/api/environment.cc index b7e213602b57d5..0fb750c5abbe00 100644 --- a/src/api/environment.cc +++ b/src/api/environment.cc @@ -20,8 +20,11 @@ using v8::Function; using v8::FunctionCallbackInfo; using v8::HandleScope; using v8::Isolate; +using v8::Just; using v8::Local; +using v8::Maybe; using v8::MaybeLocal; +using v8::Nothing; using v8::Null; using v8::Object; using v8::ObjectTemplate; @@ -523,58 +526,113 @@ void ProtoThrower(const FunctionCallbackInfo& info) { // This runs at runtime, regardless of whether the context // is created from a snapshot. -void InitializeContextRuntime(Local context) { +Maybe InitializeContextRuntime(Local context) { Isolate* isolate = context->GetIsolate(); HandleScope handle_scope(isolate); // Delete `Intl.v8BreakIterator` // https://github.com/nodejs/node/issues/14909 - Local intl_string = FIXED_ONE_BYTE_STRING(isolate, "Intl"); - Local break_iter_string = - FIXED_ONE_BYTE_STRING(isolate, "v8BreakIterator"); - Local intl_v; - if (context->Global()->Get(context, intl_string).ToLocal(&intl_v) && - intl_v->IsObject()) { - Local intl = intl_v.As(); - intl->Delete(context, break_iter_string).Check(); + { + Local intl_string = + FIXED_ONE_BYTE_STRING(isolate, "Intl"); + Local break_iter_string = + FIXED_ONE_BYTE_STRING(isolate, "v8BreakIterator"); + + Local intl_v; + if (!context->Global() + ->Get(context, intl_string) + .ToLocal(&intl_v)) { + return Nothing(); + } + + if (intl_v->IsObject() && + intl_v.As() + ->Delete(context, break_iter_string) + .IsNothing()) { + return Nothing(); + } } // Delete `Atomics.wake` // https://github.com/nodejs/node/issues/21219 - Local atomics_string = FIXED_ONE_BYTE_STRING(isolate, "Atomics"); - Local wake_string = FIXED_ONE_BYTE_STRING(isolate, "wake"); - Local atomics_v; - if (context->Global()->Get(context, atomics_string).ToLocal(&atomics_v) && - atomics_v->IsObject()) { - Local atomics = atomics_v.As(); - atomics->Delete(context, wake_string).Check(); + { + Local atomics_string = + FIXED_ONE_BYTE_STRING(isolate, "Atomics"); + Local wake_string = + FIXED_ONE_BYTE_STRING(isolate, "wake"); + + Local atomics_v; + if (!context->Global() + ->Get(context, atomics_string) + .ToLocal(&atomics_v)) { + return Nothing(); + } + + if (atomics_v->IsObject() && + atomics_v.As() + ->Delete(context, wake_string) + .IsNothing()) { + return Nothing(); + } } // Remove __proto__ // https://github.com/nodejs/node/issues/31951 - Local object_string = FIXED_ONE_BYTE_STRING(isolate, "Object"); - Local prototype_string = FIXED_ONE_BYTE_STRING(isolate, "prototype"); - Local prototype = context->Global() - ->Get(context, object_string) - .ToLocalChecked() - .As() - ->Get(context, prototype_string) - .ToLocalChecked() - .As(); - Local proto_string = FIXED_ONE_BYTE_STRING(isolate, "__proto__"); + Local prototype; + { + Local object_string = + FIXED_ONE_BYTE_STRING(isolate, "Object"); + Local prototype_string = + FIXED_ONE_BYTE_STRING(isolate, "prototype"); + + Local object_v; + if (!context->Global() + ->Get(context, object_string) + .ToLocal(&object_v)) { + return Nothing(); + } + + Local prototype_v; + if (!object_v.As() + ->Get(context, prototype_string) + .ToLocal(&prototype_v)) { + return Nothing(); + } + + prototype = prototype_v.As(); + } + + Local proto_string = + FIXED_ONE_BYTE_STRING(isolate, "__proto__"); + if (per_process::cli_options->disable_proto == "delete") { - prototype->Delete(context, proto_string).ToChecked(); + if (prototype + ->Delete(context, proto_string) + .IsNothing()) { + return Nothing(); + } } else if (per_process::cli_options->disable_proto == "throw") { - Local thrower = - Function::New(context, ProtoThrower).ToLocalChecked(); + Local thrower; + if (!Function::New(context, ProtoThrower) + .ToLocal(&thrower)) { + return Nothing(); + } + PropertyDescriptor descriptor(thrower, thrower); descriptor.set_enumerable(false); descriptor.set_configurable(true); - prototype->DefineProperty(context, proto_string, descriptor).ToChecked(); + if (prototype + ->DefineProperty(context, proto_string, descriptor) + .IsNothing()) { + return Nothing(); + } } else if (per_process::cli_options->disable_proto != "") { // Validated in ProcessGlobalArgs - FatalError("InitializeContextRuntime()", "invalid --disable-proto mode"); + FatalError("InitializeContextRuntime()", + "invalid --disable-proto mode"); } + + return Just(true); } bool InitializeContextForSnapshot(Local context) { @@ -638,8 +696,7 @@ bool InitializeContext(Local context) { return false; } - InitializeContextRuntime(context); - return true; + return InitializeContextRuntime(context).IsJust(); } uv_loop_t* GetCurrentEventLoop(Isolate* isolate) { diff --git a/src/node_contextify.cc b/src/node_contextify.cc index ae5fc2fbf1a862..482c9a7b51dbf0 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -210,7 +210,9 @@ MaybeLocal ContextifyContext::CreateV8Context( if (ctx.IsEmpty()) return MaybeLocal(); // Only partially initialize the context - the primordials are left out // and only initialized when necessary. - InitializeContextRuntime(ctx); + if (InitializeContextRuntime(ctx).IsNothing()) { + return MaybeLocal(); + } if (ctx.IsEmpty()) { return MaybeLocal(); diff --git a/src/node_internals.h b/src/node_internals.h index 8f7929994f3243..dec024bd79f2cd 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -92,7 +92,7 @@ void SignalExit(int signal, siginfo_t* info, void* ucontext); std::string GetProcessTitle(const char* default_title); std::string GetHumanReadableProcessName(); -void InitializeContextRuntime(v8::Local); +v8::Maybe InitializeContextRuntime(v8::Local context); bool InitializePrimordials(v8::Local context); class NodeArrayBufferAllocator : public ArrayBufferAllocator { diff --git a/src/node_main_instance.cc b/src/node_main_instance.cc index f232cd6a89f26a..ce319cca3edca3 100644 --- a/src/node_main_instance.cc +++ b/src/node_main_instance.cc @@ -198,7 +198,7 @@ NodeMainInstance::CreateMainEnvironment(int* exit_code, CHECK(!context.IsEmpty()); Context::Scope context_scope(context); - InitializeContextRuntime(context); + CHECK(InitializeContextRuntime(context).IsJust()); SetIsolateErrorHandlers(isolate_, {}); env->InitializeMainContext(context, env_info); #if HAVE_INSPECTOR