fix(scripting-node): stop node from clobbering Mono's SIGSEGV handler on POSIX - #4107
Open
valerisn wants to merge 1 commit into
Open
fix(scripting-node): stop node from clobbering Mono's SIGSEGV handler on POSIX#4107valerisn wants to merge 1 commit into
valerisn wants to merge 1 commit into
Conversation
… on POSIX Node installs its own SIGSEGV handler for the WebAssembly trap handler during InitializeOncePerProcess, replacing the one Mono uses to convert null dereferences in managed code into NullReferenceExceptions. Since the node component initializes after the Mono ones, a caught NRE would instead take the whole server process down on Linux.
|
As a FiveM C# user, I appreciate you 💖 |
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.
Goal of this PR
Fixes #4055.
On Linux, a null dereference inside a
try/catchin a C# (mono v1) resource kills the entireFXServerprocess instead of throwing a catchableSystem.NullReferenceException. The same code catches fine on Windows.Cause
Not the Breakpad crash handler.
InitializeExceptionHandler(server/launcher/src/ServerMiniDump.cpp) runs long before mono, so mono'smono_sigsegv_signal_handlerends up installed on top of it and chains back correctly (mono_set_crash_chaining/mono_set_signal_chaininginMonoComponentHostShared.cpp).The handler is overwritten by node.
node::InitializeOncePerProcessis called with default flags inNodeParentEnvironment::Initialize, and on POSIX node installs a baresigaction(SIGSEGV, ...)for the WebAssembly trap handler that does not chain to the previously installed handler. Components initialize in alphabetical order, socitizen-scripting-monosets up mono's handler andcitizen-scripting-nodereplaces it immediately after.From that point on, every implicit null check in JIT''ed managed code lands in node''s
TrapWebAssemblyOrContinue, V8 declines to handle it, and node re-raisesSIGSEGVwith stdio reset - the process dies with no managed output and thecatchblock never runs. Windows is unaffected because mono uses SEH there.This is the same class of problem as 76da0fe (
v8: disable in-process stack dumping to prevent Mono''s SIGSEGV handler from being overwritten).How is this PR achieving the goal
Passes
--disable-wasm-trap-handlerto node on non-Windows targets. Node then skips thesigactioncall entirely and V8 emits explicit bounds checks for WebAssembly memory instead, which is what it already does on any platform where the trap handler is unavailable.ProcessInitializationFlags::kNoDefaultSignalHandlingwould also work, but it additionally drops node''sSIGPIPEignore and itsSIGINT/SIGTERM/SIGHUPhandling, which is a far wider behaviour change than this issue calls for.The option is a per-process CLI option available since node v22.2.0 (vendor is pinned at 22.22.0) and is parsed before the trap handler block in
InitializeOncePerProcessInternal, so it takes effect.This PR applies to the following area(s)
FXServer, C# / Mono, Node
Successfully tested on
Reasoned at source level; I do not have a Linux build environment set up to verify at runtime. The repro from the issue is the check - with this change
nretestshould printcaught: System.NullReferenceExceptionand leave the server running.Checklist
Trade-off
WebAssembly running inside server-side JS resources is bounds-checked in software rather than via guard pages on Linux. Marginal, and only affects resources actually using WASM.