RUNVM
The RUNVM instruction creates an isolated VM instance, allowing code execution while safely retrieving data such as stack state, registers, and gas consumption, ensuring the caller's state remains unaffected and allowing arbitrary code to run safely. This is helpful for v4-style plugins and similar use cases.
| Fift syntax | Stack | Description |
|---|---|---|
flags RUNVM | x_1 ... x_n n code [r] [c4] [c7] [g_l] [g_m] - x'_1 ... x'_m exit_code [c4'] [c5'] [g_c] | Executes a child VM with the given code and stack values x_1 ... x_n. Returns the modified stack x'_1 ... x'_m along with an exit code. Flags determine other arguments and return values. See details below. |
RUNVMX | x_1 ... x_n n code [r] [c4] [c7] [g_l] [g_m] flags - x'_1 ... x'_m exit_code [c4'] [c5'] [g_c] | Same as RUNVM, but retrieves flags from the stack. |
Flags are the same as for Fift RUNVMX; RUNVMX reads flags from the stack:
+1: setsc3to code.+2: pushes an implicit0before executing the code. Note: it only works when the+1flag is set.+4: takes persistent datac4from the stack and returns its final value.+8: takes the gas limitg_lfrom the stack and returns the consumed gasg_c.+16: takesc7(smart contract context) from the stack.+32: returns the final value ofc5(actions).+64: pops the hard gas limitg_menabled byACCEPTfrom the stack.+128: enables "isolated gas consumption", meaning the child VM maintains a separate set of visited cells and achksgncounter.+256: pops an integerrand ensures exactlyrvalues are returned from the top of the stack:- If
RUNVMcall succeeds andris set, it returnsrelements. Ifris not set, it returns all available elements. - If
RUNVMis successful but lacks elements on the stack, meaning the stack depth is less thanr, it is treated as an exception in the child VM. Theexit_codeis set to-3, andexit_argis set to0, so0is returned as the only stack element. - If
RUNVMfails with an exception, only one element is returned,exit_arg, which should not be confused withexit_code. - In the case of running out of gas,
exit_codeis set to-14, andexit_argcontains the amount of gas.
- If
Gas cost:
- 66 gas.
- 1 gas for each stack element passed to the child VM (the first 32 elements are free).
- 1 gas for each stack element returned from the child VM (the first 32 elements are free).
See also
Was this article useful?