diff --git a/include/ucode/internal/vm.h b/include/ucode/internal/vm.h index 7adf4e124..f89e696af 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 508c4cd19..60a141ca3 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; @@ -6258,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/tests/custom/01_arithmetic/03_bitwise b/tests/custom/01_arithmetic/03_bitwise index c481b3e1f..d92d8e0cf 100644 --- a/tests/custom/01_arithmetic/03_bitwise +++ b/tests/custom/01_arithmetic/03_bitwise @@ -54,3 +54,50 @@ 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 -- + +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/tests/custom/01_arithmetic/06_shift_count b/tests/custom/01_arithmetic/06_shift_count new file mode 100644 index 000000000..93ed4b6e7 --- /dev/null +++ b/tests/custom/01_arithmetic/06_shift_count @@ -0,0 +1,58 @@ +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. An operand of 2^63 or more then counts as +the negative number with the same 64 bits. + +-- 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 ], + [ -1, 0x8000000000000000 ], + [ 0xffffffffffffffff, 1 ], [ 0xffffffffffffffff, 64 ], + [ 0xffffffffffffffff, -1 ] + ]; + + 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 ] +[ -1, 9223372036854775808 ] [ -1, -1 ] +[ 18446744073709551615, 1 ] [ 18446744073709551614, 9223372036854775807 ] +[ 18446744073709551615, 64 ] [ 18446744073709551615, 18446744073709551615 ] +[ 18446744073709551615, -1 ] [ -9223372036854775808, -1 ] +4 +-4 +-- End -- 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 000000000..be38afd55 --- /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/tests/custom/04_modules/29_dynamic_import_no_module b/tests/custom/04_modules/29_dynamic_import_no_module new file mode 100644 index 000000000..e01538368 --- /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/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 000000000..936881cd6 --- /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/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 000000000..55e736745 --- /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 000000000..fd1946d80 --- /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/64_include_at_recursion_limit b/tests/custom/99_bugs/64_include_at_recursion_limit new file mode 100644 index 000000000..071eeaffe --- /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/tests/custom/99_bugs/66_call_metamethod_cfunction b/tests/custom/99_bugs/66_call_metamethod_cfunction new file mode 100644 index 000000000..99131f155 --- /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/tests/custom/99_bugs/67_spread_argument_lifetime b/tests/custom/99_bugs/67_spread_argument_lifetime new file mode 100644 index 000000000..9e25a9a8b --- /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/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 000000000..6908dabb2 --- /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/tests/custom/99_bugs/69_object_spread_value_leak b/tests/custom/99_bugs/69_object_spread_value_leak new file mode 100644 index 000000000..86451657e --- /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/tests/custom/99_bugs/70_exception_range_boundaries b/tests/custom/99_bugs/70_exception_range_boundaries new file mode 100644 index 000000000..9647d9ea8 --- /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/tests/custom/99_bugs/70_exception_unwind_upvalues b/tests/custom/99_bugs/70_exception_unwind_upvalues new file mode 100644 index 000000000..02575e1cf --- /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/tests/custom/99_bugs/72_property_operand_gc b/tests/custom/99_bugs/72_property_operand_gc new file mode 100644 index 000000000..1849d86ef --- /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/tests/custom/99_bugs/73_object_literal_key_tostring b/tests/custom/99_bugs/73_object_literal_key_tostring new file mode 100644 index 000000000..4e5f8b95e --- /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/tests/custom/99_bugs/74_update_local_callframe_growth b/tests/custom/99_bugs/74_update_local_callframe_growth new file mode 100644 index 000000000..93b4f5070 --- /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/tests/custom/99_bugs/75_global_update_scope_lifetime b/tests/custom/99_bugs/75_global_update_scope_lifetime new file mode 100644 index 000000000..14beeae87 --- /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/tests/custom/99_bugs/76_signal_after_nested_return b/tests/custom/99_bugs/76_signal_after_nested_return new file mode 100644 index 000000000..d3090d36a --- /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/tests/custom/99_bugs/77_signal_dispatch_order b/tests/custom/99_bugs/77_signal_dispatch_order new file mode 100644 index 000000000..5b2d0e24b --- /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/types.c b/types.c index 72a6ae0f5..9ecdb4754 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)); } @@ -2842,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; @@ -2856,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 f70bf7ff2..12ab1d717 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)); } @@ -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; @@ -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) @@ -811,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) { @@ -839,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; @@ -1196,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 @@ -1207,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); @@ -1243,48 +1294,35 @@ uc_vm_handle_exception(uc_vm_t *vm) return false; chunk = uc_vm_frame_chunk(frame); - pos = frame->ip - chunk->entries; + range = uc_vm_frame_ehrange(frame, between); - /* 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)); + if (!range) + return false; - /* prepare exception object and expose it to user handler code */ - exo = uc_vm_exception_object(vm); + /* a closure may hold a variable of the block that the unwind drops */ + slot = frame->stackframe + range->slot; + uc_vm_close_upvals(vm, slot); - uc_vm_stack_push(vm, exo); + while (vm->stack.count > slot) + ucv_put(uc_vm_stack_pop(vm)); - /* reset exception information */ - uc_vm_clear_exception(vm); + /* prepare exception object and expose it to user handler code */ + exo = uc_vm_exception_object(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; - } + uc_vm_stack_push(vm, exo); -#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 - - 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 @@ -1432,11 +1470,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 +1498,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) { @@ -1586,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: @@ -1608,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 @@ -1765,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) { @@ -1800,47 +1887,45 @@ 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_stack_push(vm, NULL); - 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 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; @@ -1850,9 +1935,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 @@ -1925,6 +2011,42 @@ 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); +} + +/* 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) @@ -1932,21 +2054,24 @@ uc_vm_value_bitop(uc_vm_t *vm, uc_vm_insn_t operation, uc_value_t *value, uc_val 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(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: @@ -1966,16 +2091,13 @@ 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 << (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: @@ -2311,9 +2433,26 @@ uc_vm_insn_update_var(uc_vm_t *vm, uc_vm_insn_t insn) scope = next; } - val = uc_vm_value_arith(vm, vm->arg.u32 >> 24, val, inc); + 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)); + + /* 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); @@ -2323,43 +2462,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; @@ -2371,10 +2507,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 @@ -2418,12 +2554,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; @@ -2431,13 +2567,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 @@ -2541,18 +2676,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 @@ -2586,6 +2722,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; } @@ -2696,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); + u = ~bit_pattern(nv, &negative); - if (n < 0) { - uc_vm_stack_push(vm, ucv_int64_new(~n)); - } - else { - if (!u) u = (uint64_t)n; - - 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); @@ -2836,8 +2969,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; @@ -2848,6 +2981,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; @@ -3091,23 +3226,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; @@ -3119,8 +3257,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 */ @@ -3235,6 +3375,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) { @@ -3392,14 +3544,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 @@ -3409,39 +3619,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 @@ -3461,6 +3664,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; @@ -3718,7 +3922,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; } @@ -3730,7 +3934,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]; @@ -3740,7 +3944,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; } @@ -3751,7 +3955,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; @@ -3766,7 +3970,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 @@ -3775,9 +3983,17 @@ 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) + if (uc_vm_signal_dispatch(vm) != EXCEPTION_NONE) { + between = true; + goto exception; + } /* check for break request */ if (vm->break_requested) { @@ -3789,6 +4005,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) { @@ -3828,6 +4063,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); @@ -3868,15 +4106,35 @@ 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 depth = vm->callframes.count; + size_t argc = nargs & 0xffff; + uc_value_t *ctx, *fno; + + 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)); 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)) + goto drop; + + /* 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 uc_vm_signal_dispatch_host(vm); + +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 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; }