Skip to content

[WTF] Give WTF threads an alternate signal stack and make the suspend handler work on one - #742

Open
robobun wants to merge 4 commits into
robobun/75562ee0/signal-handler-onstackfrom
robobun/3ff782a7/signal-handler-altstack
Open

robobun wants to merge 4 commits into
robobun/75562ee0/signal-handler-onstackfrom
robobun/3ff782a7/signal-handler-altstack

Conversation

@robobun

@robobun robobun commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #741. oven-sh/bun#40169 needs commit 1 too.

Problem

  • A native stack overflow on a thread that WTF creates (heap helpers, JIT worklist) ends Bun with exit 139 and an empty stderr. wtfThreadEntryPoint (Source/WTF/wtf/posix/ThreadingPOSIX.cpp:331) registers no alternate signal stack.
  • With one, a suspend handler that has SA_ONSTACK backs off and Thread::suspend() retries without end. Go's runtime adds that flag, and bun --cpu-prof hangs on main.

Fix

  • wtfThreadEntryPoint registers a 512 KiB alternate stack with a guard page while the entry point runs, unless one is registered already.
  • A suspend handler on the alternate stack publishes its context's registers when their stack pointer is in the thread's own stack.
  • Verified: testFFI on Linux, musl and macOS, and JSTests/stress/sampling-profiler-thread-on-alternate-signal-stack.js.
  • Self-reviewed: 7 concerns raised, 5 addressed, 2 open (Notes).

Background

Downsides

  • Each thread of WTF costs 7 more syscalls, about 7 us of CPU and 516 KiB of address space. Text grows by 814 bytes.
  • Not run on FreeBSD or Android.
Notes

Self-review. Concerns that the reviews of this PR raised, and what I did:

  1. The destructor disabled the stack with size 0. The libc of Darwin rejects that, so the stack was unmapped while it was registered. Fixed, with a test.
  2. A suspend handler with SA_ONSTACK never suspends a thread that has an alternate stack. Fixed in commit 1, with a test.
  3. The stack was cut and ordered badly: this PR had the only tests of [WTF] The signal handler runs on the alternate signal stack, and a thread that runs it there can be suspended #741, and it added stacks before the check of WTF: read interrupted SP from ucontext in signalHandlerSuspendResume #235. Now four commits in that order, and [WTF] The signal handler runs on the alternate signal stack, and a thread that runs it there can be suspended #741 can take the first two.
  4. [WTF] The signal handler runs on the alternate signal stack, and a thread that runs it there can be suspended #741 with Report native stack overflows on every thread bun#40169 needs commit 1, and no test of either fails without it. Open. It needs [WTF] The signal handler runs on the alternate signal stack, and a thread that runs it there can be suspended #741 to take commits 1 and 2, or Report native stack overflows on every thread bun#40169 to pin a build of this PR. The measurements and a test for Bun are in comments on both.
  5. No test failed when the destructor stopped passing the size, and two of its branches never ran. Added testThreadReleasesAlternateSignalStack.
  6. The change insures against a crash that nobody reported, and it never ran on macOS. Run on macOS and musl through the CI of Bun. I know no report of a native stack overflow on a thread of WTF. Such a crash leaves no trace today, so the tracker cannot show how often it happens.
  7. One more concern of the last pass. Open: its text did not reach me, so I cannot say that it is addressed.

Weighed, and kept as it is:

  • PageReservation::tryReserveWithGuardPages in place of MmapSpan and mprotect. It has no user in WTF or JSC, it adds a second guard page and one more syscall, and it does not release itself.
  • A suspend handler that reads the context first and drops the currentStackPointer() test. On an architecture that has no accessor in Signals.cpp the context gives no stack pointer, and that handler would back off without end. The test of the handler's own stack pointer stays first, as upstream has it.

Commits. Four, on top of #741 (242085d):

  1. de67286: the suspend handler.
  2. b37f482: testFFI tests that hold without an alternate stack on the threads of WTF. The overflow of the main thread is the test of [WTF] The signal handler runs on the alternate signal stack, and a thread that runs it there can be suspended #741. The suspension test is the test of commit 1.
  3. d17502e: the alternate stack for the threads of WTF.
  4. f51a066: the tests that need commit 3.

Commits 1 and 2 do not depend on 3 and 4, so #741 can take them. Checked with the tests of each commit against each state of WTF:

tests WTF main thread overflow thread of WTF overflow thread has a stack suspension
commit 2 #741 pass fail (-14)
commit 2 commit 1 pass pass
commit 4 commit 1 pass fail (-11) fail pass
commit 4 commit 3 pass pass pass pass

Revisions. "Final" is f51a066e36, or f6bee26d4b and 2feae769a5 before it: all three have the same code outside of the tests. "Previous" is a66379db22: the same alternate stack, without the commit for the suspend handler. "Earlier" is a revision whose destructor passed no size to the disable call. All three make the same syscalls at the start and the end of a thread on Linux.

Platforms. My own builds and runs are Linux x64, Release jsc and testFFI with the settings of the bun-webkit-linux-amd64 lane, not LTO. The fork's CI runs testFFI and the JavaScriptCore tests on Linux x64 and arm64. The other lanes build. The CI of Bun ran testFFI on macOS and musl (below).

Tests, final revision:

  • testFFI, testNativeStackOverflowIsReported: a child process registers a handler with SA_ONSTACK and an alternate stack for the main thread, creates a VM and overflows the native stack. On the main thread the child fails on main (-11, killed by SIGSEGV) and passes with [WTF] The signal handler runs on the alternate signal stack, and a thread that runs it there can be suspended #741. On a thread of WTF it fails with [WTF] The signal handler runs on the alternate signal stack, and a thread that runs it there can be suspended #741 (-11) and passes with this branch.
  • testFFI, testThreadHasAlternateSignalStack: a thread of WTF has an active stack of 512 KiB, and 1000 threads created and joined leave no mappings behind. It fails with [WTF] The signal handler runs on the alternate signal stack, and a thread that runs it there can be suspended #741.
  • testFFI, testThreadReleasesAlternateSignalStack: a pthread key destructor looks at the alternate stack of a thread of WTF after its entry point returned. Nothing is registered then. A stack that the thread registered itself stays registered. On Linux with glibc, testFFI defines sigaltstack() itself (WTF is linked statically into it), and two more cases use that. With the rule of the libc of Darwin, nothing is registered at the end. A stack that cannot be disabled stays registered and mapped. With the destructor of the earlier revision two checks fail: the stack is still registered under the rule of Darwin, and it is unmapped when the disable call fails.
  • testFFI, testSuspendOnAlternateSignalStack: a child process adds SA_ONSTACK to the suspend handler and suspends an idle thread that has an alternate stack. The thread registers one of its own when WTF gives it none. Without the first commit the child is killed by its 30 s alarm (-14). With the first commit it passes at once.
  • The stress test takes Wasm traps and runs into time limits on an agent thread of jsc while the sampling profiler samples it. An agent thread has an alternate stack with this branch, so the test covers the suspension of a thread inside the signal handler ([WTF] The signal handler runs on the alternate signal stack, and a thread that runs it there can be suspended #741) in the fork's CI. 10 of 10 runs pass. On a build with the flag and the alternate stack and without a fix for the suspension, 0 of 10 runs finish in 60 s (earlier revision).
  • The same stress test with SA_ONSTACK added to the suspend handler by an LD_PRELOAD shim: 0 of 5 runs finish in 30 s on the previous revision, 10 of 10 pass on the final one. The jsc of [WTF] The signal handler runs on the alternate signal stack, and a thread that runs it there can be suspended #741 passes 3 of 3, because its agent threads have no alternate stack.
  • 17 sampling-profiler tests and 23 tests with agent threads from JSTests/stress, run directly with jsc: 40 of 40 pass, and 40 of 40 with the shim.

The suspend handler. A self-review of the previous revision found this. The runtime of Go, built as a shared library, adds SA_ONSTACK to each handler that it finds and does not replace (runtime.setsigstack). I loaded a Go 1.24 library that exports one function (-buildmode=c-shared) with bun:ffi: the SIGPWR handler has no SA_ONSTACK before the dlopen and has it after. A debug Bun with the previous revision then hangs in a script that idles for 6 s (1 run, killed at 40 s). With #741 it finishes. The handler then runs on the alternate stack of a thread that has one, sees a stack pointer outside of the thread's own stack and backs off. Thread::suspend() sends the signal again, without end. Measured with a shim that adds the flag, each run with a limit of 30 to 40 s:

Bun main (1.4.3, release) oven-sh/bun#40169 with #741 (debug) with the previous revision (debug) with the final revision (debug)
has an alternate stack main thread threads of Bun threads of Bun and of WTF threads of Bun and of WTF
bun --cpu-prof hangs finishes 3 of 3
node:vm timeout on the main thread hangs hangs finishes 3 of 3
node:vm timeout in a Worker finishes hangs finishes 3 of 3
a process that allocates and then idles 6 s finishes finishes 4 of 4 hangs 4 of 4 finishes 4 of 4
the same with the Go library and no shim finishes hangs finishes 3 of 3
  • In the last row the libpas scavenger suspends an idle heap helper thread to stop its allocators (pas_thread_local_cache_for_all), spins in Thread::suspend() and holds the heap lock. This needs a debug build of WebKit, where fastMalloc is libpas: it sent SIGPWR 84 to 296 times in my runs. A release build uses mimalloc there and sent it 0 times.
  • The first three rows are the hang that WTF: read interrupted SP from ucontext in signalHandlerSuspendResume #235 describes. It exists on main for the main thread of Bun, and Report native stack overflows on every thread bun#40169 extends it to Workers.
  • The commit here changes only the branch for a handler that is not on the thread's own stack. A handler on the thread's own stack does no new work.
  • With the flag set, bun --cpu-prof on the final revision writes a profile with 163 samples. Without the flag it has 140.

The release on Darwin. The destructor passes the stack size to the disable call and keeps a stack mapped that the call did not disable. The libc of Darwin returns ENOMEM for a sigaltstack call with a size below its MINSIGSTKSZ (32768), also when the call disables the stack. I have no macOS machine, so I ran testFFI on Linux with an LD_PRELOAD shim that applies this rule and watches munmap, 1001 threads of WTF:

disable calls rejected stacks unmapped while registered
destructor that passes no size (earlier revision) 1001 1001
final revision 0 0
previous revision, every disable call forced to fail 1001 0 (no stack is unmapped)

Measurements, Release builds:

main this branch revision
syscalls, 100 threads of WTF created and joined 1506 2206 earlier
calls for the stack per thread of WTF 0 7: sigaltstack 4, mmap 1, mprotect 1, munmap 1 final
syscalls, jsc -e 1 (no thread of WTF) 183 to 184 184 earlier
calls for the stack, jsc -e 1 0 0 previous
mappings after 2000 threads created and joined 51 50 earlier
a live thread of WTF +512 KiB rw-p with Rss 0 kB, +4 KiB ---p earlier
sizeof(WTF::Thread) 152 152 earlier
CPU time for one thread of WTF, start to join (median of 8 runs started in turn) 38.0 us (34.6 to 42.2) 44.7 us (38.8 to 51.7) earlier

Text, final revision against #741, same flags: size of ThreadingPOSIX.cpp.o 4117 to 4891 bytes, of Signals.cpp.o 3126 to 3166. By function: wtfThreadEntryPoint 13 to 53 bytes, AlternateSignalStack() 297, ~AlternateSignalStack() 184, signalHandlerSuspendResume 364 to 380, isOnStack 40.

The four calls at the start of a thread are the sigaltstack query, mmap, mprotect of the guard page and sigaltstack. The three at its end are the query, the disable and munmap. strace, perf, valgrind, bloaty and pahole are not installed where I ran this. The syscalls were counted with a ptrace counter and with LD_PRELOAD shims, the record layout was read with clang, the text sizes with size and nm -S.

Deepest use of the alternate stack by the fault handlers of JSC, of 524,288 bytes (earlier revision): release jsc 3,648 bytes (Wasm trap) and 3,928 (VM trap), 11,208 with the profiler suspending the thread inside the handler. Debug Bun 5,000, 5,856 and 12,776.

Bun, final revision. Debug builds without ASan of oven-sh/bun#40169 (11f96f512b), one with the preview build of #741 (autobuild-preview-pr-741-242085de) and one with the preview build of this PR (autobuild-preview-pr-742-2feae769, same code outside of the tests as the head):

with #741 with this PR
overflow on HeapHelper, 5 runs 5 exit 139, stderr empty 5 print panic(HeapHelper): Stack overflow
overflow on JITWorker, 5 runs 5 exit 139, stderr empty 5 print panic(JITWorker): Stack overflow
overflow on the collector thread, 5 runs 5 exit 139, stderr empty 5 print panic(ollector Thread): Stack overflow

CI of the fork. On the head (f51a066) all lanes build and both test jobs pass. The same on f6bee26. On 2feae76, which has the same code outside of the tests, the x64 test job failed once in run-javascriptcore-tests and passed when I ran it again. I cannot read the log of the failed attempt from where I work. That step also needed a second attempt on other branches this week (for example #741 at 5586daa, on arm64). With the LTO jsc of that build on my machine: 340 runs of the new stress test (20 times its 17 modes) have 0 failures, and 5,334 runs of tests and modes that use the sampling profiler have 1, Crashing because current footprint: 635207680 exceeds limit: 629145600. The footprint is resident memory, and main has that failure too.

macOS and musl. I have no machine for either, so oven-sh/bun#44260 (a probe, closed) pinned the preview build of the head, autobuild-preview-pr-742-f51a066e. In the CI of Bun, build 121790, all 181 jobs pass. An earlier run with the build of f6bee26, build 121721, passed too.

  • test/js/bun/jsc-stress/testFFI.test.ts runs the testFFI of the WebKit build. It passed on macOS aarch64 and x64, Alpine 3.23 aarch64 and x64 (musl), Debian 13 x64 and Ubuntu 25.04 aarch64. On macOS that is the overflow of the main thread and of a thread of WTF, the check that a thread of WTF has an alternate stack, and the check that nothing is registered when the thread ends. The last one is the real rule of the libc of Darwin. The suspension test is not built there.
  • The rest of the suite of Bun passed with this build on those lanes, on Windows and on the ASan lane.
  • Read in the sources, not run: XNU (sigaltstack in bsd/kern/kern_sig.c) reports SS_DISABLE for a thread that has no alternate stack, which is what the constructor tests. The libc wrapper returns ENOMEM for a size below MINSIGSTKSZ before it calls the kernel. In the macOS arm64 build of an earlier preview, the disassembly of ~AlternateSignalStack has the disable call with ss_flags 4 and ss_size set to the size of the mapping minus one page, and it clears the mapping (no munmap) when that call fails.

FreeBSD and Android. Built by the CI of the fork and of Bun. Not run.

ASan. ASan gives every thread an alternate stack of 58,112 bytes on my machine, without a guard page. A thread of WTF issues one sigaltstack query under ASan and keeps the stack of ASan. #741 keeps SA_ONSTACK off the handler in ASan builds, because the VM trap handler used 72,000 bytes there.

Still silent after this change.

  • ASan builds, after the first VM.
  • A thread that neither WTF nor the embedder creates. In Bun that is the mimalloc scavenger thread (mi-scavenger).
  • An overflow in a pthread TLS destructor of a thread of WTF: the stack is released when the entry point returns.
  • Android: bionic gives every thread a small alternate stack, and it stays.

Related. #235 makes the same decision from the context of the signal, for the code before #741. The first commit here is that check on top of #741, limited to the builds with the additions of Bun. #235 and #397 change other functions of ThreadingPOSIX.cpp too (initializePlatformThreading, initializeTLS). Neither changes wtfThreadEntryPoint.

Alternative for Linux. __wrap_pthread_create in Bun (src/jsc/bindings/c-bindings.cpp) is in front of every pthread_create of the Bun binary, so it can give a stack to every thread, the mimalloc thread too. Bun links it on Linux only (scripts/build/flags.ts, when: c => c.linux), and the crash handler of Bun is a signal handler on macOS too. It can be a later change in Bun for the threads that this PR does not reach.

The Bun side is oven-sh/bun#40169. It pins the preview build of #741, so it needs the first commit of this PR in its WebKit too (table above, Worker row). Its branch has no hook that sets the flag again after JSC installs its handler (CrashHandler__keepSignalHandlersOnAltStack, called once from Bun::tryCreateVM, with the lint test/internal/source-lints/vm-creation.test.ts), so Bun has nothing to delete after the upgrade. Drop of src/bun_core/signal_stack.rs there passes no size to the disable call, so on macOS it has the problem that the destructor here avoids.

@robobun

robobun commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status. Ready for review, stacked on #741 (242085d). The branch has four commits: the suspend handler, the tests that hold without an alternate stack on the threads of WTF, the alternate stack, and the tests that need it. #741 can take the first two.

How I reproduced the two problems, on Linux x64:

  • Overflow on a thread of WTF: an LD_PRELOAD hook on prctl(PR_SET_NAME) recurses without end on the thread that takes the name HeapHelper or JITWorker. Bun ends with exit 139 and an empty stderr. With an alternate stack on that thread it prints panic(HeapHelper): Stack overflow. In testFFI the new test overflows a thread from WTF::Thread::create() in a child process that has a handler with SA_ONSTACK.
  • Suspension: an LD_PRELOAD shim adds SA_ONSTACK to the SIGPWR handler, as the runtime of Go does when a process loads it. bun --cpu-prof on Bun 1.4.3 does not finish. A debug Bun with the previous revision of this PR does not finish a script that idles for 6 s, also with a real Go library loaded through bun:ffi and no shim. In testFFI the new test adds the flag and suspends a thread of WTF in a child process.

A self-review found two defects in earlier revisions, and a gap in the tests. The current revision has a fix for each:

  1. The destructor passed no size when it disabled the stack. The libc of Darwin rejects that call, so the stack was unmapped while it was still registered.
  2. With an alternate stack on the threads of WTF, a suspend handler that has SA_ONSTACK runs on that stack and backs off, and Thread::suspend() retries without end. The first commit makes the handler read the interrupted stack pointer from its context. With it, the cases above finish in a debug Bun built with the preview of this PR.
  3. No test failed when the destructor stopped passing the size. testThreadReleasesAlternateSignalStack looks at the stack from a pthread key destructor, after the entry point of the thread returned. It fails with the earlier destructor.

CI: on the head (f51a066) all lanes of the fork build and both test jobs pass.

macOS and musl: oven-sh/bun#44260 (a probe, closed) pinned the preview build of the head. In the CI of Bun all 181 jobs pass, and the testFFI of this build passed on macOS aarch64 and x64 and on Alpine aarch64 and x64.

Not run: FreeBSD and Android (built only).

Open, and not in this PR: oven-sh/bun#40169 with #741 needs the first commit here. #741 can take commits 1 and 2 as they are (a fast-forward to b37f482). When #741 merges, I rebase this branch on main and retarget it.

@robobun
robobun force-pushed the robobun/3ff782a7/signal-handler-altstack branch from 2fc6c01 to ff791a9 Compare September 29, 2026 01:06
@robobun robobun changed the title [WTF] Make SA_ONSTACK safe: suspend on any stack, skip ASan, alternate stack for WTF threads [WTF] Give WTF threads an alternate signal stack and keep SA_ONSTACK off ASan builds Sep 29, 2026
@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Preview build of f51a066: autobuild-preview-pr-742-f51a066e

robobun added a commit that referenced this pull request Sep 29, 2026
…eep SA_ONSTACK off ASan builds

jscSignalHandler decided from uc_stack.ss_flags if the code that it
interrupted ran on the alternate stack. Since Linux 4.7 that field holds the
flags that the thread passed to sigaltstack(), so the test was the same for
every signal on a thread.

The outermost invocation on a thread publishes its registers, and
registersInterruptedBySignalHandler() returns them only when their stack
pointer is in the stack of the thread. A fault inside a handler on the
alternate stack leaves the suspension to back off, as before.

ASan gives every thread an alternate stack of about 58 KB without a guard
page, and the VMTraps handler of an ASan build needs more. ASan builds keep
SA_SIGINFO alone. The change is the one of #742.
@robobun

robobun commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator Author

A note for the rebase of this PR: #741 moved to 242085d.

  • It has the ASan guard of this PR now (#if USE(BUN_JSC_ADDITIONS) && !ASAN_ENABLED in SignalHandlers::finalize(), with the same comment), so that [WTF] The signal handler runs on the alternate signal stack, and a thread that runs it there can be suspended #741 is safe when it merges alone. The hunk of Signals.cpp here becomes empty.
  • registersInterruptedBySignalHandler() takes the StackBounds of the thread and checks the stack pointer of the published registers against it. The test of uc_stack.ss_flags is gone: since Linux 4.7 that field holds the flags of the registration.

The stress test of this PR covers the suspension inside the handler in the CI of the fork, which #741 cannot do: no thread of jsc has an alternate stack there.

@robobun
robobun force-pushed the robobun/3ff782a7/signal-handler-altstack branch from ff791a9 to a66379d Compare September 29, 2026 14:52
@robobun robobun changed the title [WTF] Give WTF threads an alternate signal stack and keep SA_ONSTACK off ASan builds [WTF] Give the threads that WTF creates an alternate signal stack Sep 29, 2026
…gnal stack

Thread::signalHandlerSuspendResume() decides from its own stack pointer
whether the signal interrupted the thread's own stack. That is wrong when
the handler has SA_ONSTACK and the thread has an alternate signal stack:
the handler runs there, backs off, and Thread::suspend() tries again
without end. The runtime of Go adds SA_ONSTACK to the handlers that it
finds when a process loads it.

When the handler is not on the thread's own stack, it reads the stack
pointer that the signal interrupted from its context. If that stack
pointer is in the thread's own stack, the registers of the context are
the ones that it publishes.
@robobun
robobun force-pushed the robobun/3ff782a7/signal-handler-altstack branch from a66379d to 2feae76 Compare September 29, 2026 21:59
@robobun robobun changed the title [WTF] Give the threads that WTF creates an alternate signal stack [WTF] Give WTF threads an alternate signal stack and make the suspend handler work on one Sep 29, 2026
testFFI runs itself as a child that registers a crash handler with
SA_ONSTACK, creates a VM and overflows the native stack of the main
thread. The crash handler runs only when the handler that JSC registers
has SA_ONSTACK too.

A second child adds SA_ONSTACK to the handler that suspends a thread, as
the runtime of Go does, and suspends a thread that has an alternate
signal stack.
The kernel can only deliver a stack overflow on an alternate signal
stack: the fault is on the guard page of the thread's own stack. An
embedder with a crash handler gives one to the threads it creates. The
threads of WTF (heap helpers, JIT worklist, collector) had none.

wtfThreadEntryPoint() registers 512 KiB with a guard page below it for as
long as the entry point runs. It keeps a stack that is registered
already: bionic and the sanitizer runtimes give every thread one.

When the entry point returns, the stack is disabled and unmapped. The
disable passes the size of the stack, which Darwin checks there too, and
a stack that could not be disabled stays mapped.
…and a profiled thread on an alternate signal stack finishes

testFFI overflows the native stack of a thread that WTF creates, in the
child that has the crash handler. It also checks that a thread of WTF has
an alternate stack, and what is left of it when the thread ends: nothing
is registered, a stack that the thread registered itself stays, and a
stack that cannot be disabled stays mapped. The last case and the rule of
the libc of Darwin need a sigaltstack() that can fail. On Linux with
glibc, testFFI defines one: WTF is linked statically into it.

The stress test takes Wasm traps and runs into time limits on an agent
thread while the sampling profiler samples it. The agent thread has an
alternate stack, so the signal handlers of JSC run on it.
@robobun
robobun force-pushed the robobun/3ff782a7/signal-handler-altstack branch from f6bee26 to f51a066 Compare September 30, 2026 04:54
robobun added a commit to oven-sh/bun that referenced this pull request Sep 30, 2026
autobuild-preview-pr-742-f51a066e has one more testFFI test, which runs
on macOS too. Not for merge.
@robobun
robobun marked this pull request as ready for review September 30, 2026 08:10

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Beyond the inline findings, I also checked the guard mismatch between the new isOnStack declaration in Source/WTF/wtf/threads/Signals.h (!OS(DARWIN)) and its use in ThreadingPOSIX.cpp (HAVE(MACHINE_CONTEXT) only) — the whole signalHandlerSuspendResume body sits inside the file's #if !OS(DARWIN) block, so macOS builds never see the call. The AlternateSignalStack destructor's registered-stack check and the leak-on-disable-failure path also read correctly against the Darwin size rule.

Extended reasoning...

The change adds a per-thread 512 KiB sigaltstack RAII object to wtfThreadEntryPoint and makes the SIGPWR suspend handler read the interrupted stack pointer from the ucontext when it runs on an alternate stack, plus testFFI and a JSTests stress test. It touches signal-handling and thread-suspension code under Source/WTF (CODEOWNER-listed) but no auth, injection or data-exposure surface. Inline findings are posted, so a human look is already signalled; this note only records the Darwin guard question that was examined and ruled out.

if (isOnStack(interruptedRegisters, thread->m_stack))
registersOfOwnStack = &interruptedRegisters;
else
registersOfOwnStack = registersInterruptedBySignalHandler(thread->m_stack);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 (optional) Anyone building this fork for a Linux or FreeBSD CPU other than x64 or arm64 gets a hang in every suspension of a WTF thread that is inside a JSC signal handler, which the base branch handled. stackPointerOf in Signals.cpp:489-503 returns nullptr there, so isOnStack at ThreadingPOSIX.cpp:219 and registersInterruptedBySignalHandler at line 222 are always false and the handler backs off at line 231. With this change every WTF thread has an alternate stack, so a Wasm trap or JIT fault on a Worker thread while the GC or sampling profiler suspends it spins Thread::suspend forever. Fix: fall back to publishing the ucontext registers, or compile the alternate stack only where stackPointerOf has an accessor.

Why this was flagged

If any consumer builds this fork for Linux on ARM32, RISCV64 or PPC64 (the CI lanes in .github/scripts/lanes.mjs are all x64 or arm64, so this population is outside the fork's own builds), stackPointerOf at Signals.cpp:499-502 returns nullptr. Before this change WTF threads had no alternate stack, so jscSignalHandler ran on the thread's own stack and signalHandlerSuspendResume took the fast path at line 209 and never consulted stackPointerOf. Now ThreadingPOSIX.cpp:362-372 registers an alternate stack on every WTF thread and jscSignalHandler runs there with SA_ONSTACK; a suspend arriving during it reaches line 219, where isOnStack is false, and line 222, where registersInterruptedBySignalHandler is also false, so the thread posts null at line 231-232 and Thread::suspend at line 748-762 retries forever while the JSC handler waits for the suspender's lock (the case the comment at line 215-217 describes). The GC or SamplingProfiler then never completes. Remedy: on architectures without an accessor, skip the alternate stack or publish the ucontext registers rather than back off.

Verification: nit — trigger: the fork built for Linux/FreeBSD on a CPU other than x64/arm64 that still has HAVE(MACHINE_CONTEXT) (CPU(ARM), CPU(RISCV64) per Source/WTF/wtf/PlatformHave.h:235), a target none of the fork's CI lanes (.github/scripts/lanes.mjs: amd64/arm64 only) or Bun itself builds today; when it is built, a WTF thread (jsc agent/Worker thread) that takes a Wasm trap while the sampling profiler samples it hangs the process, which the base does not do.

Mechanism verified.

Comment on lines +9 to +10
const numberOfTraps = 20000;
const numberOfTimeLimits = 30;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 (optional) Maintainers get a new stress test that breaks the JSTests/README.md rules for new tests: it runs well over the 200 ms budget in every configuration and ignores testLoopCount. The agent waits 30 times for a 5 ms time limit (at least 150 ms) and then takes 20000 Wasm traps under a 50 us sampling interval, with both counts hard-coded at JSTests/stress/sampling-profiler-thread-on-alternate-signal-stack.js:9-10. Fix: size the two loops from testLoopCount / wasmTestLoopCount (or cut them so the test stays under 200 ms with --report-execution-time in every run-jsc-stress-tests configuration), and since it embeds a Wasm module, consider JSTests/wasm/stress per the same README.

Why this was flagged

JSTests/CLAUDE.md imports JSTests/README.md, whose rules for new tests are: tests must run under 200 ms in all configurations (rule 1), use testLoopCount or wasmTestLoopCount for iteration counts (rule 2), and put wasm-involving tests in JSTests/wasm/stress. The new file JSTests/stress/sampling-profiler-thread-on-alternate-signal-stack.js:9-10 hard-codes numberOfTraps = 20000 and numberOfTimeLimits = 30, and JSTests/stress/sampling-profiler-thread-on-alternate-signal-stack.js:47 calls $vm.callWithTimeLimit(() => { for (;;) { } }, 5) thirty times, which alone spends at least 150 ms; the 20000 SIGSEGV-driven Wasm traps under --sampleInterval=50 add more. run-jsc-stress-tests runs every stress test in multiple configurations (no-cjit, ftl-eager, etc.), so each run of the CI test job on the Linux x64 and arm64 LTO lanes pays this repeatedly. On the base branch this file does not exist. The PR description reports the test passes 10 of 10 runs but gives no execution time, so the 200 ms rule is not shown to hold.

Verification: nit. Trigger: the new stress test runs whenever run-javascriptcore-tests / run-jsc-stress-tests JSTests/stress executes (the fork's Linux x64/arm64 CI lanes do this), in every default stress configuration.

if (sigaltstack(nullptr, &current) || !(current.ss_flags & SS_DISABLE))
return;

m_mapping = MmapSpan<uint8_t>::tryAlloc(pageSize() + size);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 (optional) Every WTF thread in Bun release builds now costs twice the mapped address space and commit charge, and two extra VMAs, so hosts with strict overcommit or a low vm.max_map_count hit ENOMEM sooner. Threading.cpp:161 gives each WTF thread a 512 KiB pthread stack under USE(BUN_JSC_ADDITIONS), and ThreadingPOSIX.cpp:362 maps another 512 KiB plus a page for the alternate stack, so the dismissal's premise that the pthread stack is already the larger mapping is wrong. Fix: size the alternate stack for the handler that actually runs there (MINSIGSTKSZ-scale, or lazily) or reserve it once per process pool rather than per thread.

Why this was flagged

The dismissal assumed each pthread stack is a much larger mapping than the new alternate stack. Under USE(BUN_JSC_ADDITIONS) with !ASSERT_ENABLED, Threading.cpp:159-161 requests a 512 KiB stack for every WTF thread, the same as the alternate stack size at ThreadingPOSIX.cpp:400. Each thread now adds a 516 KiB MAP_PRIVATE|MAP_ANON mapping (line 362) that vm.overcommit_memory=2 counts fully against CommitLimit, and the mprotect at line 367 splits it into two VMAs. Processes that run near max_map_count or under strict overcommit (containers with memory-limited cgroups and overcommit_memory=2) fail thread creation or mmap in JSC earlier than on the base branch, where a WTF thread had only its pthread stack. Remedy: use a smaller alternate stack or reuse a per-process pool.

Verification: nit; acknowledged in diff: the PR description states "Each thread of WTF costs ... 516 KiB of address space", and that bound is accurate. Triggering condition: a Bun process running on a host with vm.overcommit_memory=2 (or a low vm.max_map_count) already close to its limit.

Mechanism verified.

if (typeof WebAssembly !== "object")
return ${numberOfTraps};

// (module (memory 1) (func (export "load") (param i32) (result i32) (i32.load (local.get 0))))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 nit (optional): The new test builds and runs a WebAssembly module, but JSTests/README.md says tests that involve wasm belong in JSTests/wasm/stress, not JSTests/stress. Fix: move the file under JSTests/wasm/stress (keeping the WebAssembly-absent fallback at line 14 if the runner still lists it for non-wasm builds), or split the Wasm trap half from the time-limit half.

Why this was flagged

JSTests/README.md, imported by JSTests/CLAUDE.md, states "Put tests that only target JS behavior in JSTests/stress and tests that involve wasm in JSTests/wasm/stress." The new file at JSTests/stress/sampling-profiler-thread-on-alternate-signal-stack.js:18-26 compiles a hand-encoded Wasm module and calls its export 20000 times at line 31 to take Wasm traps. The test therefore involves wasm but is placed in JSTests/stress, which the guidance reserves for JS-only behaviour. This is a convention slip only; no runtime failure follows from it.

Verification: nit. Triggering condition: any time the file is added as-is. The rule is real: /home/claude/webkit/JSTests/README.md:15 (imported by /home/claude/webkit/JSTests/CLAUDE.md:1 via @ README.md) says under "Adding Tests": "Put tests that only target JS behavior in JSTests/stress and tests that involve wasm in JSTests/wasm/stress."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant