-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set branch protection function attributes #128141
Conversation
Since LLVM 19, it is necessary to set not only module flags, but also function attributes for branch protection on aarch64. See llvm/llvm-project@e15d67c for the relevant LLVM change.
r? @Nadrieril rustbot has assigned @Nadrieril. Use |
Update to LLVM 19 Related changes: * rust-lang#127605 * rust-lang#127613 * rust-lang#127654 * rust-lang#128141 * llvm/llvm-project#98933 try-job: dist-x86_64-apple try-job: dist-apple-various try-job: x86_64-apple-1 try-job: x86_64-apple-2 try-job: dist-aarch64-apple try-job: aarch64-apple
Update to LLVM 19 Related changes: * rust-lang#127605 * rust-lang#127613 * rust-lang#127654 * rust-lang#128141 * llvm/llvm-project#98933 try-job: x86_64-msvc try-job: i686-msvc try-job: x86_64-msvc-ext try-job: dist-x86_64-msvc try-job: dist-i686-msvc try-job: dist-aarch64-msvc try-job: dist-x86_64-msvc-alt
Update to LLVM 19 Related changes: * rust-lang#127605 * rust-lang#127613 * rust-lang#127654 * rust-lang#128141 * llvm/llvm-project#98933 try-job: i686-mingw try-job: x86_64-mingw try-job: dist-i686-mingw try-job: dist-x86_64-mingw
r? @rust-lang/wg-llvm |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems the flags on the module are no longer being used? Can we remove this code?
rust/compiler/rustc_codegen_llvm/src/context.rs
Lines 277 to 312 in a5ee5cb
if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection { | |
if sess.target.arch == "aarch64" { | |
unsafe { | |
llvm::LLVMRustAddModuleFlagU32( | |
llmod, | |
llvm::LLVMModFlagBehavior::Min, | |
c"branch-target-enforcement".as_ptr().cast(), | |
bti.into(), | |
); | |
llvm::LLVMRustAddModuleFlagU32( | |
llmod, | |
llvm::LLVMModFlagBehavior::Min, | |
c"sign-return-address".as_ptr().cast(), | |
pac_ret.is_some().into(), | |
); | |
let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, key: PAuthKey::A }); | |
llvm::LLVMRustAddModuleFlagU32( | |
llmod, | |
llvm::LLVMModFlagBehavior::Min, | |
c"sign-return-address-all".as_ptr().cast(), | |
pac_opts.leaf.into(), | |
); | |
llvm::LLVMRustAddModuleFlagU32( | |
llmod, | |
llvm::LLVMModFlagBehavior::Min, | |
c"sign-return-address-with-bkey".as_ptr().cast(), | |
u32::from(pac_opts.key == PAuthKey::B), | |
); | |
} | |
} else { | |
bug!( | |
"branch-protection used on non-AArch64 target; \ | |
this should be checked in rustc_session." | |
); | |
} | |
} |
if bti { | ||
to_add.push(llvm::CreateAttrString(cx.llcx, "branch-target-enforcement")); | ||
} | ||
if let Some(PacRet { leaf, key }) = pac_ret { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, key: PAuthKey::A });
? See
let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, key: PAuthKey::A }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. Note that, while that code makes it look like non-leaf a-key is the default, this is not actually the case, as sign-return-address is still set to false in the None case and the overall functionality is disabled. (I think the reason why it's always setting all module flags is to get correct module flag merging behavior during LTO, but not entirely sure on that.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reference, this matches the corresponding clang logic for the function attrs: https://github.com/llvm/llvm-project/blob/5bd3aef5e285cce793e3fc6b21299ac9d650a947/clang/lib/CodeGen/TargetInfo.cpp#L219-L222
No, the module flags are still needed to emit the ELF attributes (and when creating functions with default attrs). |
LGTM. :) Comparing with clang's code, this seems largely consistent. But, clang doesn't always add these attributes: (Very complex options, especially under LTO.) |
LGTM as well! @bors r=DianQK,cuviper rollup |
…iaskrgr Rollup of 4 pull requests Successful merges: - rust-lang#127574 (elaborate unknowable goals) - rust-lang#128141 (Set branch protection function attributes) - rust-lang#128315 (Fix vita build of std and forbid unsafe in unsafe in the os/vita module) - rust-lang#128339 ([rustdoc] Make the buttons remain when code example is clicked) r? `@ghost` `@rustbot` modify labels: rollup
…iaskrgr Rollup of 4 pull requests Successful merges: - rust-lang#127574 (elaborate unknowable goals) - rust-lang#128141 (Set branch protection function attributes) - rust-lang#128315 (Fix vita build of std and forbid unsafe in unsafe in the os/vita module) - rust-lang#128339 ([rustdoc] Make the buttons remain when code example is clicked) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#128141 - nikic:aarch64-bti, r=DianQK,cuviper Set branch protection function attributes Since LLVM 19, it is necessary to set not only module flags, but also function attributes for branch protection on aarch64. See llvm/llvm-project@e15d67c for the relevant LLVM change. Fixes rust-lang#127829.
Update to LLVM 19 The LLVM 19.1.0 final release is planned for Sep 3rd. The rustc 1.82 stable release will be on Oct 17th. The unstable MC/DC coverage support is temporarily broken by this update. It will be restored by rust-lang#126733. The implementation changed substantially in LLVM 19, and there are no plans to support both the LLVM 18 and LLVM 19 implementation at the same time. Compatibility note for wasm: > WebAssembly target support for the `multivalue` target feature has changed when upgrading to LLVM 19. Support for generating functions with multiple returns no longer works and `-Ctarget-feature=+multivalue` has a different meaning than it did in LLVM 18 and prior. The WebAssembly target features `multivalue` and `reference-types` are now both enabled by default, but generated code is not affected by default. These features being enabled are encoded in the `target_features` custom section and may affect downstream tooling such as `wasm-opt` consuming the module, but the actual generated WebAssembly will continue to not use either `multivalue` or `reference-types` by default. There is no longer any supported means to generate a module that has a function with multiple returns. Related changes: * rust-lang#127605 * rust-lang#127613 * rust-lang#127654 * rust-lang#128141 * llvm/llvm-project#98933 Fixes rust-lang#121444. Fixes rust-lang#128212.
Update to LLVM 19 The LLVM 19.1.0 final release is planned for Sep 3rd. The rustc 1.82 stable release will be on Oct 17th. The unstable MC/DC coverage support is temporarily broken by this update. It will be restored by rust-lang#126733. The implementation changed substantially in LLVM 19, and there are no plans to support both the LLVM 18 and LLVM 19 implementation at the same time. Compatibility note for wasm: > WebAssembly target support for the `multivalue` target feature has changed when upgrading to LLVM 19. Support for generating functions with multiple returns no longer works and `-Ctarget-feature=+multivalue` has a different meaning than it did in LLVM 18 and prior. The WebAssembly target features `multivalue` and `reference-types` are now both enabled by default, but generated code is not affected by default. These features being enabled are encoded in the `target_features` custom section and may affect downstream tooling such as `wasm-opt` consuming the module, but the actual generated WebAssembly will continue to not use either `multivalue` or `reference-types` by default. There is no longer any supported means to generate a module that has a function with multiple returns. Related changes: * rust-lang#127605 * rust-lang#127613 * rust-lang#127654 * rust-lang#128141 * llvm/llvm-project#98933 Fixes rust-lang#121444. Fixes rust-lang#128212.
Update to LLVM 19 The LLVM 19.1.0 final release is planned for Sep 3rd. The rustc 1.82 stable release will be on Oct 17th. The unstable MC/DC coverage support is temporarily broken by this update. It will be restored by rust-lang#126733. The implementation changed substantially in LLVM 19, and there are no plans to support both the LLVM 18 and LLVM 19 implementation at the same time. Compatibility note for wasm: > WebAssembly target support for the `multivalue` target feature has changed when upgrading to LLVM 19. Support for generating functions with multiple returns no longer works and `-Ctarget-feature=+multivalue` has a different meaning than it did in LLVM 18 and prior. The WebAssembly target features `multivalue` and `reference-types` are now both enabled by default, but generated code is not affected by default. These features being enabled are encoded in the `target_features` custom section and may affect downstream tooling such as `wasm-opt` consuming the module, but the actual generated WebAssembly will continue to not use either `multivalue` or `reference-types` by default. There is no longer any supported means to generate a module that has a function with multiple returns. Related changes: * rust-lang#127605 * rust-lang#127613 * rust-lang#127654 * rust-lang#128141 * llvm/llvm-project#98933 Fixes rust-lang#121444. Fixes rust-lang#128212.
Update to LLVM 19 The LLVM 19.1.0 final release is planned for Sep 3rd. The rustc 1.82 stable release will be on Oct 17th. The unstable MC/DC coverage support is temporarily broken by this update. It will be restored by rust-lang/rust#126733. The implementation changed substantially in LLVM 19, and there are no plans to support both the LLVM 18 and LLVM 19 implementation at the same time. Compatibility note for wasm: > WebAssembly target support for the `multivalue` target feature has changed when upgrading to LLVM 19. Support for generating functions with multiple returns no longer works and `-Ctarget-feature=+multivalue` has a different meaning than it did in LLVM 18 and prior. The WebAssembly target features `multivalue` and `reference-types` are now both enabled by default, but generated code is not affected by default. These features being enabled are encoded in the `target_features` custom section and may affect downstream tooling such as `wasm-opt` consuming the module, but the actual generated WebAssembly will continue to not use either `multivalue` or `reference-types` by default. There is no longer any supported means to generate a module that has a function with multiple returns. Related changes: * rust-lang/rust#127605 * rust-lang/rust#127613 * rust-lang/rust#127654 * rust-lang/rust#128141 * llvm/llvm-project#98933 Fixes rust-lang/rust#121444. Fixes rust-lang/rust#128212.
Update to LLVM 19 The LLVM 19.1.0 final release is planned for Sep 3rd. The rustc 1.82 stable release will be on Oct 17th. The unstable MC/DC coverage support is temporarily broken by this update. It will be restored by rust-lang/rust#126733. The implementation changed substantially in LLVM 19, and there are no plans to support both the LLVM 18 and LLVM 19 implementation at the same time. Compatibility note for wasm: > WebAssembly target support for the `multivalue` target feature has changed when upgrading to LLVM 19. Support for generating functions with multiple returns no longer works and `-Ctarget-feature=+multivalue` has a different meaning than it did in LLVM 18 and prior. The WebAssembly target features `multivalue` and `reference-types` are now both enabled by default, but generated code is not affected by default. These features being enabled are encoded in the `target_features` custom section and may affect downstream tooling such as `wasm-opt` consuming the module, but the actual generated WebAssembly will continue to not use either `multivalue` or `reference-types` by default. There is no longer any supported means to generate a module that has a function with multiple returns. Related changes: * rust-lang/rust#127605 * rust-lang/rust#127613 * rust-lang/rust#127654 * rust-lang/rust#128141 * llvm/llvm-project#98933 Fixes rust-lang/rust#121444. Fixes rust-lang/rust#128212.
Since LLVM 19, it is necessary to set not only module flags, but also function attributes for branch protection on aarch64. See llvm/llvm-project@e15d67c for the relevant LLVM change.
Fixes #127829.