Repository navigation
Conversation
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
force-pushed
the
fix/signal-handler-null-vm
branch
from
November 30, 2026 15:12
46d833a to
3354b18
Compare
Author
I force-pushed 3354b18; In the CI image ( |
This branch has not been deployed
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.
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.