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.
This commit is contained in:
Alex Rønne Petersen 2025-10-14 09:39:15 +02:00
parent 12b1d57df1
commit b8dd40fde8
No known key found for this signature in database
2 changed files with 12 additions and 1 deletions

View file

@ -807,7 +807,6 @@ const StackIterator = union(enum) {
/// `@frameAddress` and `cpu_context.Native.current` as the caller's stack frame and /// `@frameAddress` and `cpu_context.Native.current` as the caller's stack frame and
/// our own are one and the same. /// our own are one and the same.
inline fn init(opt_context_ptr: ?CpuContextPtr) error{CannotUnwindFromContext}!StackIterator { inline fn init(opt_context_ptr: ?CpuContextPtr) error{CannotUnwindFromContext}!StackIterator {
flushSparcWindows();
if (opt_context_ptr) |context_ptr| { if (opt_context_ptr) |context_ptr| {
if (SelfInfo == void or !SelfInfo.can_unwind) return error.CannotUnwindFromContext; if (SelfInfo == void or !SelfInfo.can_unwind) return error.CannotUnwindFromContext;
// Use `di_first` here so we report the PC in the context before unwinding any further. // Use `di_first` here so we report the PC in the context before unwinding any further.
@ -826,6 +825,7 @@ const StackIterator = union(enum) {
// in our caller's frame and above. // in our caller's frame and above.
return .{ .di = .init(&.current()) }; return .{ .di = .init(&.current()) };
} }
flushSparcWindows();
return .{ .fp = @frameAddress() }; return .{ .fp = @frameAddress() };
} }
fn deinit(si: *StackIterator) void { fn deinit(si: *StackIterator) void {

View file

@ -870,6 +870,8 @@ const Sparc = extern struct {
pub const Gpr = if (native_arch == .sparc64) u64 else u32; pub const Gpr = if (native_arch == .sparc64) u64 else u32;
pub inline fn current() Sparc { pub inline fn current() Sparc {
flushWindows();
var ctx: Sparc = undefined; var ctx: Sparc = undefined;
asm volatile (if (Gpr == u64) asm volatile (if (Gpr == u64)
\\ stx %g0, [%l0 + 0] \\ stx %g0, [%l0 + 0]
@ -933,6 +935,15 @@ const Sparc = extern struct {
return ctx; return ctx;
} }
noinline fn flushWindows() void {
// Flush all register windows except the current one (hence `noinline`). This ensures that
// we actually see meaningful data on the stack when we walk the frame chain.
if (comptime builtin.target.cpu.has(.sparc, .v9))
asm volatile ("flushw" ::: .{ .memory = true })
else
asm volatile ("ta 3" ::: .{ .memory = true }); // ST_FLUSH_WINDOWS
}
pub fn dwarfRegisterBytes(ctx: *Sparc, register_num: u16) DwarfRegisterError![]u8 { pub fn dwarfRegisterBytes(ctx: *Sparc, register_num: u16) DwarfRegisterError![]u8 {
switch (register_num) { switch (register_num) {
0...7 => return @ptrCast(&ctx.g[register_num]), 0...7 => return @ptrCast(&ctx.g[register_num]),