Repository navigation
vm: a caught error closes only the upvalues of the frames it unwinds - #570
Open
itsnotyoutoday wants to merge 1 commit into
Open
itsnotyoutoday wants to merge 1 commit into
itsnotyoutoday wants to merge 1 commit into
Conversation
Raising an error closed every open upvalue on the stack (closeAllUpvalues) unless an error handler was set, and PCall cleared that flag on its way out, so it was false for the rest of any call that had made a nested one. An error caught by pcall inside a function that keeps running then closed that function's upvalues: its closures kept a copy, and the function's locals and its closures stopped seeing each other's writes. Under xpcall (an error handler set) a raise closed nothing, and the unwinding left the failed frames' upvalues open over registers SetTop then cleared, so a closure the failed call had stored read a Go nil later (a nil pointer panic in the VM). Each PCall now records the first register it unwinds (LState.catchBase, restored when it returns); a raise closes the open upvalues from there up, while the registers still hold their values, as Lua 5.1's luaD_throw/luaF_close do. A Go panic that reaches PCall without a raise closes the same upvalues before the error handler runs. Outside any protected call a raise closes everything, as before. hasErrorFunc, used only for that decision, is gone. _glua-tests/upvalues.lua covers pcall and xpcall inside running functions, closures stored by failed calls, error handlers, fresh upvalues at the same depth and coroutines.
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.
Branch: itsnotyoutoday/gopher-lua
pr-upvalues-on-error(one commit on upstream master, module path unchanged)Problem
Two bugs in how upvalues are closed when an error is raised and caught.
1. A caught error closes the upvalues of functions that keep running.
raiseErrorandErrorcallcloseAllUpvalues()unlesshasErrorFuncis set. That closes every open upvalue ofevery frame on the stack, including frames below the
pcallthat catches the error and that goon running. Their closures then hold a copy, and the function's local and its closures stop
seeing each other's writes:
hasErrorFuncdoes not protect against this either:PCallsets it only for an error handlerand clears it on its way out, so it is false for the rest of any call that has made a nested
protected call.
2. Under xpcall a failed call's upvalues stay open over cleared registers. With an error
handler set, a raise closes nothing.
PCallthen unwinds the frames andSetTop(base)sets theregisters above
baseto Gonil, while the failed frames' upvalues are still open and still inuvcache. A closure the failed call stored somewhere then reads a Gonil(a nil pointer panicin the VM on the next arithmetic or index), and a later call that puts a local in the same
register can share the stale upvalue:
Fix
Lua 5.1 closes exactly the upvalues of the frames an error unwinds, at the time of the error
(
luaD_throw->luaF_close(L, oldtop)inluaD_pcall). This change does the same:LState.catchBase(replacinghasErrorFunc, which was used only for this decision) is thefirst register the innermost
PCallunwinds:PCallsets it tobase + 1and restores theprevious value when it returns.
raiseErrorandErrorcallcloseUpvalues(ls.catchBase): the failed frames' upvalues closewhile their registers still hold the values; the frames that keep running keep theirs. Outside
any protected call
catchBaseis 0 and everything closes, as before.PCallwithout going throughraiseErrorcloses the same upvalues inPCall's recover, before the error handler runs._state.goand the generatedstate.goget the same edit.Tests
_glua-tests/upvalues.lua(added togluaTests): pcall and xpcall inside functions that keeprunning (string and non-string errors, nested pcalls), closures stored by calls that failed
under pcall and xpcall, an error handler reading the failed frame's closure, a fresh upvalue at
the same stack depth after a failure, and an error inside a coroutine. Without the fix the file
fails;
go test ./...passes with it.Found while running Mudlet's Lua test suite (busted) on gopher-lua: a failing spec under
pcall/xpcallbroke the closures of later specs.