diff options
-rw-r--r-- | src/bun.js/bindings/Process.cpp | 22 | ||||
-rw-r--r-- | test/js/node/process/process.test.js | 10 |
2 files changed, 27 insertions, 5 deletions
diff --git a/src/bun.js/bindings/Process.cpp b/src/bun.js/bindings/Process.cpp index a7798bf9f..6c58c94dd 100644 --- a/src/bun.js/bindings/Process.cpp +++ b/src/bun.js/bindings/Process.cpp @@ -230,23 +230,35 @@ JSC_DEFINE_HOST_FUNCTION(Process_functionDlopen, auto argCount = callFrame->argumentCount(); if (argCount < 2) { - JSC::throwTypeError(globalObject, scope, "dlopen requires 2 arguments"_s); return JSC::JSValue::encode(JSC::JSValue {}); } JSC::JSValue moduleValue = callFrame->uncheckedArgument(0); - if (!moduleValue.isObject()) { + JSC::JSObject* moduleObject = jsDynamicCast<JSC::JSObject*>(moduleValue); + if (UNLIKELY(!moduleObject)) { JSC::throwTypeError(globalObject, scope, "dlopen requires an object as first argument"_s); return JSC::JSValue::encode(JSC::JSValue {}); } - JSC::Identifier exportsSymbol = JSC::Identifier::fromString(vm, "exports"_s); - JSC::JSObject* exports = moduleValue.getObject()->getIfPropertyExists(globalObject, exportsSymbol).getObject(); + + JSValue exports = moduleObject->getIfPropertyExists(globalObject, builtinNames(vm).exportsPublicName()); + RETURN_IF_EXCEPTION(scope, {}); + + if (UNLIKELY(!exports)) { + JSC::throwTypeError(globalObject, scope, "dlopen requires an object with an exports property"_s); + return JSC::JSValue::encode(JSC::JSValue {}); + } + + globalObject->pendingNapiModule = exports; + if (exports.isCell()) { + vm.writeBarrier(globalObject, exports.asCell()); + } WTF::String filename = callFrame->uncheckedArgument(1).toWTFString(globalObject); + RETURN_IF_EXCEPTION(scope, {}); + CString utf8 = filename.utf8(); - globalObject->pendingNapiModule = exports; void* handle = dlopen(utf8.data(), RTLD_LAZY); if (!handle) { diff --git a/test/js/node/process/process.test.js b/test/js/node/process/process.test.js index 91479108f..539a92f2c 100644 --- a/test/js/node/process/process.test.js +++ b/test/js/node/process/process.test.js @@ -478,3 +478,13 @@ for (const stub of emptyArrayStubs) { expect(process[stub]).toHaveLength(0); }); } + +it("dlopen args parsing", () => { + expect(() => process.dlopen({ module: "42" }, "/tmp/not-found.so")).toThrow(); + expect(() => process.dlopen({ module: 42 }, "/tmp/not-found.so")).toThrow(); + expect(() => process.dlopen({ module: { exports: "42" } }, "/tmp/not-found.so")).toThrow(); + expect(() => process.dlopen({ module: { exports: 42 } }, "/tmp/not-found.so")).toThrow(); + expect(() => process.dlopen({ module: Symbol() }, "/tmp/not-found.so")).toThrow(); + expect(() => process.dlopen({ module: { exports: Symbol("123") } }, "/tmp/not-found.so")).toThrow(); + expect(() => process.dlopen({ module: { exports: Symbol("123") } }, Symbol("badddd"))).toThrow(); +}); |