Skip to content

Commit

Permalink
Merge pull request #603 from pallene-lang/stack-trace-optimizations
Browse files Browse the repository at this point in the history
Pallene Traceback Optimizations and Improvements
  • Loading branch information
hugomg authored Jun 18, 2024
2 parents 8366118 + 2b70297 commit 4e22f37
Show file tree
Hide file tree
Showing 11 changed files with 320 additions and 176 deletions.
12 changes: 12 additions & 0 deletions spec/traceback/anon_lua/anon_lua.pln
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Copyright (c) 2024, The Pallene Developers
-- Pallene is licensed under the MIT license.
-- Please refer to the LICENSE and AUTHORS files for details
-- SPDX-License-Identifier: MIT

local mod: module = {}

function mod.call_anon_lua_fn(callback: () -> ())
callback()
end

return mod
15 changes: 15 additions & 0 deletions spec/traceback/anon_lua/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- Copyright (c) 2024, The Pallene Developers
-- Pallene is licensed under the MIT license.
-- Please refer to the LICENSE and AUTHORS files for details
-- SPDX-License-Identifier: MIT

local anon = require "spec.traceback.anon_lua.anon_lua"

local function wrapper()
anon.call_anon_lua_fn(function()
error "Error from an anonymous Lua fn!"
end)
end

-- luacheck: globals pallene_tracer_debug_traceback
xpcall(wrapper, pallene_tracer_debug_traceback)
2 changes: 1 addition & 1 deletion spec/traceback/depth_recursion/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-- Please refer to the LICENSE and AUTHORS files for details
-- SPDX-License-Identifier: MIT

local pallene = require 'spec.traceback.depth_recursion.depth_recursion'
local pallene = require "spec.traceback.depth_recursion.depth_recursion"

-- luacheck: globals lua_fn
function lua_fn(depth)
Expand Down
4 changes: 2 additions & 2 deletions spec/traceback/module_lua/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
-- Please refer to the LICENSE and AUTHORS files for details
-- SPDX-License-Identifier: MIT

local another_module = require 'spec.traceback.module_lua.another_module'
local pallene = require 'spec.traceback.module_lua.module_lua'
local another_module = require "spec.traceback.module_lua.another_module"
local pallene = require "spec.traceback.module_lua.module_lua"

-- luacheck: globals lua_1
function lua_1()
Expand Down
6 changes: 3 additions & 3 deletions spec/traceback/module_pallene/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
-- Please refer to the LICENSE and AUTHORS files for details
-- SPDX-License-Identifier: MIT

local pallene = require 'spec.traceback.module_pallene.module_pallene'
local pallene_alt = require 'spec.traceback.module_pallene.module_pallene_alt'
local pallene = require "spec.traceback.module_pallene.module_pallene"
local pallene_alt = require "spec.traceback.module_pallene.module_pallene_alt"

-- luacheck: globals lua_2
function lua_2()
error "There's an error in everyday life. Shame!"
error "There's an error in everyday life. Alas!"
end

-- luacheck: globals lua_1
Expand Down
2 changes: 1 addition & 1 deletion spec/traceback/rect/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-- Please refer to the LICENSE and AUTHORS files for details
-- SPDX-License-Identifier: MIT

local rect = require 'spec.traceback.rect.rect'
local rect = require "spec.traceback.rect.rect"

-- Should be local.
-- Making it global so that it is visible in the traceback.
Expand Down
19 changes: 19 additions & 0 deletions spec/traceback/stack_overflow/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- Copyright (c) 2024, The Pallene Developers
-- Pallene is licensed under the MIT license.
-- Please refer to the LICENSE and AUTHORS files for details
-- SPDX-License-Identifier: MIT

local so = require "spec.traceback.stack_overflow.stack_overflow"

-- luacheck: globals please_dont_overflow
function please_dont_overflow()
so.no_overflow(please_dont_overflow)
end

-- luacheck: globals wrapper
function wrapper()
please_dont_overflow()
end

-- luacheck: globals pallene_tracer_debug_traceback
xpcall(wrapper, pallene_tracer_debug_traceback)
12 changes: 12 additions & 0 deletions spec/traceback/stack_overflow/stack_overflow.pln
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Copyright (c) 2024, The Pallene Developers
-- Pallene is licensed under the MIT license.
-- Please refer to the LICENSE and AUTHORS files for details
-- SPDX-License-Identifier: MIT

local mod: module = {}

function mod.no_overflow(callback: () -> ())
callback()
end

return mod
45 changes: 44 additions & 1 deletion spec/traceback_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ end)

it("Multi-module Pallene", function()
assert_test("module_pallene", [[
Runtime error: spec/traceback/module_pallene/main.lua:11: There's an error in everyday life. Shame!
Runtime error: spec/traceback/module_pallene/main.lua:11: There's an error in everyday life. Alas!
Stack traceback:
C: in function 'error'
spec/traceback/module_pallene/main.lua:11: in function 'lua_2'
Expand Down Expand Up @@ -94,3 +94,46 @@ Stack traceback:
]])
end)

it("Stack overflow", function()
assert_test("stack_overflow", [[
Runtime error: C stack overflow
Stack traceback:
spec/traceback/stack_overflow/stack_overflow.pln:8: in function 'no_overflow'
spec/traceback/stack_overflow/main.lua:10: in function 'please_dont_overflow'
spec/traceback/stack_overflow/stack_overflow.pln:8: in function 'no_overflow'
spec/traceback/stack_overflow/main.lua:10: in function 'please_dont_overflow'
spec/traceback/stack_overflow/stack_overflow.pln:8: in function 'no_overflow'
spec/traceback/stack_overflow/main.lua:10: in function 'please_dont_overflow'
spec/traceback/stack_overflow/stack_overflow.pln:8: in function 'no_overflow'
spec/traceback/stack_overflow/main.lua:10: in function 'please_dont_overflow'
spec/traceback/stack_overflow/stack_overflow.pln:8: in function 'no_overflow'
spec/traceback/stack_overflow/main.lua:10: in function 'please_dont_overflow'
... (Skipped 379 frames) ...
spec/traceback/stack_overflow/stack_overflow.pln:8: in function 'no_overflow'
spec/traceback/stack_overflow/main.lua:10: in function 'please_dont_overflow'
spec/traceback/stack_overflow/stack_overflow.pln:8: in function 'no_overflow'
spec/traceback/stack_overflow/main.lua:10: in function 'please_dont_overflow'
spec/traceback/stack_overflow/stack_overflow.pln:8: in function 'no_overflow'
spec/traceback/stack_overflow/main.lua:10: in function 'please_dont_overflow'
spec/traceback/stack_overflow/main.lua:15: in function 'wrapper'
C: in function 'xpcall'
spec/traceback/stack_overflow/main.lua:19: in <main>
C: in function '<?>'
]])
end)

it("Anonymous lua functions", function()
assert_test("anon_lua", [[
Runtime error: spec/traceback/anon_lua/main.lua:10: Error from an anonymous Lua fn!
Stack traceback:
C: in function 'error'
spec/traceback/anon_lua/main.lua:10: in function '<?>'
spec/traceback/anon_lua/anon_lua.pln:8: in function 'call_anon_lua_fn'
spec/traceback/anon_lua/main.lua:9: in function '<?>'
C: in function 'xpcall'
spec/traceback/anon_lua/main.lua:15: in <main>
C: in function '<?>'
]])
end)
52 changes: 26 additions & 26 deletions src/pallene/coder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,9 @@ function Coder:pallene_entry_point_definition(f_id)
local void_frameexit = ""
if self.flags.use_traceback then
table.insert(prologue, util.render([[
PALLENE_C_FRAMEENTER(L, "$name");
/**/
pt_cont_t *cont = pvalue(&K->uv[0].uv);
PALLENE_C_FRAMEENTER(cont, "$name");
/**/
]], {
name = func.name
Expand All @@ -470,7 +472,7 @@ function Coder:pallene_entry_point_definition(f_id)
setline = string.format("PALLENE_SETLINE(%d);", func.loc and func.loc.line or 0)

if #func.typ.ret_types == 0 then
void_frameexit = "PALLENE_FRAMEEXIT(L);"
void_frameexit = "PALLENE_FRAMEEXIT(cont);"
end
end

Expand Down Expand Up @@ -594,27 +596,26 @@ function Coder:lua_entry_point_definition(f_id)
]]

local frameenter = ""
local setline = ""
local frameexit = ""
if self.flags.use_traceback then
frameenter = util.render([[ PALLENE_LUA_FRAMEENTER(L, $fun_name); ]], {
frameenter = util.render([[
/**/
pt_cont_t *cont = pvalue(&K->uv[0].uv);
PALLENE_LUA_FRAMEENTER(cont, $fun_name);
/**/
]], {
fun_name = self:lua_entry_point_name(f_id),
})

setline = string.format("PALLENE_SETLINE(%d);", func.loc and func.loc.line or 0)

frameexit = "PALLENE_FRAMEEXIT(L);"
frameexit = "PALLENE_FRAMEEXIT(cont);"
end

local arity_check = util.render([[
int nargs = lua_gettop(L);
if (l_unlikely(nargs != $nargs)) {
${setline}
pallene_runtime_arity_error(L, $fname, $nargs, nargs);
}
]], {
nargs = C.integer(#arg_types),
setline = setline,
fname = C.string(fname),
})

Expand Down Expand Up @@ -657,9 +658,9 @@ function Coder:lua_entry_point_definition(f_id)
return (util.render([[
${fun_decl}
{
${lua_fenter}
StackValue *base = L->ci->func.p;
${init_global_userdata}
${lua_fenter}
/**/
${arity_check}
/**/
Expand All @@ -675,8 +676,8 @@ function Coder:lua_entry_point_definition(f_id)
}
]], {
fun_decl = self:lua_entry_point_declaration(f_id),
lua_fenter = frameenter,
init_global_userdata = init_global_userdata,
lua_fenter = frameenter,
arity_check = arity_check,
arg_decls = table.concat(arg_decls, "\n"),
init_args = table.concat(init_args, "\n/**/\n"),
Expand All @@ -697,10 +698,16 @@ end
define_union("Constant", {
Metatable = {"typ"},
String = {"str"},
DebugUserdata = {}
})

function Coder:init_upvalues()

-- If we are using tracebacks
if self.flags.use_traceback then
table.insert(self.constants, coder.Constant.DebugUserdata())
end

-- Metatables
for _, typ in ipairs(self.module.record_types) do
if not typ.is_upvalue_box then
Expand Down Expand Up @@ -1649,7 +1656,7 @@ end
gen_cmd["Return"] = function(self, cmd)
local frameexit = ""
if self.flags.use_traceback then
frameexit = "PALLENE_FRAMEEXIT(L);"
frameexit = "PALLENE_FRAMEEXIT(cont);"
end

if #cmd.srcs == 0 then
Expand Down Expand Up @@ -1866,6 +1873,12 @@ function Coder:generate_luaopen_function()
lua_pushstring(L, $str);]], {
str = C.string(upv.str)
}))
-- Will be used if compiling with `--use-traceback`
elseif tag == "coder.Constant.DebugUserdata" then
table.insert(init_constants, [[
/* Initialize Pallene Tracer. */
lua_pushlightuserdata(L, (void *) pallene_tracer_init(L));
]]);
else
tagged_union.error(tag)
end
Expand All @@ -1888,17 +1901,6 @@ function Coder:generate_luaopen_function()
init_function = self:lua_entry_point_name(1),
})

local init_pt = ""

if self.flags.use_traceback then
init_pt = [[
/* Initialize Pallene Tracer. */
pallene_tracer_init(L);
/**/
]]
end


-- NOTE: Version compatibility
-- ---------------------------
-- We have both a compile-time and a run-time test. The compile-time test ensures that the
Expand All @@ -1914,7 +1916,6 @@ function Coder:generate_luaopen_function()
#error "Lua version must be exactly 5.4.6"
#endif
${init_pt}
luaL_checkcoreversion(L);
/**/
Expand All @@ -1939,7 +1940,6 @@ function Coder:generate_luaopen_function()
}
]], {
name = "luaopen_" .. self.modname,
init_pt = init_pt,
n_upvalues = C.integer(#self.constants),
init_constants = table.concat(init_constants, "\n"),
init_initializers = init_initializers,
Expand Down
Loading

0 comments on commit 4e22f37

Please sign in to comment.