Skip to content

vm, types: handle signals on threads without a VM - #529

Open
Cypih wants to merge 1 commit into
ucode-lang:masterfrom
Cypih:fix/signal-handler-null-vm
Open

Cypih wants to merge 1 commit into
ucode-lang:masterfrom
Cypih:fix/signal-handler-null-vm

Conversation

@Cypih

@Cypih Cypih commented Nov 30, 2026 •

Copy link
Copy Markdown

signal() installs its C handler for the whole process, but since efeb578 the handler finds the VM only in the thread-local context. On a thread without a VM, for example a thread that a C library starts, the VM pointer is NULL and the process crashes (SIGSEGV, or the assert with -DDEBUG=ON).

Closes #527.

On such a thread the handler now sends the signal on with pthread_kill() to the thread of the first VM that set up signal handlers. uc_vm_free() of that VM stops this and waits for handlers in progress, before it restores the default actions. After that, the handler drops such signals until a new VM sets up handlers.
Limits: the wait does not end in a child of fork() that frees the VM while a parent thread is in such a handler; if the thread of that VM ends without uc_vm_free(), a later forward targets an ended thread.

Test: the new 17_lib_ffi/36_signal_foreign_thread fails on master and passes with the change (gcc, clang, ucode-san). The rest of the custom suite is unchanged.

Not tested: uloop scripts, several VMs in several threads, musl, 32-bit targets, WASM, TSan.

@jow-

jow- commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the fix. The CI fails right now due to a memory "leak" caused by pthread_create(). Can you amend the test file to pthread_join() at the end? That should make valgrind happy.

signal() installs uc_vm_signal_handler() with sigaction(), so the kernel
can run it on any thread of the process that does not block the signal.
Since commit efeb578, the handler reads the VM from the thread-local
context. On a thread that never created a VM, for example the event
thread of a C library that a script calls through the FFI,
uc_thread_context_get() allocates a context with calloc() inside the
signal handler, and the VM pointer of that context is NULL. With NDEBUG,
uc_vm_signal_raise() then writes through the NULL pointer and the process
gets SIGSEGV. Without NDEBUG, assert(vm) aborts the process.

Read the context with the new uc_thread_context_peek(), which does not
call calloc(). On a thread without a VM, send the signal on with
pthread_kill() to the thread of the first VM that set up signal handlers
in the process. That thread runs the handler again and raises the signal
in its VM, as before. So only the thread of a VM writes its bitmap of
raised signals and its self-pipe, and a handler on another thread reads
no VM memory. The handler also keeps errno now.

uc_vm_free() of that first VM stops the forwarding before it restores
the default actions. It then waits until no handler on another thread is
about to call pthread_kill(). So a forwarded signal still finds the
handler, and no pthread_kill() gets that thread after uc_vm_free()
returned. The wait is a loop around sched_yield(). It does not end in a
child of fork() that frees the VM while another thread of the parent was
in that window, or when such a handler does not return.

Limits: a signal on a thread without a VM always goes to the thread of
the first VM, also when other threads run VMs of their own. After
uc_vm_free() of that VM, the handler drops such signals until a new VM
sets up signal handlers. If the thread of the VM blocks the signal, the
signal stays pending for that thread until it unblocks it. If that
thread ends without uc_vm_free(), a later forward calls pthread_kill()
with the ID of an ended thread.

glibc before 2.34 has pthread_kill() in libpthread only, so link
libpthread when libc does not have it.

Fixes: efeb578 ("types, vm: refactor usage of global variables")
Signed-off-by: Piotr Kubik <piotr@kubik.pl>
@Cypih
Cypih force-pushed the fix/signal-handler-null-vm branch from 46d833a to 3354b18 Compare November 30, 2026 15:12
@Cypih

Cypih commented Nov 30, 2026

Copy link
Copy Markdown
Author

Thanks for the fix. The CI fails right now due to a memory "leak" caused by pthread_create(). Can you amend the test file to pthread_join() at the end? That should make valgrind happy.

I force-pushed 3354b18;
only the test changed: it now cancels the thread and joins it with pthread_join() at the end.
The cancel is needed, because the signal can arrive before the thread enters pause(), which then does not return; pause() is a cancellation point.

In the CI image (openwrt-ci/native-testing), the custom tests now pass under valgrind, test 36 included.

This branch has not been deployed

No deployments
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.

signal(): SIGSEGV when the kernel runs the handler on a thread without a VM

2 participants