Skip to content

Commit

Permalink
vm: reset signals when freeing VM
Browse files Browse the repository at this point in the history
When invoking `uc_vm_free()` on a VM with enabled `.setup_signal_handlers`
configuration, reset system signal handlers back to their default actions.

Signed-off-by: Jo-Philipp Wich <[email protected]>
  • Loading branch information
jow- committed Dec 2, 2024
1 parent 0e09c26 commit c6732e9
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,26 @@ uc_vm_signal_handlers_setup(uc_vm_t *vm)
tctx->signal_handler_vm = vm;
}

static void
uc_vm_signal_handlers_reset(uc_vm_t *vm)
{
uc_thread_context_t *tctx = uc_thread_context_get();
struct sigaction sa = { 0 };
size_t signo;

if (vm != tctx->signal_handler_vm)
return;

sa.sa_handler = SIG_DFL;
sigemptyset(&sa.sa_mask);

for (signo = 0; signo < ucv_array_length(vm->signal.handler); signo++)
if (ucv_is_callable(ucv_array_get(vm->signal.handler, signo)))
sigaction(signo, &sa, NULL);

tctx->signal_handler_vm = NULL;
}

void uc_vm_init(uc_vm_t *vm, uc_parse_config_t *config)
{
vm->exception.type = EXCEPTION_NONE;
Expand Down Expand Up @@ -217,6 +237,8 @@ void uc_vm_free(uc_vm_t *vm)
uc_upvalref_t *ref;
size_t i;

uc_vm_signal_handlers_reset(vm);

ucv_put(vm->exception.stacktrace);
free(vm->exception.message);

Expand Down

0 comments on commit c6732e9

Please sign in to comment.