From 4ae056867d96b1795fec7f4cfd0f68b124e398cd Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Mon, 5 Jul 2021 20:24:43 +0200 Subject: lib, vm: reimplement exit() as exception type Instead of invoking exit(3) from uc_exit(), use a new EXCEPTION_EXIT exception type to instruct the VM to shutdown cleanly. This is required to not terminate the host program in case libucode is embedded and loaded scripts invoke the exit() function. Signed-off-by: Jo-Philipp Wich --- vm.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'vm.c') diff --git a/vm.c b/vm.c index 15e510d..f4c56db 100644 --- a/vm.c +++ b/vm.c @@ -77,6 +77,7 @@ static const char *exception_type_strings[] = { [EXCEPTION_TYPE] = "Type error", [EXCEPTION_REFERENCE] = "Reference error", [EXCEPTION_USER] = "Error", + [EXCEPTION_EXIT] = "Exit" }; @@ -2256,6 +2257,16 @@ uc_vm_execute_chunk(uc_vm *vm, uc_value_t **retvalp) /* previous instruction raised exception */ if (vm->exception.type != EXCEPTION_NONE) { + /* VM termination was requested */ + if (vm->exception.type == EXCEPTION_EXIT) { + uc_vm_reset_callframes(vm); + + if (retvalp) + *retvalp = ucv_int64_new(vm->arg.s32); + + return STATUS_EXIT; + } + /* walk up callframes until something handles the exception or the root is reached */ while (!uc_vm_handle_exception(vm)) { /* no further callframe to pop, report unhandled exception and terminate */ -- cgit v1.2.3