Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/node_ffi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,14 @@ Maybe<void*> DynamicLibrary::ResolveSymbol(Environment* env,
Maybe<DynamicLibrary::PreparedFunction> DynamicLibrary::PrepareFunction(
Environment* env, const std::string& name, Local<Object> signature) {
std::shared_ptr<FFIFunction> fn;
auto existing = functions_.find(name);
FunctionSignature parsed;

if (!ParseFunctionSignature(env, name, signature).To(&parsed)) {
return {};
}
// Look up the cache only after parsing: the signature's getters run user
// code that may close the library, which clears `functions_`.
auto existing = functions_.find(name);
auto [return_type, args, return_type_name, arg_type_names] =
std::move(parsed);

Expand Down
15 changes: 15 additions & 0 deletions test/ffi/test-ffi-dynamic-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,21 @@ test('closed libraries reject subsequent operations', () => {
assert.throws(() => lib.getSymbols(), /Library is closed/);
});

test('closing the library from a signature getter of a cached function', () => {
const lib = new ffi.DynamicLibrary(libraryPath);
lib.getFunction('add_i32', fixtureSymbols.add_i32);

assert.throws(() => {
lib.getFunction('add_i32', {
arguments: ['i32', 'i32'],
get return() {
lib.close();
return 'i32';
},
});
}, { code: 'ERR_FFI_LIBRARY_CLOSED' });
});

test('optimized fast calls reject calls after the library is closed', () => {
const { lib, functions } = ffi.dlopen(libraryPath, {
multiply_f64: fixtureSymbols.multiply_f64,
Expand Down
Loading