diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1def4f4..e4c1fee 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -202,6 +202,10 @@ jobs: # Keep timing-sensitive reactor checks isolated, as in the shell runner. run: ./target/release/nera test -j 1 --timeout 30 --shard ${{ matrix.part }}/3 + - name: Uncaught stacktrace order + if: matrix.part == 1 + run: python3 scripts/test-stacktrace-order.py + cowlib: name: Cowlib (Linux x86_64) needs: [changes, build] diff --git a/CHANGELOG.md b/CHANGELOG.md index 2182fcd..e8415b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,22 @@ All notable milestones for the nera project are documented in this file. +## Stacktraces list the raising function first + +- Captured exception stacktraces now list the raising function before its + callers, as on BEAM. Previously they listed the outermost caller first, + so `hd(Stack)` in `try ... catch Class:Reason:Stack` identified the wrong + function. This also corrects captured traces carried in `{Reason, Stack}` + exit reasons, such as a failed `gen_server` start. +- `erlang:raise/3` keeps the supplied stacktrace's order, including when it + re-raises a captured exception. +- Uncaught crash reports keep the raising function first, with a corrected + "most recent call first" header. `process_info(self(), current_stacktrace)` + now lists the innermost function first too. +- The `stacktrace_order` fixture checks caught errors, throws, exits and + failed matches, re-raises, the current stacktrace and stack-bearing exit + reasons on native Nera and BEAM. + ## Exited processes are no longer reported alive after shutdown - `is_process_alive/1` now returns `false` once a process publishes its exit, diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7337b28..f929542 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -227,6 +227,7 @@ cargo clippy --workspace --all-targets --locked -- -D warnings cargo test --workspace --locked --no-fail-fast python3 scripts/test-allocator-config.py ./target/release/nera test -j 1 --timeout 30 +python3 scripts/test-stacktrace-order.py ``` For runtime shutdown changes, build the runtime before the compiler (so native diff --git a/FEATURES.md b/FEATURES.md index f71f4f6..6a276e9 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -59,7 +59,7 @@ and known differences from BEAM. | Implicit string concatenation | ✅ | Adjacent strings merged | | Record index (#rec.field) | ✅ | Returns 1-based field index | | Named funs (fun Name(...) -> ... end) | ✅ | With self-recursion | -| 3-part catch (Class:Reason:Stacktrace) | ✅ | OTP 21+ stacktrace binding | +| 3-part catch (Class:Reason:Stacktrace) | ✅ | OTP 21+ binding; raising function first, then callers, as on BEAM | | maybe expression (OTP 25+) | ✅ | `?=` cond match, else clauses | | Map comprehensions (OTP 26+) | ❌ | | | Sigils / triple-quoted strings (OTP 27+) | ❌ | | diff --git a/crates/nera-runtime/src/ffi/helpers.rs b/crates/nera-runtime/src/ffi/helpers.rs index b19ade8..110aa89 100644 --- a/crates/nera-runtime/src/ffi/helpers.rs +++ b/crates/nera-runtime/src/ffi/helpers.rs @@ -132,7 +132,8 @@ where /// Snapshot the current call stack into `ctx.exception_trace_ptr`. /// Reads compact frames from the cursor-based stack and reconstructs /// full StackFrames using the global func_info_table. Falls back to -/// stack_ptr (Vec) for tests and legacy callers (nera_rt_raise). +/// stack_ptr (Vec) for tests and legacy callers. Captured traces +/// are innermost-first, like Erlang stacktrace lists supplied to raise/3. /// # Safety /// The context and the pointees accessed by this operation must be live and /// exclusively accessible on this thread; no competing process execution or @@ -169,25 +170,30 @@ enum TraceFrames<'a> { fn fill_exception_trace(trace: &mut Vec, frames: TraceFrames<'_>) { trace.clear(); match frames { - TraceFrames::Full(frames) => trace.extend_from_slice(frames), - TraceFrames::Compact(frames) => { - let info_table = crate::entry::global_func_info_table(); - trace.extend(frames.iter().map(|cf| { - let (module, function, arity) = info_table - .get(cf.func_index as usize) - .copied() - .unwrap_or((0, 0, 0)); - crate::process::StackFrame { - module, - function, - arity, - line: cf.line, - } - })); - } + TraceFrames::Full(frames) => trace.extend(frames.iter().rev().copied()), + TraceFrames::Compact(frames) => trace.extend(compact_stacktrace(frames)), } } +/// Convert an active compact call stack to Erlang's innermost-first order. +pub(super) fn compact_stacktrace( + frames: &[crate::process::CompactFrame], +) -> impl Iterator + '_ { + let info_table = crate::entry::global_func_info_table(); + frames.iter().rev().map(move |cf| { + let (module, function, arity) = info_table + .get(cf.func_index as usize) + .copied() + .unwrap_or((0, 0, 0)); + crate::process::StackFrame { + module, + function, + arity, + line: cf.line, + } + }) +} + /// Helper: raise an error exception and return nil. /// Used by FFI functions when they encounter bad arguments. /// # Safety diff --git a/crates/nera-runtime/src/ffi/mod.rs b/crates/nera-runtime/src/ffi/mod.rs index 28cc8bd..d96cd0c 100644 --- a/crates/nera-runtime/src/ffi/mod.rs +++ b/crates/nera-runtime/src/ffi/mod.rs @@ -123,6 +123,12 @@ mod tests { #[test] fn exception_capture_snapshots_and_clears_trace() { + // Legacy FFI stack hooks mirror the real non-tail callers in + // tests/stacktrace_order.erl; native execution covers compact frames. + let atoms = crate::atom_table::global_atom_table(); + let module = ErlTerm::atom(atoms.intern("stacktrace_order")); + let outer = ErlTerm::atom(atoms.intern("outer")); + let inner = ErlTerm::atom(atoms.intern("inner")); let mut stack = Vec::new(); let mut trace = Vec::new(); let mut ctx = ProcessContext::new(crate::process::ProcessId::new(1)); @@ -131,18 +137,16 @@ mod tests { // SAFETY: ctx points to a valid ProcessContext with valid stack/trace pointers. unsafe { - nera_rt_stack_push(&mut ctx, ErlTerm::atom(100).0, ErlTerm::atom(200).0, 1, 0); - nera_rt_stack_push(&mut ctx, ErlTerm::atom(100).0, ErlTerm::atom(201).0, 2, 0); + nera_rt_stack_push(&mut ctx, module.0, outer.0, 1, 0); + nera_rt_stack_push(&mut ctx, module.0, inner.0, 1, 0); } - let reason = ErlTerm::small_int(42).0; + let reason = ErlTerm::atom(atoms.intern("boom")).0; // SAFETY: ctx points to a valid ProcessContext with valid stack/trace pointers. let _ = unsafe { nera_rt_error(&mut ctx, reason) }; assert_eq!(ctx.status, STATUS_EXCEPTION); assert_eq!(ctx.result, reason); - assert_eq!(trace.len(), 2); - assert_eq!(trace[0].function, 200); - assert_eq!(trace[1].function, 201); + assert_eq!(trace, stack.iter().rev().copied().collect::>()); // SAFETY: ctx points to a valid ProcessContext with valid stack/trace pointers. unsafe { diff --git a/crates/nera-runtime/src/ffi/process.rs b/crates/nera-runtime/src/ffi/process.rs index da237b7..1fb0839 100644 --- a/crates/nera-runtime/src/ffi/process.rs +++ b/crates/nera-runtime/src/ffi/process.rs @@ -1101,15 +1101,7 @@ unsafe fn gather_process_info_snapshot( if !base.is_null() && cursor > base { let depth = (cursor as usize - base as usize) / std::mem::size_of::(); let frames = unsafe { std::slice::from_raw_parts(base, depth) }; - let info_table = crate::entry::global_func_info_table(); - frames.iter().map(|cf| { - let (module, function, arity) = if (cf.func_index as usize) < info_table.len() { - info_table[cf.func_index as usize] - } else { - (0, 0, 0) - }; - crate::process::StackFrame { module, function, arity, line: cf.line } - }).collect() + compact_stacktrace(frames).collect() } else { Vec::new() } diff --git a/crates/nera-runtime/src/process.rs b/crates/nera-runtime/src/process.rs index 662512b..cc78527 100644 --- a/crates/nera-runtime/src/process.rs +++ b/crates/nera-runtime/src/process.rs @@ -1270,7 +1270,8 @@ pub struct Process { /// Compact call stack used by compiled code (func_index + line). /// Pre-allocated to STACK_DEPTH_LIMIT for cursor-based addressing. pub compact_stack: Vec, - /// Captured stack trace for the last raised exception in this process. + /// Stack trace for the last raised exception, innermost (raising) frame first. + /// Captures and traces supplied to erlang:raise/3 use the same order. pub exception_trace: Vec, /// Long-lived OTP-oriented state (waiting reason, links, monitors, /// registration hooks, timer ownership). diff --git a/crates/nera-runtime/src/scheduler.rs b/crates/nera-runtime/src/scheduler.rs index 932a911..f99223d 100644 --- a/crates/nera-runtime/src/scheduler.rs +++ b/crates/nera-runtime/src/scheduler.rs @@ -1711,8 +1711,8 @@ fn worker_loop( let trace = &proc.exception_trace; if !trace.is_empty() { let atoms = crate::atom_table::global_atom_table(); - eprintln!("Stacktrace (most recent call last):"); - for frame in trace.iter().rev() { + eprintln!("Stacktrace (most recent call first):"); + for frame in trace.iter() { let mod_name = atoms .resolve(frame.module) .unwrap_or_else(|| "?".to_string()); diff --git a/docs/ERROR_DESIGN.md b/docs/ERROR_DESIGN.md index bf71bf7..9356ee9 100644 --- a/docs/ERROR_DESIGN.md +++ b/docs/ERROR_DESIGN.md @@ -288,7 +288,7 @@ Runtime crash output (`crates/nera-runtime/src/scheduler.rs`, lines 784-803): ``` Error in process <0.42.0>: {error, badarg} -Stacktrace (most recent call last): +Stacktrace (most recent call first): lists:member/2 main:start/0 ``` @@ -396,7 +396,7 @@ stack frame — negligible. **Instead of:** ``` Error in process <0.5.0>: {error, {badmatch, {error, timeout}}} -Stacktrace (most recent call last): +Stacktrace (most recent call first): main:handle_request/1 main:loop/1 ``` @@ -431,7 +431,7 @@ Stack: **Instead of:** ``` Error in process <0.5.0>: {error, function_clause} -Stacktrace (most recent call last): +Stacktrace (most recent call first): main:process/1 main:start/0 ``` @@ -494,7 +494,7 @@ Stack: **Instead of:** ``` Error in process <0.5.0>: {error, badarg} -Stacktrace (most recent call last): +Stacktrace (most recent call first): erlang:list_to_integer/1 main:parse_id/1 ``` @@ -811,7 +811,7 @@ Did you mean `lists:map/2`? (from the stdlib) **Before:** ``` Error in process <0.5.0>: {error, {badmatch, {error, timeout}}} -Stacktrace (most recent call last): +Stacktrace (most recent call first): main:handle_request/1 main:loop/1 ``` diff --git a/scripts/test-stacktrace-order.py b/scripts/test-stacktrace-order.py new file mode 100644 index 0000000..c047ce6 --- /dev/null +++ b/scripts/test-stacktrace-order.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +"""Keep native crash reports raising-first, as Erlang stacktrace lists are. + +Run after rebuilding the runtime and compiler. The shared Erlang regression +supplies the real nested calls; this checks the uncaught report on stderr. +""" +import argparse +import os +from pathlib import Path +import re +import subprocess +import tempfile + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--nera", type=Path, default=Path("target/release/nera")) + parser.add_argument("--temp-parent", type=Path) + args = parser.parse_args() + compiler = args.nera.resolve() + source = Path(__file__).resolve().parents[1] / "tests" / "stacktrace_order.erl" + with tempfile.TemporaryDirectory(prefix="nera stacktrace order ", dir=args.temp_parent) as temporary: + work = Path(temporary) + binary = work / ("stacktrace-order.exe" if os.name == "nt" else "stacktrace-order") + subprocess.run( + [compiler, "build", source, "-e", "stacktrace_order:crash", "-o", binary], + cwd=work, check=True, timeout=120, + ) + result = subprocess.run( + [binary], cwd=work, capture_output=True, text=True, timeout=30, + ) + print(result.stderr, end="") + assert result.returncode == 1, result + assert "error:boom" in result.stderr, result.stderr + assert "Stacktrace (most recent call first):" in result.stderr, result.stderr + frames = re.findall(r"^ stacktrace_order:([^/]+)/\d+", result.stderr, re.MULTILINE) + assert frames == ["inner", "middle", "outer", "crash"], frames + print("uncaught stacktrace order ok") + + +if __name__ == "__main__": + main() diff --git a/tests/README.md b/tests/README.md index 5a19b8d..f624a1c 100644 --- a/tests/README.md +++ b/tests/README.md @@ -25,6 +25,9 @@ nera test --list # Control parallelism and timeout nera test -j 4 --timeout 30 + +# Check uncaught crash output using the same stacktrace_order fixture +python3 scripts/test-stacktrace-order.py ``` The legacy shell script `./tests/run_tests.sh` is still available but `nera test` @@ -225,6 +228,7 @@ These are automatically skipped by the runner and show up as SKIP in output. | `branch_export_reads.erl` | Names bound in `begin...end` or every `case`/`if`/`receive` branch, read afterwards: bodies, nested and scrutinee exports, call arguments, `try ... of`, fun-head and generator keys and sizes, construction sizes, guards, captures, later patterns and binary tails; funs in record field defaults; fun heads and generators still shadow, and a comprehension's first generator source keeps its bindings local; a binary tail read in a comprehension source or `maybe` block stays readable after it; a name local to an earlier fun does not stop a later read; a generator source or `catch` operand reads its own exports; a sibling clause, fun clause or receive timeout may reuse the name; also run on BEAM | 71 | | `receive_timeout_bindings.erl` | Names bound in a `receive`'s `after` timeout expression, read after the `receive`: the timeout and message paths, a block timeout, binary tails, later patterns, guards, fun captures and fun-head map keys; also run on BEAM | 6 | | `exception_basic.erl` | try/catch/throw/error/exit | ~12 | +| `stacktrace_order.erl` | Innermost function before non-tail callers: current_stacktrace, caught error/throw/exit/badmatch, full and shortened real traces through raise/3, monitored {Reason, Stack} exits and failed gen_server starts; also run on BEAM | Table-driven | | `try_of.erl` | try...of clause | ~6 | | `try_clause.erl` | Unmatched try-of results raise error:{try_clause, Value}: exact unmatched terms, failed guards, local-catch bypass and after cleanup, with matching and body-exception controls; also run on BEAM | Table-driven | | `try_catch_bound_vars.erl` | Bound names in try-of and catch class/reason patterns: exact equality, guards, fresh names, binary tails and re-raise; also run on BEAM | Table-driven | diff --git a/tests/stacktrace_order.erl b/tests/stacktrace_order.erl new file mode 100644 index 0000000..b14bbee --- /dev/null +++ b/tests/stacktrace_order.erl @@ -0,0 +1,96 @@ +%% EXPECT: stacktrace_order_ok +%% Stacktraces identify the raising function before its non-tail callers, +%% including re-raises and {Reason, Stack} exit reasons. Run on Nera and BEAM. +-module(stacktrace_order). +-export([test/0, init/1, crash/0]). + +test() -> + {outer, {middle, ok}} = outer(ok), + ok = check_current_stacktrace(), + lists:foreach(fun check_caught/1, [error, throw, exit, badmatch]), + lists:foreach(fun check_reraised/1, [error, throw, exit, badmatch]), + ok = check_exit_reason(), + ok = check_failed_start(), + io:format("stacktrace_order_ok~n"), + ok. + +check_caught(Mode) -> + {Class, Reason, Stack} = captured(Mode), + {Class, Reason} = expected(Mode), + ok = check_order(Stack), + io:format("caught ~p: ~p~n", [Mode, first_calls(Stack)]). + +captured(Mode) -> + try outer(Mode) + catch Class:Reason:Stack -> {Class, Reason, Stack} + end. + +%% Keep each call non-tail: BEAM may omit tail callers from a stacktrace. +outer(Mode) -> {outer, middle(Mode)}. +middle(Mode) -> {middle, inner(Mode)}. +inner(error) -> erlang:error(boom); +inner(throw) -> throw(boom); +inner(exit) -> exit(boom); +inner(badmatch) -> 1 = id(2), ok; +inner(current_stacktrace) -> + {current_stacktrace, erlang:process_info(self(), current_stacktrace)}; +inner(ok) -> ok. + +expected(error) -> {error, boom}; +expected(throw) -> {throw, boom}; +expected(exit) -> {exit, boom}; +expected(badmatch) -> {error, {badmatch, 2}}. + +check_order(Stack) -> + [{?MODULE, inner, 1, _}, {?MODULE, middle, 1, _}, + {?MODULE, outer, 1, _} | _] = Stack, + ok. + +first_calls(Stack) -> + [{M, F, A} || {M, F, A, _} <- lists:sublist(Stack, 3)]. + +check_current_stacktrace() -> + {outer, {middle, {current_stacktrace, {current_stacktrace, Stack}}}} = + outer(current_stacktrace), + %% BEAM includes a process_info/2 BIF frame that Nera does not record. + Calls = [Frame || {M, _, _, _} = Frame <- Stack, M =:= ?MODULE], + ok = check_order(Calls), + io:format("current stacktrace: ~p~n", [first_calls(Calls)]). + +check_reraised(Mode) -> + {Class, Reason, Original} = captured(Mode), + %% Also supply a shortened, real trace: raise/3 must keep list order, + %% rather than capture the re-raising function's current call stack. + lists:foreach(fun(Stack) -> + Raised = try erlang:raise(Class, Reason, Stack) + catch Class:Reason:Caught -> Caught + end, + Stack = Raised, + ok = check_order(Raised) + end, [Original, lists:sublist(Original, 3)]), + io:format("reraised ~p: ~p~n", [Mode, first_calls(Original)]). + +check_exit_reason() -> + {Pid, Ref} = spawn_monitor(fun() -> + {error, Reason, Stack} = captured(error), + exit({Reason, Stack}) + end), + receive + {'DOWN', Ref, process, Pid, {boom, Stack}} -> + ok = check_order(Stack), + io:format("exit reason: ~p~n", [first_calls(Stack)]) + after 5000 -> error(missing_exit_reason) + end. + +check_failed_start() -> + Previous = process_flag(trap_exit, true), + {error, {boom, Stack}} = gen_server:start_link(?MODULE, [], []), + process_flag(trap_exit, Previous), + ok = check_order(Stack), + io:format("failed start: ~p~n", [first_calls(Stack)]). + +%% Uncaught entry used by scripts/test-stacktrace-order.py to check stderr. +crash() -> {crash, outer(error)}. + +init([]) -> {ok, outer(error)}. +id(X) -> X.