Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions lib/lua.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,43 @@ defmodule Lua do
* `:exclude` - list of paths to exclude from the sandbox, e.g. `exclude: [[:require], [:package]]`
* `:debug` - (default `false`) when `true`, internal Lua VM frames are preserved in stack traces
instead of being pruned. Useful when debugging library bugs.
* `:max_call_depth` - (default `:infinity`) caps the depth of nested function calls. When a
script recurses deeper than this, a catchable `"stack overflow"` runtime error is raised
instead of letting the recursion exhaust the host process. Accepts a positive integer or
`:infinity` for no limit.

Note: this VM does not implement proper tail-call optimization, so a call in tail position
(`return f(x)`) consumes a frame like any other call. A finite `:max_call_depth` therefore
bounds tail recursion too — including loops that PUC-Lua would run indefinitely. Leave the
default `:infinity` if you rely on unbounded tail recursion.

iex> lua = Lua.new(max_call_depth: 10)
iex> {[false, message], _lua} = Lua.eval!(lua, "local function f() return f() end return pcall(f)")
iex> message =~ "stack overflow"
true
"""
def new(opts \\ []) do
opts = Keyword.validate!(opts, sandboxed: @default_sandbox, exclude: [], debug: false)
opts = Keyword.validate!(opts, sandboxed: @default_sandbox, exclude: [], debug: false, max_call_depth: :infinity)
exclude = Keyword.fetch!(opts, :exclude)
debug = Keyword.fetch!(opts, :debug)
max_call_depth = validate_max_call_depth!(Keyword.fetch!(opts, :max_call_depth))

state = Lua.VM.Stdlib.install(State.new())
state = %{Lua.VM.Stdlib.install(State.new()) | max_call_depth: max_call_depth}

opts
|> Keyword.fetch!(:sandboxed)
|> Enum.reject(fn path -> path in exclude end)
|> Enum.reduce(%__MODULE__{state: state, debug: debug}, &sandbox(&2, &1))
end

defp validate_max_call_depth!(:infinity), do: :infinity
defp validate_max_call_depth!(depth) when is_integer(depth) and depth > 0, do: depth

defp validate_max_call_depth!(other) do
raise ArgumentError,
":max_call_depth must be a positive integer or :infinity, got: #{inspect(other)}"
end

@doc """
Write Lua code that is parsed at compile-time.

Expand Down
46 changes: 35 additions & 11 deletions lib/lua/vm/dispatcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,14 @@ defmodule Lua.VM.Dispatcher do
{code, pc + 1, regs, upvalues, proto, cont, :discard, state.open_upvalues}

call_info = Executor.dispatcher_call_info(proto, name_hint, 0)
state = %{state | call_stack: [call_info | state.call_stack], open_upvalues: %{}}
State.check_call_depth!(state)

state = %{
state
| call_stack: [call_info | state.call_stack],
call_depth: state.call_depth + 1,
open_upvalues: %{}
}

dispatch(
callee_proto.bytecode,
Expand All @@ -583,9 +590,10 @@ defmodule Lua.VM.Dispatcher do
{:lua_closure, _, _} = closure ->
args = collect_args(regs, base + 1, arg_count)
call_info = Executor.dispatcher_call_info(proto, name_hint, 0)
state = %{state | call_stack: [call_info | state.call_stack]}
State.check_call_depth!(state)
state = %{state | call_stack: [call_info | state.call_stack], call_depth: state.call_depth + 1}
{_results, state} = Executor.call_function(closure, args, state)
state = %{state | call_stack: tl(state.call_stack)}
state = %{state | call_stack: tl(state.call_stack), call_depth: state.call_depth - 1}
dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames)

_ ->
Expand All @@ -612,7 +620,14 @@ defmodule Lua.VM.Dispatcher do
{code, pc + 1, regs, upvalues, proto, cont, base, state.open_upvalues}

call_info = Executor.dispatcher_call_info(proto, name_hint, 0)
state = %{state | call_stack: [call_info | state.call_stack], open_upvalues: %{}}
State.check_call_depth!(state)

state = %{
state
| call_stack: [call_info | state.call_stack],
call_depth: state.call_depth + 1,
open_upvalues: %{}
}

dispatch(
callee_proto.bytecode,
Expand All @@ -628,9 +643,10 @@ defmodule Lua.VM.Dispatcher do
{:lua_closure, _, _} = closure ->
args = collect_args(regs, base + 1, arg_count)
call_info = Executor.dispatcher_call_info(proto, name_hint, 0)
state = %{state | call_stack: [call_info | state.call_stack]}
State.check_call_depth!(state)
state = %{state | call_stack: [call_info | state.call_stack], call_depth: state.call_depth + 1}
{results, state} = Executor.call_function(closure, args, state)
state = %{state | call_stack: tl(state.call_stack)}
state = %{state | call_stack: tl(state.call_stack), call_depth: state.call_depth - 1}

first =
case results do
Expand Down Expand Up @@ -1004,7 +1020,14 @@ defmodule Lua.VM.Dispatcher do

frame = {code, pc + 1, regs, upvalues, proto, cont, dest, state.open_upvalues}
call_info = Executor.dispatcher_call_info(proto, name_hint, 0)
state = %{state | call_stack: [call_info | state.call_stack], open_upvalues: %{}}
State.check_call_depth!(state)

state = %{
state
| call_stack: [call_info | state.call_stack],
call_depth: state.call_depth + 1,
open_upvalues: %{}
}

dispatch(
callee_proto.bytecode,
Expand All @@ -1020,9 +1043,10 @@ defmodule Lua.VM.Dispatcher do
{:lua_closure, _, _} = closure ->
args = collect_args(regs, base + 1, total_args)
call_info = Executor.dispatcher_call_info(proto, name_hint, 0)
state = %{state | call_stack: [call_info | state.call_stack]}
State.check_call_depth!(state)
state = %{state | call_stack: [call_info | state.call_stack], call_depth: state.call_depth + 1}
{results, state} = Executor.call_function(closure, args, state)
state = %{state | call_stack: tl(state.call_stack)}
state = %{state | call_stack: tl(state.call_stack), call_depth: state.call_depth - 1}
apply_multi_call_result(result_count, base, results, code, pc + 1, regs, upvalues, proto, state, cont, frames)

_ ->
Expand Down Expand Up @@ -1263,7 +1287,7 @@ defmodule Lua.VM.Dispatcher do
# Every dispatcher frame corresponds to a Lua-level call that pushed a
# call_stack entry. Pop it on the way out — the interpreter's
# `do_frame_return/6` does the same at executor.ex:1767.
state = %{state | open_upvalues: saved_open, call_stack: tl(state.call_stack)}
state = %{state | open_upvalues: saved_open, call_stack: tl(state.call_stack), call_depth: state.call_depth - 1}

case dest do
:discard ->
Expand Down Expand Up @@ -1298,7 +1322,7 @@ defmodule Lua.VM.Dispatcher do

defp return_multi(results, state, [frame | rest_frames]) do
{code, pc, regs, upvalues, proto, cont, dest, saved_open} = frame
state = %{state | open_upvalues: saved_open, call_stack: tl(state.call_stack)}
state = %{state | open_upvalues: saved_open, call_stack: tl(state.call_stack), call_depth: state.call_depth - 1}

case dest do
:discard ->
Expand Down
26 changes: 26 additions & 0 deletions lib/lua/vm/error_formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,38 @@ defmodule Lua.VM.ErrorFormatter do
|> String.pad_leading(4)
end

# A deep recursion (see `:max_call_depth`) can push hundreds of frames
# onto the stack. Rendering all of them produces an unreadable wall of
# near-identical lines, so we keep the innermost frames (where the error
# fired) and the outermost frames (down to the main chunk), collapsing
# the middle into a single count. Only kicks in when the gap line
# replaces at least two frames.
@trace_head 7
@trace_tail 3

defp format_stack_trace([]), do: nil

defp format_stack_trace(call_stack) when is_list(call_stack) do
frames =
call_stack
|> build_call_stack()
|> truncate_frames()
|> Enum.map_join("\n", &format_frame/1)

"\n\n" <> IO.ANSI.cyan() <> "Stack trace:" <> IO.ANSI.reset() <> "\n" <> frames
end

defp truncate_frames(frames) do
count = length(frames)

if count > @trace_head + @trace_tail + 1 do
omitted = count - @trace_head - @trace_tail
Enum.take(frames, @trace_head) ++ [{:omitted, omitted}] ++ Enum.take(frames, -@trace_tail)
else
frames
end
end

defp build_call_stack(call_stack) when is_list(call_stack) do
Enum.map(call_stack, fn frame ->
%{
Expand All @@ -199,6 +220,11 @@ defmodule Lua.VM.ErrorFormatter do

defp build_call_stack(_), do: []

defp format_frame({:omitted, count}) do
IO.ANSI.faint() <>
" ... #{count} more frames ..." <> IO.ANSI.reset()
end

defp format_frame(%{source: source, line: line, name: name}) do
location = " #{source || "-no-source-"}:#{line}:"

Expand Down
16 changes: 14 additions & 2 deletions lib/lua/vm/executor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,14 @@ defmodule Lua.VM.Executor do

call_info = %{source: proto.source, line: line, name: hint_name(name_hint)}

state = %{state | call_stack: [call_info | state.call_stack], open_upvalues: %{}}
State.check_call_depth!(state)

state = %{
state
| call_stack: [call_info | state.call_stack],
call_depth: state.call_depth + 1,
open_upvalues: %{}
}

# Tail call — Erlang stack does not grow
do_execute(
Expand Down Expand Up @@ -1869,7 +1876,12 @@ defmodule Lua.VM.Executor do
open_upvalues: saved_open_upvalues
} = frame

state = %{state | call_stack: tl(state.call_stack), open_upvalues: saved_open_upvalues}
state = %{
state
| call_stack: tl(state.call_stack),
call_depth: state.call_depth - 1,
open_upvalues: saved_open_upvalues
}

case result_count do
-1 ->
Expand Down
27 changes: 27 additions & 0 deletions lib/lua/vm/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ defmodule Lua.VM.State do
alias Lua.VM.Table

defstruct call_stack: [],
# Call depth tracked as an O(1) counter that moves in lockstep
# with `call_stack` — `length(call_stack)` would be O(depth) per
# call. `max_call_depth` bounds it; `:infinity` (the default)
# means no limit. See `check_call_depth!/1`.
call_depth: 0,
max_call_depth: :infinity,
metatables: %{},
upvalue_cells: %{},
open_upvalues: %{},
Expand All @@ -23,6 +29,8 @@ defmodule Lua.VM.State do

@type t :: %__MODULE__{
call_stack: list(),
call_depth: non_neg_integer(),
max_call_depth: pos_integer() | :infinity,
metatables: map(),
upvalue_cells: map(),
tables: %{optional(non_neg_integer()) => Table.t()},
Expand All @@ -46,6 +54,25 @@ defmodule Lua.VM.State do
%{state | g_ref: g_ref}
end

@doc """
Guards against unbounded recursion.

Raises a Lua `"stack overflow"` runtime error when the call depth has
reached `max_call_depth`. Call it immediately before pushing a frame
onto `call_stack`.

No-op when depth is under the limit or when `max_call_depth` is
`:infinity` (the default). The clauses are ordered so both common cases
resolve in a single function-head match with no struct rebuild.
"""
@spec check_call_depth!(t()) :: :ok
def check_call_depth!(%__MODULE__{max_call_depth: :infinity}), do: :ok
def check_call_depth!(%__MODULE__{call_depth: depth, max_call_depth: max}) when depth < max, do: :ok

def check_call_depth!(%__MODULE__{call_stack: call_stack}) do
raise Lua.VM.RuntimeError, value: "stack overflow", call_stack: call_stack
end

@doc """
Returns the `_G` table reference.
"""
Expand Down
44 changes: 44 additions & 0 deletions test/lua/error_messages_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,50 @@ defmodule Lua.ErrorMessagesTest do
assert formatted =~ "test.lua:5"
assert formatted =~ "helper"
end

test "truncates very deep stack traces" do
alias Lua.VM.ErrorFormatter

# 200 frames: innermost recursion down to the main chunk.
call_stack =
for n <- 1..199, do: %{source: "deep.lua", line: n, name: "f"}

call_stack = call_stack ++ [%{source: "deep.lua", line: 1, name: nil}]

formatted =
ErrorFormatter.format(:runtime_error, "stack overflow",
source: "deep.lua",
line: 1,
call_stack: call_stack
)

# Keeps the innermost and outermost frames, collapses the middle.
assert formatted =~ "deep.lua:1: in function 'f'"
assert formatted =~ "in main chunk"
assert formatted =~ "... 190 more frames ..."

# The wall of 200 lines is gone: head (7) + tail (3) = 10 rendered
# frames, plus the single gap line.
frame_lines =
formatted
|> String.split("Stack trace:")
|> List.last()
|> String.split("\n")
|> Enum.count(&String.contains?(&1, "deep.lua:"))

assert frame_lines == 10
end

test "does not truncate short stack traces" do
alias Lua.VM.ErrorFormatter

call_stack = for n <- 1..8, do: %{source: "x.lua", line: n, name: "f"}

formatted =
ErrorFormatter.format(:runtime_error, "boom", source: "x.lua", line: 1, call_stack: call_stack)

refute formatted =~ "more frames"
end
end

describe "Lua.eval! preserves line/source on the public exception" do
Expand Down
Loading
Loading