diff --git a/lib/lua.ex b/lib/lua.ex index ea60ff9b..4f42afc0 100644 --- a/lib/lua.ex +++ b/lib/lua.ex @@ -86,13 +86,28 @@ 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) @@ -100,6 +115,14 @@ defmodule Lua do |> 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. diff --git a/lib/lua/vm/dispatcher.ex b/lib/lua/vm/dispatcher.ex index 52fc804c..66eaee1b 100644 --- a/lib/lua/vm/dispatcher.ex +++ b/lib/lua/vm/dispatcher.ex @@ -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, @@ -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) _ -> @@ -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, @@ -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 @@ -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, @@ -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) _ -> @@ -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 -> @@ -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 -> diff --git a/lib/lua/vm/error_formatter.ex b/lib/lua/vm/error_formatter.ex index b187db79..4d0c8557 100644 --- a/lib/lua/vm/error_formatter.ex +++ b/lib/lua/vm/error_formatter.ex @@ -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 -> %{ @@ -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}:" diff --git a/lib/lua/vm/executor.ex b/lib/lua/vm/executor.ex index de20b805..6d070674 100644 --- a/lib/lua/vm/executor.ex +++ b/lib/lua/vm/executor.ex @@ -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( @@ -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 -> diff --git a/lib/lua/vm/state.ex b/lib/lua/vm/state.ex index c65e8944..cbd161f0 100644 --- a/lib/lua/vm/state.ex +++ b/lib/lua/vm/state.ex @@ -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: %{}, @@ -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()}, @@ -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. """ diff --git a/test/lua/error_messages_test.exs b/test/lua/error_messages_test.exs index fc39afcc..ac7622c3 100644 --- a/test/lua/error_messages_test.exs +++ b/test/lua/error_messages_test.exs @@ -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 diff --git a/test/lua/vm/recursion_depth_test.exs b/test/lua/vm/recursion_depth_test.exs new file mode 100644 index 00000000..16099c75 --- /dev/null +++ b/test/lua/vm/recursion_depth_test.exs @@ -0,0 +1,81 @@ +defmodule Lua.VM.RecursionDepthTest do + @moduledoc """ + Pins the `:max_call_depth` recursion limit: deep recursion raises a + catchable Lua `"stack overflow"` error, the depth counter unwinds on + return and after a caught error, and `:infinity` (the default) imposes + no bound. + """ + use ExUnit.Case, async: true + + alias Lua.RuntimeException + + defp eval!(lua, code), do: Lua.eval!(lua, code) + + describe ":max_call_depth enforcement" do + test "recursion deeper than the limit raises stack overflow" do + lua = Lua.new(max_call_depth: 10) + + assert_raise RuntimeException, ~r/stack overflow/, fn -> + eval!(lua, "local function f(n) if n > 0 then f(n - 1) end end f(50)") + end + end + + test "recursion under the limit returns normally and the VM stays usable" do + lua = Lua.new(max_call_depth: 100) + + # deep(50) nests 50 frames — comfortably under the cap — and returns n. + {[50], lua} = + eval!(lua, "local function deep(n) if n > 0 then deep(n - 1) end return n end return deep(50)") + + # The depth counter must have unwound back to 0: a second deep call works. + assert {[50], _lua} = + eval!(lua, "local function deep(n) if n > 0 then deep(n - 1) end return n end return deep(50)") + end + + test "pcall catches the overflow and the VM keeps working afterward" do + lua = Lua.new(max_call_depth: 50) + + {[false, err], lua} = + eval!(lua, "local function f(n) f(n + 1) end return pcall(f, 1)") + + assert err =~ "stack overflow" + + # call_depth was restored on the rescue, so the VM is healthy. + assert {[2], _lua} = eval!(lua, "return 1 + 1") + end + + test "tail recursion is bounded too (no tail-call optimization)" do + # `return f(...)` is in tail position, which PUC-Lua would run + # unbounded. This VM does not implement TCO, so every call — tail or + # not — consumes a frame and a finite cap stops the recursion. + lua = Lua.new(max_call_depth: 10) + + assert_raise RuntimeException, ~r/stack overflow/, fn -> + eval!(lua, "local function loop(n) return loop(n + 1) end loop(1)") + end + end + end + + describe "default behavior (:infinity)" do + test "moderately deep recursion runs with no limit" do + {[200], _lua} = + Lua.eval!( + Lua.new(), + "local function deep(n) if n > 0 then deep(n - 1) end return n end return deep(200)" + ) + end + end + + describe ":max_call_depth validation" do + test "rejects non-positive integers and non-integers" do + assert_raise ArgumentError, ~r/:max_call_depth/, fn -> Lua.new(max_call_depth: 0) end + assert_raise ArgumentError, ~r/:max_call_depth/, fn -> Lua.new(max_call_depth: -5) end + assert_raise ArgumentError, ~r/:max_call_depth/, fn -> Lua.new(max_call_depth: "big") end + end + + test "accepts :infinity and positive integers" do + assert %Lua{} = Lua.new(max_call_depth: :infinity) + assert %Lua{} = Lua.new(max_call_depth: 1) + end + end +end diff --git a/website/lib/website/lua_sandbox.ex b/website/lib/website/lua_sandbox.ex index 2e8a9d1d..e06f7877 100644 --- a/website/lib/website/lua_sandbox.ex +++ b/website/lib/website/lua_sandbox.ex @@ -16,6 +16,13 @@ defmodule Website.LuaSandbox do alias Lua.Compiler.Prototype + # Cap recursion so a runaway snippet (`local function f() return f() end`) + # fails with a catchable "stack overflow" instead of growing the BEAM + # stack until the process dies. The VM has no tail-call optimization, so + # this bounds tail recursion too — 200 is generous for the teaching + # snippets here while still stopping an accidental infinite recursion. + @max_call_depth 200 + @doc """ Compiles a Lua snippet into a `Lua.Chunk` (without running it) and returns the chunk plus the disassembled prototype tree. Returns @@ -54,7 +61,7 @@ defmodule Website.LuaSandbox do output_pid = start_output_collector() lua = - Lua.new() + Lua.new(max_call_depth: @max_call_depth) |> Lua.set!([:print], fn args -> line = args