Skip to content

vm: a caught error closes only the upvalues of the frames it unwinds - #570

Open
itsnotyoutoday wants to merge 1 commit into
yuin:masterfrom
waypointmu:pr-upvalues-on-error
Open

itsnotyoutoday wants to merge 1 commit into
yuin:masterfrom
waypointmu:pr-upvalues-on-error

Conversation

@itsnotyoutoday

Copy link
Copy Markdown

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. raiseError and
Error call closeAllUpvalues() unless hasErrorFunc is set. That closes every open upvalue of
every frame on the stack, including frames below the pcall that catches the error and that go
on running. Their closures then hold a copy, and the function's local and its closures stop
seeing each other's writes:

local function f()
  local count = 0
  local inc = function() count = count + 1 end
  pcall(function() error("x") end)
  inc(); inc()
  return count
end
print(f())  --> 0   (Lua 5.1: 2)

hasErrorFunc does not protect against this either: PCall sets it only for an error handler
and 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. PCall then unwinds the frames and SetTop(base) sets the
registers above base to Go nil, while the failed frames' upvalues are still open and still in
uvcache. A closure the failed call stored somewhere then reads a Go nil (a nil pointer panic
in 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:

local saved
xpcall(function()
  local v = 42
  saved = function() return v end
  error("boom")
end, function(e) return e end)
local a, b, c = {}, {}, {}
print(saved())  --> garbage or a Go panic   (Lua 5.1: 42)

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) in luaD_pcall). This change does the same:

  • LState.catchBase (replacing hasErrorFunc, which was used only for this decision) is the
    first register the innermost PCall unwinds: PCall sets it to base + 1 and restores the
    previous value when it returns.
  • raiseError and Error call closeUpvalues(ls.catchBase): the failed frames' upvalues close
    while their registers still hold the values; the frames that keep running keep theirs. Outside
    any protected call catchBase is 0 and everything closes, as before.
  • A Go panic that reaches PCall without going through raiseError closes the same upvalues in
    PCall's recover, before the error handler runs.

_state.go and the generated state.go get the same edit.

Tests

_glua-tests/upvalues.lua (added to gluaTests): pcall and xpcall inside functions that keep
running (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/xpcall broke the closures of later specs.

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.
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.

1 participant