From 8069174ee077d72e2d56ebe88b2249567a06fa45 Mon Sep 17 00:00:00 2001 From: Harshitha KP Date: Fri, 21 Feb 2020 02:32:51 -0500 Subject: [PATCH 1/2] src: Handle NULL env scenario Convert hard assertion into a throw with a useful error message in src/module_wrap.cc --- doc/api/errors.md | 7 +++++++ src/module_wrap.cc | 13 +++++++++++-- src/node_errors.h | 47 ++++++++++++++++++++++++---------------------- 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index c244bfd7f0042f..8848eedfcf64a6 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -889,6 +889,13 @@ provided. Encoding provided to `TextDecoder()` API was not one of the [WHATWG Supported Encodings][]. + +### `ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE` + +The JS execution context is not associated with a Node.js environment. +This may occur when Node.js is used as an embedded library and some hooks +for the JS engine are not set up properly. + ### `ERR_FALSY_VALUE_REJECTION` diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 2b1708088bf449..b2afefce17eb91 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -451,7 +451,12 @@ MaybeLocal ModuleWrap::ResolveCallback(Local context, Local specifier, Local referrer) { Environment* env = Environment::GetCurrent(context); - CHECK_NOT_NULL(env); // TODO(addaleax): Handle nullptr here. + if (env == nullptr) { + Isolate* isolate = context->GetIsolate(); + THROW_ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE(isolate); + return MaybeLocal(); + } + Isolate* isolate = env->isolate(); ModuleWrap* dependent = GetFromModule(env, referrer); @@ -1443,7 +1448,11 @@ static MaybeLocal ImportModuleDynamically( Local specifier) { Isolate* iso = context->GetIsolate(); Environment* env = Environment::GetCurrent(context); - CHECK_NOT_NULL(env); // TODO(addaleax): Handle nullptr here. + if (env == nullptr) { + THROW_ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE(iso); + return MaybeLocal(); + } + EscapableHandleScope handle_scope(iso); Local import_callback = diff --git a/src/node_errors.h b/src/node_errors.h index a05ce8f6bfb1bd..84fb22951873c1 100644 --- a/src/node_errors.h +++ b/src/node_errors.h @@ -39,6 +39,7 @@ void OnFatalError(const char* location, const char* message); V(ERR_CONSTRUCT_CALL_INVALID, TypeError) \ V(ERR_CRYPTO_UNKNOWN_CIPHER, Error) \ V(ERR_CRYPTO_UNKNOWN_DH_GROUP, Error) \ + V(ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE, Error) \ V(ERR_INVALID_ARG_VALUE, TypeError) \ V(ERR_OSSL_EVP_INVALID_DIGEST, Error) \ V(ERR_INVALID_ARG_TYPE, TypeError) \ @@ -86,28 +87,30 @@ void OnFatalError(const char* location, const char* message); // Errors with predefined static messages -#define PREDEFINED_ERROR_MESSAGES(V) \ - V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, \ - "Buffer is not available for the current Context") \ - V(ERR_CONSTRUCT_CALL_INVALID, "Constructor cannot be called") \ - V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`") \ - V(ERR_CRYPTO_UNKNOWN_CIPHER, "Unknown cipher") \ - V(ERR_CRYPTO_UNKNOWN_DH_GROUP, "Unknown DH group") \ - V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \ - V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \ - V(ERR_OSSL_EVP_INVALID_DIGEST, "Invalid digest used") \ - V(ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST, \ - "MessagePort was found in message but not listed in transferList") \ - V(ERR_MISSING_PLATFORM_FOR_WORKER, \ - "The V8 platform used by this instance of Node does not support " \ - "creating Workers") \ - V(ERR_NON_CONTEXT_AWARE_DISABLED, \ - "Loading non context-aware native modules has been disabled") \ - V(ERR_SCRIPT_EXECUTION_INTERRUPTED, \ - "Script execution was interrupted by `SIGINT`") \ - V(ERR_TRANSFERRING_EXTERNALIZED_SHAREDARRAYBUFFER, \ - "Cannot serialize externalized SharedArrayBuffer") \ - V(ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED, "Failed to set PSK identity hint") \ +#define PREDEFINED_ERROR_MESSAGES(V) \ + V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, \ + "Buffer is not available for the current Context") \ + V(ERR_CONSTRUCT_CALL_INVALID, "Constructor cannot be called") \ + V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`") \ + V(ERR_CRYPTO_UNKNOWN_CIPHER, "Unknown cipher") \ + V(ERR_CRYPTO_UNKNOWN_DH_GROUP, "Unknown DH group") \ + V(ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE, \ + "Context not associated with Node.js environment") \ + V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \ + V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \ + V(ERR_OSSL_EVP_INVALID_DIGEST, "Invalid digest used") \ + V(ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST, \ + "MessagePort was found in message but not listed in transferList") \ + V(ERR_MISSING_PLATFORM_FOR_WORKER, \ + "The V8 platform used by this instance of Node does not support " \ + "creating Workers") \ + V(ERR_NON_CONTEXT_AWARE_DISABLED, \ + "Loading non context-aware native modules has been disabled") \ + V(ERR_SCRIPT_EXECUTION_INTERRUPTED, \ + "Script execution was interrupted by `SIGINT`") \ + V(ERR_TRANSFERRING_EXTERNALIZED_SHAREDARRAYBUFFER, \ + "Cannot serialize externalized SharedArrayBuffer") \ + V(ERR_TLS_PSK_SET_IDENTIY_HINT_FAILED, "Failed to set PSK identity hint") #define V(code, message) \ inline v8::Local code(v8::Isolate* isolate) { \ From a2b83105223cc153abe906c21d85251e275aa8f0 Mon Sep 17 00:00:00 2001 From: Harshitha KP Date: Thu, 5 Mar 2020 02:33:18 -0500 Subject: [PATCH 2/2] fixup: address review comment --- src/node_errors.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node_errors.h b/src/node_errors.h index 84fb22951873c1..960cb725323e92 100644 --- a/src/node_errors.h +++ b/src/node_errors.h @@ -95,7 +95,7 @@ void OnFatalError(const char* location, const char* message); V(ERR_CRYPTO_UNKNOWN_CIPHER, "Unknown cipher") \ V(ERR_CRYPTO_UNKNOWN_DH_GROUP, "Unknown DH group") \ V(ERR_EXECUTION_ENVIRONMENT_NOT_AVAILABLE, \ - "Context not associated with Node.js environment") \ + "Context not associated with Node.js environment") \ V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \ V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \ V(ERR_OSSL_EVP_INVALID_DIGEST, "Invalid digest used") \