ffi: reject fast calls after library close - #64860
Merged
nodejs-github-bot merged 2 commits intoAug 2, 2026
Merged
Conversation
Collaborator
|
Review requested:
|
Optimized Fast API calls bypass InvokeFunction and can jump directly to a symbol after DynamicLibrary::close() unloads its library. Check the function's closed state in the AArch64 and SysV x64 trampolines before entering the target. If the library is closed, schedule ERR_FFI_LIBRARY_CLOSED and return without calling the symbol. Keep the JavaScript guard on platforms without a native trampoline guard and for signatures that already require argument conversion or validation. This keeps raw scalar fast calls close to their original performance on supported platforms. Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com> Assisted-by: codex:gpt-5.6-sol
trivikr
force-pushed
the
ffi-closed-lib-cal-sigsegv-3
branch
from
July 31, 2026 04:27
8e91308 to
3b1b100
Compare
ShogunPanda
approved these changes
Jul 31, 2026
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #64860 +/- ##
==========================================
+ Coverage 90.16% 90.17% +0.01%
==========================================
Files 746 746
Lines 242760 242847 +87
Branches 45765 45768 +3
==========================================
+ Hits 218875 218987 +112
+ Misses 15375 15341 -34
- Partials 8510 8519 +9
🚀 New features to boost your workflow:
|
This comment was marked as outdated.
This comment was marked as outdated.
Collaborator
trivikr
marked this pull request as draft
July 31, 2026 20:20
Member
Author
|
Moved to draft since CI is failing on Linux on ARM not ok 8 ffi/test-ffi-dynamic-library
---
duration_ms: 509.99000
severity: crashed
exitcode: -6
stack: |-
(node:3793798) ExperimentalWarning: FFI is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
FATAL ERROR: v8::HandleScope::CreateHandle() Cannot create a handle without a HandleScope
----- Native stack trace -----
1: 0xc9ccdcdb50cc node::DumpNativeBacktrace(_IO_FILE*) [out/Debug/node]
2: 0xc9ccdcf2bad8 node::OnFatalError(char const*, char const*) [out/Debug/node]
3: 0xc9ccdd3bb228 [out/Debug/node]
4: 0xc9ccdd7e04e8 [out/Debug/node]
5: 0xc9ccdd3b1794 [out/Debug/node]
6: 0xc9ccddf78f80 [out/Debug/node]
7: 0xc9ccdd878fc8 [out/Debug/node]
8: 0xc9ccdd3f50f4 v8::String::NewFromOneByte(v8::Isolate*, unsigned char const*, v8::NewStringType, int) [out/Debug/node]
9: 0xc9ccdccfd20c node::OneByteString(v8::Isolate*, char const*, int, v8::NewStringType) [out/Debug/node]
10: 0xc9ccdcd2cd20 v8::Local<v8::String> node::FIXED_ONE_BYTE_STRING<23ul>(v8::Isolate*, char const (&) [23ul]) requires ((23ul)>(0)) [out/Debug/node]
11: 0xc9ccdd2104b8 v8::Local<v8::Object> node::ERR_FFI_LIBRARY_CLOSED<>(v8::Isolate*, std::basic_string_view<char, std::char_traits<char> >) [out/Debug/node]
12: 0xc9ccdd21043c node::THROW_ERR_FFI_LIBRARY_CLOSED(v8::Isolate*) [out/Debug/node]
13: 0xc9ccdd221ef0 node_ffi_fast_library_closed [out/Debug/node]
14: 0xebea4cb1b044
FATAL ERROR: v8::HandleScope::CreateHandle() Cannot create a handle without a HandleScope
----- Native stack trace -----
1: 0xc9ccdcdb50cc node::DumpNativeBacktrace(_IO_FILE*) [out/Debug/node]
2: 0xc9ccdcf2bad8 node::OnFatalError(char const*, char const*) [out/Debug/node]
3: 0xc9ccdd3bb228 [out/Debug/node]
4: 0xc9ccdd7e04e8 [out/Debug/node]
5: 0xc9ccdd3bc518 v8::EscapableHandleScopeBase::EscapableHandleScopeBase(v8::Isolate*) [out/Debug/node]
6: 0xc9ccdccf9a04 v8::EscapableHandleScope::EscapableHandleScope(v8::Isolate*) [out/Debug/node]
7: 0xc9ccdcf2a08c node::GetCurrentStackTrace(v8::Isolate*, int) [out/Debug/node]
8: 0xc9ccdcdb51f0 node::DumpJavaScriptBacktrace(_IO_FILE*) [out/Debug/node]
9: 0xc9ccdcf2bae4 node::OnFatalError(char const*, char const*) [out/Debug/node]
10: 0xc9ccdd3bb228 [out/Debug/node]
11: 0xc9ccdd7e04e8 [out/Debug/node]
12: 0xc9ccdd3b1794 [out/Debug/node]
13: 0xc9ccddf78f80 [out/Debug/node]
14: 0xc9ccdd878fc8 [out/Debug/node]
15: 0xc9ccdd3f50f4 v8::String::NewFromOneByte(v8::Isolate*, unsigned char const*, v8::NewStringType, int) [out/Debug/node]
16: 0xc9ccdccfd20c node::OneByteString(v8::Isolate*, char const*, int, v8::NewStringType) [out/Debug/node]
17: 0xc9ccdcd2cd20 v8::Local<v8::String> node::FIXED_ONE_BYTE_STRING<23ul>(v8::Isolate*, char const (&) [23ul]) requires ((23ul)>(0)) [out/Debug/node]
18: 0xc9ccdd2104b8 v8::Local<v8::Object> node::ERR_FFI_LIBRARY_CLOSED<>(v8::Isolate*, std::basic_string_view<char, std::char_traits<char> >) [out/Debug/node]
19: 0xc9ccdd21043c node::THROW_ERR_FFI_LIBRARY_CLOSED(v8::Isolate*) [out/Debug/node]
20: 0xc9ccdd221ef0 node_ffi_fast_library_closed [out/Debug/node]
21: 0xebea4cb1b044
...CI: https://ci.nodejs.org/job/node-test-commit-arm-debug/24671/ |
Fast API calls run without an entered HandleScope, and the generated trampolines call node_ffi_fast_library_closed() directly. Constructing ERR_FFI_LIBRARY_CLOSED allocates handles, which aborted debug builds with "Cannot create a handle without a HandleScope". Open a HandleScope in the helper before throwing. The scheduled exception is stored on the isolate, so it outlives the scope. Apply the same fix to node_ffi_fast_buffer_data(), which had the same latent bug on its throw path.
Member
Author
|
CI to test fixup: https://ci.nodejs.org/job/node-test-commit-arm-debug/24674/ |
Collaborator
trivikr
marked this pull request as ready for review
July 31, 2026 22:50
This comment was marked as outdated.
This comment was marked as outdated.
jasnell
approved these changes
Aug 2, 2026
Collaborator
|
Landed in c55eb4a |
aduh95
pushed a commit
that referenced
this pull request
Aug 3, 2026
Optimized Fast API calls bypass InvokeFunction and can jump directly to a symbol after DynamicLibrary::close() unloads its library. Check the function's closed state in the AArch64 and SysV x64 trampolines before entering the target. If the library is closed, schedule ERR_FFI_LIBRARY_CLOSED and return without calling the symbol. Keep the JavaScript guard on platforms without a native trampoline guard and for signatures that already require argument conversion or validation. This keeps raw scalar fast calls close to their original performance on supported platforms. Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com> Assisted-by: codex:gpt-5.6-sol PR-URL: #64860 Fixes: #64854 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes: #64854
Optimized FFI Fast API calls bypass
DynamicLibrary::InvokeFunction()and enter the generated trampoline directly. AfterDynamicLibrary::close()unloads the library, optimized code could call the stale symbol address and crash withSIGSEGV.This adds a closed-state check to the AArch64 and SysV x64 fast trampolines. The hot path continues to the native symbol when the library is open. The cold path schedules
ERR_FFI_LIBRARY_CLOSEDand returns without entering the unloaded library.Assisted-by: codex:gpt-5.6-sol