Fix stack truncation on metamethod return corrupting numeric for loops#326
Open
Jackarunda wants to merge 3 commits into
Open
Fix stack truncation on metamethod return corrupting numeric for loops#326Jackarunda wants to merge 3 commits into
Jackarunda wants to merge 3 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a VM stack-pop bug where returning a single value from a metamethod could truncate the caller’s stack below still-live registers, which could corrupt numeric for loop control slots and lead to non-terminating loops.
Changes:
- Adjusted the VM’s “single-result return” path to restore the caller’s stack top (using
CurrentReturnFrameBase) rather than truncating to the destination register. - Added regression tests covering numeric
forloops invoking__addand__indexmetamethods to ensure loop state isn’t corrupted.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/Lua/Runtime/LuaVirtualMachine.cs |
Fixes stack restoration when popping a metamethod call frame that produced a single result. |
tests/Lua.Tests/LoopTests.cs |
Adds regression tests that would previously hang or compute incorrect results if loop control registers were truncated. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
My first contribution to this project. We're using this interpreter in our upcoming Unity game, so I think it appropriate to contribute where we can. We made our own Vector class in Lua with operator overrides like
__addand whatnot. When trying to do these operations in a for loop, it crashes everything. I've explained the issue and fix below as best I can:Summary
A metamethod that returns a single value could truncate the caller's stack
below still-live registers, causing numeric
forloops to run forever.Root cause
When an opcode's metamethod resolves to a single result, the VM writes the
result into the target register and then pops the metamethod's call frame. The
"one result" path (
default:case inLuaVirtualMachine.Call) popped the stackdown to
target + 1:That assumes nothing above
targetis live. Butis the assignment destination, which can be a low register when the result is stored into a local declared before the current expression. A numeric
forloop is the clearest trigger: its hidden control slots (index/limit/step) are allocated in registers above those locals, andForLoopre-reads them fromiA,iA+1,iA+2on every iteration:So for a body like:
the
__addreturn truncated the stack pastacc, discarding the loop's index/limit/step slots.stepthen reads back as0, the termination checkstep >= 0 ? index <= limit : ...never advances, and the loop spins foreverNotably, the sibling
Selfcase already pops totarget + 2rather thantarget + 1(LuaVirtualMachine.cs line 164), acknowledging that registers above the result can be live — thedefault:path simply missed the general case.Fix
Restore the caller's stack top instead of truncating to the result register. The metamethod frame's
ReturnBase, captured asCurrentReturnFrameBasewhen the frame is entered (LuaVirtualMachine.cs lines 39 and 115), is the caller's top at call time, so it preserves any registers that were live above the result:Math.Maxkeeps the original behavior when the result register is already at or above the caller's top, and only extends the retained region when live slots sit above it.The regression tests I added mirror the situation we were having when we noticed the problem in the first place.
Please review this and let me know if this appropriate to do, or if there's a better way.