From dbd9a6ac7aff01c2c705caef10beb987bcb28ac9 Mon Sep 17 00:00:00 2001 From: John Crispin Date: Thu, 29 Oct 2026 07:57:18 +0100 Subject: [PATCH 01/21] vm: release only the call frames of the current invocation on exit On exit(), uc_vm_execute_chunk() releases the call frames and the stack slots of all active invocations, also those of C functions that called back into ucode and still run. Such a C function then returns to an empty stack: after an exit() in the code that include() or call() runs with a scope, uc_callfunc() pops below the base of the stack. Release the call frames only down to the depth of the invocation, like for an uncaught exception. Keep the exit pending while a call frame is active, so that a C function that goes on after the failed call can neither run more code nor replace the exit with another exception. Fixes: 4ae056867d96 ("lib, vm: reimplement exit() as exception type") Signed-off-by: John Crispin Signed-off-by: Felix Fietkau --- include/ucode/internal/vm.h | 4 + lib.c | 28 ++---- .../06_lib_uloop/14_callback_exit_same_pass | 27 ++++++ .../99_bugs/64_exit_from_scoped_include | 52 ++++++++++ .../68_exit_pending_in_native_function | 94 +++++++++++++++++++ types.c | 10 +- vm.c | 74 ++++++++++++--- 7 files changed, 250 insertions(+), 39 deletions(-) create mode 100644 tests/custom/06_lib_uloop/14_callback_exit_same_pass create mode 100644 tests/custom/99_bugs/64_exit_from_scoped_include create mode 100644 tests/custom/99_bugs/68_exit_pending_in_native_function diff --git a/include/ucode/internal/vm.h b/include/ucode/internal/vm.h index 7adf4e12..f89e696a 100644 --- a/include/ucode/internal/vm.h +++ b/include/ucode/internal/vm.h @@ -20,6 +20,7 @@ #define UCODE_INTERNAL_VM_H #include "ucode/vm.h" +#include "ucode/internal/util.h" #define __insns \ __insn(NOOP) \ @@ -109,6 +110,9 @@ extern const int8_t uc_vm_insn_format[__I_MAX]; int8_t uc_vm_insn_to_argtype(uc_vm_insn_t insn); +__hidden void uc_vm_raise_exception_trace(uc_vm_t *vm, uc_exception_type_t type, + const char *message, uc_value_t *stacktrace); + static inline bool uc_vm_is_strict(uc_vm_t *vm) { diff --git a/lib.c b/lib.c index 508c4cd1..69a7410c 100644 --- a/lib.c +++ b/lib.c @@ -941,12 +941,8 @@ uc_die(uc_vm_t *vm, size_t nargs) s = msg ? uc_cast_string(vm, &msg, &freeable) : "Died"; - vm->exception.type = etype; - free(vm->exception.message); - vm->exception.message = strdup(s); - - ucv_put(vm->exception.stacktrace); - vm->exception.stacktrace = ucv_get(ucv_object_get(arg, "stacktrace", NULL)); + uc_vm_raise_exception_trace(vm, etype, s, + ucv_object_get(arg, "stacktrace", NULL)); } else { @@ -3044,19 +3040,15 @@ uc_require_ucode(uc_vm_t *vm, const char *path, uc_value_t *scope, uc_value_t ** *res = uc_callfunc(vm, 3); - if (vm->exception.type != EXCEPTION_EXIT) { - if (module_mode) { - ucv_put(*res); - *res = uc_require_imports(vm, closure); - } - - if (vm->stack.count >= 3) { - uc_vm_stack_pop(vm); - uc_vm_stack_pop(vm); - uc_vm_stack_pop(vm); - ucv_put(closure); - } + if (module_mode && vm->exception.type != EXCEPTION_EXIT) { + ucv_put(*res); + *res = uc_require_imports(vm, closure); } + + uc_vm_stack_pop(vm); + uc_vm_stack_pop(vm); + uc_vm_stack_pop(vm); + ucv_put(closure); } vm->config = prev_config; diff --git a/tests/custom/06_lib_uloop/14_callback_exit_same_pass b/tests/custom/06_lib_uloop/14_callback_exit_same_pass new file mode 100644 index 00000000..55e73674 --- /dev/null +++ b/tests/custom/06_lib_uloop/14_callback_exit_same_pass @@ -0,0 +1,27 @@ +Calling exit() in a uloop callback terminates the script with the given +code, also when the loop runs a further callback in the same pass. + +-- Testcase -- +{% + import * as uloop from 'uloop'; + + uloop.init(); + + uloop.timer(1, () => { print("first\n"); exit(5); }); + uloop.timer(1, () => print("second\n")); + + // both timers are due when the loop starts + sleep(50); + + uloop.run(); + print("run() returned\n"); +%} +-- End -- + +-- Expect stdout -- +first +-- End -- + +-- Expect exitcode -- +5 +-- End -- diff --git a/tests/custom/99_bugs/64_exit_from_scoped_include b/tests/custom/99_bugs/64_exit_from_scoped_include new file mode 100644 index 00000000..fd1946d8 --- /dev/null +++ b/tests/custom/99_bugs/64_exit_from_scoped_include @@ -0,0 +1,52 @@ +When exit() was called within code that a C function such as include() +or call() invoked with a scope, the VM released the call frames of all +active invocations, so the C function popped a value from an empty stack +afterwards and corrupted the memory below it. + +-- Testcase -- +{% + try { + include("files/part.uc", { value: 7 }); + } + catch (e) { + print("caught\n"); + } +%} +-- End -- + +-- File part.uc -- +{% + let o = {}; + o.self = o; + gc(); + print(value, "\n"); + exit(value); +%} +-- End -- + +-- Expect stdout -- +7 +-- End -- + +-- Expect exitcode -- +7 +-- End -- + + +Calling exit() within a function that call() runs with a scope ends the +program with the given code. + +-- Testcase -- +{% + call(function() { print(a, "\n"); exit(3); }, null, { a: 1 }); + print("not reached\n"); +%} +-- End -- + +-- Expect stdout -- +1 +-- End -- + +-- Expect exitcode -- +3 +-- End -- diff --git a/tests/custom/99_bugs/68_exit_pending_in_native_function b/tests/custom/99_bugs/68_exit_pending_in_native_function new file mode 100644 index 00000000..6908dabb --- /dev/null +++ b/tests/custom/99_bugs/68_exit_pending_in_native_function @@ -0,0 +1,94 @@ +When a callback of a C function called exit() and the C function went on, +the exit request was lost: replace() called its callback for the next match, +which cleared the pending exit. + +-- Testcase -- +{% + let n = 0; + let r = replace("aaa", /a/g, function() { if (!n++) exit(3); return "b"; }); + print("not reached: ", r, "\n"); +%} +-- End -- + +-- Expect stdout -- +-- End -- + +-- Expect exitcode -- +3 +-- End -- + + +A type error that a C function or the VM raises after the exit request does +not replace it, so that try does not catch it. + +-- Testcase -- +{% + let o = proto({}, { tostring: function() { exit(15); } }); + + function f(...args) {} + + try { + f(...o); + } + catch (e) { + print("caught ", e.message, "\n"); + } + + print("not reached\n"); +%} +-- End -- + +-- Expect stdout -- +-- End -- + +-- Expect exitcode -- +15 +-- End -- + +-- Testcase -- +{% + let o = proto({}, { tostring: function() { exit(16); } }); + + try { + print([ ...o ], "\n"); + } + catch (e) { + print("caught ", e.message, "\n"); + } + + print("not reached\n"); +%} +-- End -- + +-- Expect stdout -- +-- End -- + +-- Expect exitcode -- +16 +-- End -- + + +An exception object that die() raises after the exit request does not +replace it. + +-- Testcase -- +{% + let m = proto({}, { tostring: function() { exit(17); } }); + + try { + die({ type: "Type error", message: m, stacktrace: [ { filename: "x", line: 1, byte: 1 } ] }); + } + catch (e) { + print("caught ", e.type, "\n"); + } + + print("not reached\n"); +%} +-- End -- + +-- Expect stdout -- +-- End -- + +-- Expect exitcode -- +17 +-- End -- diff --git a/types.c b/types.c index 72a6ae0f..d5599d99 100644 --- a/types.c +++ b/types.c @@ -2043,7 +2043,6 @@ ucv_to_stringbuf_formatted(uc_vm_t *vm, uc_stringbuf_t *pb, uc_value_t *uv, size bool json = (pad_char != '\0'); bool constified = false; bool pending = false; - size_t stackoff = 0; uc_resource_type_t *restype; uc_cfunction_t *cfunction; uc_function_t *function; @@ -2068,7 +2067,6 @@ ucv_to_stringbuf_formatted(uc_vm_t *vm, uc_stringbuf_t *pb, uc_value_t *uv, size * traversal; no callbacks are invoked while an exception is pending since * calling them would discard it */ if (vm != NULL) { - stackoff = vm->stack.count; uc_vm_stack_push(vm, ucv_get(uv)); pending = (vm->exception.type != EXCEPTION_NONE); @@ -2077,8 +2075,7 @@ ucv_to_stringbuf_formatted(uc_vm_t *vm, uc_stringbuf_t *pb, uc_value_t *uv, size goto out; /* do not fall back to formatting the value after its callback - * failed, exit() may also have released uv while resetting the - * VM stack */ + * failed, nor while an exit is pending: the program ends */ if (vm->exception.type == EXCEPTION_EXIT || (!pending && vm->exception.type != EXCEPTION_NONE)) goto out; @@ -2305,16 +2302,13 @@ ucv_to_stringbuf_formatted(uc_vm_t *vm, uc_stringbuf_t *pb, uc_value_t *uv, size json ? "\"" : ""); } - /* exit() within a child callback may have reset the VM stack, but uv - * is not freed while marked, so its flags can still be restored */ ucv_clear_mark(uv); if (constified) ucv_set_constant(uv, false); out: - /* exit() within a callback resets the entire stack, our slot included */ - if (vm != NULL && vm->stack.count > stackoff) + if (vm != NULL) ucv_put(uc_vm_stack_pop(vm)); } diff --git a/vm.c b/vm.c index f70bf7ff..f4a3a489 100644 --- a/vm.c +++ b/vm.c @@ -128,9 +128,9 @@ static void uc_vm_close_upvals(uc_vm_t *vm, size_t slot); static void -uc_vm_reset_callframes(uc_vm_t *vm) +uc_vm_reset_callframes(uc_vm_t *vm, size_t depth) { - while (vm->callframes.count > 0) + while (vm->callframes.count > depth) ucv_put(uc_vm_callframe_pop(vm)); } @@ -318,7 +318,7 @@ void uc_vm_free(uc_vm_t *vm) for (i = 0; i < vm->restypes.count; i++) ucv_put(vm->restypes.entries[i]->proto); - uc_vm_reset_callframes(vm); + uc_vm_reset_callframes(vm, 0); uc_vm_reset_stack(vm); uc_vector_clear(&vm->stack); uc_vector_clear(&vm->callframes); @@ -588,6 +588,13 @@ uc_vm_stack_peek(uc_vm_t *vm, size_t offset) return vm->stack.entries[vm->stack.count + (-1 - offset)]; } +static void +uc_vm_stack_drop(uc_vm_t *vm, size_t count) +{ + while (count-- > 0) + ucv_put(uc_vm_stack_pop(vm)); +} + static void uc_vm_stack_set(uc_vm_t *vm, size_t offset, uc_value_t *value) { @@ -621,10 +628,7 @@ uc_vm_call_native(uc_vm_t *vm, uc_value_t *ctx, uc_cfunction_t *fptr, bool mcall res = fptr->cfn(vm, nargs); - /* Reset stack, check for callframe depth since an uncatched exception in managed - * code executed by fptr->cfn() could've reset the callframe stack already. */ - if (vm->callframes.count > 0) - ucv_put(uc_vm_callframe_pop(vm)); + ucv_put(uc_vm_callframe_pop(vm)); /* push return value */ if (!vm->exception.type) @@ -1432,11 +1436,22 @@ uc_vm_get_error_context(uc_vm_t *vm) return stacktrace; } +/* An exit ends every active invocation: it stays pending until the C + * functions that go on after a failed uc_vm_call() returned. */ +static bool +uc_vm_exit_pending(uc_vm_t *vm) +{ + return vm->exception.type == EXCEPTION_EXIT && vm->callframes.count > 0; +} + void __attribute__((format(printf, 3, 0))) uc_vm_raise_exception(uc_vm_t *vm, uc_exception_type_t type, const char *fmt, ...) { va_list ap; + if (uc_vm_exit_pending(vm)) + return; + vm->exception.type = type; free(vm->exception.message); @@ -1449,6 +1464,23 @@ uc_vm_raise_exception(uc_vm_t *vm, uc_exception_type_t type, const char *fmt, .. vm->exception.stacktrace = uc_vm_get_error_context(vm); } +void +uc_vm_raise_exception_trace(uc_vm_t *vm, uc_exception_type_t type, + const char *message, uc_value_t *stacktrace) +{ + if (uc_vm_exit_pending(vm)) + return; + + vm->exception.type = type; + + free(vm->exception.message); + vm->exception.message = xstrdup(message); + + ucv_get(stacktrace); + ucv_put(vm->exception.stacktrace); + vm->exception.stacktrace = stacktrace; +} + static bool uc_vm_test_strict_equality(uc_value_t *v1, uc_value_t *v2, bool nan_equal) { @@ -3718,7 +3750,7 @@ uc_vm_execute_chunk(uc_vm_t *vm) if (vm->exception.type != EXCEPTION_NONE) { /* VM termination was requested */ if (vm->exception.type == EXCEPTION_EXIT) { - uc_vm_reset_callframes(vm); + uc_vm_reset_callframes(vm, caller); return STATUS_EXIT; } @@ -3740,7 +3772,7 @@ uc_vm_execute_chunk(uc_vm_t *vm) /* "quit" was issued from within the breakpoint's * CLI session */ if (vm->exception.type == EXCEPTION_EXIT) { - uc_vm_reset_callframes(vm); + uc_vm_reset_callframes(vm, caller); return STATUS_EXIT; } @@ -3868,16 +3900,32 @@ uc_vm_execute(uc_vm_t *vm, uc_program_t *program, uc_value_t **retval) uc_exception_type_t uc_vm_call(uc_vm_t *vm, bool mcall, size_t nargs) { - uc_value_t *ctx = mcall ? ucv_get(uc_vm_stack_peek(vm, nargs + 1)) : NULL; - uc_value_t *fno = ucv_get(uc_vm_stack_peek(vm, nargs)); - bool native = (ucv_type(fno) == UC_CFUNCTION); + size_t argc = nargs & 0xffff; + uc_value_t *ctx, *fno; + bool native; + + if (uc_vm_exit_pending(vm)) + goto drop; + + ctx = mcall ? ucv_get(uc_vm_stack_peek(vm, nargs + 1)) : NULL; + fno = ucv_get(uc_vm_stack_peek(vm, nargs)); + native = (ucv_type(fno) == UC_CFUNCTION); uc_vm_clear_exception(vm); - if (uc_vm_call_function(vm, ctx, fno, mcall, nargs & 0xffff, false) && !native) + if (uc_vm_call_function(vm, ctx, fno, mcall, argc, false) && !native) uc_vm_execute_chunk(vm); return vm->exception.type; + +drop: + /* the release of the call frame drops the callee, its arguments and the + * receiver of a method call after a call that failed; a call that a + * pending exit prevents drops them here, so that each caller finds the + * same stack */ + uc_vm_stack_drop(vm, argc + 1 + (mcall ? 1 : 0)); + + return vm->exception.type; } uc_value_t * From 3d7653c560ae18f4f608c9ffebf3bd9710f039a0 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 19 Nov 2026 08:38:22 +0100 Subject: [PATCH 02/21] vm: drop the callee of a call that fails before its frame exists A call that fails in the callee releases its call frame, which drops the callee, its arguments and the receiver of a method call from the stack. A call that fails before uc_vm_call() pushed the call frame, for example at the recursion limit, leaves them on the stack. include() then pops the wrong slots and releases the loaded function while a stack slot still refers to it. Let uc_vm_call() drop the callee, its arguments and the receiver when a call fails before its call frame exists. Fixes: 080cbd79727f ("lib: release the main function of a required file") Signed-off-by: Felix Fietkau --- .../99_bugs/64_include_at_recursion_limit | 47 +++++++++++++++++++ types.c | 12 +---- vm.c | 11 +++-- 3 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 tests/custom/99_bugs/64_include_at_recursion_limit diff --git a/tests/custom/99_bugs/64_include_at_recursion_limit b/tests/custom/99_bugs/64_include_at_recursion_limit new file mode 100644 index 00000000..071eeaff --- /dev/null +++ b/tests/custom/99_bugs/64_include_at_recursion_limit @@ -0,0 +1,47 @@ +When the call of an included file failed before its call frame existed, as +at the recursion limit, the stack still held the called function and its +arguments. include() then took the wrong slots off the stack and freed the +loaded function while a slot still referred to it. + +-- Testcase -- +{% + function nest(n) { + if (n == 0) + return include("files/part.uc", { a: 1 }); + + let rv = nest(n - 1); + + return rv; + } + + for (let n = 990; n <= 1000; n++) { + try { + nest(n); + } + catch (e) { + print(n, ": ", e.message, "\n"); + } + } + + print("done\n"); +%} +-- End -- + +-- File part.uc -- +{% print("included ", a, "\n") %} +-- End -- + +-- Expect stdout -- +included 1 +included 1 +included 1 +included 1 +included 1 +included 1 +included 1 +997: Too much recursion +998: Too much recursion +999: Too much recursion +1000: Too much recursion +done +-- End -- diff --git a/types.c b/types.c index d5599d99..9ecdb475 100644 --- a/types.c +++ b/types.c @@ -2836,7 +2836,7 @@ static bool ucv_meta_call(uc_vm_t *vm, uc_value_t *scope, uc_value_t *meta, uc_value_t *key, uc_value_t *val, uc_value_t **rvp) { - size_t base = vm->stack.count, nargs = 1; + size_t nargs = 1; if (rvp) *rvp = NULL; @@ -2850,16 +2850,8 @@ ucv_meta_call(uc_vm_t *vm, uc_value_t *scope, uc_value_t *meta, nargs = 2; } - if (uc_vm_call(vm, true, nargs) != EXCEPTION_NONE) { - /* An exception raised within the metamethod unwinds its frame - and - * with it the operands pushed above - while one raised before that - * frame got established, out of recursion for instance, leaves them - * behind; either way, drop back to the slots pushed here. */ - while (vm->stack.count > base) - ucv_put(uc_vm_stack_pop(vm)); - + if (uc_vm_call(vm, true, nargs) != EXCEPTION_NONE) return false; - } *rvp = uc_vm_stack_pop(vm); diff --git a/vm.c b/vm.c index f4a3a489..ad3233e9 100644 --- a/vm.c +++ b/vm.c @@ -3913,16 +3913,19 @@ uc_vm_call(uc_vm_t *vm, bool mcall, size_t nargs) uc_vm_clear_exception(vm); - if (uc_vm_call_function(vm, ctx, fno, mcall, argc, false) && !native) + if (!uc_vm_call_function(vm, ctx, fno, mcall, argc, false)) + goto drop; + + if (!native) uc_vm_execute_chunk(vm); return vm->exception.type; drop: /* the release of the call frame drops the callee, its arguments and the - * receiver of a method call after a call that failed; a call that a - * pending exit prevents drops them here, so that each caller finds the - * same stack */ + * receiver of a method call after a call that failed; a call that fails + * without a frame drops them here, so that each caller finds the same + * stack */ uc_vm_stack_drop(vm, argc + 1 + (mcall ? 1 : 0)); return vm->exception.type; From 35b40c2a14a4364d666676bae7073dc22e7befc6 Mon Sep 17 00:00:00 2001 From: John Crispin Date: Sat, 31 Oct 2026 12:30:07 +0100 Subject: [PATCH 03/21] vm: run no interpreter after a C function that __call__ resolves to uc_vm_call() decides by the type of the called value whether the interpreter runs after the call. For an object whose __call__ metamethod is a C function, that function already ran in uc_vm_call_function(). The interpreter then runs the call frame on top of the stack, which belongs to the caller, and the caller later pops from a stack that the release of that frame emptied. Reachable with such an object as a signal handler or as a tostring method. Run the interpreter only if the call pushed a call frame. Fixes: af69c8d226a9 ("types, vm, lib: add metamethod support for prototyped values") Signed-off-by: John Crispin Signed-off-by: Felix Fietkau --- .../99_bugs/66_call_metamethod_cfunction | 20 +++++++++++++++++++ vm.c | 7 ++++--- 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 tests/custom/99_bugs/66_call_metamethod_cfunction diff --git a/tests/custom/99_bugs/66_call_metamethod_cfunction b/tests/custom/99_bugs/66_call_metamethod_cfunction new file mode 100644 index 00000000..99131f15 --- /dev/null +++ b/tests/custom/99_bugs/66_call_metamethod_cfunction @@ -0,0 +1,20 @@ +When C code such as the signal dispatch called an object whose __call__ +metamethod is a C function, the VM ran the interpreter after the call as +if a closure had been called, which continued the bytecode frame on top +of the stack from the wrong position and corrupted the memory. + +The handler prints the signal number. SIGTERM is 15 on every platform; +the number of SIGUSR1 depends on the architecture. + +-- Testcase -- +{% + signal("SIGTERM", proto({}, { __call__: print })); + system("kill -TERM $PPID"); + print("\nafter\n"); +%} +-- End -- + +-- Expect stdout -- +15 +after +-- End -- diff --git a/vm.c b/vm.c index ad3233e9..8f0f34f8 100644 --- a/vm.c +++ b/vm.c @@ -3900,23 +3900,24 @@ uc_vm_execute(uc_vm_t *vm, uc_program_t *program, uc_value_t **retval) uc_exception_type_t uc_vm_call(uc_vm_t *vm, bool mcall, size_t nargs) { + size_t depth = vm->callframes.count; size_t argc = nargs & 0xffff; uc_value_t *ctx, *fno; - bool native; if (uc_vm_exit_pending(vm)) goto drop; ctx = mcall ? ucv_get(uc_vm_stack_peek(vm, nargs + 1)) : NULL; fno = ucv_get(uc_vm_stack_peek(vm, nargs)); - native = (ucv_type(fno) == UC_CFUNCTION); uc_vm_clear_exception(vm); if (!uc_vm_call_function(vm, ctx, fno, mcall, argc, false)) goto drop; - if (!native) + /* only a closure leaves a new frame behind, a C function that __call__ + * resolves to already ran */ + if (vm->callframes.count > depth) uc_vm_execute_chunk(vm); return vm->exception.type; From 0c25918a1ad78e59d0cc2c1bcc0d5273f9f9d639 Mon Sep 17 00:00:00 2001 From: John Crispin Date: Sat, 31 Oct 2026 12:34:39 +0100 Subject: [PATCH 04/21] vm: release the value that an object spread rejects uc_vm_insn_mobj() pops the spread value and raises a type error if it is neither an object nor an array. That path leaks the reference to the value. Release the value. Fixes: 3756806674da ("treewide: rewrite ucode interpreter") Signed-off-by: John Crispin Signed-off-by: Felix Fietkau --- .../99_bugs/69_object_spread_value_leak | 55 +++++++++++++++++++ vm.c | 1 + 2 files changed, 56 insertions(+) create mode 100644 tests/custom/99_bugs/69_object_spread_value_leak diff --git a/tests/custom/99_bugs/69_object_spread_value_leak b/tests/custom/99_bugs/69_object_spread_value_leak new file mode 100644 index 00000000..86451657 --- /dev/null +++ b/tests/custom/99_bugs/69_object_spread_value_leak @@ -0,0 +1,55 @@ +When an object literal spread a value that is neither an object nor an +array, the error path leaked a reference to that value. + +-- Testcase -- +{% + let s = "a string that the VM keeps on the heap: " + time(); + + try { + print({ ...s }, "\n"); + } + catch (e) { + print(replace(e.message, /[0-9]+/, "N"), "\n"); + } +%} +-- End -- + +-- Expect stdout -- +Value (a string that the VM keeps on the heap: N) is not iterable +-- End -- + + +The leaked reference kept a value alive that nothing refers to: the number +of live values grew with each rejected spread. + +-- Testcase -- +{% + function reject(v) { + try { + return { ...v }; + } + catch (e) { + return e.message; + } + } + + function run(n) { + for (let i = 0; i < n; i++) { + let f = function() {}; + + reject(f); + } + } + + run(1); + + let before = gc("count"); + + run(10); + print(gc("count") - before, "\n"); +%} +-- End -- + +-- Expect stdout -- +0 +-- End -- diff --git a/vm.c b/vm.c index 8f0f34f8..edc68cbf 100644 --- a/vm.c +++ b/vm.c @@ -2618,6 +2618,7 @@ uc_vm_insn_mobj(uc_vm_t *vm, uc_vm_insn_t insn) s = ucv_to_string(vm, src); uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Value (%s) is not iterable", s); free(s); + ucv_put(src); break; } From 2fa9c514c30fdccbe56e952eb8fd8be34691c011 Mon Sep 17 00:00:00 2001 From: John Crispin Date: Sat, 31 Oct 2026 12:34:39 +0100 Subject: [PATCH 05/21] vm: keep the arguments of a spread call on the stack uc_vm_call_function() moves the arguments of a call with spread arguments into a temporary array that only a C variable refers to. For a spread value that is not an array, the VM formats an error, and a tostring method of that value can run the collector. The collector frees the array, and the error path then releases it again. Keep the arguments on the stack until their expansion is complete. Also release the rejected value on that error path, which leaked a reference to it. Fixes: 3756806674da ("treewide: rewrite ucode interpreter") Fixes: 47528f02e737 ("vm: support automatic periodic GC runs") Signed-off-by: John Crispin Signed-off-by: Felix Fietkau --- .../99_bugs/67_spread_argument_lifetime | 29 ++++++ vm.c | 95 ++++++++++++------- 2 files changed, 91 insertions(+), 33 deletions(-) create mode 100644 tests/custom/99_bugs/67_spread_argument_lifetime diff --git a/tests/custom/99_bugs/67_spread_argument_lifetime b/tests/custom/99_bugs/67_spread_argument_lifetime new file mode 100644 index 00000000..9e25a9a8 --- /dev/null +++ b/tests/custom/99_bugs/67_spread_argument_lifetime @@ -0,0 +1,29 @@ +When a call spread its arguments, the VM moved them into a temporary array +that the garbage collector did not see. A tostring method that ran gc() +while the error for a spread value that is not an array was formatted freed +the array, and the error path leaked a reference to that spread value. + +-- Testcase -- +{% + let o = proto({}, { tostring: function() { gc(); return "custom"; } }); + + try { + print(...[ {} ], ...o); + } + catch (e) { + print(e.message, "\n"); + } + + try { + print(.../x/); + } + catch (e) { + print(e.message, "\n"); + } +%} +-- End -- + +-- Expect stdout -- +(custom) is not iterable +(/x/) is not iterable +-- End -- diff --git a/vm.c b/vm.c index edc68cbf..dbbebecf 100644 --- a/vm.c +++ b/vm.c @@ -815,18 +815,68 @@ uc_vm_callee_expr(uc_vm_t *vm) uc_program_function_srcpos(function, insn)); } +/* The arguments stay on the stack below their expansion, where the GC sees + * them when a tostring method runs. `idx` counts from the last argument. */ +static uc_value_t * +uc_vm_spread_arg(uc_vm_t *vm, size_t base, size_t nargs, size_t idx) +{ + return vm->stack.entries[base + nargs - 1 - idx]; +} + +static bool +uc_vm_spread_push(uc_vm_t *vm, size_t base, size_t nargs, size_t from, size_t spread) +{ + uc_value_t *arg; + size_t j; + char *s; + + for (j = from; j > spread + 1UL; j--) + uc_vm_stack_push(vm, ucv_get(uc_vm_spread_arg(vm, base, nargs, j - 1))); + + arg = uc_vm_spread_arg(vm, base, nargs, spread); + + if (ucv_type(arg) != UC_ARRAY) { + s = ucv_to_string(vm, arg); + uc_vm_raise_exception(vm, EXCEPTION_TYPE, "(%s) is not iterable", s); + free(s); + + return false; + } + + for (j = 0; j < ucv_array_length(arg); j++) + uc_vm_stack_push(vm, ucv_get(ucv_array_get(arg, j))); + + return true; +} + +static void +uc_vm_spread_finish(uc_vm_t *vm, size_t base, size_t nargs, size_t from) +{ + size_t i, count; + + for (i = from; i > 0; i--) + uc_vm_stack_push(vm, ucv_get(uc_vm_spread_arg(vm, base, nargs, i - 1))); + + for (i = 0; i < nargs; i++) + ucv_put(vm->stack.entries[base + i]); + + count = vm->stack.count - base - nargs; + memmove(&vm->stack.entries[base], &vm->stack.entries[base + nargs], + count * sizeof(vm->stack.entries[0])); + vm->stack.count = base + count; +} + static bool uc_vm_call_function(uc_vm_t *vm, uc_value_t *ctx, uc_value_t *fno, bool mcall, size_t argspec, bool tail) { - size_t i, j, stackoff, nargs = argspec & 0xffff; + size_t i, stackoff, nargs = argspec & 0xffff; size_t nspreads = (argspec >> 16) & 0x7fff; uc_callframe_t *frame = NULL; - uc_value_t *ellip, *arg; uc_function_t *function; uc_closure_t *closure; + uc_value_t *ellip; uint16_t slot, tmp; - char *s; /* a tail invocation reuses the current frame, so it never grows the stack */ if (!tail && vm->callframes.count >= 1000) { @@ -843,52 +893,31 @@ uc_vm_call_function(uc_vm_t *vm, uc_value_t *ctx, uc_value_t *fno, bool mcall, if (nspreads > 0) { frame = uc_vm_current_frame(vm); - /* create temporary array */ - ellip = ucv_array_new_length(vm, nargs); - - /* pop original stack values and push to temp array in reverse order */ - for (i = 0; i < nargs; i++) - ucv_array_push(ellip, uc_vm_stack_pop(vm)); - /* for each spread value index ... */ for (i = 0, slot = nargs; i < nspreads; i++) { /* decode stack depth value */ tmp = frame->ip[0] * 0x100 + frame->ip[1]; frame->ip += 2; - /* push each preceeding non-spread value to the stack */ - for (j = slot; j > tmp + 1UL; j--) - uc_vm_stack_push(vm, ucv_get(ucv_array_get(ellip, j - 1))); + if (tmp >= slot) { + uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "spread index out of range"); + ucv_put(ctx); + ucv_put(fno); - /* read spread value at index... */ - slot = tmp; - arg = ucv_get(ucv_array_get(ellip, slot)); + return false; + } - /* ... ensure that it is an array type ... */ - if (ucv_type(arg) != UC_ARRAY) { - s = ucv_to_string(vm, arg); - uc_vm_raise_exception(vm, EXCEPTION_TYPE, "(%s) is not iterable", s); - free(s); + if (!uc_vm_spread_push(vm, stackoff + 1, nargs, slot, tmp)) { ucv_put(ctx); ucv_put(fno); - ucv_put(ellip); return false; } - /* ... and push each spread array value as argument to the stack */ - for (j = 0; j < ucv_array_length(arg); j++) - uc_vm_stack_push(vm, ucv_get(ucv_array_get(arg, j))); - - ucv_put(arg); + slot = tmp; } - /* push remaining non-spread arguments to the stack */ - for (i = slot; i > 0; i--) - uc_vm_stack_push(vm, ucv_get(ucv_array_get(ellip, i - 1))); - - /* free temp array */ - ucv_put(ellip); + uc_vm_spread_finish(vm, stackoff + 1, nargs, slot); /* update arg count */ nargs = vm->stack.count - stackoff - 1; From 92a7e6b2e9d66136ee8bfb4f2b7cfb02778a5eda Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 12 Nov 2026 00:04:38 +0100 Subject: [PATCH 06/21] vm: select the exception handler by the instruction that raised uc_vm_handle_exception() tests the exception ranges with the position after the failing instruction. An instruction directly before a try block is thus inside its range, and the catch block runs with the stack of the code before the block. The last instruction of a try block is outside its range. Test the last byte of the failing instruction. Keep the position of the next instruction for an exception of a signal handler, since the handler runs between two instructions. Fixes: 3756806674da ("treewide: rewrite ucode interpreter") Signed-off-by: Felix Fietkau --- .../99_bugs/70_exception_range_boundaries | 245 ++++++++++++++++++ vm.c | 107 ++++---- 2 files changed, 304 insertions(+), 48 deletions(-) create mode 100644 tests/custom/99_bugs/70_exception_range_boundaries diff --git a/tests/custom/99_bugs/70_exception_range_boundaries b/tests/custom/99_bugs/70_exception_range_boundaries new file mode 100644 index 00000000..9647d9ea --- /dev/null +++ b/tests/custom/99_bugs/70_exception_range_boundaries @@ -0,0 +1,245 @@ +The VM selected the catch block by the position after the instruction that +raised. An exception of the instruction directly before a try block ran +the catch block of that try, with a stack that the block did not expect. + +-- Testcase -- +{% + function fail(x) { die("fail " + x); } + function count(...args) { return length(args); } + + function before_try() { + let v = fail(1); + + try { + print("in try\n"); + } + catch (e) { + return [ "wrong catch", type(e), type(v) ]; + } + } + + function spread_before_try() { + let v = count(...[ 1 ], ...null); + + try { + print("in try\n"); + } + catch (e) { + return [ "wrong catch", type(e), type(v) ]; + } + } + + for (let fn in [ before_try, spread_before_try ]) { + try { + print(fn(), "\n"); + } + catch (e) { + print("propagated: ", e.message, "\n"); + } + } +%} +-- End -- + +-- Expect stdout -- +propagated: fail 1 +propagated: (null) is not iterable +-- End -- + + +An exception of the last instruction of a try block was not caught by the +catch block of that try. + +-- Testcase -- +{% + let o = proto({}, { tostring: function() { die("no string"); } }); + + function last_insn() { + try { %}{{ o }}{% } + catch (e) { + return "caught: " + e.message; + } + + return "end"; + } + + try { + print(last_insn(), "\n"); + } + catch (e) { + print("propagated: ", e.message, "\n"); + } +%} +-- End -- + +-- Expect stdout -- +caught: no string +-- End -- + + +A signal handler runs between two instructions. Its exception belongs to +the instruction that follows: after a call, that is the instruction after +the call. + +-- Testcase -- +{% + signal("SIGUSR1", function() { die("sig"); }); + + const kill = "kill -USR1 $PPID"; + + function raise() { return system(kill); } + + function call_before_try() { + let v = system(kill); + + try { + print("in try\n"); + } + catch (e) { + return "try caught " + e.message; + } + + return "end"; + } + + function return_in_try() { + try { + return system(kill); + } + catch (e) { + return "try caught " + e.message; + } + } + + function store_in_try() { + let v; + + try { + v = system(kill); + } + catch (e) { + return "try caught " + e.message; + } + + return "end"; + } + + function call_after_try() { + try { + print("in try\n"); + } + catch (e) { + return "try caught " + e.message; + } + + system(kill); + + return "end"; + } + + function call_after_inner_try() { + try { + try { + print("in try\n"); + } + catch (e) { + return "inner caught " + e.message; + } + + system(kill); + } + catch (e) { + return "outer caught " + e.message; + } + + return "end"; + } + + function call_before_inner_try() { + try { + let v = system(kill); + + try { + print("in try\n"); + } + catch (e) { + return "inner caught " + e.message; + } + } + catch (e) { + return "outer caught " + e.message; + } + + return "end"; + } + + function statement_before_try() { + for (let i = 0; i < 2; i++) { + if (i == 0) + system(kill); + + try { + print("in try\n"); + } + catch (e) { + return "try caught " + e.message; + } + } + + return "end"; + } + + function method_statement_before_try() { + let o = { system }; + + o.system(kill); + + try { + print("in try\n"); + } + catch (e) { + return "try caught " + e.message; + } + + return "end"; + } + + // the handler runs in the function that made the tail call + function failed_call_before_try() { + let v = raise(); + + try { + print("in try\n"); + } + catch (e) { + return "try caught " + e.message; + } + + return "end"; + } + + for (let fn in [ call_before_try, return_in_try, store_in_try, call_after_try, + call_after_inner_try, call_before_inner_try, statement_before_try, + method_statement_before_try, failed_call_before_try ]) { + try { + print(fn(), "\n"); + } + catch (e) { + print("propagated: ", e.message, "\n"); + } + } +%} +-- End -- + +-- Expect stdout -- +try caught sig +try caught sig +try caught sig +in try +propagated: sig +in try +outer caught sig +inner caught sig +propagated: sig +propagated: sig +propagated: sig +-- End -- diff --git a/vm.c b/vm.c index dbbebecf..601fffdb 100644 --- a/vm.c +++ b/vm.c @@ -1229,6 +1229,29 @@ static uint8_t uc_breakpoint_uncaught_exception_storage; uint8_t *const UC_BREAKPOINT_UNCAUGHT_EXCEPTION = &uc_breakpoint_uncaught_exception_storage; +/* An instruction that raised lies before the instruction pointer. A signal + * handler raises `between` two instructions, in the range of the second. */ +static uc_ehrange_t * +uc_vm_frame_ehrange(uc_callframe_t *frame, bool between) +{ + uc_chunk_t *chunk = uc_vm_frame_chunk(frame); + size_t off = frame->ip - chunk->entries; + size_t i; + + if (!between && off == 0) + return NULL; + + if (!between) + off--; + + for (i = 0; i < chunk->ehranges.count; i++) + if (off >= chunk->ehranges.entries[i].from && + off < chunk->ehranges.entries[i].to) + return &chunk->ehranges.entries[i]; + + return NULL; +} + /* Non-destructively predict whether uc_vm_handle_exception()'s real unwind * loop (below) would find a handler for the currently raised exception * anywhere between the current callframe and `caller` (the frame depth this @@ -1240,34 +1263,29 @@ uint8_t *const UC_BREAKPOINT_UNCAUGHT_EXCEPTION = * frame - locals, exact position - is still fully intact, since once * uc_vm_handle_exception() starts really popping frames that's gone. */ static bool -uc_vm_exception_would_be_caught(uc_vm_t *vm, size_t caller) +uc_vm_exception_would_be_caught(uc_vm_t *vm, size_t caller, bool between) { - for (size_t i = vm->callframes.count; i > caller; i--) { + for (size_t i = vm->callframes.count; i > caller; i--, between = false) { uc_callframe_t *frame = &vm->callframes.entries[i - 1]; if (!frame->closure) return false; - uc_chunk_t *chunk = &frame->closure->function->chunk; - size_t pos = frame->ip - chunk->entries; - - for (size_t j = 0; j < chunk->ehranges.count; j++) { - if (pos >= chunk->ehranges.entries[j].from && - pos < chunk->ehranges.entries[j].to) - return true; - } + if (uc_vm_frame_ehrange(frame, between)) + return true; } return false; } static bool -uc_vm_handle_exception(uc_vm_t *vm) +uc_vm_handle_exception(uc_vm_t *vm, bool between) { uc_callframe_t *frame = NULL; uc_chunk_t *chunk = NULL; + uc_ehrange_t *range; uc_value_t *exo; - size_t i, pos; + size_t slot; if (vm->callframes.count) frame = uc_vm_current_frame(vm); @@ -1276,48 +1294,33 @@ uc_vm_handle_exception(uc_vm_t *vm) return false; chunk = uc_vm_frame_chunk(frame); - pos = frame->ip - chunk->entries; - - /* iterate the known exception ranges, see if the current ip falls into any of them */ - for (i = 0; i < chunk->ehranges.count; i++) { - /* skip nonmatching ranges */ - if (pos < chunk->ehranges.entries[i].from || - pos >= chunk->ehranges.entries[i].to) - continue; - - /* we found a matching range... first unwind stack */ - while (vm->stack.count > frame->stackframe + chunk->ehranges.entries[i].slot) - ucv_put(uc_vm_stack_pop(vm)); + range = uc_vm_frame_ehrange(frame, between); - /* prepare exception object and expose it to user handler code */ - exo = uc_vm_exception_object(vm); + if (!range) + return false; - uc_vm_stack_push(vm, exo); + slot = frame->stackframe + range->slot; - /* reset exception information */ - uc_vm_clear_exception(vm); + while (vm->stack.count > slot) + ucv_put(uc_vm_stack_pop(vm)); - /* jump to exception handler */ - if (chunk->ehranges.entries[i].target >= chunk->count) { - uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "jump target out of range"); - return false; - } + /* prepare exception object and expose it to user handler code */ + exo = uc_vm_exception_object(vm); -#if 0 - if (vm->trace && chunk->entries + chunk->ehranges.entries[i].target > frame->ip) { - while (frame->ip < chunk->entries + chunk->ehranges.entries[i].target) { - fprintf(stderr, "(eh:skip) [%p:%zu] ", chunk, frame->ip - chunk->entries); - uc_dump_insn(vm, frame->ip, uc_vm_decode_insn(vm, frame, chunk)); - } - } -#endif + uc_vm_stack_push(vm, exo); - frame->ip = chunk->entries + chunk->ehranges.entries[i].target; + /* reset exception information */ + uc_vm_clear_exception(vm); - return true; + /* jump to exception handler */ + if (range->target >= chunk->count) { + uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "jump target out of range"); + return false; } - return false; + frame->ip = chunk->entries + range->target; + + return true; } /* Adds the line and byte column of the source position `off` to `object`, under @@ -3523,6 +3526,7 @@ uc_vm_execute_chunk(uc_vm_t *vm) uc_callframe_t *frame = NULL; uc_chunk_t *chunk = NULL; size_t caller = vm->callframes.count - 1; + bool between = false; uc_value_t *retval; uc_vm_insn_t insn; uint8_t *ip; @@ -3792,7 +3796,7 @@ uc_vm_execute_chunk(uc_vm_t *vm) * stack *before* uc_vm_handle_exception()'s loop below starts * popping frames - once that happens, the original throwing * frame's locals and exact position are gone for good. */ - if (!uc_vm_exception_would_be_caught(vm, caller)) { + if (!uc_vm_exception_would_be_caught(vm, caller, between)) { for (size_t i = 0; i < vm->breakpoints.count; i++) { uc_breakpoint_t *bk = vm->breakpoints.entries[i]; @@ -3813,7 +3817,7 @@ uc_vm_execute_chunk(uc_vm_t *vm) } /* walk up callframes until something handles the exception or the original caller is reached */ - while (!uc_vm_handle_exception(vm)) { + while (!uc_vm_handle_exception(vm, between)) { /* no further callframe, report unhandled exception and terminate */ if (vm->callframes.count == 0) return ERROR_RUNTIME; @@ -3828,7 +3832,11 @@ uc_vm_execute_chunk(uc_vm_t *vm) /* do not bubble past original call depth */ if (vm->callframes.count <= caller) return ERROR_RUNTIME; + + between = false; } + + between = false; } /* the compiler emits the exports of a module as one block, do not run @@ -3838,8 +3846,11 @@ uc_vm_execute_chunk(uc_vm_t *vm) continue; /* run handler for signal(s) delivered during previous instruction */ - if (uc_vm_signal_dispatch(vm) != EXCEPTION_NONE) + if (uc_vm_signal_dispatch(vm) != EXCEPTION_NONE) { + between = true; + goto exception; + } /* check for break request */ if (vm->break_requested) { From f9b7a44531a2d866424075f76c3c7a51bf2aaa88 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 12 Nov 2026 22:14:06 +0100 Subject: [PATCH 07/21] vm: close the upvalues of the slots that an exception unwind drops uc_vm_handle_exception() drops the stack slots of a try block before the catch block runs, but leaves the upvalues that refer to them open. A closure that the try block created then reads and writes the slot of the exception object in place of its variable. Close the upvalues of the dropped slots, like the release of a call frame does. Fixes: 3756806674da ("treewide: rewrite ucode interpreter") Signed-off-by: Felix Fietkau --- .../99_bugs/70_exception_unwind_upvalues | 29 +++++++++++++++++++ vm.c | 2 ++ 2 files changed, 31 insertions(+) create mode 100644 tests/custom/99_bugs/70_exception_unwind_upvalues diff --git a/tests/custom/99_bugs/70_exception_unwind_upvalues b/tests/custom/99_bugs/70_exception_unwind_upvalues new file mode 100644 index 00000000..02575e1c --- /dev/null +++ b/tests/custom/99_bugs/70_exception_unwind_upvalues @@ -0,0 +1,29 @@ +The unwind to the catch block dropped the variables of the try block +without closing the upvalues that refer to them, so that a closure read the +exception object in place of its variable. + +-- Testcase -- +{% + function closures() { + let fns = []; + + try { + let t = "try"; + + push(fns, () => t); + die("x"); + } + catch (e) { + push(fns, () => e.message); + } + + return map(fns, fn => fn()); + } + + print(closures(), "\n"); +%} +-- End -- + +-- Expect stdout -- +[ "try", "x" ] +-- End -- diff --git a/vm.c b/vm.c index 601fffdb..27312372 100644 --- a/vm.c +++ b/vm.c @@ -1299,7 +1299,9 @@ uc_vm_handle_exception(uc_vm_t *vm, bool between) if (!range) return false; + /* a closure may hold a variable of the block that the unwind drops */ slot = frame->stackframe + range->slot; + uc_vm_close_upvals(vm, slot); while (vm->stack.count > slot) ucv_put(uc_vm_stack_pop(vm)); From 8031af025512212594153f6581352fdc3f9c1016 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 12 Nov 2026 22:14:06 +0100 Subject: [PATCH 08/21] vm: run no signal handler after the last RETURN of an invocation uc_vm_execute_chunk() runs the handlers of pending signals after each instruction, also after the RETURN that ends a nested invocation. The call frame on top then belongs to the code that made the call, which is in the middle of an instruction. An exception of a handler unwinds that frame, and a catch block in it runs with a wrong stack. Leave the signal pending after that RETURN. The code that made the call dispatches it after its current instruction. A call of uc_vm_call() or uc_vm_execute() that ends with no call frame left returns to a C host, where no instruction follows. Such a call so far returned with the signal pending. Run the handlers before it returns. An exception of a handler is then the result of the call, like an exception of the called function. Fixes: 29b1c0deba84 ("vm: introduce basic signal handling infrastructure") Signed-off-by: Felix Fietkau --- .../99_bugs/76_signal_after_nested_return | 158 ++++++++++++++++++ vm.c | 29 +++- 2 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 tests/custom/99_bugs/76_signal_after_nested_return diff --git a/tests/custom/99_bugs/76_signal_after_nested_return b/tests/custom/99_bugs/76_signal_after_nested_return new file mode 100644 index 00000000..d3090d36 --- /dev/null +++ b/tests/custom/99_bugs/76_signal_after_nested_return @@ -0,0 +1,158 @@ +A signal handler that raises directly after the RETURN that ends a nested +invocation, here the tostring method that a string conversion calls, must +not unwind the frame of the function that made the conversion as if the +handler ran between two of its instructions. The catch block got a wrong +stack, and without a catch block the VM released the frame under the +running C function. + +The RETURN of tostring() releases the pipe handle, which waits for the +child, so the signal is pending when the method returns. + +-- Testcase -- +{% + import * as fs from 'fs'; + + signal("SIGUSR1", () => die("from handler")); + + let o = proto({}, { tostring: function() { + let p = fs.popen("kill -USR1 $PPID; sleep 0.2", "r"); + + return "str"; + } }); + + function caught() { + let l = [ 1, 2 ]; + + try { + let s = "" + o; + + return "end " + s; + } + catch (e) { + return "caught " + e.message + " " + l; + } + } + + function uncaught() { + let l = [ 1, 2 ]; + let s = "" + o; + + return "end " + s + l; + } + + print(caught(), "\n"); + + try { + print(uncaught(), "\n"); + } + catch (e) { + print("propagated ", e.message, "\n"); + } + + print("done\n"); +%} +-- End -- + +-- Expect stdout -- +caught from handler [ 1, 2 ] +propagated from handler +done +-- End -- + + +A handler that does not raise leaves the result of the invocation in place. + +-- Testcase -- +{% + import * as fs from 'fs'; + + signal("SIGUSR1", () => print("handler\n")); + + let o = proto({}, { tostring: function() { + let p = fs.popen("kill -USR1 $PPID; sleep 0.2", "r"); + + return "str"; + } }); + + let s = "result " + o; + + print(s, "\n"); +%} +-- End -- + +-- Expect stdout -- +handler +result str +-- End -- + + +An exit request of the handler ends the program. + +-- Testcase -- +{% + import * as fs from 'fs'; + + signal("SIGUSR1", () => exit(3)); + + let o = proto({}, { tostring: function() { + let p = fs.popen("kill -USR1 $PPID; sleep 0.2", "r"); + + return "str"; + } }); + + try { + let s = "result " + o; + + print(s, "\n"); + } + catch (e) { + print("caught\n"); + } + + print("not reached\n"); +%} +-- End -- + +-- Expect stdout -- +-- End -- + +-- Expect exitcode -- +3 +-- End -- + + +A signal that comes in during the last RETURN of the program runs its +handler before the program ends. The RETURN releases the pipe handle of the +program, which waits for the child. An exception of the handler is the +result of the program. + +-- Testcase -- +{% + import * as fs from 'fs'; + + signal("SIGUSR1", () => print("handler\n")); + + let p = fs.popen("kill -USR1 $PPID; sleep 0.2", "r"); +%} +-- End -- + +-- Expect stdout -- +handler +-- End -- + +-- Testcase -- +{% + import * as fs from 'fs'; + + signal("SIGUSR1", () => exit(4)); + + let p = fs.popen("kill -USR1 $PPID; sleep 0.2", "r"); +%} +-- End -- + +-- Expect stdout -- +-- End -- + +-- Expect exitcode -- +4 +-- End -- diff --git a/vm.c b/vm.c index 27312372..2d291ca4 100644 --- a/vm.c +++ b/vm.c @@ -3847,6 +3847,11 @@ uc_vm_execute_chunk(uc_vm_t *vm) if (insn == I_EXPORT && frame->ip[0] == I_EXPORT) continue; + /* a handler that raises needs a frame of this invocation to unwind; + * after the last RETURN the caller dispatches the signal */ + if (vm->callframes.count <= caller) + break; + /* run handler for signal(s) delivered during previous instruction */ if (uc_vm_signal_dispatch(vm) != EXCEPTION_NONE) { between = true; @@ -3864,6 +3869,25 @@ uc_vm_execute_chunk(uc_vm_t *vm) return STATUS_OK; } +/* The handlers of signals run between two instructions of a frame. A call + * that returns to a C host leaves no frame behind, so the handlers of the + * signals of its last instructions run here, in place of its result. */ +static uc_exception_type_t +uc_vm_signal_dispatch_host(uc_vm_t *vm) +{ + uc_exception_type_t ex; + + if (vm->callframes.count > 0 || vm->exception.type != EXCEPTION_NONE) + return vm->exception.type; + + ex = uc_vm_signal_dispatch(vm); + + if (ex != EXCEPTION_NONE) + ucv_put(uc_vm_stack_pop(vm)); + + return ex; +} + uc_vm_status_t uc_vm_execute(uc_vm_t *vm, uc_program_t *program, uc_value_t **retval) { @@ -3903,6 +3927,9 @@ uc_vm_execute(uc_vm_t *vm, uc_program_t *program, uc_value_t **retval) status = uc_vm_execute_chunk(vm); + if (status == STATUS_OK && uc_vm_signal_dispatch_host(vm) != EXCEPTION_NONE) + status = uc_vm_exception_type_to_status(vm); + switch (status) { case STATUS_OK: val = uc_vm_stack_pop(vm); @@ -3963,7 +3990,7 @@ uc_vm_call(uc_vm_t *vm, bool mcall, size_t nargs) if (vm->callframes.count > depth) uc_vm_execute_chunk(vm); - return vm->exception.type; + return uc_vm_signal_dispatch_host(vm); drop: /* the release of the call frame drops the callee, its arguments and the From da3d93ceb67e64208b715c357b57284f3f61185b Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 12 Nov 2026 00:04:38 +0100 Subject: [PATCH 09/21] vm: keep the operands of a property operation on the stack The handlers of LVAL, SVAL, UVAL and DELETE pop their operands and hold them in C variables during the operation. A metamethod or a tostring method that the operation calls can run the collector, which frees an operand that only the C variable refers to. The handler then uses freed memory. Keep the operands on the stack until the operation ends. Fixes: af69c8d226a9 ("types, vm, lib: add metamethod support for prototyped values") Fixes: 47528f02e737 ("vm: support automatic periodic GC runs") Signed-off-by: Felix Fietkau --- tests/custom/99_bugs/72_property_operand_gc | 79 +++++++++++ vm.c | 142 +++++++++++--------- 2 files changed, 157 insertions(+), 64 deletions(-) create mode 100644 tests/custom/99_bugs/72_property_operand_gc diff --git a/tests/custom/99_bugs/72_property_operand_gc b/tests/custom/99_bugs/72_property_operand_gc new file mode 100644 index 00000000..1849d86e --- /dev/null +++ b/tests/custom/99_bugs/72_property_operand_gc @@ -0,0 +1,79 @@ +The VM kept the operands of a property operation in C variables only. A +metamethod of the property, or a tostring method of an operand or of a key, +that ran the garbage collector freed them while the operation still used +them. + +A __set__ method that collects garbage during a compound assignment freed +the value that the __get__ method returned, and the operand. + +-- Testcase -- +{% + let o = proto({}, { + __get__: function(k) { return (k == "list") ? [ k, {} ] : "text"; }, + __set__: function(k, v) { gc(); } + }); + + function update() { + o.list += 1; + o.text += [ { a: 1 } ]; + } + + update(); + print("ok\n"); +%} +-- End -- + +-- Expect stdout -- +ok +-- End -- + + +A tostring method of the old value that collects garbage freed the object +and the key of the compound assignment. + +-- Testcase -- +{% + let t = proto({}, { tostring: function() { gc(); return "t"; } }); + let o = { "[ 1, 2 ]": t }; + + function update() { + o[[ 1, 2 ]] += "b"; + + return ({ x: t, y: [ 1, 2, 3 ] }).x += "a"; + } + + print(update(), " ", o, "\n"); +%} +-- End -- + +-- Expect stdout -- +ta { "[ 1, 2 ]": "tb" } +-- End -- + + +A tostring method of a key that collects garbage freed the object of a +property read, store or delete. + +-- Testcase -- +{% + let k = proto({}, { tostring: function() { gc(); return "key"; } }); + + function read() { + return ({ key: [ 1, 2 ], other: { a: 1 } })[k]; + } + + function store() { + return (({ key: [ 1, 2 ], other: { a: 1 } })[k] = [ 5 ]); + } + + function remove() { + return delete ({ key: [ 1, 2 ], other: { a: 1 } })[k]; + } + + print(read(), " ", store(), " ", remove(), "\n"); +%} +-- End -- + +-- Expect stdout -- +[ 1, 2 ] [ 5 ] true +-- End -- diff --git a/vm.c b/vm.c index 2d291ca4..1545f1c7 100644 --- a/vm.c +++ b/vm.c @@ -1652,18 +1652,23 @@ uc_vm_insn_load_var(uc_vm_t *vm, uc_vm_insn_t insn) uc_vm_stack_push(vm, ucv_get(val)); } +/* A metamethod or a tostring method of a key may collect garbage, so the + * operands of a property operation stay on the stack while it runs. */ static void uc_vm_insn_load_val(uc_vm_t *vm, uc_vm_insn_t insn) { - uc_value_t *k = uc_vm_stack_pop(vm); - uc_value_t *v = uc_vm_stack_pop(vm); + uc_value_t *k = uc_vm_stack_peek(vm, 0); + uc_value_t *v = uc_vm_stack_peek(vm, 1); + uc_value_t *res = NULL; + bool ok = false; switch (ucv_type(v)) { case UC_RESOURCE: case UC_OBJECT: case UC_ARRAY: /* aware read: dispatches __get__ on a missing key */ - uc_vm_stack_push(vm, ucv_key_get(vm, v, k)); + res = ucv_key_get(vm, v, k); + ok = true; break; default: @@ -1674,8 +1679,10 @@ uc_vm_insn_load_val(uc_vm_t *vm, uc_vm_insn_t insn) break; } - ucv_put(k); - ucv_put(v); + uc_vm_stack_drop(vm, 2); + + if (ok) + uc_vm_stack_push(vm, res); } static void @@ -1877,7 +1884,6 @@ static bool assert_mutable_value(uc_vm_t *vm, uc_value_t *val) { if (ucv_is_constant(val)) { - uc_vm_stack_push(vm, NULL); uc_vm_raise_exception(vm, EXCEPTION_TYPE, "%s value is immutable", ucv_typename(val)); @@ -1891,22 +1897,27 @@ assert_mutable_value(uc_vm_t *vm, uc_value_t *val) static void uc_vm_insn_store_val(uc_vm_t *vm, uc_vm_insn_t insn) { - uc_value_t *v = uc_vm_stack_pop(vm); - uc_value_t *k = uc_vm_stack_pop(vm); - uc_value_t *o = uc_vm_stack_pop(vm); + uc_value_t *v = uc_vm_stack_peek(vm, 0); + uc_value_t *k = uc_vm_stack_peek(vm, 1); + uc_value_t *o = uc_vm_stack_peek(vm, 2); + uc_value_t *res = NULL; + bool ok = false; switch (ucv_type(o)) { case UC_OBJECT: case UC_ARRAY: case UC_RESOURCE: - if (assert_mutable_value(vm, o)) { - /* ucv_key_set() retains the stored value on its own and returns a - * reference to it, which becomes the value of the assignment; for - * resources it dispatches a __set__ metamethod when present. In - * strict mode it raises a type error when a non-index property - * store on an array would otherwise be silently dropped. */ - uc_vm_stack_push(vm, ucv_key_set(vm, o, k, v)); - } + ok = true; + + if (!assert_mutable_value(vm, o)) + break; + + /* ucv_key_set() retains the stored value on its own and returns a + * reference to it, which becomes the value of the assignment; for + * resources it dispatches a __set__ metamethod when present. In + * strict mode it raises a type error when a non-index property + * store on an array would otherwise be silently dropped. */ + res = ucv_key_set(vm, o, k, v); break; @@ -1916,9 +1927,10 @@ uc_vm_insn_store_val(uc_vm_t *vm, uc_vm_insn_t insn) ucv_typename(o)); } - ucv_put(v); - ucv_put(o); - ucv_put(k); + uc_vm_stack_drop(vm, 3); + + if (ok) + uc_vm_stack_push(vm, res); } static void @@ -2389,43 +2401,40 @@ uc_vm_insn_update_var(uc_vm_t *vm, uc_vm_insn_t insn) static void uc_vm_insn_update_val(uc_vm_t *vm, uc_vm_insn_t insn) { - uc_value_t *inc = uc_vm_stack_pop(vm); - uc_value_t *k = uc_vm_stack_pop(vm); - uc_value_t *v = uc_vm_stack_pop(vm); - uc_value_t *val = NULL; + uc_value_t *inc = uc_vm_stack_peek(vm, 0); + uc_value_t *k = uc_vm_stack_peek(vm, 1); + uc_value_t *v = uc_vm_stack_peek(vm, 2); + uc_value_t *val, *nv, *res = NULL; uc_vm_insn_t op; + bool ok = false; switch (ucv_type(v)) { case UC_OBJECT: case UC_ARRAY: case UC_RESOURCE: - if (assert_mutable_value(vm, v)) { - uc_value_t *nv, *rv; + ok = true; - /* reading the property may dispatch a __get__ metamethod which runs - * on the current stack, clobbers vm->arg and may collect garbage, so - * remember the operation and keep the operands reachable */ - op = vm->arg.u8; - uc_vm_stack_push(vm, ucv_get(inc)); - - val = ucv_key_get(vm, v, k); - uc_vm_stack_push(vm, ucv_get(val)); - - nv = uc_vm_value_arith(vm, op, val, inc); + if (!assert_mutable_value(vm, v)) + break; - ucv_put(uc_vm_stack_pop(vm)); - ucv_put(uc_vm_stack_pop(vm)); + /* a metamethod of the property or a tostring method of an operand + * runs on the current stack, clobbers vm->arg and may collect + * garbage, so remember the operation and keep the old value + * reachable until the new one is stored */ + op = vm->arg.u8; + val = ucv_key_get(vm, v, k); + uc_vm_stack_push(vm, val); - /* an aborted read leaves nothing to store; the exception it raised is - * left to propagate instead of overwriting the property */ - rv = vm->exception.type != EXCEPTION_NONE ? NULL : ucv_key_set(vm, v, k, nv); + nv = uc_vm_value_arith(vm, op, val, inc); - /* the store keeps a reference of its own; rv is the value of the - * update expression, null if storing failed */ - ucv_put(nv); + /* an aborted read leaves nothing to store; the exception it raised is + * left to propagate instead of overwriting the property */ + res = vm->exception.type != EXCEPTION_NONE ? NULL : ucv_key_set(vm, v, k, nv); - uc_vm_stack_push(vm, rv); - } + /* the store keeps a reference of its own; the result is the value of + * the update expression, null if storing failed */ + ucv_put(nv); + ucv_put(uc_vm_stack_pop(vm)); break; @@ -2437,10 +2446,10 @@ uc_vm_insn_update_val(uc_vm_t *vm, uc_vm_insn_t insn) break; } - ucv_put(val); - ucv_put(inc); - ucv_put(v); - ucv_put(k); + uc_vm_stack_drop(vm, 3); + + if (ok) + uc_vm_stack_push(vm, res); } static void @@ -3158,23 +3167,26 @@ uc_vm_insn_print(uc_vm_t *vm, uc_vm_insn_t insn) static void uc_vm_insn_delete(uc_vm_t *vm, uc_vm_insn_t insn) { - uc_value_t *k = uc_vm_stack_pop(vm); - uc_value_t *v = uc_vm_stack_pop(vm); - bool rv; + uc_value_t *k = uc_vm_stack_peek(vm, 0); + uc_value_t *v = uc_vm_stack_peek(vm, 1); + uc_value_t *res = NULL; + bool ok = false; switch (ucv_type(v)) { case UC_OBJECT: case UC_ARRAY: case UC_RESOURCE: - if (assert_mutable_value(vm, v)) { - /* ucv_key_delete() removes own object keys directly and consults the - * __delete__ metamethod for keys the value has no own storage for. - * Arrays and resources have no own-key storage at all, so there the - * metamethod is the only way to handle a key; without one it raises - * the same reference error the default case below reports */ - rv = ucv_key_delete(vm, v, k); - uc_vm_stack_push(vm, ucv_boolean_new(rv)); - } + ok = true; + + if (!assert_mutable_value(vm, v)) + break; + + /* ucv_key_delete() removes own object keys directly and consults the + * __delete__ metamethod for keys the value has no own storage for. + * Arrays and resources have no own-key storage at all, so there the + * metamethod is the only way to handle a key; without one it raises + * the same reference error the default case below reports */ + res = ucv_boolean_new(ucv_key_delete(vm, v, k)); break; @@ -3186,8 +3198,10 @@ uc_vm_insn_delete(uc_vm_t *vm, uc_vm_insn_t insn) break; } - ucv_put(k); - ucv_put(v); + uc_vm_stack_drop(vm, 2); + + if (ok) + uc_vm_stack_push(vm, res); } /* a closure that runs again holds the binding of its previous run */ From fc787e97a465d7bf46a750a3ed1837940921bb0a Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 19 Nov 2026 08:38:40 +0100 Subject: [PATCH 10/21] vm: read the operand of SOBJ before the conversion of a key uc_vm_insn_sobj() reads vm->arg again after ucv_key_rawset(). The tostring method of a key runs instructions that replace vm->arg, and the handler then drops a wrong number of values from the stack. Read the operand before the first call. Fixes: c40255127723 ("vm: fix crash on object literals with non-string computed properties") Signed-off-by: Felix Fietkau --- .../99_bugs/73_object_literal_key_tostring | 39 +++++++++++++++++++ vm.c | 13 ++++--- 2 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 tests/custom/99_bugs/73_object_literal_key_tostring diff --git a/tests/custom/99_bugs/73_object_literal_key_tostring b/tests/custom/99_bugs/73_object_literal_key_tostring new file mode 100644 index 00000000..4e5f8b95 --- /dev/null +++ b/tests/custom/99_bugs/73_object_literal_key_tostring @@ -0,0 +1,39 @@ +The instruction that sets the keys of an object literal read its operand +count again after each key. A tostring method of a key runs instructions of +its own, which replace that operand, so the VM then dropped a wrong number +of values from the stack. + +-- Testcase -- +{% + function unused() { return "k"; } + + let key = proto({}, { tostring: function() { return "u"; } }); + let obj = { [key]: 1, other: 2 }; + let next = 5; + + print(type(obj), " ", obj, " ", next, "\n"); +%} +-- End -- + +-- Expect stdout -- +object { "u": 1, "other": 2 } 5 +-- End -- + + +exit() in the tostring method ends the program with the given code. + +-- Testcase -- +{% + let key = proto({}, { tostring: function() { exit(21); } }); + let obj = { a: 1, b: 2, [key]: 3, c: 4 }; + + print("not reached\n"); +%} +-- End -- + +-- Expect stdout -- +-- End -- + +-- Expect exitcode -- +21 +-- End -- diff --git a/vm.c b/vm.c index 1545f1c7..34289f7b 100644 --- a/vm.c +++ b/vm.c @@ -2616,18 +2616,19 @@ uc_vm_insn_stcall(uc_vm_t *vm, uc_vm_insn_t insn) static void uc_vm_insn_sobj(uc_vm_t *vm, uc_vm_insn_t insn) { - uc_value_t *obj = uc_vm_stack_peek(vm, vm->arg.u32); + /* a tostring method of a key runs instructions, which replace vm->arg */ + size_t count = vm->arg.u32; + uc_value_t *obj = uc_vm_stack_peek(vm, count); size_t idx; /* object literals create own keys, a __set__ metamethod of the object under * construction must not intercept the keys it is built from */ - for (idx = 0; idx < vm->arg.u32; idx += 2) + for (idx = 0; idx < count; idx += 2) ucv_put(ucv_key_rawset(vm, obj, - uc_vm_stack_peek(vm, vm->arg.u32 - idx - 1), - uc_vm_stack_peek(vm, vm->arg.u32 - idx - 2))); + uc_vm_stack_peek(vm, count - idx - 1), + uc_vm_stack_peek(vm, count - idx - 2))); - for (idx = 0; idx < vm->arg.u32; idx++) - ucv_put(uc_vm_stack_pop(vm)); + uc_vm_stack_drop(vm, count); } static void From 28011e59a8a8758df61df755c07c2fdb43eaebd8 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 19 Nov 2026 08:38:40 +0100 Subject: [PATCH 11/21] vm: do not use the call frame pointer after a tostring call uc_vm_insn_update_local() uses its call frame pointer after uc_vm_value_arith(). A tostring call in there can grow the call frame vector and move it, and the handler then reads freed memory. Compute the stack slot before the call. Fixes: 35af4ba4fc21 ("treewide: rework internal data type system") Signed-off-by: Felix Fietkau --- .../99_bugs/74_update_local_callframe_growth | 31 +++++++++++++++++++ vm.c | 11 +++---- 2 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 tests/custom/99_bugs/74_update_local_callframe_growth diff --git a/tests/custom/99_bugs/74_update_local_callframe_growth b/tests/custom/99_bugs/74_update_local_callframe_growth new file mode 100644 index 00000000..93b4f507 --- /dev/null +++ b/tests/custom/99_bugs/74_update_local_callframe_growth @@ -0,0 +1,31 @@ +The update of a local variable read its call frame again after the +operation. A tostring method of an operand that calls deep enough makes the +VM move the call frames, so the read used released memory. + +-- Testcase -- +{% + function deep(n) { return n ? deep(n - 1) + 0 : 0; } + + function update_object() { + let t = proto({}, { tostring: function() { deep(300); return "t"; } }); + + t += "x"; + + return t; + } + + function update_string() { + let t = "x"; + + t += proto({}, { tostring: function() { deep(300); return "t"; } }); + + return t; + } + + print(update_object(), " ", update_string(), "\n"); +%} +-- End -- + +-- Expect stdout -- +tx xt +-- End -- diff --git a/vm.c b/vm.c index 34289f7b..8c376041 100644 --- a/vm.c +++ b/vm.c @@ -2493,12 +2493,12 @@ uc_vm_insn_update_upval(uc_vm_t *vm, uc_vm_insn_t insn) static void uc_vm_insn_update_local(uc_vm_t *vm, uc_vm_insn_t insn) { - uc_callframe_t *frame = uc_vm_current_frame(vm); - size_t slot = vm->arg.u32 & 0x00FFFFFF; + /* a tostring method of an operand can move the call frames */ + size_t slot = uc_vm_current_frame(vm)->stackframe + (vm->arg.u32 & 0x00FFFFFF); uc_value_t *inc; uc_value_t *val; - if (frame->stackframe + slot >= vm->stack.count) { + if (slot >= vm->stack.count) { uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, "local slot out of range"); return; @@ -2506,13 +2506,12 @@ uc_vm_insn_update_local(uc_vm_t *vm, uc_vm_insn_t insn) inc = uc_vm_stack_pop(vm); - val = uc_vm_value_arith(vm, vm->arg.u32 >> 24, - vm->stack.entries[frame->stackframe + slot], inc); + val = uc_vm_value_arith(vm, vm->arg.u32 >> 24, vm->stack.entries[slot], inc); uc_vm_stack_push(vm, val); ucv_put(inc); - uc_vm_stack_set(vm, frame->stackframe + slot, ucv_get(uc_vm_stack_peek(vm, 0))); + uc_vm_stack_set(vm, slot, ucv_get(uc_vm_stack_peek(vm, 0))); } static void From 44ae2ad5122f1b775114f5de179861c22012f7e9 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 12 Nov 2026 00:04:38 +0100 Subject: [PATCH 12/21] vm: release the condition of a jump that is out of range uc_vm_insn_jmpz() pops the condition and then returns with the "jump target out of range" error, without a release of the value. The compiler emits no such jump; a crafted program file can have one. Check the range before the pop. Fixes: 3756806674da ("treewide: rewrite ucode interpreter") Signed-off-by: Felix Fietkau --- vm.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/vm.c b/vm.c index 8c376041..df56c666 100644 --- a/vm.c +++ b/vm.c @@ -2912,8 +2912,8 @@ uc_vm_insn_jmpz(uc_vm_t *vm, uc_vm_insn_t insn) { uc_callframe_t *frame = uc_vm_current_frame(vm); uc_chunk_t *chunk = uc_vm_frame_chunk(frame); - uc_value_t *v = uc_vm_stack_pop(vm); int32_t addr = vm->arg.s32; + uc_value_t *v; /* ip already has been incremented */ addr -= 5; @@ -2924,6 +2924,8 @@ uc_vm_insn_jmpz(uc_vm_t *vm, uc_vm_insn_t insn) return; } + v = uc_vm_stack_pop(vm); + if (!ucv_is_truish(v)) frame->ip += addr; From fdbee95c0ea6c9724eeb992564fdc1cb8ca0c003 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 12 Nov 2026 00:04:38 +0100 Subject: [PATCH 13/21] vm: hold the scope during the update of a global variable uc_vm_insn_update_var() holds the scope object in a C variable, without a reference, across uc_vm_value_arith(). A tostring method of an operand that changes the prototype chain of the global scope can free that object, and the store then uses freed memory. Keep the scope on the stack during the operation. Fixes: 47528f02e737 ("vm: support automatic periodic GC runs") Signed-off-by: Felix Fietkau --- .../99_bugs/75_global_update_scope_lifetime | 31 +++++++++++++++++++ vm.c | 5 +++ 2 files changed, 36 insertions(+) create mode 100644 tests/custom/99_bugs/75_global_update_scope_lifetime diff --git a/tests/custom/99_bugs/75_global_update_scope_lifetime b/tests/custom/99_bugs/75_global_update_scope_lifetime new file mode 100644 index 00000000..14beeae8 --- /dev/null +++ b/tests/custom/99_bugs/75_global_update_scope_lifetime @@ -0,0 +1,31 @@ +The update of a global variable kept the scope object that holds the +variable without a reference. A tostring method of an operand that removes +that scope from the prototype chain of the globals freed it, and the VM +then stored the result in the released object. + +-- Testcase -- +{% + function setup() { + proto(global, { x: "a value that the VM keeps on the heap" }); + } + + setup(); + + let o = proto({}, { + tostring: function() { + proto(global, {}); + gc(); + + return " and more"; + } + }); + + print(x += o, "\n"); + print(x == null, "\n"); +%} +-- End -- + +-- Expect stdout -- +a value that the VM keeps on the heap and more +true +-- End -- diff --git a/vm.c b/vm.c index df56c666..e0d651ed 100644 --- a/vm.c +++ b/vm.c @@ -2389,9 +2389,14 @@ uc_vm_insn_update_var(uc_vm_t *vm, uc_vm_insn_t insn) scope = next; } + /* a tostring method of an operand can take the scope out of the chain + * of the globals and collect garbage */ + uc_vm_stack_push(vm, ucv_get(scope)); + val = uc_vm_value_arith(vm, vm->arg.u32 >> 24, val, inc); ucv_object_add(scope, ucv_string_get(name), ucv_get(val)); + ucv_put(uc_vm_stack_pop(vm)); uc_vm_stack_push(vm, val); ucv_put(name); From e1d412ff482720ee1aab10543db7860f97824564 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 12 Nov 2026 06:42:56 +0100 Subject: [PATCH 14/21] vm, lib: install the signal handlers without SA_ONSTACK uc_vm_signal_handlers_ensure() installs the signal handler of the VM with SA_ONSTACK, but nothing sets up an alternate stack. The flag has no effect on a normal run. Under valgrind, the delivery of a signal on a deep C stack fails and ends the program: Can't extend stack to 0x1ffefe79e8 during signal delivery for thread 1: too small or bad protection modes Install the handler without the flag. Also drop the flag where signal() sets a signal to "ignore" or "default". Fixes: 29b1c0deba84 ("vm: introduce basic signal handling infrastructure") Signed-off-by: Felix Fietkau --- lib.c | 2 +- vm.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib.c b/lib.c index 69a7410c..60a141ca 100644 --- a/lib.c +++ b/lib.c @@ -6250,7 +6250,7 @@ uc_signal(uc_vm_t *vm, size_t nargs) if (ucv_type(sighandler) == UC_STRING) { sigstr = ucv_string_get(sighandler); - sa.sa_flags = SA_ONSTACK | SA_RESTART; + sa.sa_flags = SA_RESTART; sigemptyset(&sa.sa_mask); if (!strcmp(sigstr, "ignore")) diff --git a/vm.c b/vm.c index e0d651ed..759b6429 100644 --- a/vm.c +++ b/vm.c @@ -195,7 +195,7 @@ uc_vm_signal_handlers_ensure(uc_vm_t *vm) vm->signal.handler = ucv_array_new_length(vm, UC_SYSTEM_SIGNAL_COUNT); vm->signal.sa.sa_handler = uc_vm_signal_handler; - vm->signal.sa.sa_flags = SA_RESTART | SA_ONSTACK; + vm->signal.sa.sa_flags = SA_RESTART; sigemptyset(&vm->signal.sa.sa_mask); tctx->signal_handler_vm = vm; From 215ec412c7758e60cbf5fa38ece3ad281848ffd8 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 12 Nov 2026 06:42:56 +0100 Subject: [PATCH 15/21] vm: run the handlers of pending signals in the order of their numbers uc_vm_signal_dispatch() clears one raised signal and calls its handler. The dispatch after the first instruction of that handler then runs the handlers of the other pending signals inside of it, so the handlers finish in the reverse order of the signal numbers. Take all raised signals before the first handler runs, so that a dispatch inside a handler finds only the signals that arrived after it. Block the signals while the function takes the set and empties the pipe, so that a signal that arrives in that time keeps both its flag and its byte in the pipe. Fixes: 29b1c0deba84 ("vm: introduce basic signal handling infrastructure") Signed-off-by: Felix Fietkau --- tests/custom/99_bugs/77_signal_dispatch_order | 84 +++++++++++++ vm.c | 113 +++++++++++++----- 2 files changed, 166 insertions(+), 31 deletions(-) create mode 100644 tests/custom/99_bugs/77_signal_dispatch_order diff --git a/tests/custom/99_bugs/77_signal_dispatch_order b/tests/custom/99_bugs/77_signal_dispatch_order new file mode 100644 index 00000000..5b2d0e24 --- /dev/null +++ b/tests/custom/99_bugs/77_signal_dispatch_order @@ -0,0 +1,84 @@ +The handlers of signals that are pending at the same time run in the order +of the signal numbers, one after the other. The interpreter started the +handler of the lowest number, and the dispatch after its first instruction +ran the handlers of the other signals inside of it, so they finished in +the reverse order. + +SIGHUP is 1, SIGINT is 2 and SIGTERM is 15 on every platform. + +-- Testcase -- +{% + let order = []; + + function handler(name) { + return () => { + push(order, name + " begin"); + push(order, name + " end"); + }; + } + + signal("SIGHUP", handler("hup")); + signal("SIGINT", handler("int")); + signal("SIGTERM", handler("term")); + + system("kill -TERM $PPID; kill -INT $PPID; kill -HUP $PPID"); + print(join(", ", order), "\n"); +%} +-- End -- + +-- Expect stdout -- +hup begin, hup end, int begin, int end, term begin, term end +-- End -- + + +A signal that comes in while a handler runs interrupts that handler. + +-- Testcase -- +{% + let order = []; + + signal("SIGHUP", () => { + push(order, "hup begin"); + system("kill -INT $PPID"); + push(order, "hup end"); + }); + + signal("SIGINT", () => push(order, "int")); + + system("kill -HUP $PPID"); + print(join(", ", order), "\n"); +%} +-- End -- + +-- Expect stdout -- +hup begin, int, hup end +-- End -- + + +After an exception of a handler, the handlers of the other pending signals +still run. They run at the next dispatch. The place of that dispatch in the +catch block is not fixed, so the test sorts the entries. + +-- Testcase -- +{% + let order = []; + + signal("SIGHUP", () => die("from hup")); + signal("SIGINT", () => push(order, "int")); + signal("SIGTERM", () => push(order, "term")); + + try { + system("kill -TERM $PPID; kill -INT $PPID; kill -HUP $PPID"); + push(order, "not reached"); + } + catch (e) { + push(order, "caught " + e.message); + } + + print(join(", ", sort(order)), "\n"); +%} +-- End -- + +-- Expect stdout -- +caught from hup, int, term +-- End -- diff --git a/vm.c b/vm.c index 759b6429..d0ee41da 100644 --- a/vm.c +++ b/vm.c @@ -3480,14 +3480,72 @@ uc_vm_output_exception(uc_vm_t *vm, uc_exception_t *ex) fprintf(stderr, "\n"); } +static uc_exception_type_t +uc_vm_signal_handler_call(uc_vm_t *vm, size_t sig) +{ + uc_value_t *handler = ucv_array_get(vm->signal.handler, sig); + uc_exception_type_t ex; + + if (!ucv_is_callable(handler)) + return EXCEPTION_NONE; + + uc_vm_stack_push(vm, ucv_get(handler)); + uc_vm_stack_push(vm, ucv_int64_new(sig)); + + ex = uc_vm_call(vm, false, 1); + + if (ex == EXCEPTION_NONE) + ucv_put(uc_vm_stack_pop(vm)); + + return ex; +} + +/* uc_vm_signal_raise() runs in a signal handler. While all signals are + * blocked it cannot run between the read of the pipe and the read of a word, + * or between the read and the store of a word. */ +static void +uc_vm_signal_pending_take(uc_vm_t *vm, uint64_t *pending) +{ + sigset_t all, prev; + int buf, rv; + size_t i; + + sigfillset(&all); + sigprocmask(SIG_BLOCK, &all, &prev); + + do { + rv = read(vm->signal.sigpipe[0], &buf, sizeof(buf)); + } while (rv > 0 || (rv == -1 && errno == EINTR)); + + for (i = 0; i < ARRAY_SIZE(vm->signal.raised); i++) { + pending[i] = vm->signal.raised[i]; + vm->signal.raised[i] = 0; + } + + sigprocmask(SIG_SETMASK, &prev, NULL); +} + +static void +uc_vm_signal_pending_restore(uc_vm_t *vm, const uint64_t *pending) +{ + sigset_t all, prev; + size_t i; + + sigfillset(&all); + sigprocmask(SIG_BLOCK, &all, &prev); + + for (i = 0; i < ARRAY_SIZE(vm->signal.raised); i++) + vm->signal.raised[i] |= pending[i]; + + sigprocmask(SIG_SETMASK, &prev, NULL); +} + uc_exception_type_t uc_vm_signal_dispatch(uc_vm_t *vm) { - uc_exception_type_t ex; - uc_value_t *handler; - uint64_t mask; - size_t i, j; - int sig, rv; + uint64_t pending[ARRAY_SIZE(vm->signal.raised)], any = 0, mask; + uc_exception_type_t ex = EXCEPTION_NONE; + size_t i, sig; /* Check whether the signal self-pipe was actually set up, rather than * re-checking config->setup_signal_handlers directly: the pipe may @@ -3497,39 +3555,32 @@ uc_vm_signal_dispatch(uc_vm_t *vm) if (vm->signal.sigpipe[0] < 0) return EXCEPTION_NONE; - for (i = 0; i < ARRAY_SIZE(vm->signal.raised); i++) { - if (!vm->signal.raised[i]) - continue; + for (i = 0; i < ARRAY_SIZE(pending); i++) + any |= vm->signal.raised[i]; - do { - rv = read(vm->signal.sigpipe[0], &sig, sizeof(sig)); - } while (rv > 0 || (rv == -1 && errno == EINTR)); - - for (j = 0; j < 64; j++) { - mask = 1ull << j; - - if (vm->signal.raised[i] & mask) { - vm->signal.raised[i] &= ~mask; - - sig = i * 64 + j; - handler = ucv_array_get(vm->signal.handler, sig); + if (!any) + return EXCEPTION_NONE; - if (ucv_is_callable(handler)) { - uc_vm_stack_push(vm, ucv_get(handler)); - uc_vm_stack_push(vm, ucv_int64_new(sig)); + /* The handlers run in the order of the signal numbers. The dispatch + * inside a handler must find only the signals that came in after it. */ + uc_vm_signal_pending_take(vm, pending); - ex = uc_vm_call(vm, false, 1); + for (sig = 0; sig < ARRAY_SIZE(pending) * 64 && ex == EXCEPTION_NONE; sig++) { + mask = 1ull << (sig % 64); - if (ex != EXCEPTION_NONE) - return ex; + if (!(pending[sig / 64] & mask)) + continue; - ucv_put(uc_vm_stack_pop(vm)); - } - } - } + pending[sig / 64] &= ~mask; + ex = uc_vm_signal_handler_call(vm, sig); } - return EXCEPTION_NONE; + /* a further handler call would drop the exception, so a later + * dispatch runs the handlers of the remaining signals */ + if (ex != EXCEPTION_NONE) + uc_vm_signal_pending_restore(vm, pending); + + return ex; } static uc_vm_status_t From f26ddd3c4959e504894bd403bc0a82451a9565a0 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 12 Nov 2026 14:43:37 +0100 Subject: [PATCH 16/21] vm: take the count of a shift modulo 64 for signed operands If one operand of a shift is negative, uc_vm_value_bitop() shifts the signed values with the count as given. A negative count, a count of 64 or more and a left shift of a negative value are undefined in C, so the result depends on the compiler and the target. A build with the undefined behaviour sanitiser reports for `1 << -1`: runtime error: shift exponent -1 is negative Take the count modulo 64, like the path for two unsigned operands, and shift the bits of a signed value to the left as an unsigned number. The results of an x86-64 build do not change. Fixes: a56887df2a0f ("Initial commit") Signed-off-by: Felix Fietkau --- tests/custom/01_arithmetic/06_shift_count | 53 +++++++++++++++++++++++ vm.c | 14 ++++-- 2 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 tests/custom/01_arithmetic/06_shift_count diff --git a/tests/custom/01_arithmetic/06_shift_count b/tests/custom/01_arithmetic/06_shift_count new file mode 100644 index 00000000..7dfb362c --- /dev/null +++ b/tests/custom/01_arithmetic/06_shift_count @@ -0,0 +1,53 @@ +The shift operators use the low six bits of the count. A negative count +and a count of 64 or more give the result of a shift by the count modulo +64. If one of the operands is negative, the result is a signed number and +the right shift keeps the sign. + +-- Testcase -- +{% + function shift(value, count) { + return [ value << count, value >> count ]; + } + + let cases = [ + [ 1, 63 ], [ 1, 64 ], [ 1, 65 ], [ 1, 0xffffffffffffffff ], + [ 1, -1 ], [ 1, -63 ], [ 1, -64 ], [ 5, -62 ], + [ -8, 1 ], [ -8, 64 ], [ -8, 65 ], [ -8, -63 ], + [ -1, 63 ], [ -1, -1 ], [ -1, 0xffffffffffffffff ], + [ 0xffffffffffffffff, 1 ], [ 0xffffffffffffffff, 64 ] + ]; + + for (let c in cases) + print(c, " ", shift(c[0], c[1]), "\n"); + + let n = 1; + n <<= -62; + print(n, "\n"); + + n = -16; + n >>= 66; + print(n, "\n"); +%} +-- End -- + +-- Expect stdout -- +[ 1, 63 ] [ 9223372036854775808, 0 ] +[ 1, 64 ] [ 1, 1 ] +[ 1, 65 ] [ 2, 0 ] +[ 1, 18446744073709551615 ] [ 9223372036854775808, 0 ] +[ 1, -1 ] [ -9223372036854775808, 0 ] +[ 1, -63 ] [ 2, 0 ] +[ 1, -64 ] [ 1, 1 ] +[ 5, -62 ] [ 20, 1 ] +[ -8, 1 ] [ -16, -4 ] +[ -8, 64 ] [ -8, -8 ] +[ -8, 65 ] [ -16, -4 ] +[ -8, -63 ] [ -16, -4 ] +[ -1, 63 ] [ -9223372036854775808, -1 ] +[ -1, -1 ] [ -9223372036854775808, -1 ] +[ -1, 18446744073709551615 ] [ -9223372036854775808, -1 ] +[ 18446744073709551615, 1 ] [ 18446744073709551614, 9223372036854775807 ] +[ 18446744073709551615, 64 ] [ 18446744073709551615, 18446744073709551615 ] +4 +-4 +-- End -- diff --git a/vm.c b/vm.c index d0ee41da..884be345 100644 --- a/vm.c +++ b/vm.c @@ -2003,6 +2003,12 @@ abs64(int64_t n) return n; } +/* C leaves a shift by a negative count or by the width of the type undefined */ +static unsigned int +shift_count(uint64_t count) +{ + return count % (sizeof(uint64_t) * CHAR_BIT); +} static uc_value_t * uc_vm_value_bitop(uc_vm_t *vm, uc_vm_insn_t operation, uc_value_t *value, uc_value_t *operand) @@ -2020,11 +2026,11 @@ uc_vm_value_bitop(uc_vm_t *vm, uc_vm_insn_t operation, uc_value_t *value, uc_val if (n1 < 0 || n2 < 0) { switch (operation) { case I_LSHIFT: - rv = ucv_int64_new(n1 << n2); + rv = ucv_int64_new((int64_t)((uint64_t)n1 << shift_count(n2))); break; case I_RSHIFT: - rv = ucv_int64_new(n1 >> n2); + rv = ucv_int64_new(n1 >> shift_count(n2)); break; case I_BAND: @@ -2049,11 +2055,11 @@ uc_vm_value_bitop(uc_vm_t *vm, uc_vm_insn_t operation, uc_value_t *value, uc_val switch (operation) { case I_LSHIFT: - rv = ucv_uint64_new(u1 << (u2 % (sizeof(uint64_t) * CHAR_BIT))); + rv = ucv_uint64_new(u1 << shift_count(u2)); break; case I_RSHIFT: - rv = ucv_uint64_new(u1 >> (u2 % (sizeof(uint64_t) * CHAR_BIT))); + rv = ucv_uint64_new(u1 >> shift_count(u2)); break; case I_BAND: From e1b42d2a475e493d8aac11513b4b9fc380d54102 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 19 Nov 2026 08:39:13 +0100 Subject: [PATCH 17/21] vm: keep the 64 bits of each operand of a bit operation If one operand of &, |, ^, << or >> is negative, uc_vm_value_bitop() works on the signed values that int64() gives. int64() turns an operand of 2^63 or more, and a double below -2^63, into INT64_MAX. Thus `-1 & 0x8000000000000000` gives 9223372036854775807. Work on the 64 bit two's complement of each operand: an integer keeps its bits, and a double outside of the range of int64_t wraps modulo 2^64. The result is signed if one operand is negative, as before, and does not change for operands in the range of int64_t. An integer of 2^63 or more then counts as the negative number with the same bits: `0xffffffffffffffff >> -1` gives -1, where it gave 0. The wrap also applies if no operand is negative: a double of 2^64 or more no longer counts as UINT64_MAX, and an infinite double counts as 0, like NaN. Fixes: b605dbfcf04f ("treewide: rework numeric value handling") Signed-off-by: Felix Fietkau --- tests/custom/01_arithmetic/03_bitwise | 29 ++++++++++++++++ tests/custom/01_arithmetic/06_shift_count | 9 +++-- vm.c | 42 +++++++++++++++++++---- 3 files changed, 72 insertions(+), 8 deletions(-) diff --git a/tests/custom/01_arithmetic/03_bitwise b/tests/custom/01_arithmetic/03_bitwise index c481b3e1..55ef3cfd 100644 --- a/tests/custom/01_arithmetic/03_bitwise +++ b/tests/custom/01_arithmetic/03_bitwise @@ -54,3 +54,32 @@ Complement: ~0 = {{ ~0 }} ~10.4 = {{ ~10.4 }} -- End -- + +An operand keeps its 64 bits. If one of the operands is negative, an +integer of 2^63 or more thus counts as the negative number with the same +bits. A double out of the 64 bit range wraps modulo 2^64, and an infinite +double counts as 0. + +-- Expect stdout -- +-1 & 0x8000000000000000 = -9223372036854775808 +-1 | 0x8000000000000000 = -1 +-1 ^ 0xffffffffffffffff = 0 +-2 & 0xffffffffffffffff = -2 +0x8000000000000000 & 0x8000000000000001 = 9223372036854775808 +-1 & 1e19 = -8446744073709551616 +-1e19 | 0 = 8446744073709551616 +1e20 & 0xffffffff = 1661992960 +Infinity | 0 = 0 +-- End -- + +-- Testcase -- +-1 & 0x8000000000000000 = {{ -1 & 0x8000000000000000 }} +-1 | 0x8000000000000000 = {{ -1 | 0x8000000000000000 }} +-1 ^ 0xffffffffffffffff = {{ -1 ^ 0xffffffffffffffff }} +-2 & 0xffffffffffffffff = {{ -2 & 0xffffffffffffffff }} +0x8000000000000000 & 0x8000000000000001 = {{ 0x8000000000000000 & 0x8000000000000001 }} +-1 & 1e19 = {{ -1 & 1e19 }} +-1e19 | 0 = {{ -1e19 | 0 }} +1e20 & 0xffffffff = {{ 1e20 & 0xffffffff }} +Infinity | 0 = {{ Infinity | 0 }} +-- End -- diff --git a/tests/custom/01_arithmetic/06_shift_count b/tests/custom/01_arithmetic/06_shift_count index 7dfb362c..93ed4b6e 100644 --- a/tests/custom/01_arithmetic/06_shift_count +++ b/tests/custom/01_arithmetic/06_shift_count @@ -1,7 +1,8 @@ The shift operators use the low six bits of the count. A negative count and a count of 64 or more give the result of a shift by the count modulo 64. If one of the operands is negative, the result is a signed number and -the right shift keeps the sign. +the right shift keeps the sign. An operand of 2^63 or more then counts as +the negative number with the same 64 bits. -- Testcase -- {% @@ -14,7 +15,9 @@ the right shift keeps the sign. [ 1, -1 ], [ 1, -63 ], [ 1, -64 ], [ 5, -62 ], [ -8, 1 ], [ -8, 64 ], [ -8, 65 ], [ -8, -63 ], [ -1, 63 ], [ -1, -1 ], [ -1, 0xffffffffffffffff ], - [ 0xffffffffffffffff, 1 ], [ 0xffffffffffffffff, 64 ] + [ -1, 0x8000000000000000 ], + [ 0xffffffffffffffff, 1 ], [ 0xffffffffffffffff, 64 ], + [ 0xffffffffffffffff, -1 ] ]; for (let c in cases) @@ -46,8 +49,10 @@ the right shift keeps the sign. [ -1, 63 ] [ -9223372036854775808, -1 ] [ -1, -1 ] [ -9223372036854775808, -1 ] [ -1, 18446744073709551615 ] [ -9223372036854775808, -1 ] +[ -1, 9223372036854775808 ] [ -1, -1 ] [ 18446744073709551615, 1 ] [ 18446744073709551614, 9223372036854775807 ] [ 18446744073709551615, 64 ] [ 18446744073709551615, 18446744073709551615 ] +[ 18446744073709551615, -1 ] [ -9223372036854775808, -1 ] 4 -4 -- End -- diff --git a/vm.c b/vm.c index 884be345..cf56a02f 100644 --- a/vm.c +++ b/vm.c @@ -2010,20 +2010,53 @@ shift_count(uint64_t count) return count % (sizeof(uint64_t) * CHAR_BIT); } +/* The bit operators work on the 64 bit two's complement of a number: a + * double out of range wraps modulo 2^64 */ +static uint64_t +bit_pattern(uc_value_t *nv, bool *negative) +{ + int64_t n = ucv_int64_get(nv); + double d; + + *negative = false; + + if (errno != ERANGE) { + *negative = (n < 0); + + return (uint64_t)n; + } + + if (ucv_type(nv) == UC_INTEGER) + return ucv_uint64_get(nv); + + d = ucv_double_get(nv); + + if (!isfinite(d)) + return 0; + + *negative = (d < 0); + d = fmod(d, ldexp(1.0, 64)); + + return (d < 0) ? -(uint64_t)-d : (uint64_t)d; +} + static uc_value_t * uc_vm_value_bitop(uc_vm_t *vm, uc_vm_insn_t operation, uc_value_t *value, uc_value_t *operand) { uc_value_t *nv1, *nv2, *rv = NULL; uint64_t u1, u2; int64_t n1, n2; + bool neg1, neg2; nv1 = ucv_to_number(value); nv2 = ucv_to_number(operand); - n1 = int64(nv1, &u1); - n2 = int64(nv2, &u2); + u1 = bit_pattern(nv1, &neg1); + u2 = bit_pattern(nv2, &neg2); + n1 = (int64_t)u1; + n2 = (int64_t)u2; - if (n1 < 0 || n2 < 0) { + if (neg1 || neg2) { switch (operation) { case I_LSHIFT: rv = ucv_int64_new((int64_t)((uint64_t)n1 << shift_count(n2))); @@ -2050,9 +2083,6 @@ uc_vm_value_bitop(uc_vm_t *vm, uc_vm_insn_t operation, uc_value_t *value, uc_val } } else { - if (!u1) u1 = (uint64_t)n1; - if (!u2) u2 = (uint64_t)n2; - switch (operation) { case I_LSHIFT: rv = ucv_uint64_new(u1 << shift_count(u2)); From 7bc19117b61e3cc236d0a75057f937e09d28f1b2 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 12 Nov 2026 14:43:37 +0100 Subject: [PATCH 18/21] vm: resolve an upvalue reference in the update of a global variable call(), include() and render() copy the entries of a scope object without a prototype into the scope of the code they run. For a module namespace these entries are upvalue references to the exported variables. uc_vm_insn_update_var() passes such a reference to uc_vm_value_arith() unresolved, so a compound assignment, an increment or a decrement of the variable gives NaN. Read the value through the reference, like uc_vm_insn_load_var(). The result replaces the entry of the scope, like in uc_vm_insn_store_var(). Fixes: d85bc716df9b ("vm: introduce import and export opcodes") Signed-off-by: Felix Fietkau --- .../custom/04_modules/28_export_in_call_scope | 50 +++++++++++++++++++ vm.c | 7 ++- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 tests/custom/04_modules/28_export_in_call_scope diff --git a/tests/custom/04_modules/28_export_in_call_scope b/tests/custom/04_modules/28_export_in_call_scope new file mode 100644 index 00000000..be38afd5 --- /dev/null +++ b/tests/custom/04_modules/28_export_in_call_scope @@ -0,0 +1,50 @@ +call() copies the entries of a scope object without a prototype into the +global scope of the called function. The entries of a module namespace +are references to the exported variables. A compound assignment, an +increment and a decrement of such a global variable read the value of +the exported variable. Like a plain assignment they store the result in +the scope of the function, and the variable of the module keeps its value. + +-- Testcase -- +import * as mod from "./files/counter.uc"; + +function assign() { counter = counter + 2; return counter; } +function add() { counter += 2; return counter; } +function append() { label += "b"; return label; } +function shift() { counter <<= 3; return counter; } +function increment() { return [ counter++, ++counter, counter ]; } +function decrement() { return [ counter--, --counter, counter ]; } +function copy() { let scope = { ...mod }; scope.counter += 2; scope.counter++; return scope.counter; } + +for (let fn in [ assign, add, append, shift, increment, decrement, copy ]) + print(call(fn, null, mod), " ", mod.read(), "\n"); + +mod.step(); + +for (let fn in [ add, increment ]) + print(call(fn, null, mod), " ", mod.read(), "\n"); +-- End -- + +-- File counter.uc -- +export let counter = 5; +export let label = "a"; + +export function read() { return [ counter, label ]; }; +export function step() { counter++; }; +-- End -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +7 [ 5, "a" ] +7 [ 5, "a" ] +ab [ 5, "a" ] +40 [ 5, "a" ] +[ 5, 7, 7 ] [ 5, "a" ] +[ 5, 3, 3 ] [ 5, "a" ] +8 [ 5, "a" ] +8 [ 6, "a" ] +[ 6, 8, 8 ] [ 6, "a" ] +-- End -- diff --git a/vm.c b/vm.c index cf56a02f..2eb66868 100644 --- a/vm.c +++ b/vm.c @@ -2429,10 +2429,15 @@ uc_vm_insn_update_var(uc_vm_t *vm, uc_vm_insn_t insn) * of the globals and collect garbage */ uc_vm_stack_push(vm, ucv_get(scope)); - val = uc_vm_value_arith(vm, vm->arg.u32 >> 24, val, inc); + /* the push resolves an upvalue reference that the scope holds for the + * export of a module */ + uc_vm_stack_push(vm, ucv_get(val)); + + val = uc_vm_value_arith(vm, vm->arg.u32 >> 24, uc_vm_stack_peek(vm, 0), inc); ucv_object_add(scope, ucv_string_get(name), ucv_get(val)); ucv_put(uc_vm_stack_pop(vm)); + ucv_put(uc_vm_stack_pop(vm)); uc_vm_stack_push(vm, val); ucv_put(name); From 35b8ceb9d8518d0e4d756f40acfa161b96977176 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 12 Nov 2026 14:43:37 +0100 Subject: [PATCH 19/21] vm: raise an exception if an import gets no module uc_vm_insn_dynload() takes a NULL result of uc_require_library() for a pending exception and returns without a result. For an import, uc_require_library() also returns NULL without an exception: for a name that is not a string and for a null entry of the module table. After import(5) the stack of the function is thus one value short, and the values of its variables move by one slot. Raise a type error for a name that is not a string and a runtime error for a null entry of the module table, like every other failed import raises an exception. Fixes: c9442f12ee05 ("vm: introduce new I_DYNLOAD opcode") Fixes: 926f20b55e13 ("compiler: add support for runtime module imports") Signed-off-by: Felix Fietkau --- .../04_modules/29_dynamic_import_no_module | 86 +++++++++++++++++++ vm.c | 12 +++ 2 files changed, 98 insertions(+) create mode 100644 tests/custom/04_modules/29_dynamic_import_no_module diff --git a/tests/custom/04_modules/29_dynamic_import_no_module b/tests/custom/04_modules/29_dynamic_import_no_module new file mode 100644 index 00000000..e0153836 --- /dev/null +++ b/tests/custom/04_modules/29_dynamic_import_no_module @@ -0,0 +1,86 @@ +import() raises an exception if its argument is not a string and if the +module table holds null for the name. The values of the function that +called import() stay in place. + +-- Testcase -- +function load(name) { + let before = "before", module, after = "after"; + + try { + module = import(name); + } + catch (e) { + module = `${e.type}: ${e.message}`; + } + + return [ before, module, after ]; +} + +modules.absent = null; + +print(load(5), "\n"); +print(load(null), "\n"); +print(load([ "fs" ]), "\n"); +print(load("absent"), "\n"); +print([ 1, type(import("fs")), 3 ], "\n"); +-- End -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +[ "before", "Type error: Passed module name is not a string", "after" ] +[ "before", "Type error: Passed module name is not a string", "after" ] +[ "before", "Type error: Passed module name is not a string", "after" ] +[ "before", "Runtime error: Module 'absent' is null", "after" ] +[ 1, "object", 3 ] +-- End -- + +The static import of a native module loads it at run time as well. If the +module table holds null for the name, the import raises the same +exception, for a list of names and for a wildcard import. + +-- Testcase -- +modules.fs = null; + +import { readfile } from 'fs'; + +print(type(readfile), "\n"); +-- End -- + +-- Args -- +-R +-- End -- + +-- Expect stderr -- +Runtime error: Module 'fs' is null +In line 3, byte 26: + + `import { readfile } from 'fs';` + ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~ + + +-- End -- + +-- Testcase -- +modules.fs = null; + +import * as fs from 'fs'; + +print(type(fs), "\n"); +-- End -- + +-- Args -- +-R +-- End -- + +-- Expect stderr -- +Runtime error: Module 'fs' is null +In line 3, byte 21: + + `import * as fs from 'fs';` + ~~~~~~~~~~~~~~~~~~~~^~~~~ + + +-- End -- diff --git a/vm.c b/vm.c index 2eb66868..c54b2637 100644 --- a/vm.c +++ b/vm.c @@ -3364,6 +3364,18 @@ uc_vm_insn_dynload(uc_vm_t *vm, uc_vm_insn_t insn) /* Attempt to load module. Will raise exception on error */ name = uc_vm_stack_pop(vm); modscope = uc_require_library(vm, name, true); + + /* the two cases for which uc_require_library() gives null and raises + * nothing, as require() needs it */ + if (!modscope && vm->exception.type == EXCEPTION_NONE) { + if (ucv_type(name) != UC_STRING) + uc_vm_raise_exception(vm, EXCEPTION_TYPE, + "Passed module name is not a string"); + else + uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, + "Module '%s' is null", ucv_string_get(name)); + } + ucv_put(name); if (!modscope || vm->exception.type != EXCEPTION_NONE) { From 3e44e7841b886050b204033b88bc30b7754b6c4e Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 12 Nov 2026 14:43:37 +0100 Subject: [PATCH 20/21] vm: raise a type error for the store of a global in a constant scope The store of a global variable searches the name in the scope of the function and in its prototypes. With call(fn, null, proto({}, ns)) and a module namespace ns, the search ends in the namespace, which is a constant object. ucv_object_add() stores nothing there, so the assignment has no effect and raises no error. Raise the "object value is immutable" type error, like a property store on a constant object does. The global object is constant too while sort() orders its keys and while a conversion to a string formats it. A store to a global variable from the comparator or from a tostring method now raises the type error as well. Fixes: d85bc716df9b ("vm: introduce import and export opcodes") Signed-off-by: Felix Fietkau --- .../30_global_store_in_constant_scope | 94 +++++++++++++++++++ vm.c | 45 ++++++--- 2 files changed, 124 insertions(+), 15 deletions(-) create mode 100644 tests/custom/04_modules/30_global_store_in_constant_scope diff --git a/tests/custom/04_modules/30_global_store_in_constant_scope b/tests/custom/04_modules/30_global_store_in_constant_scope new file mode 100644 index 00000000..936881cd --- /dev/null +++ b/tests/custom/04_modules/30_global_store_in_constant_scope @@ -0,0 +1,94 @@ +A function that runs with a scope whose prototype is a module namespace +finds its global variables in the namespace, which is a constant object. +A store to such a variable raises the type error of a property store on a +constant object, for an assignment, a compound assignment, an increment +and for a name that no object of the scope holds. The variable of the +module keeps its value. + +-- Testcase -- +import * as mod from "./files/counter.uc"; + +function assign() { counter = 7; return counter; } +function update() { counter += 1.5; return counter; } +function increment() { return counter++; } +function create() { other = 1; return other; } +function assign_strict() { "use strict"; counter = 7; return counter; } +function update_strict() { "use strict"; counter += 1.5; return counter; } +function read() { return counter; } + +for (let fn in [ assign, update, increment, create, assign_strict, update_strict, read ]) { + try { + print(call(fn, null, proto({}, mod)), "\n"); + } + catch (e) { + print(e.type, ": ", e.message, "\n"); + } +} + +print(mod.read(), "\n"); + +let scope = proto({ counter: 1 }, mod); + +print(call(update, null, scope), " ", scope.counter, " ", mod.read(), "\n"); +-- End -- + +-- File counter.uc -- +export let counter = 5; + +export function read() { return counter; }; +-- End -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +Type error: object value is immutable +Type error: object value is immutable +Type error: object value is immutable +Type error: object value is immutable +Type error: object value is immutable +Type error: object value is immutable +5 +5 +2.5 2.5 5 +-- End -- + +The global object is constant while sort() orders its keys and while a +conversion to a string formats it. A store to a global variable from the +comparator or from a tostring method raises the same type error then. + +-- Testcase -- +let n = 0; + +try { + sort(global, (k1, v1, k2, v2) => { counter = ++n; return 0; }); + print("sorted\n"); +} +catch (e) { + print(e.type, ": ", e.message, "\n"); +} + +x = 1; +holder = proto({}, { tostring: function() { x = 2; return "y"; } }); + +try { + sprintf("%s", global); + print("formatted\n"); +} +catch (e) { + print(e.type, ": ", e.message, "\n"); +} + +print([ counter, x ], "\n"); +-- End -- + +-- Args -- +-R +-- End -- + +-- Expect stdout -- +Type error: object value is immutable +Type error: object value is immutable +[ null, 1 ] +-- End -- diff --git a/vm.c b/vm.c index c54b2637..a8de2b19 100644 --- a/vm.c +++ b/vm.c @@ -1838,6 +1838,20 @@ uc_vm_insn_load_closure(uc_vm_t *vm, uc_vm_insn_t insn) } } +static bool +assert_mutable_value(uc_vm_t *vm, uc_value_t *val) +{ + if (ucv_is_constant(val)) { + uc_vm_raise_exception(vm, EXCEPTION_TYPE, + "%s value is immutable", + ucv_typename(val)); + + return false; + } + + return true; +} + static void uc_vm_insn_store_var(uc_vm_t *vm, uc_vm_insn_t insn) { @@ -1873,25 +1887,19 @@ uc_vm_insn_store_var(uc_vm_t *vm, uc_vm_insn_t insn) scope = next; } - if (scope && ucv_type(name) == UC_STRING) - ucv_object_add(scope, ucv_string_get(name), ucv_get(v)); - - ucv_put(name); - uc_vm_stack_push(vm, v); -} + if (scope && ucv_type(name) == UC_STRING) { + if (!assert_mutable_value(vm, scope)) { + ucv_put(name); + ucv_put(v); -static bool -assert_mutable_value(uc_vm_t *vm, uc_value_t *val) -{ - if (ucv_is_constant(val)) { - uc_vm_raise_exception(vm, EXCEPTION_TYPE, - "%s value is immutable", - ucv_typename(val)); + return; + } - return false; + ucv_object_add(scope, ucv_string_get(name), ucv_get(v)); } - return true; + ucv_put(name); + uc_vm_stack_push(vm, v); } static void @@ -2425,6 +2433,13 @@ uc_vm_insn_update_var(uc_vm_t *vm, uc_vm_insn_t insn) scope = next; } + if (!assert_mutable_value(vm, scope)) { + ucv_put(name); + ucv_put(inc); + + return; + } + /* a tostring method of an operand can take the scope out of the chain * of the globals and collect garbage */ uc_vm_stack_push(vm, ucv_get(scope)); From 3d5123ed08a48c75e305119f3e48489005ec77f0 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 19 Nov 2026 10:52:37 +0100 Subject: [PATCH 21/21] vm: keep the 64 bits of the operand of the complement uc_vm_insn_complement() converts its operand with int64(), which clamps a double outside of the range of int64_t. `~1e20` thus gives 0, while `1e20 ^ 0xffffffffffffffff` gives 10680464442257309695. Work on the 64 bit two's complement of the operand, like the other bit operators do. The result does not change for an integer and for a double in the range of int64_t. Fixes: b605dbfcf04f ("treewide: rework numeric value handling") Signed-off-by: Felix Fietkau --- tests/custom/01_arithmetic/03_bitwise | 18 ++++++++++++++++++ vm.c | 16 ++++++---------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/tests/custom/01_arithmetic/03_bitwise b/tests/custom/01_arithmetic/03_bitwise index 55ef3cfd..d92d8e0c 100644 --- a/tests/custom/01_arithmetic/03_bitwise +++ b/tests/custom/01_arithmetic/03_bitwise @@ -83,3 +83,21 @@ Infinity | 0 = 0 1e20 & 0xffffffff = {{ 1e20 & 0xffffffff }} Infinity | 0 = {{ Infinity | 0 }} -- End -- + +The complement works on the same 64 bits as the other bit operators. + +-- Expect stdout -- +~0x8000000000000000 = 9223372036854775807 +~1e20 = 10680464442257309695 +~-1e20 = 7766279631452241919 +~Infinity = 18446744073709551615 +~-Infinity = 18446744073709551615 +-- End -- + +-- Testcase -- +~0x8000000000000000 = {{ ~0x8000000000000000 }} +~1e20 = {{ ~1e20 }} +~-1e20 = {{ ~-1e20 }} +~Infinity = {{ ~Infinity }} +~-Infinity = {{ ~-Infinity }} +-- End -- diff --git a/vm.c b/vm.c index a8de2b19..12ab1d71 100644 --- a/vm.c +++ b/vm.c @@ -2833,20 +2833,16 @@ uc_vm_insn_complement(uc_vm_t *vm, uc_vm_insn_t insn) { uc_value_t *v = uc_vm_stack_pop(vm); uc_value_t *nv; + bool negative; uint64_t u; - int64_t n; nv = ucv_to_number(v); - n = int64(nv, &u); - - if (n < 0) { - uc_vm_stack_push(vm, ucv_int64_new(~n)); - } - else { - if (!u) u = (uint64_t)n; + u = ~bit_pattern(nv, &negative); - uc_vm_stack_push(vm, ucv_uint64_new(~u)); - } + if (negative) + uc_vm_stack_push(vm, ucv_int64_new((int64_t)u)); + else + uc_vm_stack_push(vm, ucv_uint64_new(u)); ucv_put(nv); ucv_put(v);