Skip to content

std.debug: fix some corner cases #23927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions lib/std/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10809,8 +10809,7 @@ pub const getcontext = if (builtin.target.abi.isAndroid() or builtin.target.os.t
{} // android bionic and openbsd libc does not implement getcontext
else if (native_os == .linux and builtin.target.abi.isMusl())
linux.getcontext
else
private.getcontext;
else if (native_os == .windows) {} else private.getcontext;

pub const max_align_t = if (native_abi == .msvc or native_abi == .itanium)
f64
Expand Down
42 changes: 38 additions & 4 deletions lib/std/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const native_arch = builtin.cpu.arch;
const native_os = builtin.os.tag;
const native_endian = native_arch.endian();

/// Maximum number of frames to walk when iterating through a stack trace.
pub const max_stack_trace_depth = 50;

pub const MemoryAccessor = @import("debug/MemoryAccessor.zig");
pub const FixedBufferReader = @import("debug/FixedBufferReader.zig");
pub const Dwarf = @import("debug/Dwarf.zig");
Expand Down Expand Up @@ -418,6 +421,10 @@ pub fn dumpStackTraceFromBase(context: *ThreadContext) void {
return;
}
const stderr = io.getStdErr().writer();
if (!have_ucontext) {
stderr.print("Unable to dump stack trace: platform 'ucontext_t' not defined\n", .{}) catch return;
return;
}
if (builtin.strip_debug_info) {
stderr.print("Unable to dump stack trace: debug info stripped\n", .{}) catch return;
return;
Expand Down Expand Up @@ -445,9 +452,16 @@ pub fn dumpStackTraceFromBase(context: *ThreadContext) void {
// DWARF unwinding on aarch64-macos is not complete so we need to get pc address from mcontext
const pc_addr = if (builtin.target.os.tag.isDarwin() and native_arch == .aarch64)
context.mcontext.ss.pc
else if (it.unwind_state != null)
it.unwind_state.?.dwarf_context.pc
else
it.unwind_state.?.dwarf_context.pc;
printSourceAtAddress(debug_info, stderr, pc_addr, tty_config) catch return;
0;

if (pc_addr != 0) {
printSourceAtAddress(debug_info, stderr, pc_addr, tty_config) catch return;
}

var depth: usize = 0;

while (it.next()) |return_address| {
printLastUnwindError(&it, debug_info, stderr, tty_config);
Expand All @@ -459,6 +473,12 @@ pub fn dumpStackTraceFromBase(context: *ThreadContext) void {
// same behaviour for x86-windows-msvc
const address = if (return_address == 0) return_address else return_address - 1;
printSourceAtAddress(debug_info, stderr, address, tty_config) catch return;

depth += 1;
if (depth > max_stack_trace_depth) {
stderr.print("Abandoned stack trace after {} frames.\n", .{max_stack_trace_depth}) catch {};
break;
}
} else printLastUnwindError(&it, debug_info, stderr, tty_config);
}
}
Expand Down Expand Up @@ -866,8 +886,14 @@ pub const StackIterator = struct {
var address = it.next_internal() orelse return null;

if (it.first_address) |first_address| {
var depth: usize = 0;
while (address != first_address) {
address = it.next_internal() orelse return null;

depth += 1;
if (depth > max_stack_trace_depth) {
return null;
}
}
it.first_address = null;
}
Expand Down Expand Up @@ -964,8 +990,8 @@ pub fn writeCurrentStackTrace(
start_addr: ?usize,
) !void {
if (native_os == .windows) {
var context: ThreadContext = undefined;
assert(getContext(&context));
var context: windows.CONTEXT = std.mem.zeroes(windows.CONTEXT);
windows.ntdll.RtlCaptureContext(&context);
return writeStackTraceWindows(out_stream, debug_info, tty_config, &context, start_addr);
}
var context: ThreadContext = undefined;
Expand All @@ -976,6 +1002,8 @@ pub fn writeCurrentStackTrace(
} else null) orelse StackIterator.init(start_addr, null);
defer it.deinit();

var depth: usize = 0;

while (it.next()) |return_address| {
printLastUnwindError(&it, debug_info, out_stream, tty_config);

Expand All @@ -986,6 +1014,12 @@ pub fn writeCurrentStackTrace(
// same behaviour for x86-windows-msvc
const address = return_address -| 1;
try printSourceAtAddress(debug_info, out_stream, address, tty_config);

depth += 1;
if (depth > max_stack_trace_depth) {
out_stream.print("Abandoned stack trace after {} frames.\n", .{max_stack_trace_depth}) catch {};
break;
}
} else printLastUnwindError(&it, debug_info, out_stream, tty_config);
}

Expand Down
2 changes: 2 additions & 0 deletions test/standalone/stack_iterator/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub fn build(b: *std.Build) void {
});

const run_cmd = b.addRunArtifact(exe);
run_cmd.addCheck(.{ .expect_stderr_match = "Test complete." });
test_step.dependOn(&run_cmd.step);
}

Expand All @@ -55,6 +56,7 @@ pub fn build(b: *std.Build) void {
});

const run_cmd = b.addRunArtifact(exe);
run_cmd.addCheck(.{ .expect_stderr_match = "Test complete." });
test_step.dependOn(&run_cmd.step);
}

Expand Down
Loading