From e3546151fd173a231cd9724e1047e23f06514761 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Larch?= Date: Wed, 18 Nov 2026 12:30:50 +0100 Subject: [PATCH 1/4] src: rethrow linked binding initializer errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GetLinkedBinding() reads module.exports after it calls the register function of a linked binding. If the register function threw, that V8 API call clears the pending exception (PrepareForExecutionScope), so the error never reaches the caller of process._linkedBinding(), which gets the empty exports object instead. This affects every linked binding, including Node-API ones, whose module registration already rethrows the error of the initializer. Addons loaded with process.dlopen() are not affected, because DLOpen() does not call into V8 after the register function. Rethrow the error before reading module.exports. Signed-off-by: René Larch --- src/node_binding.cc | 25 +++++++++++++---- test/cctest/test_linked_binding.cc | 44 ++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/node_binding.cc b/src/node_binding.cc index b8a1a6f64c47..7e03de61b574 100644 --- a/src/node_binding.cc +++ b/src/node_binding.cc @@ -281,6 +281,7 @@ using v8::Local; using v8::Object; using v8::ObjectTemplate; using v8::String; +using v8::TryCatch; using v8::Value; // Globals per process @@ -1080,16 +1081,28 @@ void GetLinkedBinding(const FunctionCallbackInfo& args) { return; } - if (mod->nm_context_register_func != nullptr) { - mod->nm_context_register_func( - exports, module, env->context(), mod->nm_priv); - } else if (mod->nm_register_func != nullptr) { - mod->nm_register_func(exports, module, mod->nm_priv); - } else { + if (mod->nm_context_register_func == nullptr && + mod->nm_register_func == nullptr) { return THROW_ERR_INVALID_MODULE( env, "Linked binding has no declared entry point."); } + { + TryCatch try_catch(env->isolate()); + if (mod->nm_context_register_func != nullptr) { + mod->nm_context_register_func( + exports, module, env->context(), mod->nm_priv); + } else { + mod->nm_register_func(exports, module, mod->nm_priv); + } + // Reading module.exports below would clear an exception thrown by the + // binding's initializer, so rethrow it to the caller here. + if (try_catch.HasCaught()) { + if (!try_catch.HasTerminated()) try_catch.ReThrow(); + return; + } + } + Local effective_exports; if (module->Get(env->context(), env->exports_string()) .ToLocal(&effective_exports)) { diff --git a/test/cctest/test_linked_binding.cc b/test/cctest/test_linked_binding.cc index d36b5874e741..4ba6040e569d 100644 --- a/test/cctest/test_linked_binding.cc +++ b/test/cctest/test_linked_binding.cc @@ -419,3 +419,47 @@ TEST_F(LinkedBindingTest, ManyBindingsTest) { CHECK_EQ(strcmp(*utf8val, "world"), 0); CHECK_EQ(calls, 5); } + +void InitializeThrowingBinding(v8::Local exports, + v8::Local module, + v8::Local context, + void* priv) { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + isolate->ThrowException(v8::Exception::Error( + v8::String::NewFromUtf8Literal(isolate, "init failed"))); +} + +TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingThrowsTest) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env test_env{handle_scope, argv}; + + AddLinkedBinding( + *test_env, "local_linked_throws", InitializeThrowingBinding, nullptr); + + const char* run_script = + "try { process._linkedBinding('local_linked_throws'); 'not thrown'; }" + " catch (err) { err.message; }"; + CHECK_EQ(RunScript(isolate_, run_script), "init failed"); +} + +napi_value InitializeThrowingNapiBinding(napi_env env, napi_value exports) { + CHECK_EQ(napi_throw_error(env, nullptr, "init failed"), napi_ok); + return nullptr; +} + +TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingNapiThrowsTest) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env test_env{handle_scope, argv}; + + AddLinkedBinding(*test_env, + "local_linked_napi_throws", + InitializeThrowingNapiBinding, + NAPI_VERSION); + + const char* run_script = + "try { process._linkedBinding('local_linked_napi_throws');" + " 'not thrown'; } catch (err) { err.message; }"; + CHECK_EQ(RunScript(isolate_, run_script), "init failed"); +} From b3e3a709668305e3caebbc028d5b3080ea058c06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Larch?= Date: Thu, 19 Nov 2026 00:21:29 +0100 Subject: [PATCH 2/4] test: cover linked binding that sets exports and throws MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: René Larch --- test/cctest/test_linked_binding.cc | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/cctest/test_linked_binding.cc b/test/cctest/test_linked_binding.cc index 4ba6040e569d..9cf47d9efbf9 100644 --- a/test/cctest/test_linked_binding.cc +++ b/test/cctest/test_linked_binding.cc @@ -443,6 +443,42 @@ TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingThrowsTest) { CHECK_EQ(RunScript(isolate_, run_script), "init failed"); } +void InitializeExportsThenThrowingBinding(v8::Local exports, + v8::Local module, + v8::Local context, + void* priv) { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + v8::Local new_exports = v8::Object::New(isolate); + CHECK(new_exports + ->Set(context, + v8::String::NewFromUtf8Literal(isolate, "key"), + v8::String::NewFromUtf8Literal(isolate, "value")) + .FromJust()); + CHECK(module.As() + ->Set(context, + v8::String::NewFromUtf8Literal(isolate, "exports"), + new_exports) + .FromJust()); + isolate->ThrowException(v8::Exception::Error( + v8::String::NewFromUtf8Literal(isolate, "init failed"))); +} + +TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingExportsThenThrowsTest) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env test_env{handle_scope, argv}; + + AddLinkedBinding(*test_env, + "local_linked_exports_throws", + InitializeExportsThenThrowingBinding, + nullptr); + + const char* run_script = + "try { process._linkedBinding('local_linked_exports_throws').key; }" + " catch (err) { err.message; }"; + CHECK_EQ(RunScript(isolate_, run_script), "init failed"); +} + napi_value InitializeThrowingNapiBinding(napi_env env, napi_value exports) { CHECK_EQ(napi_throw_error(env, nullptr, "init failed"), napi_ok); return nullptr; From db196f4c6217a95cce80139a78267cee05dc5ffb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Larch?= Date: Thu, 19 Nov 2026 11:32:53 +0100 Subject: [PATCH 3/4] src: check for a pending exception in GetLinkedBinding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Return right after the register function when it left an exception pending, instead of catching and rethrowing it with a TryCatch. This keeps the code structure of main and matches the check in napi_module_register_by_symbol. Signed-off-by: René Larch --- src/node_binding.cc | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/node_binding.cc b/src/node_binding.cc index 7e03de61b574..b4560045eba1 100644 --- a/src/node_binding.cc +++ b/src/node_binding.cc @@ -281,7 +281,6 @@ using v8::Local; using v8::Object; using v8::ObjectTemplate; using v8::String; -using v8::TryCatch; using v8::Value; // Globals per process @@ -1081,27 +1080,19 @@ void GetLinkedBinding(const FunctionCallbackInfo& args) { return; } - if (mod->nm_context_register_func == nullptr && - mod->nm_register_func == nullptr) { + if (mod->nm_context_register_func != nullptr) { + mod->nm_context_register_func( + exports, module, env->context(), mod->nm_priv); + } else if (mod->nm_register_func != nullptr) { + mod->nm_register_func(exports, module, mod->nm_priv); + } else { return THROW_ERR_INVALID_MODULE( env, "Linked binding has no declared entry point."); } - { - TryCatch try_catch(env->isolate()); - if (mod->nm_context_register_func != nullptr) { - mod->nm_context_register_func( - exports, module, env->context(), mod->nm_priv); - } else { - mod->nm_register_func(exports, module, mod->nm_priv); - } - // Reading module.exports below would clear an exception thrown by the - // binding's initializer, so rethrow it to the caller here. - if (try_catch.HasCaught()) { - if (!try_catch.HasTerminated()) try_catch.ReThrow(); - return; - } - } + // Reading module.exports below would clear an exception thrown by the + // binding's initializer, so return and let it reach the caller. + if (env->isolate()->HasPendingException()) return; Local effective_exports; if (module->Get(env->context(), env->exports_string()) From 42ecb4715ce885c59a2e12a927c6606a6671fbaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Larch?= Date: Thu, 19 Nov 2026 11:32:53 +0100 Subject: [PATCH 4/4] node-api: keep init errors when an addon returns new exports MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a Node-API module's init function throws and returns an object other than the exports it received, napi_module_register_by_symbol() sets module.exports to that object. That V8 API call clears the pending exception, so require() of such an addon returned the new exports instead of throwing, and process._linkedBinding() did the same for a linked Node-API binding. Return before setting module.exports when the init function left an exception pending. Signed-off-by: René Larch --- src/node_api.cc | 4 +++ test/cctest/test_linked_binding.cc | 28 +++++++++++++++++++ test/node-api/test_init_exception/binding.gyp | 8 ++++++ test/node-api/test_init_exception/test.js | 10 +++++++ .../test_init_exception/test_init_exception.c | 17 +++++++++++ 5 files changed, 67 insertions(+) create mode 100644 test/node-api/test_init_exception/binding.gyp create mode 100644 test/node-api/test_init_exception/test.js create mode 100644 test/node-api/test_init_exception/test_init_exception.c diff --git a/src/node_api.cc b/src/node_api.cc index 64d26a7a502e..cc6ec220b599 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -786,6 +786,10 @@ void napi_module_register_by_symbol(v8::Local exports, _exports = init(env, v8impl::JsValueFromV8LocalValue(exports)); }); + // Setting module.exports below would clear an exception thrown by the + // module's init function, so return and let it reach the caller. + if (env->isolate->HasPendingException()) return; + // If register function returned a non-null exports object different from // the exports object we passed it, set that as the "exports" property of // the module. diff --git a/test/cctest/test_linked_binding.cc b/test/cctest/test_linked_binding.cc index 9cf47d9efbf9..32f9e4d8d7cf 100644 --- a/test/cctest/test_linked_binding.cc +++ b/test/cctest/test_linked_binding.cc @@ -499,3 +499,31 @@ TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingNapiThrowsTest) { " 'not thrown'; } catch (err) { err.message; }"; CHECK_EQ(RunScript(isolate_, run_script), "init failed"); } + +napi_value InitializeExportsThenThrowingNapiBinding(napi_env env, + napi_value exports) { + napi_value new_exports; + napi_value value; + CHECK_EQ(napi_create_object(env, &new_exports), napi_ok); + CHECK_EQ(napi_create_string_utf8(env, "value", NAPI_AUTO_LENGTH, &value), + napi_ok); + CHECK_EQ(napi_set_named_property(env, new_exports, "key", value), napi_ok); + CHECK_EQ(napi_throw_error(env, nullptr, "init failed"), napi_ok); + return new_exports; +} + +TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingNapiExportsThrowsTest) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env test_env{handle_scope, argv}; + + AddLinkedBinding(*test_env, + "local_linked_napi_exports_throws", + InitializeExportsThenThrowingNapiBinding, + NAPI_VERSION); + + const char* run_script = + "try { process._linkedBinding('local_linked_napi_exports_throws').key; }" + " catch (err) { err.message; }"; + CHECK_EQ(RunScript(isolate_, run_script), "init failed"); +} diff --git a/test/node-api/test_init_exception/binding.gyp b/test/node-api/test_init_exception/binding.gyp new file mode 100644 index 000000000000..bfd03ff5d9b2 --- /dev/null +++ b/test/node-api/test_init_exception/binding.gyp @@ -0,0 +1,8 @@ +{ + "targets": [ + { + "target_name": "test_init_exception", + "sources": [ "test_init_exception.c" ] + } + ] +} diff --git a/test/node-api/test_init_exception/test.js b/test/node-api/test_init_exception/test.js new file mode 100644 index 000000000000..41524301840d --- /dev/null +++ b/test/node-api/test_init_exception/test.js @@ -0,0 +1,10 @@ +'use strict'; +const common = require('../../common'); +const assert = require('assert'); + +// An exception thrown by the init function must reach the caller of +// require(), even when the init function also returns new exports. +assert.throws( + () => require(`./build/${common.buildType}/test_init_exception`), + { message: 'Error during Init' }, +); diff --git a/test/node-api/test_init_exception/test_init_exception.c b/test/node-api/test_init_exception/test_init_exception.c new file mode 100644 index 000000000000..6c32521f5f5b --- /dev/null +++ b/test/node-api/test_init_exception/test_init_exception.c @@ -0,0 +1,17 @@ +#include +#include "../../js-native-api/common.h" + +// Throws after creating new exports. require() must throw the error instead +// of returning the new exports. +static napi_value Init(napi_env env, napi_value exports) { + napi_value new_exports; + napi_value value; + NODE_API_CALL(env, napi_create_object(env, &new_exports)); + NODE_API_CALL( + env, napi_create_string_utf8(env, "value", NAPI_AUTO_LENGTH, &value)); + NODE_API_CALL(env, napi_set_named_property(env, new_exports, "key", value)); + NODE_API_CALL(env, napi_throw_error(env, NULL, "Error during Init")); + return new_exports; +} + +NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)