| Issue |
240684
|
| Summary |
[LoongArch][StackClash] LoongArchFrameLowering::allocateStack Variable-Length Probe Loop Emits Residual SP Adjustment and Probe Store Before Updating .cfi_def_cfa_offset
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
zjy-dev
|
In LoongArchFrameLowering::allocateStack (llvm/lib/Target/LoongArch/LoongArchFrameLowering.cpp:259-297), when `Offset >= ProbeSize * 5` (the variable-length loop path), line 279 switches the DWARF CFA register back from `TargetReg` (`$t1`/`R13`) to `SPReg` (`$sp`) via `CFIBuilder.buildDefCFARegister(SPReg)`, where the active CFA offset is still `RoundedSize + CFAAdjust` (`RealStackSize - Residual`). Next, lines 282-293 emit `adjustReg(MBB, MBBI, DL, SPReg, SPReg, -Residual, Flag)` (`addi.d $sp, $sp, -Residual`) and the residual stack-clash probe `st.d $zero, $sp, 0` (when `DynAllocation` is true), and only at lines 295-296 emit `CFIBuilder.buildDefCFAOffset(RealStackSize)`. (By contrast, the unrolled path at lines 241-255 correctly emits `CFIBuilder.buildDefCFAOffset(RealStackSize)` at line 244 BEFORE the residual probe store at line 248.) Thus, at the exact instruction `st.d $zero, $sp, 0` designed to fault on the stack guard page, the DWARF CFA is off by `Residual` bytes.
### Reproducer (`trigger.c`)
```c
void sink(volatile char *a, volatile char *b);
int vla_with_big_fixed_frame(int n) {
volatile char fixed[200];
volatile char dyn[n];
fixed[0] = 1;
dyn[0] = 2;
sink(fixed, dyn);
return fixed[0] + dyn[0];
}
```
Compile / reproduction command (`['--target=loongarch64-unknown-linux-gnu', '-O2', '-fstack-clash-protection', '-mstack-probe-size=32', '-fasynchronous-unwind-tables', '-S']`):
```bash
podman exec -i -e TMPDIR=/dev/shm defuzz-toolchain-daemon /opt/llvm23/bin/clang --target=loongarch64-unknown-linux-gnu -O2 -fstack-clash-protection -mstack-probe-size=32 -fasynchronous-unwind-tables -x c - -S -o -
```
Observed output on LLVM 23.1.2:
```text
vla_with_big_fixed_frame:
.cfi_startproc
# %bb.0:
addi.d $t1, $sp, -224
.cfi_def_cfa 13, 224
ori $t2, $zero, 32
.LBB0_1:
sub.d $sp, $sp, $t2
st.d $zero, $sp, 0
bne $sp, $t1, .LBB0_1
# %bb.2:
.cfi_def_cfa_register 3
addi.d $sp, $sp, -16
st.d $zero, $sp, 0
.cfi_def_cfa_offset 240
```
### Expected behavior
In `LoongArchFrameLowering::allocateStack` (lines 282-296), `CFIBuilder.buildDefCFAOffset(RealStackSize)` must be emitted immediately after `adjustReg(MBB, MBBI, DL, SPReg, SPReg, -Residual, Flag)` and BEFORE the `st.d $zero, $sp, 0` probe instruction, matching the unrolled path at lines 242-254.
### Root cause & impact
- **Source location**: `llvm/lib/Target/LoongArch/LoongArchFrameLowering.cpp:277-297`
- **Impact**: Inaccurate asynchronous DWARF unwind info (`.eh_frame`) on LoongArch64 at the residual stack-clash probe instruction (`st.d $zero, $sp, 0`): if the residual probe faults on the stack guard page, the signal/exception unwinder computes CFA as `$sp + 224` instead of `$sp + 240`.
### Suggested fix
Move `if (EmitCFI) CFIBuilder.buildDefCFAOffset(RealStackSize);` from lines 295-296 of `LoongArchFrameLowering.cpp` into `if (Residual)` right after `adjustReg(MBB, MBBI, DL, SPReg, SPReg, -Residual, Flag)` at line 283.
Environment: LLVM 23.1.2 (`llvmorg-23.1.2`), affected targets: `loongarch64`.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs