Skip to content

Commit a76775b

Browse files
squeek502Vexu
authored andcommitted
Fix stack traces with non-null first_address on Windows
Before this commit, the passed in length would always be given to the RtlCaptureStackBackTrace call. Now we always give the length of the actual buffer we're using (the addr_buf_stack size of 32 or the passed in length if it's larger than 32; this matches what the doc comment says the function was meant to be doing as well). This was causing empty stack traces for things like the GeneralPurposeAllocator leak checking. Fixes #6687
1 parent 76f8328 commit a76775b

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

lib/std/debug.zig

+4-4
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,18 @@ pub fn dumpStackTraceFromBase(bp: usize, ip: usize) void {
180180
pub fn captureStackTrace(first_address: ?usize, stack_trace: *std.builtin.StackTrace) void {
181181
if (native_os == .windows) {
182182
const addrs = stack_trace.instruction_addresses;
183-
const u32_addrs_len = @intCast(u32, addrs.len);
184183
const first_addr = first_address orelse {
185184
stack_trace.index = windows.ntdll.RtlCaptureStackBackTrace(
186185
0,
187-
u32_addrs_len,
186+
@intCast(u32, addrs.len),
188187
@ptrCast(**anyopaque, addrs.ptr),
189188
null,
190189
);
191190
return;
192191
};
193192
var addr_buf_stack: [32]usize = undefined;
194193
const addr_buf = if (addr_buf_stack.len > addrs.len) addr_buf_stack[0..] else addrs;
195-
const n = windows.ntdll.RtlCaptureStackBackTrace(0, u32_addrs_len, @ptrCast(**anyopaque, addr_buf.ptr), null);
194+
const n = windows.ntdll.RtlCaptureStackBackTrace(0, @intCast(u32, addr_buf.len), @ptrCast(**anyopaque, addr_buf.ptr), null);
196195
const first_index = for (addr_buf[0..n]) |addr, i| {
197196
if (addr == first_addr) {
198197
break i;
@@ -201,7 +200,8 @@ pub fn captureStackTrace(first_address: ?usize, stack_trace: *std.builtin.StackT
201200
stack_trace.index = 0;
202201
return;
203202
};
204-
const slice = addr_buf[first_index..n];
203+
const end_index = math.min(first_index + addrs.len, n);
204+
const slice = addr_buf[first_index..end_index];
205205
// We use a for loop here because slice and addrs may alias.
206206
for (slice) |addr, i| {
207207
addrs[i] = addr;

0 commit comments

Comments
 (0)