Issue 235751
Summary [RISCV] -msave-restore: save libcall clobbers the callee-saved register t0 in preserve_most functions
Labels new issue
Assignees
Reporter leidian977
    Under the `preserve_most` calling convention, RISC-V treats `t0` (x5) as a
callee-saved register (the CSR list is `CSR_RT_MostRegs`, i.e. `x1, x5,
x8-x27, x29-x31`). However, `-msave-restore` prologues are emitted as
`call t0, __riscv_save_N`, which **writes t0 with the return address of the
save call itself**. The caller's `t0` value is therefore destroyed, and
nothing restores it — a silent ABI violation / miscompile.

`RISCVMachineFunctionInfo::useSaveRestoreLibCalls()` guards on pushable,
varargs, tail calls and interrupt attributes, but not on the calling
convention, so the combination is never rejected.

## Reproducer 1 (callee side)

`llc -mtriple=riscv64 -mattr=+save-restore` (also reproduces on riscv32),
LLVM 24.0.0git at cc2acc33a5b22db755c1e98c99862dacb117bf48:

```llvm
define preserve_mostcc i32 @f(i32 %a) nounwind {
entry:
  call void asm sideeffect "nop", "~{x8},~{x9}"() nounwind
  ret i32 %a
}
```

Output:

```asm
f:
        call    t0, __riscv_save_2
        addi    sp, sp, -16
        sd      t0, 8(sp)                       # spills the *return address*
        ...
        ld      t0, 8(sp)
        addi    sp, sp, 16
        tail    __riscv_restore_2
```

`t0` is callee-saved here, so the `sd t0` after the save call stores the
address inside `f` rather than the caller's `t0`; the reload restores the
wrong value. Even without the explicit asm clobber of `t0`, the plain
`call t0, __riscv_save_N` already overwrites the caller's `t0`.

## Reproducer 2 (end-to-end miscompile, differential)

Same module compiled with and without `-mattr=+save-restore`; the caller
keeps a value in `t0` across the `preserve_most` call, as the ABI permits:

```llvm
define preserve_mostcc void @f() nounwind {
entry:
  call void asm sideeffect "nop", "~{x8}"() nounwind
  ret void
}

define i32 @caller() nounwind {
entry:
  %v = call i32 asm sideeffect "li $0, 42", "={x5}"() nounwind
  call preserve_mostcc void @f()
  %r = call i32 asm sideeffect "mv $0, $1", "=r,{x5}"(i32 %v) nounwind
  ret i32 %r
}
```

With `-mattr=+save-restore`, `f`'s prologue `call t0, __riscv_save_1`
destroys `t0` inside the callee, so `caller` returns a code address instead
of 42. Without `-mattr=+save-restore` (or when `f`'s frame is handled
inline) the value survives and 42 is returned.

The inline asm only exists to keep the reproducer small; any `preserve_most`
function compiled with `-msave-restore` that spills any s-register has this
problem, and ordinary code keeping a live value in `t0` across the call is
miscompiled.

## Related

- #235697 and #235692 cover the other hidden side effect of the save/restore
  libcalls (`t1`); this one is about `t0` itself being the return-address
  carrier while also being a CSR under `preserve_most`.
- PreserveMost support was added in #148214, which did not touch the
  save-restore path.

## Expected

`useSaveRestoreLibCalls()` should return false for `preserve_mostcc`
functions (the restore tail call has the same conflict), so the frame is
saved inline, as already happens for interrupts and tail calls.

## Possible fix

```diff
   bool useSaveRestoreLibCalls(const MachineFunction &MF) const {
     return !isPushable(MF) &&
            MF.getSubtarget<RISCVSubtarget>().enableSaveRestore() &&
            VarArgsSaveSize == 0 && !MF.getFrameInfo().hasTailCall() &&
+           MF.getFunction().getCallingConv() != CallingConv::PreserveMost &&
            !MF.getFunction().hasFnAttribute("interrupt");
   }
```

With this change the `preserve_most` function falls back to inline
`sd ra / sd s0 / ...` and `t0` is preserved.

Note that Zcmp `cm.push` does *not* have this problem (`cm.push {ra, s0-s1}`
never writes `t0`), so only the libcall path is affected.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to