authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-09-18 13:32:47+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-09-30 13:44:55+01:00
log2ab650b4817cbb22244c17de828e82cbb0ccf15e
treedeebb1090f939f52a363de30179f2136f8819588
parent9434bab3134edadae7ae7e575f6b025cafc6a59a
signaturelock-open Commit is signed but in an unrecognized format.

std.debug: go back to storing return addresses instead of call addresses

...and just deal with signal handlers by adding 1 to create a fake "return address". The system I tried out where the addresses returned by `StackIterator` were pre-subtracted didn't play nicely with error traces, which in hindsight, makes perfect sense. This definition also removes some ugly off-by-one issues in matching `first_address`, so I do think this is a better approach.

7 files changed, 65 insertions(+), 48 deletions(-)

lib/std/debug.zig+21-18
......@@ -577,14 +577,12 @@ pub fn captureCurrentStackTrace(options: StackUnwindOptions, addr_buf: []usize)
577577 while (true) switch (it.next()) {
578578 .switch_to_fp => if (!it.stratOk(options.allow_unsafe_unwind)) break,
579579 .end => break,
580 .frame => |pc_addr| {
580 .frame => |ret_addr| {
581581 if (wait_for) |target| {
582 // Possible off-by-one error: `pc_addr` might be one less than the return address (so
583 // that it falls *inside* the function call), while `target` *is* a return address.
584 if (pc_addr != target and pc_addr + 1 != target) continue;
582 if (ret_addr != target) continue;
585583 wait_for = null;
586584 }
587 if (frame_idx < addr_buf.len) addr_buf[frame_idx] = pc_addr;
585 if (frame_idx < addr_buf.len) addr_buf[frame_idx] = ret_addr;
588586 frame_idx += 1;
589587 },
590588 };
......@@ -659,14 +657,14 @@ pub fn writeCurrentStackTrace(options: StackUnwindOptions, writer: *Writer, tty_
659657 }
660658 },
661659 .end => break,
662 .frame => |pc_addr| {
660 .frame => |ret_addr| {
663661 if (wait_for) |target| {
664 // Possible off-by-one error: `pc_addr` might be one less than the return address (so
665 // that it falls *inside* the function call), while `target` *is* a return address.
666 if (pc_addr != target and pc_addr + 1 != target) continue;
662 if (ret_addr != target) continue;
667663 wait_for = null;
668664 }
669 try printSourceAtAddress(di_gpa, di, writer, pc_addr, tty_config);
665 // `ret_addr` is the return address, which is *after* the function call.
666 // Subtract 1 to get an address *in* the function call for a better source location.
667 try printSourceAtAddress(di_gpa, di, writer, ret_addr -| 1, tty_config);
670668 printed_any_frame = true;
671669 },
672670 };
......@@ -712,8 +710,10 @@ pub fn writeStackTrace(st: *const std.builtin.StackTrace, writer: *Writer, tty_c
712710 },
713711 };
714712 const captured_frames = @min(n_frames, st.instruction_addresses.len);
715 for (st.instruction_addresses[0..captured_frames]) |pc_addr| {
716 try printSourceAtAddress(di_gpa, di, writer, pc_addr, tty_config);
713 for (st.instruction_addresses[0..captured_frames]) |ret_addr| {
714 // `ret_addr` is the return address, which is *after* the function call.
715 // Subtract 1 to get an address *in* the function call for a better source location.
716 try printSourceAtAddress(di_gpa, di, writer, ret_addr -| 1, tty_config);
717717 }
718718 if (n_frames > captured_frames) {
719719 tty_config.setColor(writer, .bold) catch {};
......@@ -787,7 +787,7 @@ const StackIterator = union(enum) {
787787 }
788788
789789 const Result = union(enum) {
790 /// A stack frame has been found; this is the corresponding program counter address.
790 /// A stack frame has been found; this is the corresponding return address.
791791 frame: usize,
792792 /// The end of the stack has been reached.
793793 end,
......@@ -797,18 +797,21 @@ const StackIterator = union(enum) {
797797 err: SelfInfo.Error,
798798 },
799799 };
800
800801 fn next(it: *StackIterator) Result {
801802 switch (it.*) {
802803 .di_first => |unwind_context| {
803804 const first_pc = unwind_context.pc;
804805 if (first_pc == 0) return .end;
805806 it.* = .{ .di = unwind_context };
806 return .{ .frame = first_pc };
807 // The caller expects *return* addresses, where they will subtract 1 to find the address of the call.
808 // However, we have the actual current PC, which should not be adjusted. Compensate by adding 1.
809 return .{ .frame = first_pc +| 1 };
807810 },
808811 .di => |*unwind_context| {
809812 const di = getSelfDebugInfo() catch unreachable;
810813 const di_gpa = getDebugInfoAllocator();
811 di.unwindFrame(di_gpa, unwind_context) catch |err| {
814 const ret_addr = di.unwindFrame(di_gpa, unwind_context) catch |err| {
812815 const pc = unwind_context.pc;
813816 it.* = .{ .fp = unwind_context.getFp() };
814817 return .{ .switch_to_fp = .{
......@@ -816,8 +819,8 @@ const StackIterator = union(enum) {
816819 .err = err,
817820 } };
818821 };
819 const pc = unwind_context.pc;
820 return if (pc == 0) .end else .{ .frame = pc };
822 if (ret_addr <= 1) return .end;
823 return .{ .frame = ret_addr };
821824 },
822825 .fp => |fp| {
823826 if (fp == 0) return .end; // we reached the "sentinel" base pointer
......@@ -845,7 +848,7 @@ const StackIterator = union(enum) {
845848 it.fp = bp;
846849 const ra = stripInstructionPtrAuthCode(ra_ptr.*);
847850 if (ra <= 1) return .end;
848 return .{ .frame = ra - 1 };
851 return .{ .frame = ra };
849852 },
850853 }
851854 }
lib/std/debug/SelfInfo.zig+20-15
......@@ -53,7 +53,7 @@ pub fn deinit(self: *SelfInfo, gpa: Allocator) void {
5353 if (Module.LookupCache != void) self.lookup_cache.deinit(gpa);
5454}
5555
56pub fn unwindFrame(self: *SelfInfo, gpa: Allocator, context: *UnwindContext) Error!void {
56pub fn unwindFrame(self: *SelfInfo, gpa: Allocator, context: *UnwindContext) Error!usize {
5757 comptime assert(supports_unwinding);
5858 const module: Module = try .lookup(&self.lookup_cache, gpa, context.pc);
5959 const gop = try self.modules.getOrPut(gpa, module.key());
......@@ -124,15 +124,14 @@ pub fn getModuleNameForAddress(self: *SelfInfo, gpa: Allocator, address: usize)
124124/// /// pointer is unknown, 0 may be returned instead.
125125/// pub fn getFp(uc: *UnwindContext) usize;
126126/// };
127/// /// Only required if `supports_unwinding == true`. Unwinds a single stack frame.
128/// /// The caller will read the new instruction poiter from the `pc` field.
129/// /// `pc = 0` indicates end of stack / no more frames.
127/// /// Only required if `supports_unwinding == true`. Unwinds a single stack frame, and returns
128/// /// the frame's return address.
130129/// pub fn unwindFrame(
131130/// mod: *const Module,
132131/// gpa: Allocator,
133132/// di: *DebugInfo,
134133/// ctx: *UnwindContext,
135/// ) SelfInfo.Error!void;
134/// ) SelfInfo.Error!usize;
136135/// ```
137136const Module: type = Module: {
138137 // Allow overriding the target-specific `SelfInfo` implementation by exposing `root.debug.Module`.
......@@ -312,7 +311,7 @@ pub const DwarfUnwindContext = struct {
312311 unwind: *const Dwarf.Unwind,
313312 load_offset: usize,
314313 explicit_fde_offset: ?usize,
315 ) Error!void {
314 ) Error!usize {
316315 return unwindFrameInner(context, gpa, unwind, load_offset, explicit_fde_offset) catch |err| switch (err) {
317316 error.InvalidDebugInfo, error.MissingDebugInfo, error.OutOfMemory => |e| return e,
318317
......@@ -360,10 +359,10 @@ pub const DwarfUnwindContext = struct {
360359 unwind: *const Dwarf.Unwind,
361360 load_offset: usize,
362361 explicit_fde_offset: ?usize,
363 ) !void {
362 ) !usize {
364363 comptime assert(supports_unwinding);
365364
366 if (context.pc == 0) return;
365 if (context.pc == 0) return 0;
367366
368367 const pc_vaddr = context.pc - load_offset;
369368
......@@ -443,13 +442,19 @@ pub const DwarfUnwindContext = struct {
443442 // The new CPU context is complete; flush changes.
444443 context.cpu_context = new_cpu_context;
445444
446 // Also update the stored pc. However, because `return_address` points to the instruction
447 // *after* the call, it could (in the case of noreturn functions) actually point outside of
448 // the caller's address range, meaning an FDE lookup would fail. We can handle this by
449 // subtracting 1 from `return_address` so that the next lookup is guaranteed to land inside
450 // the `call` instruction. The exception to this rule is signal frames, where the return
451 // address is the same instruction that triggered the handler.
452 context.pc = if (cie.is_signal_frame) return_address else return_address -| 1;
445 // The caller will subtract 1 from the return address to get an address corresponding to the
446 // function call. However, if this is a signal frame, that's actually incorrect, because the
447 // "return address" we have is the instruction which triggered the signal (if the signal
448 // handler returned, the instruction would be re-run). Compensate for this by incrementing
449 // the address in that case.
450 const adjusted_ret_addr = if (cie.is_signal_frame) return_address +| 1 else return_address;
451
452 // We also want to do that same subtraction here to get the PC for the next frame's FDE.
453 // This is because if the callee was noreturn, then the function call might be the caller's
454 // last instruction, so `return_address` might actually point outside of it!
455 context.pc = adjusted_ret_addr -| 1;
456
457 return adjusted_ret_addr;
453458 }
454459 /// Since register rules are applied (usually) during a panic,
455460 /// checked addition / subtraction is used so that we can return
lib/std/debug/SelfInfo/DarwinModule.zig+9-3
......@@ -324,7 +324,7 @@ pub const UnwindContext = std.debug.SelfInfo.DwarfUnwindContext;
324324/// Unwind a frame using MachO compact unwind info (from __unwind_info).
325325/// If the compact encoding can't encode a way to unwind a frame, it will
326326/// defer unwinding to DWARF, in which case `.eh_frame` will be used if available.
327pub fn unwindFrame(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) Error!void {
327pub fn unwindFrame(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) Error!usize {
328328 return unwindFrameInner(module, gpa, di, context) catch |err| switch (err) {
329329 error.InvalidDebugInfo,
330330 error.MissingDebugInfo,
......@@ -340,7 +340,7 @@ pub fn unwindFrame(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo,
340340 => return error.InvalidDebugInfo,
341341 };
342342}
343fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) !void {
343fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) !usize {
344344 if (di.unwind == null) di.unwind = module.loadUnwindInfo();
345345 const unwind = &di.unwind.?;
346346
......@@ -640,7 +640,13 @@ fn unwindFrameInner(module: *const DarwinModule, gpa: Allocator, di: *DebugInfo,
640640 else => comptime unreachable, // unimplemented
641641 };
642642
643 context.pc = std.debug.stripInstructionPtrAuthCode(new_ip) -| 1;
643 const ret_addr = std.debug.stripInstructionPtrAuthCode(new_ip);
644
645 // Like `DwarfUnwindContext.unwindFrame`, adjust our next lookup pc in case the `call` was this
646 // function's last instruction making `ret_addr` one byte past its end.
647 context.pc = ret_addr -| 1;
648
649 return ret_addr;
644650}
645651pub const DebugInfo = struct {
646652 unwind: ?Unwind,
lib/std/debug/SelfInfo/ElfModule.zig+1-1
......@@ -230,7 +230,7 @@ fn loadUnwindInfo(module: *const ElfModule, gpa: Allocator, di: *DebugInfo) Erro
230230 else => unreachable,
231231 }
232232}
233pub fn unwindFrame(module: *const ElfModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) Error!void {
233pub fn unwindFrame(module: *const ElfModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) Error!usize {
234234 if (di.unwind[0] == null) try module.loadUnwindInfo(gpa, di);
235235 std.debug.assert(di.unwind[0] != null);
236236 for (&di.unwind) |*opt_unwind| {
lib/std/debug/SelfInfo/WindowsModule.zig+6-3
......@@ -373,7 +373,7 @@ pub const UnwindContext = struct {
373373 return ctx.cur.getRegs().bp;
374374 }
375375};
376pub fn unwindFrame(module: *const WindowsModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) !void {
376pub fn unwindFrame(module: *const WindowsModule, gpa: Allocator, di: *DebugInfo, context: *UnwindContext) !usize {
377377 _ = module;
378378 _ = gpa;
379379 _ = di;
......@@ -403,9 +403,12 @@ pub fn unwindFrame(module: *const WindowsModule, gpa: Allocator, di: *DebugInfo,
403403 const tib = &windows.teb().NtTib;
404404 if (next_regs.sp < @intFromPtr(tib.StackLimit) or next_regs.sp > @intFromPtr(tib.StackBase)) {
405405 context.pc = 0;
406 } else {
407 context.pc = next_regs.ip -| 1;
406 return 0;
408407 }
408 // Like `DwarfUnwindContext.unwindFrame`, adjust our next lookup pc in case the `call` was this
409 // function's last instruction making `next_regs.ip` one byte past its end.
410 context.pc = next_regs.ip -| 1;
411 return next_regs.ip;
409412}
410413
411414const WindowsModule = @This();
test/standalone/stack_iterator/unwind.zig+4-4
......@@ -3,7 +3,7 @@ const builtin = @import("builtin");
33const fatal = std.process.fatal;
44
55noinline fn frame3(expected: *[4]usize, addr_buf: *[4]usize) std.builtin.StackTrace {
6 expected[0] = @returnAddress() - 1;
6 expected[0] = @returnAddress();
77 return std.debug.captureCurrentStackTrace(.{
88 .first_address = @returnAddress(),
99 .allow_unsafe_unwind = true,
......@@ -58,12 +58,12 @@ noinline fn frame2(expected: *[4]usize, addr_buf: *[4]usize) std.builtin.StackTr
5858 }
5959 }
6060
61 expected[1] = @returnAddress() - 1;
61 expected[1] = @returnAddress();
6262 return frame3(expected, addr_buf);
6363}
6464
6565noinline fn frame1(expected: *[4]usize, addr_buf: *[4]usize) std.builtin.StackTrace {
66 expected[2] = @returnAddress() - 1;
66 expected[2] = @returnAddress();
6767
6868 // Use a stack frame that is too big to encode in __unwind_info's stack-immediate encoding
6969 // to exercise the stack-indirect encoding path
......@@ -74,7 +74,7 @@ noinline fn frame1(expected: *[4]usize, addr_buf: *[4]usize) std.builtin.StackTr
7474}
7575
7676noinline fn frame0(expected: *[4]usize, addr_buf: *[4]usize) std.builtin.StackTrace {
77 expected[3] = @returnAddress() - 1;
77 expected[3] = @returnAddress();
7878 return frame1(expected, addr_buf);
7979}
8080
test/standalone/stack_iterator/unwind_freestanding.zig+4-4
......@@ -3,7 +3,7 @@
33const std = @import("std");
44
55noinline fn frame3(expected: *[4]usize, addr_buf: *[4]usize) std.builtin.StackTrace {
6 expected[0] = @returnAddress() - 1;
6 expected[0] = @returnAddress();
77 return std.debug.captureCurrentStackTrace(.{
88 .first_address = @returnAddress(),
99 .allow_unsafe_unwind = true,
......@@ -11,12 +11,12 @@ noinline fn frame3(expected: *[4]usize, addr_buf: *[4]usize) std.builtin.StackTr
1111}
1212
1313noinline fn frame2(expected: *[4]usize, addr_buf: *[4]usize) std.builtin.StackTrace {
14 expected[1] = @returnAddress() - 1;
14 expected[1] = @returnAddress();
1515 return frame3(expected, addr_buf);
1616}
1717
1818noinline fn frame1(expected: *[4]usize, addr_buf: *[4]usize) std.builtin.StackTrace {
19 expected[2] = @returnAddress() - 1;
19 expected[2] = @returnAddress();
2020
2121 // Use a stack frame that is too big to encode in __unwind_info's stack-immediate encoding
2222 // to exercise the stack-indirect encoding path
......@@ -27,7 +27,7 @@ noinline fn frame1(expected: *[4]usize, addr_buf: *[4]usize) std.builtin.StackTr
2727}
2828
2929noinline fn frame0(expected: *[4]usize, addr_buf: *[4]usize) std.builtin.StackTrace {
30 expected[3] = @returnAddress() - 1;
30 expected[3] = @returnAddress();
3131 return frame1(expected, addr_buf);
3232}
3333