Repository navigation
Conversation
|
Thanks for the pull request, and welcome! The Rust Project has assigned @JonathanBrouwer (or someone else) to review your changes, you should hear from them (or someone else) within the next two weeks. Please see the contribution instructions and our LLM policy for more information. Why was this reviewer chosen?The reviewer was selected based on:
|
|
@rustbot reroll |
|
A better reviewer: r? @rcvalle |
LLVM's RISC-V backend expects a `cf-branch-label-scheme` module flag whenever `cf-protection-branch` is set, and crashes without one. Emit it with `unlabeled`, which is the only scheme LLVM implements. The Zicfiss shadow stack is enabled per function through the `hw-shadow-stack` attribute rather than by the module flag, so add that for `-Zcf-protection=return` and `full`. Both match what Clang does for `-fcf-protection` on RISC-V. The shadow stack instructions are encoded as Zimop instructions, so they are illegal on processors without Zimop. Without the feature, LLVM silently skips the shadow stack, but the `cf-protection-return` module flag still marks the object as shadow stack compatible. Reject this, like Clang does. Before LLVM 24, the RISC-V backend tail calls through `t1` unless Zicfilp is enabled, and only `t2` is exempt from landing pad checks. A direct tail call to a function without a landing pad then faults once landing pads are enforced. With these versions, enable Zicfilp for code generation whenever `-Zcf-protection` covers branches. Landing pads are hints, so the code still runs without Zicfilp.
583aaa3 to
db44b6e
Compare
There was a problem hiding this comment.
This seems reasonable to me but I'd like to hear from the RISC-V folks.
@rustbot ping risc-v
|
Hey RISC-V Group! This issue has been identified as a good "RISC-V candidate". |
|
LGTM in general, it seems to be matching Clang's behaviour Before LLVM 24, Will it miss Compiler-generated functions ? attribute is added in Will LTO work with Clang that predates the unlabeled default (llvm#223297) |
Yes,
Generally it covers compiler-generated functions (e.g. allocator shims), with 2 exceptions:
By default it will not work. One would need to pass |
-Zcf-protection=branch(orfull) currently crashes rustc when targeting RISC-V:LLVM's RISC-V backend expects a
cf-branch-label-schememodule flag whenevercf-protection-branchis set, and segfaults inRISCVAsmPrinter::emitNoteGnuPropertywithout it. Clang always emits the two together.-Zcf-protection=returndoesn't crash, but it also doesn't do anything: the Zicfiss shadow stack is enabled per function through thehw-shadow-stackattribute, and thecf-protection-returnmodule flag only sets the bit in.note.gnu.property.This PR emits the label scheme flag on RISC-V and adds
hw-shadow-stackforreturn/full, matching what Clang does for-fcf-protection. Other targets are unaffected.The label scheme is always
unlabeled. An earlier version of this had a-Zcf-branch-label-scheme=unlabeled|func-sigoption to mirror Clang's-mcf-branch-label-scheme, but I dropped it sincefunc-sigisn't implemented in LLVM yet (the backend errors out with "the complete func-sig label scheme feature is not implemented yet", see llvm/llvm-project#111661), so the only non-default value couldn't compile anything. Clang recently madeunlabeledits default as well (llvm/llvm-project#223297). The option can be added later if LLVM grows support.With LLVM 22 the new module flag is just ignored, so there's no version check.return/fullnow require the zimop target feature, since the shadow stack instructions are Zimop encodings. With LLVM 23 and older,branchnow also enables Zicfilp for code generation, because those versions only do direct tail calls through the landing-pad-exempt t2 with it (was an llvm bug; fixed in llvm/llvm-project#220657).Dynamically linked code also needs a landing-pad-aware PLT from lld (llvm/llvm-project#145461). That's not in LLVM 23 and
will be a separate backport(has been backported, submodule still needs to be updated).This also fixes a sentence in the sanitizer docs that said ShadowCallStack uses the Zicfiss
sspregister when available. It always usesgp; the hardware shadow stack only comes fromhw-shadow-stack.Tracking issue: #93754