| Issue |
240316
|
| Summary |
[RISCV][Zicfiss] RISCVTargetCodeGenInfo::setTargetAttributes Early-Returns on Null Decl, Omitting hw-shadow-stack on Multi-Version IFUNC Resolvers Under -fcf-protection=return
|
| Labels |
backend:RISC-V
|
| Assignees |
|
| Reporter |
zjy-dev
|
When Clang emits a multi-versioned function resolver (`__attribute__((target_clones(...)))` or `target_version`), `CodeGenModule::setMultiVersionResolverAttributes` (clang/lib/CodeGen/CodeGenModule.cpp:5469) invokes `getTargetCodeGenInfo().setTargetAttributes(/*D=*/nullptr, Resolver, *this)` with `D = nullptr`. However, `RISCVTargetCodeGenInfo::setTargetAttributes` (clang/lib/CodeGen/Targets/RISCV.cpp:1098-1107) begins with `const auto *FD = dyn_cast_or_null<FunctionDecl>(D); if (!FD) return;` BEFORE checking `if (CGM.getCodeGenOpts().CFProtectionReturn) Fn->addFnAttr("hw-shadow-stack");`. Because `D` is `nullptr`, it returns early without adding `"hw-shadow-stack"` to `foo.resolver`, even though `foo.resolver` is a non-leaf function that spills `ra` to `[sp, #8]`, calls `__init_riscv_feature_bits`, and returns.
### Reproducer (`trigger.c`)
```c
__attribute__((target_clones("default", "arch=+zbb")))
int foo(int x) { return x + 1; }
int bar(int x) { return foo(x); }
```
Compile / reproduction command (`['--target=riscv64-unknown-linux-gnu', '-march=rv64gc_zicfiss1p0', '-menable-experimental-extensions', '-fcf-protection=return', '-O2', '-S']`):
```bash
podman exec -i -e TMPDIR=/dev/shm defuzz-toolchain-daemon /opt/llvm23/bin/clang --target=riscv64-unknown-linux-gnu -march=rv64gc_zicfiss1p0 -menable-experimental-extensions -fcf-protection=return -O2 -x c - -S -o -
```
Observed output on LLVM 23.1.2:
```text
foo.resolver:
.cfi_startproc
# %bb.0:
addi sp, sp, -16
.cfi_def_cfa_offset 16
sd ra, 8(sp) # 8-byte Folded Spill
.cfi_offset ra, -8
li a0, 0
call __init_riscv_feature_bits
...
.LBB2_3:
ld ra, 8(sp) # 8-byte Folded Reload
.cfi_restore ra
addi sp, sp, 16
.cfi_def_cfa_offset 0
ret
```
### Expected behavior
RISCVTargetCodeGenInfo::setTargetAttributes must attach `"hw-shadow-stack"` to `llvm::Function` objects before checking `if (!FD) return;` (matching `AArch64TargetCodeGenInfo::setTargetAttributes`), so that compiler-synthesized IFUNC resolvers are protected by Zicfiss `sspush`/`sspopchk`.
### Root cause & impact
- **Source location**: `clang/lib/CodeGen/Targets/RISCV.cpp:1098-1107; clang/lib/CodeGen/CodeGenModule.cpp:5463-5469`
- **Impact**: Leaves the non-leaf IFUNC resolver (`foo.resolver`, which calls `__init_riscv_feature_bits` while `ra` sits on the data stack) unprotected by hardware shadow stack (`Zicfiss`) in a binary whose `.note.gnu.property` claims full `GNU_PROPERTY_RISCV_FEATURE_1_BCFI` shadow-stack enforcement.
### Suggested fix
In `clang/lib/CodeGen/Targets/RISCV.cpp:1098-1107`, move `if ( auto *Fn = dyn_cast<llvm::Function>(GV); Fn && CGM.getCodeGenOpts().CFProtectionReturn ) Fn->addFnAttr("hw-shadow-stack");` above `const auto *FD = dyn_cast_or_null<FunctionDecl>(D); if (!FD) return;`.
Environment: LLVM 23.1.2 (`llvmorg-23.1.2`), affected targets: `riscv64`.
Also reproduced with clang trunk (Compiler Explorer, 2026-11-09); `foo.resolver` still has no `sspush`/`sspopchk`.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs