Skip to content

pass a fault outside Scheme code to the previously installed handler - #1076

Merged
mflatt merged 1 commit into
cisco:mainfrom
burinc:chain-host-signals
Oct 10, 2026
Merged

mflatt merged 1 commit into
cisco:mainfrom
burinc:chain-host-signals

Conversation

@burinc

@burinc burinc commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

When Chez Scheme is embedded in a process that already handles SIGSEGV or
SIGBUS, Sscheme_init replaces that handler. .NET and Java runtimes work
this way: they turn a null dereference in their own code into an exception
from a signal handler. Once Chez Scheme is initialized, a fault like that on
one of the host's threads reaches handle_signal, which finds no thread
context and aborts with "nonrecoverable invalid memory reference", even
though no Scheme code was running.

This keeps the handlers that init_signal_handlers replaces for SIGSEGV,
SIGBUS, SIGFPE and SIGILL. When one of those signals arrives on a thread
that has no context, or whose context is deactivated (in a __collect_safe
call or after Sdeactivate_thread), it goes to the saved handler instead. If
there was no saved handler (SIG_DFL or SIG_IGN), or the fault happens
while the thread is running Scheme code, behavior is unchanged.
Sscheme_deinit puts the saved handlers back, since the signals belong to
the host again once Chez Scheme is gone, and a later Sscheme_init saves
them afresh.

I tested it with a small C program that installs a SIGSEGV/SIGBUS handler,
then calls Sscheme_init, and dereferences NULL in a few situations
(macOS arm64, tarm64osx):

case before after
fault on a pthread that never entered Scheme abort host handler
fault on the main thread after Sdeactivate_thread abort host handler
Sscheme_deinit, Sscheme_init, then fault on a new pthread abort host handler
Sscheme_deinit, then fault on the main thread abort host handler
(foreign-ref 'int 0 0) in Scheme code "invalid memory reference" same

make test-one passes. I can add the C program to the PR if that's useful,
though I didn't see an existing place in mats for tests that need to own
main.

Some things this doesn't do:

  • The saved handler is called directly, so its own sa_mask and flags such
    as SA_ONSTACK and SA_RESETHAND aren't applied. In particular, a stack
    overflow in host code still can't be handled, since Chez Scheme's handler
    isn't installed with SA_ONSTACK.
  • The run time also deactivates a thread around its own blocking I/O, so a
    fault inside that C code now goes to the host's handler too, if the host
    has one.
  • Without pthreads a thread always has a context, so nothing changes in a
    non-threaded build.

When Chez Scheme is embedded in a process that handles SIGSEGV/SIGBUS
itself (.NET and Java runtimes turn a null dereference into an exception
that way), Sscheme_init replaces the host's handler: a fault in host code
then reaches handle_signal, which aborts with "nonrecoverable invalid
memory reference" although no Scheme code was involved.

Keep the handlers that init_signal_handlers replaces for SIGSEGV, SIGBUS,
SIGFPE and SIGILL, and pass such a signal to the saved handler when the
thread has no context or its context is deactivated. Sscheme_deinit puts
the saved handlers back, so a fault after it reaches the host, and a later
Sscheme_init saves them afresh. Without a saved handler, or in Scheme
code, nothing changes.
@mflatt

mflatt commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

I will look at this more, but I think it's probably a good idea.

burinc pushed a commit to burinc/jolt that referenced this pull request Oct 9, 2026
Sscheme_init takes SIGSEGV/SIGBUS/SIGFPE/SIGILL for the whole process, so
a host that handles those itself (.NET, the JVM) aborted with "invalid
memory reference" on a null dereference in its own code once a jolt
library was loaded. The library stub now saves the host's handlers before
init, puts a handler in front of Chez's that sends a fault to the host's
when the thread has no Chez context or is the released init thread, and
gives the signals back at shutdown. A fault in jolt code on other threads
is still a jolt exception.

This stands in for cisco/ChezScheme#1076, which checks the context's
active flag directly; jolt can't see that flag, so a fault in jolt code
while the released init thread is inside an export goes to the host.

The signals driver now also checks a fault in Scheme code and a host
fault on a worker thread that called an export, and the smoke step fails
instead of reporting.

Fixes jolt-lang#1277
@mflatt
mflatt merged commit 41d5ffc into cisco:main Oct 10, 2026
17 checks passed
@burgerrg

Copy link
Copy Markdown
Contributor

Should fault_handler use not_running_scheme? It's used on Windows and currently checks for a zero thread context.

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.

3 participants

⌂