From c6732e9e738cedecd19171df2846a7331a25dc35 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Mon, 2 Dec 2024 12:56:19 +0100 Subject: [PATCH] vm: reset signals when freeing VM 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 --- vm.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/vm.c b/vm.c index ce9c2170..1c40307c 100644 --- a/vm.c +++ b/vm.c @@ -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; @@ -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);