Skip to content

vm: fix call frame, exception, signal and operand lifetime defects - #523

Open
nbd168 wants to merge 21 commits into
ucode-lang:masterfrom
nbd168:fixes-vm
Open

nbd168 wants to merge 21 commits into
ucode-lang:masterfrom
nbd168:fixes-vm

Conversation

@nbd168

@nbd168 nbd168 commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

This series fixes defects in the VM core. Each commit holds one fix and,
where a script can reach the defect, a test for it.

Calls and exit():

  • exit() releases only the call frames of the current invocation and
    stays pending until the C functions above it return
  • a call that fails before its call frame exists drops the callee and
    its arguments
  • no interpreter run after a C function that call resolves to
  • spread call arguments stay on the stack; an object spread releases
    the value that it rejects

Exceptions and signals:

  • the exception handler is selected by the instruction that raised
  • an exception unwind closes the upvalues of the slots that it drops
  • no signal handler runs after the last RETURN of a nested invocation
  • pending signal handlers run in the order of their signal numbers
  • signal handlers are installed without SA_ONSTACK

Operand lifetime:

  • property operations keep their operands on the stack
  • SOBJ and the update of a local variable read their instruction state
    before a tostring call
  • the update of a global variable holds its scope
  • an out-of-range jump releases its condition

Bit operators:

  • a shift takes its count modulo 64 for signed operands
  • &, |, ^, <<, >> and ~ work on the 64 bit two's complement of each
    operand

Globals and imports:

  • the update of a global variable resolves an upvalue reference
  • an import that gets no module raises an exception
  • a store to a global variable in a constant scope raises a type error

Behaviour changes:

  • uc_vm_call() and uc_vm_execute() run pending signal handlers before
    they return to a C host with no call frame left; an exception of a
    handler is then the result of the call
  • the bit operators give other results for an integer of 2^63 or more
    together with a negative operand, and for a double outside of the
    range of int64_t
  • import() of a name whose entry in the module table is null raises a
    runtime error
  • a store to a global variable from a sort() comparator of the global
    object, or from a tostring method while the global object is
    formatted, raises "object value is immutable"

blogic and others added 21 commits November 19, 2026 10:16
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: 4ae0568 ("lib, vm: reimplement exit() as exception type")
Signed-off-by: John Crispin <[email protected]>
Signed-off-by: Felix Fietkau <[email protected]>
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: 080cbd7 ("lib: release the main function of a required file")
Signed-off-by: Felix Fietkau <[email protected]>
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: af69c8d ("types, vm, lib: add metamethod support for prototyped values")
Signed-off-by: John Crispin <[email protected]>
Signed-off-by: Felix Fietkau <[email protected]>
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: 3756806 ("treewide: rewrite ucode interpreter")
Signed-off-by: John Crispin <[email protected]>
Signed-off-by: Felix Fietkau <[email protected]>
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: 3756806 ("treewide: rewrite ucode interpreter")
Fixes: 47528f0 ("vm: support automatic periodic GC runs")
Signed-off-by: John Crispin <[email protected]>
Signed-off-by: Felix Fietkau <[email protected]>
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: 3756806 ("treewide: rewrite ucode interpreter")
Signed-off-by: Felix Fietkau <[email protected]>
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: 3756806 ("treewide: rewrite ucode interpreter")
Signed-off-by: Felix Fietkau <[email protected]>
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: 29b1c0d ("vm: introduce basic signal handling infrastructure")
Signed-off-by: Felix Fietkau <[email protected]>
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: af69c8d ("types, vm, lib: add metamethod support for prototyped values")
Fixes: 47528f0 ("vm: support automatic periodic GC runs")
Signed-off-by: Felix Fietkau <[email protected]>
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: c402551 ("vm: fix crash on object literals with non-string computed properties")
Signed-off-by: Felix Fietkau <[email protected]>
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: 35af4ba ("treewide: rework internal data type system")
Signed-off-by: Felix Fietkau <[email protected]>
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: 3756806 ("treewide: rewrite ucode interpreter")
Signed-off-by: Felix Fietkau <[email protected]>
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: 47528f0 ("vm: support automatic periodic GC runs")
Signed-off-by: Felix Fietkau <[email protected]>
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: 29b1c0d ("vm: introduce basic signal handling infrastructure")
Signed-off-by: Felix Fietkau <[email protected]>
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: 29b1c0d ("vm: introduce basic signal handling infrastructure")
Signed-off-by: Felix Fietkau <[email protected]>
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: a56887d ("Initial commit")
Signed-off-by: Felix Fietkau <[email protected]>
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: b605dbf ("treewide: rework numeric value handling")
Signed-off-by: Felix Fietkau <[email protected]>
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: d85bc71 ("vm: introduce import and export opcodes")
Signed-off-by: Felix Fietkau <[email protected]>
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: c9442f1 ("vm: introduce new I_DYNLOAD opcode")
Fixes: 926f20b ("compiler: add support for runtime module imports")
Signed-off-by: Felix Fietkau <[email protected]>
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: d85bc71 ("vm: introduce import and export opcodes")
Signed-off-by: Felix Fietkau <[email protected]>
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: b605dbf ("treewide: rework numeric value handling")
Signed-off-by: Felix Fietkau <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants