Issue 240683
Summary [RISCV][CFI] RISCVFrameLowering Enables CFIFixup Pass Without Implementing resetCFIToInitialState, Emitting Framed DWARF CFA State Across Shrink-Wrapped Frameless Return Blocks
Labels new issue
Assignees
Reporter zjy-dev
    RISC-V opts into the `CFIFixup` pass by overriding `RISCVFrameLowering::enableCFIFixup` to return `true` (`RISCVFrameLowering.cpp:1765`) and scheduling `createCFIFixup()` in `RISCVTargetMachine.cpp:195`. In `CFIFixup.cpp:277-285`, when a frameless basic block (`!Info.HasFrameOnEntry`, such as a shrink-wrapped early-exit block) is physically laid out after a framed basic block (`PrevHasFrame`) whose epilogue was guarded by `.cfi_remember_state`/`.cfi_restore_state`, `CFIFixup` calls `TFL.resetCFIToInitialState(CurrBB)` and marks `InsertPt->HasFrameOnExit = false`. However, `RISCVFrameLowering` never overrides `TargetFrameLowering::resetCFIToInitialState` (`TargetFrameLowering.h:238`, which is an empty no-op `{}`!). As a result, no `.cfi_def_cfa` or `.cfi_same_value` directive is emitted on the frameless return block, leaving the previous block's framed CFA (`sp + 16`) and saved-register offsets (`ra = [CFA - 8]`, `s0 = [CFA - 16]`) active across the frameless return block.

### Reproducer (`trigger.c`)

```c
extern int use(int);

int shrink(int x) {
  if (x <= 0)
    return 1;
  return use(x) - use(-x);
}

int shrink2(int x) {
  if (x == 0)
    return 7;
 if (x & 1)
    return use(x - 1) + 1;
  return 1 - use(x + 1);
}
```

Compile / reproduction command (`['--target=riscv64-unknown-linux-gnu', '-O2', '-fasynchronous-unwind-tables', '-c']`):

```bash
podman exec -i -e TMPDIR=/dev/shm defuzz-toolchain-daemon bash -c '/opt/llvm23/bin/clang --target=riscv64-unknown-linux-gnu -O2 -fasynchronous-unwind-tables -x c - -c -o /dev/shm/t43.o && /opt/llvm23/bin/llvm-objdump --dwarf=frames -d /dev/shm/t43.o'
```

Observed output on LLVM 23.1.2:

```text
0000000000000036 <shrink2>:
      36: c101 	beqz	a0, 0x36 <shrink2>
      38: 1141         	addi	sp, sp, -0x10
 3a: e406         	sd	ra, 0x8(sp)
...
      56: 451d         	li	a0, 0x7
      58: 8082         	ret

FDE pc=00000036...0000006c
  0x36: CFA=reg2
  0x3a: CFA=reg2+16
  0x3c: CFA=reg2+16: reg1=[CFA-8]
  0x5a: CFA=reg2+16: reg1=[CFA-8]
  (At 0x56..0x58, the frameless early return still has CFA=reg2+16:
   reg1=[CFA-8] instead of CFA=reg2!)
```

### Expected behavior

RISCVFrameLowering must implement `resetCFIToInitialState(MachineBasicBlock &MBB)` (just as `AArch64FrameLowering` and `LoongArchFrameLowering` do) to emit `.cfi_def_cfa sp, 0` and `.cfi_same_value` for callee-saved registers when `CFIFixup` transitions from a framed block to a frameless block.

### Root cause & impact

- **Source location**: `llvm/lib/CodeGen/CFIFixup.cpp:277-285; llvm/lib/Target/RISCV/RISCVFrameLowering.cpp:1765-1770; llvm/include/llvm/CodeGen/TargetFrameLowering.h:238-241`
- **Impact**: Corrupts asynchronous DWARF unwind tables (`.eh_frame`) on RISC-V for shrink-wrapped functions: an asynchronous signal, profiler sample, or exception unwinder interrupting the frameless return block (`0x20..0x24`) reads `ra` and `s0` from `sp + 8` and `sp + 0` of the caller's frame instead of the live registers.

### Suggested fix

Override `resetCFIToInitialState(MachineBasicBlock &MBB)` in `RISCVFrameLowering` to emit `CFIBuilder.buildDefCFA(SPReg, 0)` and `CFIBuilder.buildSameValue(CS.getReg())` for each callee-saved register.

Environment: LLVM 23.1.2 (`llvmorg-23.1.2`), affected targets: `riscv64`.

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

Reply via email to