Skip to content

fix(vm): charge goto loops and native re-entry to the instruction and depth limits - #446

Open
adriencohen wants to merge 1 commit into
deviceio:mainfrom
adriencohen:fix/vm-charge-goto-and-reentry
Open

adriencohen wants to merge 1 commit into
deviceio:mainfrom
adriencohen:fix/vm-charge-goto-and-reentry

Conversation

@adriencohen

Copy link
Copy Markdown

A script could loop or recurse without bound despite :max_instructions and :max_call_depth: goto loops, __call chains, metamethod work, and recursion that re-enters the VM from native code (pcall, stdlib callbacks, a host callback calling Lua.call_function!/3) were never charged. Fixes #411.

Script before after
::top:: goto top still running after 5 s budget exceeded in 93 ms
local function f() return pcall(f) end return f() killed at 512 MB after 4 s stops at depth 200 in 4 ms
local a = {} setmetatable(a, {__call = a}) return a() killed at 512 MB after 0.7 s '__call' chain too long in <1 ms
table.sort comparator that calls table.sort killed at 512 MB after 0.6 s stack overflow in 3 ms

max_instructions: 1_000_000, max_call_depth: 200, each script in a process capped at 512 MB, Apple M2, OTP 29.

A backward goto now ticks the budget like a loop back-edge, and every call into a function from native code costs one tick and one depth level, native callees included. A top-level Lua.call_function/3 now costs one depth level and one instruction, like a call made from Lua. A direct call from Lua to a native function, and the native's own work, are still uncharged.

… depth limits

goto loops, __call chains, metamethod work and recursion that re-enters
Lua from native code (pcall, stdlib callbacks, Lua.call_function/3 from a
host callback) were not charged to :max_instructions or :max_call_depth,
so a script could loop or recurse without bound.
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.

vm: :max_instructions under-counts metamethod work behind executor bridges

1 participant