From f4a3339c97b44275d3f8df9495c2abc47d1923da Mon Sep 17 00:00:00 2001 From: Oskar Wirga Date: Wed, 11 Nov 2026 12:28:58 -0500 Subject: [PATCH 1/6] Enable pointer authentication for Apple arm64e targets Construct a Darwin configuration through the existing pointer-authentication machinery. Keep ELF module flags and init/fini schemas specific to ELF, and share the Apple arm64e target predicate with the existing Mach-O code. Add codegen coverage for the three Apple targets, ordinary arm64 and Windows arm64ec controls, and custom Mach-O initializer sections. --- compiler/rustc_codegen_llvm/src/base.rs | 6 ++- compiler/rustc_codegen_llvm/src/consts.rs | 6 ++- compiler/rustc_codegen_ssa/src/back/apple.rs | 3 +- .../rustc_codegen_ssa/src/back/metadata.rs | 7 +-- compiler/rustc_session/src/session.rs | 23 +++++++- .../rustc_target/src/spec/base/apple/mod.rs | 2 +- compiler/rustc_target/src/spec/mod.rs | 4 ++ .../codegen-llvm/pauth/pauth-arm64e-apple.rs | 52 +++++++++++++++++++ .../pauth/pauth-arm64e-disabled.rs | 28 ++++++++++ .../pauth/pauth-arm64e-init-fini.rs | 25 +++++++++ 10 files changed, 142 insertions(+), 14 deletions(-) create mode 100644 tests/codegen-llvm/pauth/pauth-arm64e-apple.rs create mode 100644 tests/codegen-llvm/pauth/pauth-arm64e-disabled.rs create mode 100644 tests/codegen-llvm/pauth/pauth-arm64e-init-fini.rs diff --git a/compiler/rustc_codegen_llvm/src/base.rs b/compiler/rustc_codegen_llvm/src/base.rs index 0b230f1608da1..b12aada8df867 100644 --- a/compiler/rustc_codegen_llvm/src/base.rs +++ b/compiler/rustc_codegen_llvm/src/base.rs @@ -25,7 +25,7 @@ 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::{LlvmAbi, SanitizerSet}; use crate::builder::Builder; use crate::builder::gpu_offload::OffloadGlobals; @@ -163,7 +163,9 @@ pub(crate) fn compile_codegen_unit( cx.add_objc_module_flags(); } - if cx.sess().pointer_authentication() { + if cx.sess().pointer_authentication() + && cx.sess().target.llvm_abiname == LlvmAbi::Pauthtest + { let cfg = cx.sess().pointer_auth_config.as_ref().unwrap(); let aarch64_elf_pauthabi_version = diff --git a/compiler/rustc_codegen_llvm/src/consts.rs b/compiler/rustc_codegen_llvm/src/consts.rs index 4952fdc732e9f..b421a2cd80961 100644 --- a/compiler/rustc_codegen_llvm/src/consts.rs +++ b/compiler/rustc_codegen_llvm/src/consts.rs @@ -16,7 +16,7 @@ use rustc_middle::mono::MonoItem; use rustc_middle::ty::layout::{HasTypingEnv, LayoutOf}; use rustc_middle::ty::{self, Instance}; use rustc_span::{Symbol, bug, span_bug}; -use rustc_target::spec::Arch; +use rustc_target::spec::{Arch, BinaryFormat}; use tracing::{debug, instrument, trace}; use crate::common::CodegenCx; @@ -166,7 +166,9 @@ fn codegen_static_initializer<'ll, 'tcx>( .link_section .map(|link_section| { let s = link_section.as_str(); - if s.starts_with(".init_array") || s.starts_with(".fini_array") { + if cx.sess().target.binary_format == BinaryFormat::Elf + && (s.starts_with(".init_array") || s.starts_with(".fini_array")) + { IsInitOrFini::Yes } else { IsInitOrFini::No diff --git a/compiler/rustc_codegen_ssa/src/back/apple.rs b/compiler/rustc_codegen_ssa/src/back/apple.rs index 104a3de012615..3eacbd2a942d1 100644 --- a/compiler/rustc_codegen_ssa/src/back/apple.rs +++ b/compiler/rustc_codegen_ssa/src/back/apple.rs @@ -85,8 +85,7 @@ pub(super) fn add_data_and_relocation( target: &Target, kind: SymbolExportKind, ) -> object::write::Result<()> { - let authenticated_pointer = - kind == SymbolExportKind::Text && target.llvm_target.starts_with("arm64e"); + let authenticated_pointer = kind == SymbolExportKind::Text && target.is_arm64e_apple(); let data: &[u8] = match target.pointer_width { _ if authenticated_pointer => &[0, 0, 0, 0, 0, 0, 0, 0x80], diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index 45dcde58dd524..6d0f8c6b9fd14 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -212,7 +212,7 @@ pub(crate) fn create_object_file(sess: &Session) -> Option object::write::MachO build_version } -/// Is Apple's CPU subtype `arm64e`s -fn macho_is_arm64e(target: &Target) -> bool { - target.llvm_target.starts_with("arm64e") -} - pub(crate) enum MetadataPosition { First, Last, diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index f829877d95857..90eba319862ef 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -123,7 +123,7 @@ pub struct PointerAuthSchema { } impl PointerAuthSchema { pub fn function_pointers_default(target: &Target) -> Self { - assert!(target.cfg_abi == CfgAbi::Pauthtest); + assert!(target.cfg_abi == CfgAbi::Pauthtest || target.is_arm64e_apple()); return Self { is_address_discriminated: PointerAuthAddressDiscriminator::HardwareAddress(false), discrimination_kind: PointerAuthDiscrimination::None, @@ -167,6 +167,24 @@ pub struct PointerAuthConfig { pub vt_ptr_type_discrimination: bool, } impl PointerAuthConfig { + fn arm64e_apple(target: &Target) -> Self { + assert!(target.is_arm64e_apple()); + Self { + return_addresses: true, + auth_traps: true, + indirect_gotos: true, + elf_got: false, + aarch64_jump_table_hardening: true, + // C function pointers must use Clang's IA/zero convention for interoperability. + function_pointers: Some(PointerAuthSchema::function_pointers_default(target)), + init_fini: None, + intrinsics: true, + typeinfo_vt_ptr_discrimination: false, + vt_ptr_addr_discrimination: false, + vt_ptr_type_discrimination: false, + } + } + fn default(target: &Target) -> Self { assert!(target.cfg_abi == CfgAbi::Pauthtest); return Self { @@ -229,6 +247,9 @@ impl PointerAuthConfig { pauth_abi_version } pub fn from_raw(raw: &[(PointerAuthOption, bool)], target: &Target) -> Option { + if target.is_arm64e_apple() { + return Some(Self::arm64e_apple(target)); + } if target.cfg_abi != CfgAbi::Pauthtest { return None; } diff --git a/compiler/rustc_target/src/spec/base/apple/mod.rs b/compiler/rustc_target/src/spec/base/apple/mod.rs index d1d91cbd37884..dfda1341e9864 100644 --- a/compiler/rustc_target/src/spec/base/apple/mod.rs +++ b/compiler/rustc_target/src/spec/base/apple/mod.rs @@ -332,7 +332,7 @@ impl OSVersion { (Os::MacOs, crate::spec::Arch::AArch64, _) => (11, 0, 0), (Os::IOs, crate::spec::Arch::AArch64, Env::MacAbi) => (14, 0, 0), (Os::IOs, crate::spec::Arch::AArch64, Env::Sim) => (14, 0, 0), - (Os::IOs, _, _) if target.llvm_target.starts_with("arm64e") => (14, 0, 0), + (Os::IOs, _, _) if target.is_arm64e_apple() => (14, 0, 0), // Mac Catalyst defaults to 13.1 in Clang. (Os::IOs, _, Env::MacAbi) => (13, 1, 0), (Os::TvOs, crate::spec::Arch::AArch64, Env::Sim) => (14, 0, 0), diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index 476970b10ea3a..d5852f056477f 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -2875,6 +2875,10 @@ impl DerefMut for Target { } impl Target { + pub fn is_arm64e_apple(&self) -> bool { + self.is_like_darwin && self.llvm_target.starts_with("arm64e-") + } + pub fn is_abi_supported(&self, abi: ExternAbi) -> bool { let abi_map = AbiMap::from_target(self); abi_map.canonize_abi(abi, false).is_mapped() diff --git a/tests/codegen-llvm/pauth/pauth-arm64e-apple.rs b/tests/codegen-llvm/pauth/pauth-arm64e-apple.rs new file mode 100644 index 0000000000000..9a94caaf22b58 --- /dev/null +++ b/tests/codegen-llvm/pauth/pauth-arm64e-apple.rs @@ -0,0 +1,52 @@ +//@ add-minicore +//@ needs-llvm-components: aarch64 +//@ revisions: IOS DARWIN TVOS +//@ compile-flags: -Copt-level=0 -Cpanic=abort +//@ [IOS] compile-flags: --target=arm64e-apple-ios +//@ [DARWIN] compile-flags: --target=arm64e-apple-darwin +//@ [TVOS] compile-flags: --target=arm64e-apple-tvos +//@ filecheck-flags: --implicit-check-not=aarch64-elf-pauthabi +//@ filecheck-flags: --implicit-check-not=ptrauth-elf-got +//@ filecheck-flags: --implicit-check-not=ptrauth-sign-personality + +#![feature(no_core)] +#![no_core] +#![crate_type = "lib"] + +extern crate minicore; + +extern "C" { + fn external_c(); +} + +// CHECK: @C_POINTER = {{.*}}ptr ptrauth (ptr @external_c, i32 0) +#[no_mangle] +pub static C_POINTER: unsafe extern "C" fn() = external_c; + +// CHECK-LABEL: define {{.*}} @call_c( +// CHECK-SAME: #[[ATTR:[0-9]+]] +// CHECK: call void %{{.*}}(){{.*}}[ "ptrauth"(i32 0, i64 0) ] +#[no_mangle] +pub unsafe extern "C" fn call_c(f: unsafe extern "C" fn()) { + f(); +} + +// CHECK-LABEL: define {{.*}} @call_system( +// CHECK: call void %{{.*}}(){{.*}}[ "ptrauth"(i32 0, i64 0) ] +#[no_mangle] +pub unsafe fn call_system(f: unsafe extern "system" fn()) { + f(); +} + +// CHECK-LABEL: define {{.*}} @take_c( +// CHECK: ret ptr ptrauth (ptr @external_c, i32 0) +#[no_mangle] +pub fn take_c() -> unsafe extern "C" fn() { + external_c +} + +// CHECK: attributes #[[ATTR]] = { {{.*}}"aarch64-jump-table-hardening" +// CHECK-SAME: "ptrauth-auth-traps" +// CHECK-SAME: "ptrauth-calls" +// CHECK-SAME: "ptrauth-indirect-gotos" +// CHECK-SAME: "ptrauth-returns" diff --git a/tests/codegen-llvm/pauth/pauth-arm64e-disabled.rs b/tests/codegen-llvm/pauth/pauth-arm64e-disabled.rs new file mode 100644 index 0000000000000..cf344c6e04ac1 --- /dev/null +++ b/tests/codegen-llvm/pauth/pauth-arm64e-disabled.rs @@ -0,0 +1,28 @@ +//@ add-minicore +//@ needs-llvm-components: aarch64 +//@ revisions: ARM64 ARM64EC +//@ compile-flags: -Copt-level=0 -Cpanic=abort +//@ [ARM64] compile-flags: --target=aarch64-apple-darwin +//@ [ARM64EC] compile-flags: --target=arm64ec-pc-windows-msvc +//@ filecheck-flags: --implicit-check-not=ptrauth + +#![feature(no_core)] +#![no_core] +#![crate_type = "lib"] + +extern crate minicore; + +extern "C" { + fn external_c(); +} + +// CHECK: @C_POINTER = {{.*}}ptr @external_c +#[no_mangle] +pub static C_POINTER: unsafe extern "C" fn() = external_c; + +// CHECK-LABEL: define {{.*}} @call_c( +// CHECK: call void %{{.*}}() +#[no_mangle] +pub unsafe extern "C" fn call_c(f: unsafe extern "C" fn()) { + f(); +} diff --git a/tests/codegen-llvm/pauth/pauth-arm64e-init-fini.rs b/tests/codegen-llvm/pauth/pauth-arm64e-init-fini.rs new file mode 100644 index 0000000000000..899cbbcbcb2cb --- /dev/null +++ b/tests/codegen-llvm/pauth/pauth-arm64e-init-fini.rs @@ -0,0 +1,25 @@ +//@ add-minicore +//@ needs-llvm-components: aarch64 +//@ compile-flags: --target=arm64e-apple-ios -Copt-level=0 -Cpanic=abort + +#![feature(no_core)] +#![no_core] +#![crate_type = "lib"] + +extern crate minicore; + +extern "C" { + fn external_c(); +} + +// CHECK-DAG: @CUSTOM_FINI = {{.*}}ptr ptrauth (ptr @external_c, i32 0), section ".fini_array,__data" +#[used] +#[no_mangle] +#[link_section = ".fini_array,__data"] +pub static CUSTOM_FINI: unsafe extern "C" fn() = external_c; + +// CHECK-DAG: @CUSTOM_INIT = {{.*}}ptr ptrauth (ptr @external_c, i32 0), section ".init_array,__data" +#[used] +#[no_mangle] +#[link_section = ".init_array,__data"] +pub static CUSTOM_INIT: unsafe extern "C" fn() = external_c; From 99bfa311c0d8ab86de05c5d5b01df41d86145c82 Mon Sep 17 00:00:00 2001 From: Oskar Wirga Date: Wed, 11 Nov 2026 12:32:58 -0500 Subject: [PATCH 2/6] Authenticate Rust function pointers on Apple arm64e Apply the Apple function-pointer schema to Rust calling conventions as well as C/System. Keep address signing and call authentication paired for closures, trait-object vtables, static tables and weak imports. Bare function symbols and LLVM intrinsics remain outside the indirect-call path. Extend minicore mutable-reference dispatch support and add codegen tests for Rust function pointers, closure shims, dyn Fn, trait objects and weak imports. --- compiler/rustc_codegen_llvm/src/builder.rs | 9 ++- compiler/rustc_codegen_llvm/src/common.rs | 18 +++-- compiler/rustc_codegen_llvm/src/consts.rs | 3 +- compiler/rustc_session/src/session.rs | 8 ++ tests/auxiliary/minicore.rs | 1 + .../pauth/pauth-arm64e-extern-weak.rs | 29 +++++++ .../pauth/pauth-arm64e-rust-abi.rs | 81 +++++++++++++++++++ 7 files changed, 137 insertions(+), 12 deletions(-) create mode 100644 tests/codegen-llvm/pauth/pauth-arm64e-extern-weak.rs create mode 100644 tests/codegen-llvm/pauth/pauth-arm64e-rust-abi.rs diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs index 32e9578355976..3f3233214605b 100644 --- a/compiler/rustc_codegen_llvm/src/builder.rs +++ b/compiler/rustc_codegen_llvm/src/builder.rs @@ -2156,9 +2156,12 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> { ) -> Option> { let schema = self.sess().pointer_authentication_functions()?; - // Pointer authentication support is currently limited to extern "C" calls; filter out other - // ABIs. - if fn_abi?.conv != CanonAbi::C { + if self.sess().pointer_authentication_covers_all_abis() { + // A bare function symbol was never signed, so authenticating it would be invalid. + if !unsafe { llvm::LLVMRustIsNonGVFunctionPointerTy(llfn) } { + return None; + } + } else if fn_abi?.conv != CanonAbi::C { return None; } // Filter out LLVM intrinsics. diff --git a/compiler/rustc_codegen_llvm/src/common.rs b/compiler/rustc_codegen_llvm/src/common.rs index 7e47267a29f8b..5499265a259fa 100644 --- a/compiler/rustc_codegen_llvm/src/common.rs +++ b/compiler/rustc_codegen_llvm/src/common.rs @@ -35,15 +35,17 @@ pub(crate) fn maybe_sign_fn_ptr<'ll, 'tcx>( 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; - } - // Only C ABI - let abi = cx.tcx.fn_sig(def_id).skip_binder().abi(); - if !matches!(abi, ExternAbi::C { .. } | ExternAbi::System { .. }) { - return llfn; + if !cx.sess().pointer_authentication_covers_all_abis() { + // Only free functions or methods + if !matches!(cx.tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) { + return llfn; + } + // 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.") { diff --git a/compiler/rustc_codegen_llvm/src/consts.rs b/compiler/rustc_codegen_llvm/src/consts.rs index b421a2cd80961..10cbd170d018f 100644 --- a/compiler/rustc_codegen_llvm/src/consts.rs +++ b/compiler/rustc_codegen_llvm/src/consts.rs @@ -214,7 +214,8 @@ fn check_and_apply_linkage<'ll, 'tcx>( let fn_abi = cx.fn_abi_of_fn_ptr(fn_sig, ty::List::empty()); // Decide if the initializer needs to be signed if cx.sess().pointer_authentication_functions().is_some() - && matches!(fn_sig.abi(), ExternAbi::C { .. } | ExternAbi::System { .. }) + && (cx.sess().pointer_authentication_covers_all_abis() + || matches!(fn_sig.abi(), ExternAbi::C { .. } | ExternAbi::System { .. })) { should_sign = true; } diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 90eba319862ef..9d068c2d51ca1 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -156,6 +156,8 @@ pub struct PointerAuthConfig { pub aarch64_jump_table_hardening: bool, /// The ABI for C function pointers. pub function_pointers: Option, + /// Whether the function-pointer schema also covers Rust calling conventions. + pub covers_all_abis: bool, /// The ABI for function addresses in .init_array and .fini_array pub init_fini: Option, /// Use of pointer authentication intrinsics. @@ -177,6 +179,7 @@ impl PointerAuthConfig { aarch64_jump_table_hardening: true, // C function pointers must use Clang's IA/zero convention for interoperability. function_pointers: Some(PointerAuthSchema::function_pointers_default(target)), + covers_all_abis: true, init_fini: None, intrinsics: true, typeinfo_vt_ptr_discrimination: false, @@ -194,6 +197,7 @@ impl PointerAuthConfig { elf_got: false, aarch64_jump_table_hardening: true, function_pointers: Some(PointerAuthSchema::function_pointers_default(target)), + covers_all_abis: false, init_fini: Some(PointerAuthSchema::init_fini_default(target)), intrinsics: true, typeinfo_vt_ptr_discrimination: true, @@ -1303,6 +1307,10 @@ impl Session { self.pointer_auth_config.as_ref().and_then(|cfg| cfg.function_pointers.as_ref()) } + pub fn pointer_authentication_covers_all_abis(&self) -> bool { + self.pointer_auth_config.as_ref().is_some_and(|cfg| cfg.covers_all_abis) + } + pub fn pointer_authentication_init_fini(&self) -> Option<&PointerAuthSchema> { self.pointer_auth_config.as_ref().and_then(|cfg| cfg.init_fini.as_ref()) } diff --git a/tests/auxiliary/minicore.rs b/tests/auxiliary/minicore.rs index aca69642057ab..857d9493e0a07 100644 --- a/tests/auxiliary/minicore.rs +++ b/tests/auxiliary/minicore.rs @@ -391,6 +391,7 @@ pub trait Fn: FnMut { trait DispatchFromDyn {} impl<'a, T: PointeeSized + Unsize, U: PointeeSized> DispatchFromDyn<&'a U> for &'a T {} +impl<'a, T: PointeeSized + Unsize, U: PointeeSized> DispatchFromDyn<&'a mut U> for &'a mut T {} #[lang = "unsize"] trait Unsize: PointeeSized {} diff --git a/tests/codegen-llvm/pauth/pauth-arm64e-extern-weak.rs b/tests/codegen-llvm/pauth/pauth-arm64e-extern-weak.rs new file mode 100644 index 0000000000000..51313d9f2d65a --- /dev/null +++ b/tests/codegen-llvm/pauth/pauth-arm64e-extern-weak.rs @@ -0,0 +1,29 @@ +//@ add-minicore +//@ needs-llvm-components: aarch64 +//@ compile-flags: --target=arm64e-apple-ios -Copt-level=0 -Cpanic=abort + +#![feature(no_core, lang_items, linkage)] +#![no_core] +#![crate_type = "lib"] + +extern crate minicore; +use minicore::*; + +#[lang = "Option"] +pub enum WeakOption { + None, + Some(T), +} + +extern "Rust" { + #[linkage = "extern_weak"] + static weak_rust: WeakOption; +} + +// CHECK: @_rust_extern_with_linkage_{{[0-9a-f]+}}_weak_rust = internal {{.*}}global ptr ptrauth (ptr @weak_rust, i32 0) +// CHECK-LABEL: define {{.*}} @weak_address( +// CHECK: ret ptr @_rust_extern_with_linkage_{{[0-9a-f]+}}_weak_rust +#[no_mangle] +pub unsafe fn weak_address() -> *const WeakOption { + &raw const weak_rust +} diff --git a/tests/codegen-llvm/pauth/pauth-arm64e-rust-abi.rs b/tests/codegen-llvm/pauth/pauth-arm64e-rust-abi.rs new file mode 100644 index 0000000000000..cfca4baf3ea3a --- /dev/null +++ b/tests/codegen-llvm/pauth/pauth-arm64e-rust-abi.rs @@ -0,0 +1,81 @@ +//@ add-minicore +//@ needs-llvm-components: aarch64 +//@ compile-flags: --target=arm64e-apple-ios -Copt-level=0 -Cpanic=abort + +#![feature(no_core, unboxed_closures)] +#![no_core] +#![crate_type = "lib"] + +extern crate minicore; +use minicore::*; + +// CHECK-DAG: @RUST_POINTER = {{.*}}ptr ptrauth (ptr @rust_target, i32 0) +// CHECK-DAG: @RUST_TABLE = {{.*}}ptr ptrauth (ptr @rust_target, i32 0), ptr ptrauth (ptr @rust_other, i32 0) +// CHECK-DAG: @[[VTABLE:vtable\.[0-9]+]] = {{.*}}ptr ptrauth (ptr @[[GET:[^, )]+]], i32 0) +#[no_mangle] +pub static RUST_POINTER: fn() -> usize = rust_target; + +#[no_mangle] +pub static RUST_TABLE: [fn() -> usize; 2] = [rust_target, rust_other]; + +pub trait Getter { + fn get(&self) -> usize; +} + +impl Getter for usize { + fn get(&self) -> usize { + *self + } +} + +// CHECK-LABEL: define {{.*}} @call_dyn_fn( +// CHECK: call i64 %{{.*}}({{.*}}){{.*}}[ "ptrauth"(i32 0, i64 0) ] +#[no_mangle] +pub fn call_dyn_fn(f: &dyn Fn() -> usize) -> usize { + f() +} + +// CHECK-LABEL: define {{.*}} @call_dyn_trait( +// CHECK: call i64 %{{.*}}({{.*}}){{.*}}[ "ptrauth"(i32 0, i64 0) ] +#[no_mangle] +pub fn call_dyn_trait(f: &dyn Getter) -> usize { + f.get() +} + +// CHECK-LABEL: define {{.*}} @call_rust( +// CHECK: call i64 %{{.*}}(){{.*}}[ "ptrauth"(i32 0, i64 0) ] +#[no_mangle] +pub fn call_rust(f: fn() -> usize) -> usize { + f() +} + +// CHECK-LABEL: define {{.*}} @make_dyn_trait( +// CHECK: insertvalue { ptr, ptr } {{.*}}, ptr @[[VTABLE]], 1 +#[no_mangle] +pub fn make_dyn_trait(value: &usize) -> &dyn Getter { + value +} + +#[no_mangle] +pub fn rust_other() -> usize { + 9 +} + +#[no_mangle] +pub fn rust_target() -> usize { + 7 +} + +// CHECK-LABEL: define {{.*}} @take_closure( +// CHECK: ret ptr ptrauth (ptr @{{[^, )]+}}, i32 0) +#[no_mangle] +pub fn take_closure() -> fn() -> usize { + || 11 +} + +// CHECK-LABEL: define {{.*}} @take_rust( +// CHECK: ret ptr ptrauth (ptr @rust_target, i32 0) +#[no_mangle] +pub fn take_rust() -> fn() -> usize { + rust_target +} From 552fd6f42fe2837ad9ec8131fbb7dafe106ea68b Mon Sep 17 00:00:00 2001 From: Oskar Wirga Date: Wed, 11 Nov 2026 12:35:02 -0500 Subject: [PATCH 3/6] Sign function pointers materialized from MIR constants Pass the configured function-pointer schema through immediate scalar constants and scalar reads from indirect constant allocations. Function provenance is signed consistently with reification and static allocations; data pointers remain unchanged. Cover scalar and scalar-pair constants on Apple arm64e and ELF pauthtest. --- compiler/rustc_codegen_ssa/src/mir/operand.rs | 14 ++++- .../pauth/pauth-const-fn-pointer.rs | 61 +++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 tests/codegen-llvm/pauth/pauth-const-fn-pointer.rs diff --git a/compiler/rustc_codegen_ssa/src/mir/operand.rs b/compiler/rustc_codegen_ssa/src/mir/operand.rs index 7164bf51e7049..86c39f667d017 100644 --- a/compiler/rustc_codegen_ssa/src/mir/operand.rs +++ b/compiler/rustc_codegen_ssa/src/mir/operand.rs @@ -182,7 +182,12 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> { let BackendRepr::Scalar(scalar) = layout.backend_repr else { bug!("from_const: invalid ByVal layout: {:#?}", layout); }; - let llval = bx.scalar_to_backend(x, scalar, bx.immediate_backend_type(layout)); + let llval = bx.scalar_to_backend_with_pac( + x, + scalar, + bx.immediate_backend_type(layout), + bx.sess().pointer_authentication_functions(), + ); OperandValue::Immediate(llval) } ConstValue::ZeroSized => return OperandRef::zero_sized(layout), @@ -225,7 +230,12 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> { alloc_range(start, size), /*read_provenance*/ matches!(s.primitive(), abi::Primitive::Pointer(_)), ) { - Ok(val) => bx.scalar_to_backend(val, s, ty), + Ok(val) => bx.scalar_to_backend_with_pac( + val, + s, + ty, + bx.sess().pointer_authentication_functions(), + ), Err(_) => bx.const_poison(ty), } }; diff --git a/tests/codegen-llvm/pauth/pauth-const-fn-pointer.rs b/tests/codegen-llvm/pauth/pauth-const-fn-pointer.rs new file mode 100644 index 0000000000000..59547059e2eb9 --- /dev/null +++ b/tests/codegen-llvm/pauth/pauth-const-fn-pointer.rs @@ -0,0 +1,61 @@ +//@ add-minicore +//@ needs-llvm-components: aarch64 +//@ revisions: ARM64E PAUTHTEST +//@ compile-flags: -Copt-level=0 -Cpanic=abort +//@ [ARM64E] compile-flags: --target=arm64e-apple-ios +//@ [PAUTHTEST] compile-flags: --target=aarch64-unknown-linux-pauthtest + +#![feature(no_core)] +#![no_core] +#![crate_type = "lib"] + +extern crate minicore; + +extern "C" { + fn callback(); + fn other_callback(); + static DATA: usize; +} + +type Callback = unsafe extern "C" fn(); +const CALLBACK: Callback = callback; +const CALLBACK_PAIR: (Callback, Callback) = (callback, other_callback); + +// CHECK: @STATIC_CALLBACK = {{.*}}ptr ptrauth (ptr @callback, i32 0) +#[no_mangle] +pub static STATIC_CALLBACK: Callback = callback; + +// CHECK-LABEL: define {{.*}} @const_as_raw( +// CHECK: ret ptr ptrauth (ptr @callback, i32 0) +#[no_mangle] +pub fn const_as_raw() -> *const () { + CALLBACK as *const () +} + +// CHECK-LABEL: define {{.*}} @get_const( +// CHECK: ret ptr ptrauth (ptr @callback, i32 0) +#[no_mangle] +pub fn get_const() -> Callback { + CALLBACK +} + +// CHECK-LABEL: define {{.*}} @get_data( +// CHECK: ret ptr @DATA +#[no_mangle] +pub unsafe fn get_data() -> *const usize { + &raw const DATA +} + +// CHECK-LABEL: define {{.*}} @get_pair( +// CHECK: ret { ptr, ptr } { ptr ptrauth (ptr @callback, i32 0), ptr ptrauth (ptr @other_callback, i32 0) } +#[no_mangle] +pub fn get_pair() -> (Callback, Callback) { + CALLBACK_PAIR +} + +// CHECK-LABEL: define {{.*}} @get_reified( +// CHECK: ret ptr ptrauth (ptr @callback, i32 0) +#[no_mangle] +pub fn get_reified() -> Callback { + callback +} From fe760f209bbbe7d3a8ca620acdc115293c8c0ca7 Mon Sep 17 00:00:00 2001 From: Oskar Wirga Date: Wed, 11 Nov 2026 12:37:00 -0500 Subject: [PATCH 4/6] Keep known arm64e callees on the direct-call path Request an unsigned address for known direct callees on Apple arm64e, while retaining the function-pointer schema for escaping addresses. This preserves direct calls and inlining opportunities without relying on LLVM to undo a signed constant callee. Keep pauthtest behavior unchanged. Check optimized and unoptimized direct calls and independently check their escaping addresses remain signed. --- compiler/rustc_codegen_llvm/src/builder.rs | 10 ++-- compiler/rustc_codegen_ssa/src/base.rs | 2 +- compiler/rustc_codegen_ssa/src/common.rs | 2 +- compiler/rustc_codegen_ssa/src/mir/block.rs | 6 +-- compiler/rustc_codegen_ssa/src/mir/rvalue.rs | 2 +- compiler/rustc_session/src/session.rs | 4 ++ .../pauth/pauth-arm64e-direct-calls.rs | 50 +++++++++++++++++++ 7 files changed, 63 insertions(+), 13 deletions(-) create mode 100644 tests/codegen-llvm/pauth/pauth-arm64e-direct-calls.rs diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs index 3f3233214605b..fd0daa6fd144f 100644 --- a/compiler/rustc_codegen_llvm/src/builder.rs +++ b/compiler/rustc_codegen_llvm/src/builder.rs @@ -2169,13 +2169,9 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> { 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: + // FIXME(jchlanda) The pauthtest ABI still signs known direct callees in `get_fn_addr`, + // so those calls must also receive operand bundles. See + // . Some(llvm::OperandBundleBox::new( "ptrauth", &[ diff --git a/compiler/rustc_codegen_ssa/src/base.rs b/compiler/rustc_codegen_ssa/src/base.rs index 4361e192972f4..ed70f52c71fb1 100644 --- a/compiler/rustc_codegen_ssa/src/base.rs +++ b/compiler/rustc_codegen_ssa/src/base.rs @@ -576,7 +576,7 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( DUMMY_SP, ); let start_fn = - cx.get_fn_addr(start_instance, cx.sess().pointer_authentication_functions()); + cx.get_fn_addr(start_instance, cx.sess().pointer_authentication_direct_calls()); let i8_ty = cx.type_i8(); let arg_sigpipe = bx.const_u8(sigpipe); diff --git a/compiler/rustc_codegen_ssa/src/common.rs b/compiler/rustc_codegen_ssa/src/common.rs index b5f1fee9407f4..67625cd773c42 100644 --- a/compiler/rustc_codegen_ssa/src/common.rs +++ b/compiler/rustc_codegen_ssa/src/common.rs @@ -96,7 +96,7 @@ pub(crate) fn build_langcall<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( let instance = ty::Instance::mono(tcx, def_id); ( bx.fn_abi_of_instance(instance, ty::List::empty()), - bx.get_fn_addr(instance, tcx.sess.pointer_authentication_functions()), + bx.get_fn_addr(instance, tcx.sess.pointer_authentication_direct_calls()), instance, ) } diff --git a/compiler/rustc_codegen_ssa/src/mir/block.rs b/compiler/rustc_codegen_ssa/src/mir/block.rs index f5a6575e3c848..ae73628571c9b 100644 --- a/compiler/rustc_codegen_ssa/src/mir/block.rs +++ b/compiler/rustc_codegen_ssa/src/mir/block.rs @@ -714,7 +714,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { } _ => ( false, - bx.get_fn_addr(drop_fn, bx.sess().pointer_authentication_functions()), + bx.get_fn_addr(drop_fn, bx.sess().pointer_authentication_direct_calls()), bx.fn_abi_of_instance(drop_fn, ty::List::empty()), drop_fn, ), @@ -1138,7 +1138,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { None, Some(bx.get_fn_addr( instance, - bx.sess().pointer_authentication_functions(), + bx.sess().pointer_authentication_direct_calls(), )), ) } @@ -1465,7 +1465,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { let fn_ptr = match (instance, llfn) { (Some(instance), None) => { - bx.get_fn_addr(instance, bx.sess().pointer_authentication_functions()) + bx.get_fn_addr(instance, bx.sess().pointer_authentication_direct_calls()) } (_, Some(llfn)) => llfn, _ => span_bug!(fn_span, "no instance or llfn for call"), diff --git a/compiler/rustc_codegen_ssa/src/mir/rvalue.rs b/compiler/rustc_codegen_ssa/src/mir/rvalue.rs index 642ee3badbf29..496875aa64268 100644 --- a/compiler/rustc_codegen_ssa/src/mir/rvalue.rs +++ b/compiler/rustc_codegen_ssa/src/mir/rvalue.rs @@ -762,7 +762,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { args: ty::GenericArgs::empty(), }; let fn_ptr = - bx.get_fn_addr(instance, bx.sess().pointer_authentication_functions()); + bx.get_fn_addr(instance, bx.sess().pointer_authentication_direct_calls()); let fn_abi = bx.fn_abi_of_instance(instance, ty::List::empty()); let fn_ty = bx.fn_decl_backend_type(fn_abi); let fn_attrs = if bx.tcx().def_kind(instance.def_id()).has_codegen_attrs() { diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 9d068c2d51ca1..44fa3c895b73e 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -1311,6 +1311,10 @@ impl Session { self.pointer_auth_config.as_ref().is_some_and(|cfg| cfg.covers_all_abis) } + pub fn pointer_authentication_direct_calls(&self) -> Option<&PointerAuthSchema> { + if self.target.is_arm64e_apple() { None } else { self.pointer_authentication_functions() } + } + pub fn pointer_authentication_init_fini(&self) -> Option<&PointerAuthSchema> { self.pointer_auth_config.as_ref().and_then(|cfg| cfg.init_fini.as_ref()) } diff --git a/tests/codegen-llvm/pauth/pauth-arm64e-direct-calls.rs b/tests/codegen-llvm/pauth/pauth-arm64e-direct-calls.rs new file mode 100644 index 0000000000000..ef61e0efb8045 --- /dev/null +++ b/tests/codegen-llvm/pauth/pauth-arm64e-direct-calls.rs @@ -0,0 +1,50 @@ +//@ add-minicore +//@ needs-llvm-components: aarch64 +//@ revisions: O0 O3 +//@ compile-flags: --target=arm64e-apple-ios -Cpanic=abort +//@ [O0] compile-flags: -Copt-level=0 +//@ [O3] compile-flags: -Copt-level=3 + +#![feature(no_core)] +#![no_core] +#![crate_type = "lib"] + +extern crate minicore; + +extern "C" { + fn external_c(); +} + +extern "Rust" { + fn external_rust(); +} + +// CHECK-LABEL: define {{.*}} @direct_c( +// CHECK: call void @external_c() +// CHECK: ret void +#[no_mangle] +pub unsafe fn direct_c() { + external_c(); +} + +// CHECK-LABEL: define {{.*}} @direct_rust( +// CHECK: call void @external_rust() +// CHECK: ret void +#[no_mangle] +pub unsafe fn direct_rust() { + external_rust(); +} + +// CHECK-LABEL: define {{.*}} @take_c( +// CHECK: ret ptr ptrauth (ptr @external_c, i32 0) +#[no_mangle] +pub fn take_c() -> unsafe extern "C" fn() { + external_c +} + +// CHECK-LABEL: define {{.*}} @take_rust( +// CHECK: ret ptr ptrauth (ptr @external_rust, i32 0) +#[no_mangle] +pub fn take_rust() -> unsafe fn() { + external_rust +} From 321652b26662f16d36aa50248e06ef2bf95ba715 Mon Sep 17 00:00:00 2001 From: Oskar Wirga Date: Wed, 11 Nov 2026 12:39:50 -0500 Subject: [PATCH 5/6] Emit the Apple arm64e pointer-authentication ABI version --- compiler/rustc_codegen_llvm/src/context.rs | 14 +++++++++++ compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 8 +++++++ compiler/rustc_codegen_llvm/src/llvm/mod.rs | 11 +++++++++ .../rustc_llvm/llvm-wrapper/RustWrapper.cpp | 7 ++++++ .../pauth/pauth-arm64e-abi-version.rs | 23 +++++++++++++++++++ 5 files changed, 63 insertions(+) create mode 100644 tests/codegen-llvm/pauth/pauth-arm64e-abi-version.rs diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs index 56f0be1a89e2a..2bfdcb308bc1d 100644 --- a/compiler/rustc_codegen_llvm/src/context.rs +++ b/compiler/rustc_codegen_llvm/src/context.rs @@ -449,6 +449,20 @@ fn init_module<'ll, 'tcx>(tcx: TyCtxt<'tcx>, llcx: llvm::Context<'ll>, llmod: ll } } + if sess.target.is_arm64e_apple() { + // Match Clang's user-space ABI version so Apple-derived LLVMs emit versioned objects. + let version = cx.md_node_in_context(&[ + llvm::LLVMValueAsMetadata(cx.get_const_i32(0)), + llvm::LLVMValueAsMetadata(cx.get_const_int(cx.type_i1(), 0)), + ]); + llvm::add_module_flag_metadata( + llmod, + llvm::ModuleFlagMergeBehavior::AppendUnique, + "ptrauth.abi-version", + cx.md_node_in_context(&[version]), + ); + } + // Pass on the control-flow protection flags to LLVM (equivalent to `-fcf-protection` in Clang). if let CFProtection::Branch | CFProtection::Full = sess.opts.unstable_opts.cf_protection { llvm::add_module_flag_u32( diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index c53dfe6fe5d05..5bdfbc8f1e0b4 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -2280,6 +2280,14 @@ unsafe extern "C" { ValueLen: size_t, ); + pub(crate) fn LLVMRustAddModuleFlagMetadata<'ll>( + M: Module<'ll>, + MergeBehavior: ModuleFlagMergeBehavior, + Name: *const c_uchar, // See "PTR_LEN_STR". + NameLen: size_t, + Value: &'ll Metadata, + ); + /// We can't use LLVM-C's `LLVMDIBuilderCreateCompileUnit` because it hardcodes /// `DICompileUnit::DebugNameTableKind::Default`, but we want to be able to /// pass other values. diff --git a/compiler/rustc_codegen_llvm/src/llvm/mod.rs b/compiler/rustc_codegen_llvm/src/llvm/mod.rs index c662ad09f49d7..007449d883194 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/mod.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/mod.rs @@ -467,6 +467,17 @@ pub(crate) fn add_module_flag_str<'ll>( } } +pub(crate) fn add_module_flag_metadata<'ll>( + module: Module<'ll>, + merge_behavior: ModuleFlagMergeBehavior, + key: &str, + value: &'ll Metadata, +) { + unsafe { + LLVMRustAddModuleFlagMetadata(module, merge_behavior, key.as_ptr(), key.len(), value); + } +} + pub(crate) fn set_dllimport_storage_class<'ll>(v: Value<'ll>) { unsafe { LLVMSetDLLStorageClass(v, DLLStorageClass::DllImport); diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp index d723d8aa97167..2f1c7fc933610 100644 --- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp @@ -941,6 +941,13 @@ extern "C" void LLVMRustAddModuleFlagString( MDString::get(unwrap(M)->getContext(), StringRef(Value, ValueLen))); } +extern "C" void LLVMRustAddModuleFlagMetadata( + LLVMModuleRef M, LLVMRustModuleFlagMergeBehavior MergeBehavior, + const char *Name, size_t NameLen, LLVMMetadataRef Value) { + unwrap(M)->addModuleFlag(fromRust(MergeBehavior), StringRef(Name, NameLen), + unwrap(Value)); +} + extern "C" LLVMValueRef LLVMRustGetLastInstruction(LLVMBasicBlockRef BB) { auto Point = unwrap(BB)->rbegin(); if (Point != unwrap(BB)->rend()) diff --git a/tests/codegen-llvm/pauth/pauth-arm64e-abi-version.rs b/tests/codegen-llvm/pauth/pauth-arm64e-abi-version.rs new file mode 100644 index 0000000000000..4b96f08d2067f --- /dev/null +++ b/tests/codegen-llvm/pauth/pauth-arm64e-abi-version.rs @@ -0,0 +1,23 @@ +//@ add-minicore +//@ needs-llvm-components: aarch64 +//@ revisions: ARM64E ARM64 PAUTHTEST +//@ compile-flags: -Copt-level=0 -Cpanic=abort +//@ [ARM64E] compile-flags: --target=arm64e-apple-ios +//@ [ARM64] compile-flags: --target=aarch64-apple-ios +//@ [PAUTHTEST] compile-flags: --target=aarch64-unknown-linux-pauthtest + +#![feature(no_core)] +#![no_core] +#![crate_type = "lib"] + +extern crate minicore; + +// CHECK-LABEL: define {{.*}} @abi_version( +#[no_mangle] +pub extern "C" fn abi_version() {} + +// ARM64E: !{{[0-9]+}} = !{i32 6, !"ptrauth.abi-version", ![[VERSIONS:[0-9]+]]} +// ARM64E: ![[VERSIONS]] = !{![[VERSION:[0-9]+]]} +// ARM64E: ![[VERSION]] = !{i32 0, i1 false} +// ARM64-NOT: ptrauth.abi-version +// PAUTHTEST-NOT: ptrauth.abi-version From 958e808fb9dde942d032fc755f0169ef37bce1ae Mon Sep 17 00:00:00 2001 From: Oskar Wirga Date: Wed, 11 Nov 2026 12:48:09 -0500 Subject: [PATCH 6/6] Exercise Apple arm64e pointer authentication end to end --- .../src/directives/directive_names.rs | 1 + tests/assembly-llvm/pauth-arm64e.rs | 52 +++++++++ .../pauth/pauth-arm64e-special-funcs.rs | 17 +++ tests/run-make/pauth-arm64e/helper.c | 21 ++++ tests/run-make/pauth-arm64e/main.rs | 100 ++++++++++++++++++ tests/run-make/pauth-arm64e/rmake.rs | 18 ++++ 6 files changed, 209 insertions(+) create mode 100644 tests/assembly-llvm/pauth-arm64e.rs create mode 100644 tests/codegen-llvm/pauth/pauth-arm64e-special-funcs.rs create mode 100644 tests/run-make/pauth-arm64e/helper.c create mode 100644 tests/run-make/pauth-arm64e/main.rs create mode 100644 tests/run-make/pauth-arm64e/rmake.rs diff --git a/src/tools/compiletest/src/directives/directive_names.rs b/src/tools/compiletest/src/directives/directive_names.rs index 7af285775380c..25bfe49fca87d 100644 --- a/src/tools/compiletest/src/directives/directive_names.rs +++ b/src/tools/compiletest/src/directives/directive_names.rs @@ -227,6 +227,7 @@ pub(crate) const KNOWN_DIRECTIVE_NAMES: &[&str] = &[ "only-aarch64-unknown-uefi", "only-apple", "only-arm", + "only-arm64e-apple-darwin", "only-arm64ec", "only-armv7-unknown-linux-gnueabihf", "only-avr", diff --git a/tests/assembly-llvm/pauth-arm64e.rs b/tests/assembly-llvm/pauth-arm64e.rs new file mode 100644 index 0000000000000..207ca1458d693 --- /dev/null +++ b/tests/assembly-llvm/pauth-arm64e.rs @@ -0,0 +1,52 @@ +//@ add-minicore +//@ assembly-output: emit-asm +//@ needs-llvm-components: aarch64 +//@ revisions: IOS DARWIN +//@ [IOS] compile-flags: --target=arm64e-apple-ios +//@ [DARWIN] compile-flags: --target=arm64e-apple-darwin +//@ compile-flags: -Copt-level=0 -Cpanic=abort + +#![feature(no_core, thread_local)] +#![no_core] +#![crate_type = "lib"] + +extern crate minicore; +use minicore::*; + +extern "C" { + fn external_target(value: usize) -> usize; + #[thread_local] + static EXTERNAL_TLS: usize; +} + +// CHECK-LABEL: _call_direct: +// CHECK: pacibsp +// CHECK: bl _external_target +// CHECK: retab +#[no_mangle] +pub unsafe extern "C" fn call_direct(value: usize) -> usize { + external_target(value) +} + +// CHECK-LABEL: _call_indirect: +// CHECK: pacibsp +// CHECK: blraaz x{{[0-9]+}} +// CHECK: retab +#[no_mangle] +pub extern "C" fn call_indirect(f: extern "C" fn(usize) -> usize, value: usize) -> usize { + f(value) +} + +// CHECK-LABEL: _load_tls: +// CHECK: pacibsp +// CHECK: blraaz x{{[0-9]+}} +// CHECK: retab +#[no_mangle] +pub unsafe extern "C" fn load_tls() -> usize { + EXTERNAL_TLS +} + +// CHECK-LABEL: _SIGNED_POINTER: +// CHECK: .quad _external_target@AUTH(ia,0) +#[no_mangle] +pub static SIGNED_POINTER: unsafe extern "C" fn(usize) -> usize = external_target; diff --git a/tests/codegen-llvm/pauth/pauth-arm64e-special-funcs.rs b/tests/codegen-llvm/pauth/pauth-arm64e-special-funcs.rs new file mode 100644 index 0000000000000..6c5b65ba9e2a1 --- /dev/null +++ b/tests/codegen-llvm/pauth/pauth-arm64e-special-funcs.rs @@ -0,0 +1,17 @@ +//@ only-arm64e-apple-darwin +//@ compile-flags: -Copt-level=0 + +// CHECK: define {{.*}} @__rust_try{{.*}} [[ATTR:#[0-9]+]] +// CHECK: invoke void %{{.*}}({{.*}}) [ "ptrauth"(i32 0, i64 0) ] +// CHECK: call void %{{.*}}({{.*}}) [ "ptrauth"(i32 0, i64 0) ] +// CHECK: define {{.*}} @main{{.*}} [[ATTR]] +// CHECK: call i64 @{{.*}}(ptr ptrauth (ptr @{{.*}}, i32 0), +// CHECK: attributes [[ATTR]] = { {{.*}}"aarch64-jump-table-hardening" +// CHECK-SAME: "ptrauth-auth-traps" +// CHECK-SAME: "ptrauth-calls" +// CHECK-SAME: "ptrauth-indirect-gotos" +// CHECK-SAME: "ptrauth-returns" +// CHECK-NOT: !"ptrauth-sign-personality" +fn main() { + let _ = std::panic::catch_unwind(|| panic!("BOOM")); +} diff --git a/tests/run-make/pauth-arm64e/helper.c b/tests/run-make/pauth-arm64e/helper.c new file mode 100644 index 0000000000000..c1b7190dc9a4a --- /dev/null +++ b/tests/run-make/pauth-arm64e/helper.c @@ -0,0 +1,21 @@ +#include + +#ifndef __arm64e__ +#error This test must compile its C half for arm64e +#endif + +typedef uintptr_t (*callback_t)(uintptr_t); + +_Thread_local uintptr_t c_tls = 37; + +uintptr_t c_target(uintptr_t value) { return value + 3; } + +callback_t c_get_callback(void) { return c_target; } + +uintptr_t c_call(callback_t callback, uintptr_t value) { + return callback(value); +} + +extern uintptr_t rust_c_target(uintptr_t); + +uintptr_t c_call_rust(uintptr_t value) { return rust_c_target(value); } diff --git a/tests/run-make/pauth-arm64e/main.rs b/tests/run-make/pauth-arm64e/main.rs new file mode 100644 index 0000000000000..0b1fb25d72ca1 --- /dev/null +++ b/tests/run-make/pauth-arm64e/main.rs @@ -0,0 +1,100 @@ +#![feature(linkage, thread_local)] + +use std::hint::black_box; +use std::sync::atomic::{AtomicUsize, Ordering}; + +extern "C" { + fn c_target(value: usize) -> usize; + fn c_get_callback() -> unsafe extern "C" fn(usize) -> usize; + fn c_call(callback: extern "C" fn(usize) -> usize, value: usize) -> usize; + fn c_call_rust(value: usize) -> usize; + #[thread_local] + static c_tls: usize; + #[linkage = "extern_weak"] + static arm64e_missing_function: Option; +} + +#[no_mangle] +#[inline(never)] +pub extern "C" fn rust_c_target(value: usize) -> usize { + value + 5 +} + +#[inline(never)] +fn rust_target(value: usize) -> usize { + value + 7 +} + +const C_POINTER: extern "C" fn(usize) -> usize = rust_c_target; +const RUST_POINTERS: (fn(usize) -> usize, fn(usize) -> usize) = (rust_target, |value| value + 11); +static STATIC_POINTER: fn(usize) -> usize = rust_target; +static INITIALIZED: AtomicUsize = AtomicUsize::new(0); + +extern "C" fn initialize() { + INITIALIZED.store(1, Ordering::Relaxed); +} + +#[used] +#[link_section = "__DATA,__mod_init_func"] +static INITIALIZER: extern "C" fn() = initialize; + +#[inline(never)] +fn const_c_pointer() -> extern "C" fn(usize) -> usize { + C_POINTER +} + +#[inline(never)] +fn const_rust_pointers() -> (fn(usize) -> usize, fn(usize) -> usize) { + RUST_POINTERS +} + +trait Getter { + fn get(&self) -> usize; +} + +static DROPS: AtomicUsize = AtomicUsize::new(0); + +struct Value(usize); + +impl Getter for Value { + fn get(&self) -> usize { + self.0 + } +} + +impl Drop for Value { + fn drop(&mut self) { + DROPS.fetch_add(1, Ordering::Relaxed); + } +} + +fn main() { + assert_eq!(INITIALIZED.load(Ordering::Relaxed), 1); + unsafe { + assert_eq!(c_target(black_box(2)), 5); + assert_eq!(black_box(c_get_callback())(4), 7); + assert_eq!(c_call(rust_c_target, 6), 11); + assert_eq!(c_call_rust(8), 13); + assert_eq!(black_box(c_tls), 37); + assert!(arm64e_missing_function.is_none()); + } + + assert_eq!(black_box(const_c_pointer())(10), 15); + let pair = black_box(const_rust_pointers()); + assert_eq!((pair.0)(12), 19); + assert_eq!((pair.1)(14), 25); + assert_eq!(black_box(STATIC_POINTER)(16), 23); + + let captured = black_box(19); + let closure = |value| value + captured; + let callable: &dyn Fn(usize) -> usize = black_box(&closure); + assert_eq!(callable(23), 42); + + let value: Box = black_box(Box::new(Value(29))); + assert_eq!(value.get(), 29); + drop(value); + assert_eq!(DROPS.load(Ordering::Relaxed), 1); + + assert_eq!(std::panic::catch_unwind(|| black_box(43)).unwrap(), 43); + println!("arm64e pointer authentication: PASS"); +} diff --git a/tests/run-make/pauth-arm64e/rmake.rs b/tests/run-make/pauth-arm64e/rmake.rs new file mode 100644 index 0000000000000..cb32f4d338985 --- /dev/null +++ b/tests/run-make/pauth-arm64e/rmake.rs @@ -0,0 +1,18 @@ +//@ only-arm64e-apple-darwin + +use run_make_support::{cc, run, rustc}; + +fn main() { + cc().arg("-c").arg("-O0").input("helper.c").output("helper.o").run(); + for opt in ["0", "3"] { + let exe = format!("arm64e-o{opt}"); + rustc() + .input("main.rs") + .opt_level(opt) + .arg("-Clink-arg=helper.o") + .arg("-Clink-arg=-Wl,-U,_arm64e_missing_function") + .output(&exe) + .run(); + run(&exe).assert_stdout_equals("arm64e pointer authentication: PASS\n"); + } +}