Skip to content

ffi: throw on fast calls to a closed library - #64855

Closed
trivikr wants to merge 1 commit into
nodejs:mainfrom
trivikr:ffi-closed-lib-cal-sigsegv
Closed

ffi: throw on fast calls to a closed library#64855
trivikr wants to merge 1 commit into
nodejs:mainfrom
trivikr:ffi-closed-lib-cal-sigsegv

Conversation

@trivikr

@trivikr trivikr commented Jul 31, 2026

Copy link
Copy Markdown
Member

Fixes: #64854

The closed checks in the C++ invokers cannot run on the fast-call path, since V8 embeds the trampoline address when it compiles the call site. So each DynamicLibrary gets a one-byte flag that Close() sets, and the JS wrapper tests it before entering the trampoline; freeing the trampoline instead would leave compiled code calling unmapped memory.

That only works if every fast function is wrapped, so CreateFunction() now attaches kFastArguments to all of them and signatures needing no argument conversion get a guard-only wrapper. For those signatures lib.functions and friends now return a JS wrapper instead of a native function, with name, length, and pointer carried over.


Assisted-by: claude:opus-5

Close() marks every FFIFunction closed, but the generated fast-call
path never sees it: V8 embeds the trampoline address when it compiles
the call site, so an optimized call jumps into the library that
dlclose() unloaded. Freeing the trampoline in Close() instead would
leave compiled code calling unmapped memory.

Give each library a one-byte flag that Close() sets and test it in the
JavaScript wrapper before entering the trampoline. Fast signatures that
need no argument conversion were handed to user code as the raw
function, so they now get a guard-only wrapper too.

Signed-off-by: Trivikram Kamat <16024985+trivikr@users.noreply.github.com>
Assisted-by: claude:opus-5
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/ffi

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. ffi Issues and PRs related to experimental Foreign Function Interface support. needs-ci PRs that need a full CI run. labels Jul 31, 2026
@trivikr trivikr added the request-ci Add this label to start a Jenkins CI on a PR. label Jul 31, 2026
@trivikr
trivikr marked this pull request as draft July 31, 2026 03:03
@trivikr

trivikr commented Jul 31, 2026

Copy link
Copy Markdown
Member Author

@trivikr trivikr closed this Jul 31, 2026
@trivikr
trivikr deleted the ffi-closed-lib-cal-sigsegv branch July 31, 2026 04:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. ffi Issues and PRs related to experimental Foreign Function Interface support. needs-ci PRs that need a full CI run. request-ci Add this label to start a Jenkins CI on a PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ffi: closed library still callable via fast path, SIGSEGV instead of ERR_FFI_LIBRARY_CLOSED

2 participants