authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-10-14 09:39:15+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-10-15 13:59:17+02:00
logb8dd40fde8aed212ffa8a98b45821ccdf0c2e17d
tree83eb99de3c864c8e555210db3f7ee5ee4136c16b
parent12b1d57df1e0efed6e5a3eb3933de7f08d42d6d5
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

std.debug.cpu_context.Sparc: flush register windows in current()

It's better to do this here than in StackIterator.init() so that std.debug.cpu_context.Native.current() isn't a footgun on SPARC.

2 files changed, 12 insertions(+), 1 deletions(-)

lib/std/debug.zig+1-1
...@@ -807,7 +807,6 @@ const StackIterator = union(enum) {...@@ -807,7 +807,6 @@ const StackIterator = union(enum) {
807 /// `@frameAddress` and `cpu_context.Native.current` as the caller's stack frame and807 /// `@frameAddress` and `cpu_context.Native.current` as the caller's stack frame and
808 /// our own are one and the same.808 /// our own are one and the same.
809 inline fn init(opt_context_ptr: ?CpuContextPtr) error{CannotUnwindFromContext}!StackIterator {809 inline fn init(opt_context_ptr: ?CpuContextPtr) error{CannotUnwindFromContext}!StackIterator {
810 flushSparcWindows();
811 if (opt_context_ptr) |context_ptr| {810 if (opt_context_ptr) |context_ptr| {
812 if (SelfInfo == void or !SelfInfo.can_unwind) return error.CannotUnwindFromContext;811 if (SelfInfo == void or !SelfInfo.can_unwind) return error.CannotUnwindFromContext;
813 // Use `di_first` here so we report the PC in the context before unwinding any further.812 // Use `di_first` here so we report the PC in the context before unwinding any further.
...@@ -826,6 +825,7 @@ const StackIterator = union(enum) {...@@ -826,6 +825,7 @@ const StackIterator = union(enum) {
826 // in our caller's frame and above.825 // in our caller's frame and above.
827 return .{ .di = .init(&.current()) };826 return .{ .di = .init(&.current()) };
828 }827 }
828 flushSparcWindows();
829 return .{ .fp = @frameAddress() };829 return .{ .fp = @frameAddress() };
830 }830 }
831 fn deinit(si: *StackIterator) void {831 fn deinit(si: *StackIterator) void {
lib/std/debug/cpu_context.zig+11
...@@ -870,6 +870,8 @@ const Sparc = extern struct {...@@ -870,6 +870,8 @@ const Sparc = extern struct {
870 pub const Gpr = if (native_arch == .sparc64) u64 else u32;870 pub const Gpr = if (native_arch == .sparc64) u64 else u32;
871871
872 pub inline fn current() Sparc {872 pub inline fn current() Sparc {
873 flushWindows();
874
873 var ctx: Sparc = undefined;875 var ctx: Sparc = undefined;
874 asm volatile (if (Gpr == u64)876 asm volatile (if (Gpr == u64)
875 \\ stx %g0, [%l0 + 0]877 \\ stx %g0, [%l0 + 0]
...@@ -933,6 +935,15 @@ const Sparc = extern struct {...@@ -933,6 +935,15 @@ const Sparc = extern struct {
933 return ctx;935 return ctx;
934 }936 }
935937
938 noinline fn flushWindows() void {
939 // Flush all register windows except the current one (hence `noinline`). This ensures that
940 // we actually see meaningful data on the stack when we walk the frame chain.
941 if (comptime builtin.target.cpu.has(.sparc, .v9))
942 asm volatile ("flushw" ::: .{ .memory = true })
943 else
944 asm volatile ("ta 3" ::: .{ .memory = true }); // ST_FLUSH_WINDOWS
945 }
946
936 pub fn dwarfRegisterBytes(ctx: *Sparc, register_num: u16) DwarfRegisterError![]u8 {947 pub fn dwarfRegisterBytes(ctx: *Sparc, register_num: u16) DwarfRegisterError![]u8 {
937 switch (register_num) {948 switch (register_num) {
938 0...7 => return @ptrCast(&ctx.g[register_num]),949 0...7 => return @ptrCast(&ctx.g[register_num]),