compile: fence the LOADNIL merge peephole at jump targets - #39
Open
wolfy-j wants to merge 1 commit into
Open
Conversation
The AddLoadNil peephole folds a new LOADNIL into the previous instruction whenever the register spans are adjacent. When the previous LOADNIL ends a conditionally-skipped path — the nil arm of an 'or nil' expression — the fold moves the next statement's nil-register init onto that skippable path. The short-circuit path then reads an uninitialized stack slot and the VM dereferences an empty LValue: 'local v = t[key] or nil' inside a generic for loop panics with a Go nil pointer whenever t[key] is truthy. codeStore now records the highest label pc (MarkLabelPc, fed by SetLabelPc) and AddLoadNil merges only when the previous instruction lies strictly after every label position, mirroring PUC Lua's fs->lasttarget fence in luaK_nil. Claude-Session: https://claude.ai/code/session_0134uPSdqJwq5Sp8qyUseaq5
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.
Defect
local v = t[key] or nil(any… or nillocal initializer) inside a genericforbody panics the VM with a Go nil pointer dereference whenever the left operand is truthy.or false, numericfor,while, and the same statement outside a loop are unaffected.Root cause
codeStore.AddLoadNilmerges a new LOADNIL into the previous instruction whenever the register spans are adjacent, with no jump-target fence. The nil arm ofor nilends in a LOADNIL that the short-circuit path jumps over; folding the next statement's nil-register init into it moves that init onto the skipped path, so the comparison reads an uninitialized stack slot:PUC Lua guards exactly this with
fs->lasttargetinluaK_nil.Fix
codeStorerecords the highest label pc (MarkLabelPc, fed byfuncContext.SetLabelPc);AddLoadNilmerges only when the previous instruction lies strictly after every label position. Adjacent nil locals with no label between them still fold.Tests
TestLoadNilMergeStopsAtJumpTargetcovers ipairs/pairs hit+miss, scalaror nil, guarded chains, and the still-folding case. Full package suite passes.https://claude.ai/code/session_01CGwbFUspyUu7dbwdWsXL7J