-
-
Notifications
You must be signed in to change notification settings - Fork 15.1k
Introduce aarch64-unknown-linux-pauthtest target #155722
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
base: main
Are you sure you want to change the base?
Changes from all commits
3112620
38cd6c1
5396ebb
5ae4549
c728ee5
8a044d9
5733b13
df73acf
6da24b0
b9c1650
1fc3327
c117a49
2db4afd
b0e8e26
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 |
|---|---|---|
|
|
@@ -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, HasTargetSpec, LlvmAbi, 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, | ||
|
|
@@ -1406,6 +1411,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, | ||
|
|
@@ -1836,6 +1846,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, | ||
|
|
@@ -1955,6 +1970,40 @@ 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.llvm_abiname != LlvmAbi::Pauthtest { | ||
| return None; | ||
| } | ||
| // Pointer authentication support is currently limited to extern "C" calls; filter out other | ||
| // ABIs. | ||
| if fn_abi?.conv != CanonAbi::C { | ||
|
Contributor
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. Nit: Also
Contributor
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. Also, should this test for
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. It should not. This prevents us from picking up on personality functions that form part of function definitions, for instance: pub extern "C" fn may_unwind(x: i32) -> i32 {
// I will panic here!
}Is compiled down to: define i32 @may_unwind(i32 %x) unnamed_addr #2 personality ptr @rust_eh_personality {And the check makes sure that we do not generate a pathological code, with personality function signed. Incorrect function definition: define i32 @may_unwind(i32 %x) unnamed_addr #2 personality ptr ptrauth (ptr @rust_eh_personality, i32 0) { |
||
| 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( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,23 +4,81 @@ use std::borrow::Borrow; | |
|
|
||
| use libc::{c_char, c_uint}; | ||
| use rustc_abi::Primitive::Pointer; | ||
| use rustc_abi::{self as abi, HasDataLayout as _}; | ||
| use rustc_abi::{self as abi, ExternAbi, HasDataLayout as _}; | ||
| use rustc_ast::Mutability; | ||
| use rustc_codegen_ssa::common::TypeKind; | ||
| use rustc_codegen_ssa::traits::*; | ||
| use rustc_data_structures::stable_hash::{StableHash, StableHasher}; | ||
| use rustc_hashes::Hash128; | ||
| use rustc_hir::def::DefKind; | ||
| use rustc_hir::def_id::DefId; | ||
| use rustc_middle::bug; | ||
| use rustc_middle::mir::interpret::{GlobalAlloc, PointerArithmetic, Scalar}; | ||
| use rustc_middle::ty::TyCtxt; | ||
| use rustc_middle::ty::{Instance, TyCtxt}; | ||
| use rustc_session::cstore::DllImport; | ||
| use rustc_target::spec::LlvmAbi; | ||
| use tracing::debug; | ||
|
|
||
| use crate::consts::const_alloc_to_llvm; | ||
| use crate::consts::{IsInitOrFini, IsStatic, const_alloc_to_llvm}; | ||
| pub(crate) use crate::context::CodegenCx; | ||
| use crate::context::{GenericCx, SCx}; | ||
| use crate::llvm::{self, BasicBlock, ConstantInt, FALSE, TRUE, ToLlvmBool, Type, Value}; | ||
| use crate::llvm::{ | ||
| self, BasicBlock, ConstantInt, FALSE, TRUE, ToLlvmBool, Type, Value, const_ptr_auth, | ||
| }; | ||
|
|
||
| #[inline] | ||
| pub(crate) fn pauth_fn_attrs() -> &'static [&'static str] { | ||
| // FIXME(jchlanda) This is not an exhaustive list of all `ptrauth`-related attributes, but only | ||
| // those currently supported. The list is expected to grow as additional functionality is | ||
| // implemented, particularly for C++ interoperability. | ||
| &[ | ||
| "aarch64-jump-table-hardening", | ||
| "ptrauth-indirect-gotos", | ||
| "ptrauth-calls", | ||
| "ptrauth-returns", | ||
| "ptrauth-auth-traps", | ||
| ] | ||
| } | ||
|
|
||
| pub(crate) fn maybe_sign_fn_ptr<'ll, 'tcx>( | ||
| cx: &CodegenCx<'ll, '_>, | ||
| instance: Instance<'tcx>, | ||
| llfn: &'ll llvm::Value, | ||
| pac: PacMetadata, | ||
| ) -> &'ll llvm::Value { | ||
| if cx.sess().target.llvm_abiname != LlvmAbi::Pauthtest { | ||
| return llfn; | ||
| } | ||
|
|
||
| // Only free functions or methods | ||
| let def_id = instance.def_id(); | ||
| if !matches!(cx.tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) { | ||
| return llfn; | ||
| } | ||
|
Comment on lines
+53
to
+57
Contributor
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. Question: What other
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. Yes, |
||
| // Only C ABI | ||
| let abi = cx.tcx.fn_sig(def_id).skip_binder().abi(); | ||
| if !matches!(abi, ExternAbi::C { .. } | ExternAbi::System { .. }) { | ||
| return llfn; | ||
| } | ||
| // Ignore LLVM intrinsics | ||
| if llvm::get_value_name(llfn).starts_with(b"llvm.") { | ||
| return llfn; | ||
| } | ||
| if Some(def_id) == cx.tcx.lang_items().eh_personality() { | ||
| return llfn; | ||
| } | ||
|
|
||
| let addr_diversity = match pac.addr_diversity { | ||
| AddressDiversity::None => None, | ||
| AddressDiversity::Real => Some(llfn), | ||
| AddressDiversity::Synthetic(val) => { | ||
| let llval = cx.const_u64(val); | ||
| let llty = cx.val_ty(llfn); | ||
| Some(unsafe { llvm::LLVMConstIntToPtr(llval, llty) }) | ||
| } | ||
| }; | ||
| const_ptr_auth(llfn, pac.key, pac.disc, addr_diversity) | ||
| } | ||
|
|
||
| /* | ||
| * A note on nomenclature of linking: "extern", "foreign", and "upcall". | ||
|
|
@@ -268,7 +326,13 @@ impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> { | |
| }) | ||
| } | ||
|
|
||
| fn scalar_to_backend(&self, cv: Scalar, layout: abi::Scalar, llty: &'ll Type) -> &'ll Value { | ||
| fn scalar_to_backend_with_pac( | ||
| &self, | ||
| cv: Scalar, | ||
| layout: abi::Scalar, | ||
| llty: &'ll Type, | ||
| pac: Option<PacMetadata>, | ||
| ) -> &'ll Value { | ||
| let bitsize = if layout.is_bool() { 1 } else { layout.size(self).bits() }; | ||
| match cv { | ||
| Scalar::Int(int) => { | ||
|
|
@@ -297,8 +361,12 @@ impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> { | |
| self.const_bitcast(llval, llty) | ||
| }; | ||
| } else { | ||
| let init = | ||
| const_alloc_to_llvm(self, alloc.inner(), /*static*/ false); | ||
| let init = const_alloc_to_llvm( | ||
| self, | ||
| alloc.inner(), | ||
| IsStatic::No, | ||
| IsInitOrFini::No, | ||
| ); | ||
| let alloc = alloc.inner(); | ||
| let value = match alloc.mutability { | ||
| Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None), | ||
|
|
@@ -319,7 +387,7 @@ impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> { | |
| value | ||
| } | ||
| } | ||
| GlobalAlloc::Function { instance, .. } => self.get_fn_addr(instance), | ||
| GlobalAlloc::Function { instance, .. } => self.get_fn_addr(instance, pac), | ||
| GlobalAlloc::VTable(ty, dyn_ty) => { | ||
| let alloc = self | ||
| .tcx | ||
|
|
@@ -330,7 +398,12 @@ impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> { | |
| }), | ||
| ))) | ||
| .unwrap_memory(); | ||
| let init = const_alloc_to_llvm(self, alloc.inner(), /*static*/ false); | ||
| let init = const_alloc_to_llvm( | ||
| self, | ||
| alloc.inner(), | ||
| IsStatic::No, | ||
| IsInitOrFini::No, | ||
| ); | ||
| self.static_addr_of_impl(init, alloc.inner().align, None) | ||
| } | ||
| GlobalAlloc::Static(def_id) => { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.