authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2021-04-25 19:10:11+02:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2021-04-25 19:10:11+02:00
log54db36cd88835ed6b9d9584d9568e9aa72e41480
treefa69c82ecc88148f9a2126db0bc8c0239a8d0caf
parent37b05742ff1544bccf7c8ae9b12c6707a5a54df2

std: Fix backtraces on sparcv9

Flush all the register windows to stack before starting the stack walk, we may otherwise try to read garbage and crash and burn. Add a few comptime annotations to debloat some functions.

1 files changed, 11 insertions(+), 4 deletions(-)

lib/std/debug.zig+11-4
...@@ -336,6 +336,13 @@ pub const StackIterator = struct {...@@ -336,6 +336,13 @@ pub const StackIterator = struct {
336 fp: usize,336 fp: usize,
337337
338 pub fn init(first_address: ?usize, fp: ?usize) StackIterator {338 pub fn init(first_address: ?usize, fp: ?usize) StackIterator {
339 if (builtin.arch == .sparcv9) {
340 // Flush all the register windows on stack.
341 asm volatile (
342 \\ flushw
343 ::: "memory");
344 }
345
339 return StackIterator{346 return StackIterator{
340 .first_address = first_address,347 .first_address = first_address,
341 .fp = fp orelse @frameAddress(),348 .fp = fp orelse @frameAddress(),
...@@ -343,18 +350,18 @@ pub const StackIterator = struct {...@@ -343,18 +350,18 @@ pub const StackIterator = struct {
343 }350 }
344351
345 // Offset of the saved BP wrt the frame pointer.352 // Offset of the saved BP wrt the frame pointer.
346 const fp_offset = if (builtin.arch.isRISCV())353 const fp_offset = if (comptime builtin.arch.isRISCV())
347 // On RISC-V the frame pointer points to the top of the saved register354 // On RISC-V the frame pointer points to the top of the saved register
348 // area, on pretty much every other architecture it points to the stack355 // area, on pretty much every other architecture it points to the stack
349 // slot where the previous frame pointer is saved.356 // slot where the previous frame pointer is saved.
350 2 * @sizeOf(usize)357 2 * @sizeOf(usize)
351 else if (builtin.arch.isSPARC())358 else if (comptime builtin.arch.isSPARC())
352 // On SPARC the previous frame pointer is stored at 14 slots past %fp+BIAS.359 // On SPARC the previous frame pointer is stored at 14 slots past %fp+BIAS.
353 14 * @sizeOf(usize)360 14 * @sizeOf(usize)
354 else361 else
355 0;362 0;
356363
357 const fp_bias = if (builtin.arch.isSPARC())364 const fp_bias = if (comptime builtin.arch.isSPARC())
358 // On SPARC frame pointers are biased by a constant.365 // On SPARC frame pointers are biased by a constant.
359 2047366 2047
360 else367 else
...@@ -380,7 +387,7 @@ pub const StackIterator = struct {...@@ -380,7 +387,7 @@ pub const StackIterator = struct {
380 }387 }
381388
382 fn next_internal(self: *StackIterator) ?usize {389 fn next_internal(self: *StackIterator) ?usize {
383 const fp = if (builtin.arch.isSPARC())390 const fp = if (comptime builtin.arch.isSPARC())
384 // On SPARC the offset is positive. (!)391 // On SPARC the offset is positive. (!)
385 math.add(usize, self.fp, fp_offset) catch return null392 math.add(usize, self.fp, fp_offset) catch return null
386 else393 else