Issue 235235
Summary [SPARC] delay slots are filled at -O0, so gdb reads arguments before they are stored
Labels new issue
Assignees
Reporter shalseth
    At -O0 the SPARC delay slot filler still moves the instruction before a call into its delay slot, and at -O0 that's typically the spill of an argument. A breakpoint on the line of the call stops on the `call` itself, before the store has run, so gdb reads stale stack:

```c
struct point { long x, y; };

__attribute__((noinline)) void zzz(void) { __asm__ volatile(""); }

long sum(struct point p, long scale)
{
	zzz();
	return (p.x + p.y) * scale;
}

int main(void)
{
	struct point p = { 2222, 3333 };
	return sum(p, 1111) == 0;
}
```

```
$ clang -g -O0 -o o0 o0.c
$ gdb -batch -ex 'break o0.c:7' -ex run -ex 'print scale' ./o0
Breakpoint 1, sum (p=..., scale=3571503287955172) at o0.c:7
$1 = 3571503287955172
```

```
00000000000007ec <sum>:
 7ec:	save  %sp, -208, %sp
 7f0:	stx  %i0, [ %fp + 0x7ef ]
 7f4:	stx  %i1, [ %fp + 0x7f7 ]
 7f8:	call  7e0 <zzz>
 7fc:	stx  %i2, [ %fp + 0x7e7 ]
```

gcc 16.2 with the same flags gives `scale=1111`. As far as I know gcc only fills delay slots from -O1 (`-fdelayed-branch`), and the Mips filler in LLVM skips -O0 too.

This is behind most of the rustc debuginfo test failures on sparc64 (rust-lang/rust#71437), since those tests all break on a call to a dummy function.

Two things I noticed while looking at it, in case someone tries turning the filler off at -O0:

- `-disable-sparc-delay-filler` isn't safe as it is. A tail call from a leaf function is emitted as `call` and needs the `%o7` restore in its delay slot, and with the filler disabled it ends up in front of the call, so the callee returns into whatever comes after:

 ```
  $ llc -O2 tail.ll -o -
  	mov	%o7, %g1
  	call bar
  	mov	%g1, %o7
  $ llc -O2 -disable-sparc-delay-filler tail.ll -o -
  	mov	%o7, %g1
  	mov	%g1, %o7
  	call bar
  	nop
  ```

  tail.ll:

 ```llvm
  target triple = "sparc64-unknown-linux-gnu"

  declare i64 @bar(i64)

  define i64 @foo(i64 %x) {
    %r = tail call i64 @bar(i64 %x)
    ret i64 %r
  }
  ```

  A Rust hello world built with `-C llvm-args=--disable-sparc-delay-filler` aborts with a double free because of this (`__rust_alloc` returns into `__rust_dealloc`).

- The stack adjustment in a leaf function's epilogue doesn't get a debug location, and today that's hidden because it ends up in the `retl` delay slot. Without the fill, the line of the return starts after the frame is released and `%sp`-based variables read the wrong slots.

clang 23.1.4, GCC 16.2.1, gdb 18.1, UltraSPARC T4-1, Linux sparc64.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to