-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[WIP] Introduce aarch64-unknown-linux-pauthtest target #154759
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
Changes from 7 commits
528c831
7005959
154b381
666af81
b994701
0491274
e2a6ac3
6bf047f
ba2045e
e4537ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,12 +25,13 @@ use rustc_middle::mono::Visibility; | |
| use rustc_middle::ty::TyCtxt; | ||
| use rustc_session::config::{DebugInfo, Offload}; | ||
| use rustc_span::Symbol; | ||
| use rustc_target::spec::SanitizerSet; | ||
| use rustc_target::spec::{Env, SanitizerSet}; | ||
|
|
||
| use super::ModuleLlvm; | ||
| use crate::attributes; | ||
| use crate::builder::Builder; | ||
| use crate::builder::gpu_offload::OffloadGlobals; | ||
| use crate::common::pauth_fn_attrs; | ||
| use crate::context::CodegenCx; | ||
| use crate::llvm::{self, Value}; | ||
|
|
||
|
|
@@ -123,7 +124,14 @@ pub(crate) fn compile_codegen_unit( | |
| if let Some(entry) = | ||
| maybe_create_entry_wrapper::<Builder<'_, '_, '_>>(&cx, cx.codegen_unit) | ||
| { | ||
| let attrs = attributes::sanitize_attrs(&cx, tcx, SanitizerFnAttrs::default()); | ||
| let mut attrs = attributes::sanitize_attrs(&cx, tcx, SanitizerFnAttrs::default()); | ||
| // For pauthtest make sure that the ptrauth-* attributes are also attached to the | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not pauthtest-specific. This is generic pointer authentication code. I would rephrase the comment
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| // entry wrapper. | ||
| if cx.sess().target.env == Env::Pauthtest { | ||
| for &ptrauth_attr in pauth_fn_attrs() { | ||
| attrs.push(llvm::CreateAttrString(cx.llcx, ptrauth_attr)); | ||
| } | ||
| } | ||
| attributes::apply_to_llfn(entry, llvm::AttributePlace::Function, &attrs); | ||
| } | ||
|
|
||
|
|
@@ -140,6 +148,30 @@ pub(crate) fn compile_codegen_unit( | |
| cx.add_objc_module_flags(); | ||
| } | ||
|
|
||
| if cx.sess().target.env == Env::Pauthtest { | ||
| // FIXME(jchlanda): In LLVM/Clang, there are also `aarch64-elf-pauthabi-platform` | ||
| // and `aarch64-elf-pauthabi-version` module flags. These are emitted into the | ||
| // PAuth core info section of the resulting ELF, which the linker uses to enforce | ||
| // binary compatibility. | ||
| // | ||
| // We intentionally do not emit these flags now, since only a subset of features | ||
| // included in pauthtest ABI is currently supported. By default, the absence of | ||
| // this info is treated as compatible with any binary. | ||
| // | ||
| // Please note, that this would cause compatibility issues, specifically runtime | ||
| // crashes due to authentication failures (while compiling and linking | ||
| // successfully) when linking against binaries that support larger set of features | ||
| // (for example, signing of C++ member function pointers, virtual function | ||
| // pointers, virtual table pointers). | ||
| // | ||
| // Link to PAuth core info documentation: | ||
| // <https://github.com/ARM-software/abi-aa/blob/2025Q4/pauthabielf64/pauthabielf64.rst#core-information> | ||
| if cx.sess().opts.unstable_opts.ptrauth_elf_got { | ||
| cx.add_ptrauth_elf_got_flag(); | ||
| } | ||
| cx.add_ptrauth_sign_personality_flag(); | ||
| } | ||
|
|
||
| // Finalize code coverage by injecting the coverage map. Note, the coverage map will | ||
| // also be added to the `llvm.compiler.used` variable, created next. | ||
| if cx.sess().instrument_coverage() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ pub(crate) mod autodiff; | |
| pub(crate) mod gpu_offload; | ||
|
|
||
| use libc::{c_char, c_uint}; | ||
| use rustc_abi::{self as abi, Align, Size, WrappingRange}; | ||
| use rustc_abi::{self as abi, Align, CanonAbi, Size, WrappingRange}; | ||
| use rustc_codegen_ssa::MemFlags; | ||
| use rustc_codegen_ssa::common::{IntPredicate, RealPredicate, SynchronizationScope, TypeKind}; | ||
| use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue}; | ||
|
|
@@ -25,7 +25,7 @@ use rustc_sanitizers::{cfi, kcfi}; | |
| use rustc_session::config::OptLevel; | ||
| use rustc_span::Span; | ||
| use rustc_target::callconv::{FnAbi, PassMode}; | ||
| use rustc_target::spec::{Arch, HasTargetSpec, SanitizerSet, Target}; | ||
| use rustc_target::spec::{Arch, Env, HasTargetSpec, SanitizerSet, Target}; | ||
| use smallvec::SmallVec; | ||
| use tracing::{debug, instrument}; | ||
|
|
||
|
|
@@ -429,6 +429,11 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { | |
| bundles.push(kcfi_bundle); | ||
| } | ||
|
|
||
| let pauth = self.ptrauth_operand_bundle(llfn, fn_abi); | ||
| if let Some(p) = pauth.as_ref().map(|b| b.as_ref()) { | ||
| bundles.push(p); | ||
| } | ||
|
|
||
| let invoke = unsafe { | ||
| llvm::LLVMBuildInvokeWithOperandBundles( | ||
| self.llbuilder, | ||
|
|
@@ -1402,6 +1407,11 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { | |
| bundles.push(kcfi_bundle); | ||
| } | ||
|
|
||
| let pauth = self.ptrauth_operand_bundle(llfn, fn_abi); | ||
| if let Some(p) = pauth.as_ref().map(|b| b.as_ref()) { | ||
| bundles.push(p); | ||
| } | ||
|
|
||
| let call = unsafe { | ||
| llvm::LLVMBuildCallWithOperandBundles( | ||
| self.llbuilder, | ||
|
|
@@ -1849,6 +1859,11 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> { | |
| bundles.push(kcfi_bundle); | ||
| } | ||
|
|
||
| let pauth = self.ptrauth_operand_bundle(llfn, fn_abi); | ||
| if let Some(p) = pauth.as_ref().map(|b| b.as_ref()) { | ||
| bundles.push(p); | ||
| } | ||
|
|
||
| let callbr = unsafe { | ||
| llvm::LLVMBuildCallBr( | ||
| self.llbuilder, | ||
|
|
@@ -1968,6 +1983,39 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> { | |
| kcfi_bundle | ||
| } | ||
|
|
||
| // Emits pauth operand bundle. | ||
| fn ptrauth_operand_bundle( | ||
| &mut self, | ||
| llfn: &'ll Value, | ||
| fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>, | ||
| ) -> Option<llvm::OperandBundleBox<'ll>> { | ||
| if self.sess().target.env != Env::Pauthtest { | ||
| return None; | ||
| } | ||
| // Pauthtest only supports extern "C" calls, filter out other ABIs. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be generic pointer authentication
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| if fn_abi?.conv != CanonAbi::C { | ||
| return None; | ||
| } | ||
| // Filter out LLVM intrinsics. | ||
| if llvm::get_value_name(llfn).starts_with(b"llvm.") { | ||
| return None; | ||
| } | ||
|
|
||
| // FIXME(jchlanda) Operand bundles should only be attached to indirect function calls. | ||
| // However, function pointer signing is currently performed in `get_fn_addr`, which causes | ||
| // the logic to be applied too broadly, including to function values (not just pointers). | ||
| // As a result, direct calls using signed function values must also receive operand | ||
| // bundles. | ||
| // Once this is resolved, we should analyze each call and skip direct calls. See the | ||
| // discussion in the rust-lang issue: <https://github.com/rust-lang/rust/issues/152532> | ||
| let key: u32 = 0; | ||
| let discriminator: u64 = 0; | ||
| Some(llvm::OperandBundleBox::new( | ||
| "ptrauth", | ||
| &[self.const_u32(key), self.const_u64(discriminator)], | ||
| )) | ||
| } | ||
|
|
||
| /// Emits a call to `llvm.instrprof.increment`. Used by coverage instrumentation. | ||
| #[instrument(level = "debug", skip(self))] | ||
| pub(crate) fn instrprof_increment( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
View changes since the review
What I do not like is that these
Envchecks sprinkled over all codebase, this smells like a layering violation. For the context: in clang we're solving platform / environment specifics on the driver level and later one everything is clear: we're using language flags that are there regardless of the platform.Note that
pauthtestin clang is an interim thing. How we can enable pauth on, say, bare-metal platform? Or on some downstream platform?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.
While there is already code gated by
Envin codegen llvm, you are right thatpauthtestis an outlier, such that all its functionality is behind the environment checks. And yes, handling it earlier in the pipeline would make for a better design, one that decouples target/triple specifics from pauth logic. MaybeSessionwould be a good candidate to hold that info?This does sound like a follow up task, are you ok with me submitting a ticket (once this PR goes in)?
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.
Specifically for this bit, I suggested on libc that env could be set to
muslsince that's what most libraries will care about, and then use theTargetOptions.cfg_abifield for pauthtest. Which means that you cancfg(target_abi = "pauthtest")regardless of whether it's musl or bare metal.Either way, it could also be encapsulated in an
.should_emit_pauth() -> boolfunction that can do the relevant checks in one place.