src: rethrow linked binding initializer errors

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 <renelarch@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67571
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
René Larch authored and GitHub committed 2026-11-25 14:28:20 +00:00
1 parent bf201ef3e3
commit 2fd4580d4a
6 files changed
+151

No files matched your search

+4
View File
@@ -786,6 +786,10 @@ void napi_module_register_by_symbol(v8::Local<v8::Object> 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.
+4
View File
@@ -1090,6 +1090,10 @@ void GetLinkedBinding(const FunctionCallbackInfo<Value>& args) {
env, "Linked binding has no declared entry point.");
}
// 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<Value> effective_exports;
if (module->Get(env->context(), env->exports_string())
.ToLocal(&effective_exports)) {
+108
View File
@@ -419,3 +419,111 @@ TEST_F(LinkedBindingTest, ManyBindingsTest) {
CHECK_EQ(strcmp(*utf8val, "world"), 0);
CHECK_EQ(calls, 5);
}
void InitializeThrowingBinding(v8::Local<v8::Object> exports,
v8::Local<v8::Value> module,
v8::Local<v8::Context> 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");
}
void InitializeExportsThenThrowingBinding(v8::Local<v8::Object> exports,
v8::Local<v8::Value> module,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Local<v8::Object> 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<v8::Object>()
->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;
}
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");
}
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");
}
@@ -0,0 +1,8 @@
{
"targets": [
{
"target_name": "test_init_exception",
"sources": [ "test_init_exception.c" ]
}
]
}
+10
View File
@@ -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' },
);
@@ -0,0 +1,17 @@
#include <node_api.h>
#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)