Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
ee1e7a9
Introduce `aarch64-unknown-linux-pauthtest`
jchlanda Apr 3, 2026
742cdcd
Define ptrauth call operand bundle and introduce `const_ptr_aut`
jchlanda Apr 3, 2026
e46ab11
New signature of `get_fn_addr`, teach Rust how to sign fn pointers
jchlanda Apr 3, 2026
10df079
Add pauth attributes and first IR tests
jchlanda Apr 3, 2026
f044445
Add support for init/fini signing and correctly sign extern weak globals
jchlanda Apr 3, 2026
01b7c77
Teach bootstrap how to build rust_test_helpers for pauthtest
jchlanda Apr 3, 2026
043f275
For pauthtest landing pads must be signed
jchlanda Apr 3, 2026
800956e
TODO: Add to ATTRIBUTES
jchlanda Apr 9, 2026
9ae2f17
TODO: Add to INTRODUCE
jchlanda Apr 9, 2026
dcf75a8
TODO: Add to TEACH BOOTSTRAP
jchlanda Apr 9, 2026
cfd34be
TODO: Add to ATTRIBUTES
jchlanda Apr 9, 2026
21295d1
TODO: Add to INTRODUCE
jchlanda Apr 9, 2026
e09063b
TODO: Add to INTRODUCE
jchlanda Apr 9, 2026
3fb6822
TODO: Add to INTRODUCE - platform support
jchlanda Apr 9, 2026
6f6e0ca
TODO: Add to ATTRIBUTES
jchlanda Apr 10, 2026
8ab66df
TODO: Add to INIT/FINI
jchlanda Apr 10, 2026
33680a2
TODO: Add to TEACH BOOTSTRAP
jchlanda Apr 10, 2026
58c8451
TODO: Add to INTRODUCE - platform support
jchlanda Apr 10, 2026
1d9de6d
TODO: Add to INTRODUCE
jchlanda Apr 10, 2026
6f37ca7
TODO: Add to INIT/FINI
jchlanda Apr 10, 2026
52a74ef
TODO: Add to TEACH BOOTSTRAP
jchlanda Apr 10, 2026
604fb71
TODO: Add to TEACH BOOTSTRAP
jchlanda Apr 10, 2026
8a33b60
TODO: Add to INTRODUCE
jchlanda Apr 10, 2026
a6ada3d
TODO: Add to INTRODUCE
jchlanda Apr 10, 2026
37a5cd9
TODO: Add to ATTRIBUTES
jchlanda Apr 10, 2026
45ad45d
TODO: Add to INTRODUCE
jchlanda Apr 13, 2026
2c78c27
TODO: Add to INIT/FINI
jchlanda Apr 13, 2026
2a3e2d7
TODO: Add to TEACH BOOTSTRAP
jchlanda Apr 13, 2026
05206d7
TODO: Add to INIT/FINI
jchlanda Apr 14, 2026
d7a394c
TODO: Add to ATTRIBUTES
jchlanda Apr 14, 2026
5ed4b1f
TODO: Add to TEACH BOOTSTRAP
jchlanda Apr 14, 2026
4a17f9a
TODO: Add to INTRODUCE
jchlanda Apr 14, 2026
0606d27
TODO: Add to INIT/FINI
jchlanda Apr 14, 2026
e0f1994
TODO: Add to TEACH BOOTSTRAP
jchlanda Apr 14, 2026
1c84193
TODO: Add to TEACH BOOTSTRAP
jchlanda Apr 14, 2026
2d949b2
TODO: Add to INTRODUCE
jchlanda Apr 14, 2026
d4eb772
TODO: Add to TEACH BOOTSTRAP
jchlanda Apr 14, 2026
9f2ac73
TODO: Add to INTRODUCE
jchlanda Apr 14, 2026
d550d39
TODO: Add to INTRODUCE
jchlanda Apr 14, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions compiler/rustc_codegen_gcc/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use gccjit::{LValue, RValue, ToRValue, Type};
use rustc_abi::Primitive::Pointer;
use rustc_abi::{self as abi, HasDataLayout};
use rustc_codegen_ssa::traits::{
BaseTypeCodegenMethods, ConstCodegenMethods, MiscCodegenMethods, StaticCodegenMethods,
BaseTypeCodegenMethods, ConstCodegenMethods, MiscCodegenMethods, PacMetadata,
StaticCodegenMethods,
};
use rustc_middle::mir::Mutability;
use rustc_middle::mir::interpret::{GlobalAlloc, PointerArithmetic, Scalar};
Expand Down Expand Up @@ -229,7 +230,13 @@ impl<'gcc, 'tcx> ConstCodegenMethods for CodegenCx<'gcc, 'tcx> {
None
}

fn scalar_to_backend(&self, cv: Scalar, layout: abi::Scalar, ty: Type<'gcc>) -> RValue<'gcc> {
fn scalar_to_backend_with_pac(
&self,
cv: Scalar,
layout: abi::Scalar,
ty: Type<'gcc>,
_pac: Option<PacMetadata>,
) -> RValue<'gcc> {
let bitsize = if layout.is_bool() { 1 } else { layout.size(self).bits() };
match cv {
Scalar::Int(int) => {
Expand Down Expand Up @@ -278,7 +285,7 @@ impl<'gcc, 'tcx> ConstCodegenMethods for CodegenCx<'gcc, 'tcx> {
}
value
}
GlobalAlloc::Function { instance, .. } => self.get_fn_addr(instance),
GlobalAlloc::Function { instance, .. } => self.get_fn_addr(instance, None),
GlobalAlloc::VTable(ty, dyn_ty) => {
let alloc = self
.tcx
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_codegen_gcc/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use gccjit::{
use rustc_abi::{Align, HasDataLayout, PointeeInfo, Size, TargetDataLayout, VariantIdx};
use rustc_codegen_ssa::base::wants_msvc_seh;
use rustc_codegen_ssa::errors as ssa_errors;
use rustc_codegen_ssa::traits::{BackendTypes, BaseTypeCodegenMethods, MiscCodegenMethods};
use rustc_codegen_ssa::traits::{
BackendTypes, BaseTypeCodegenMethods, MiscCodegenMethods, PacMetadata,
};
use rustc_data_structures::base_n::{ALPHANUMERIC_ONLY, ToBaseN};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_middle::mir::interpret::Allocation;
Expand Down Expand Up @@ -403,7 +405,7 @@ impl<'gcc, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
get_fn(self, instance)
}

fn get_fn_addr(&self, instance: Instance<'tcx>) -> RValue<'gcc> {
fn get_fn_addr(&self, instance: Instance<'tcx>, _pac: Option<PacMetadata>) -> RValue<'gcc> {
let func_name = self.tcx.symbol_name(instance).name;

let func = if let Some(variable) = self.get_declared_value(func_name) {
Expand Down
9 changes: 8 additions & 1 deletion compiler/rustc_codegen_llvm/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ use rustc_middle::ty::{self, TyCtxt};
use rustc_session::config::{BranchProtection, FunctionReturn, OptLevel, PAuthKey, PacRet};
use rustc_span::sym;
use rustc_symbol_mangling::mangle_internal_symbol;
use rustc_target::spec::{Arch, FramePointer, SanitizerSet, StackProbeType, StackProtector};
use rustc_target::spec::{Arch, Env, FramePointer, SanitizerSet, StackProbeType, StackProtector};
use smallvec::SmallVec;

use crate::common::pauth_fn_attrs;
use crate::context::SimpleCx;
use crate::errors::{PackedStackBackchainNeedsSoftfloat, SanitizerMemtagRequiresMte};
use crate::llvm::AttributePlace::Function;
Expand Down Expand Up @@ -605,6 +606,12 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
}
}

if sess.target.env == Env::Pauthtest {
Copy link
Copy Markdown

@asl asl Apr 9, 2026

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 Env checks 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 pauthtest in clang is an interim thing. How we can enable pauth on, say, bare-metal platform? Or on some downstream platform?

Copy link
Copy Markdown
Author

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 Env in codegen llvm, you are right that pauthtest is 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. Maybe Session would 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)?

for &ptrauth_attr in pauth_fn_attrs() {
to_add.push(llvm::CreateAttrString(cx.llcx, ptrauth_attr));
}
}

to_add.extend(target_features_attr(cx, tcx, function_features));

attributes::apply_to_llfn(llfn, Function, &to_add);
Expand Down
33 changes: 31 additions & 2 deletions compiler/rustc_codegen_llvm/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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
// 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);
}

Expand All @@ -140,6 +148,27 @@ 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 this flags now, since only a subset of pointer
// authentication features 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 when linking against
// fully PAuth-enabled C/C++ binaries.
//
// Link to PAuth core info:
// <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() {
Expand Down
52 changes: 50 additions & 2 deletions compiler/rustc_codegen_llvm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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};

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
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(
Expand Down
91 changes: 82 additions & 9 deletions compiler/rustc_codegen_llvm/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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_hasher::{HashStable, 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::Env;
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 `pauthtest`-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.env != Env::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;
}
// Only C ABI
let abi = cx.tcx.fn_sig(def_id).skip_binder().abi();
if !matches!(abi, ExternAbi::C { .. }) {
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".
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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),
Expand All @@ -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
Expand All @@ -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) => {
Expand Down
Loading
Loading