Skip to content

Commit

Permalink
Add pointer authentication intrinsics
Browse files Browse the repository at this point in the history
  • Loading branch information
arttet committed Dec 7, 2023
1 parent 17a520a commit 32d34b2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
8 changes: 8 additions & 0 deletions compiler/rustc_codegen_llvm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,14 @@ impl<'ll> CodegenCx<'ll, '_> {
ifn!("llvm.va_end", fn(ptr) -> void);
ifn!("llvm.va_copy", fn(ptr, ptr) -> void);

// pointer authentication intrinsics
ifn!("llvm.ptrauth.sign.i64", fn(t_i64, t_i32, t_i64) -> t_i64);
ifn!("llvm.ptrauth.auth.i64", fn(t_i64, t_i32, t_i64) -> t_i64);
ifn!("llvm.ptrauth.strip.i64", fn(t_i64, t_i32) -> t_i64);
ifn!("llvm.ptrauth.resign.i64", fn(t_i64, t_i32, t_i64, t_i32, t_i64) -> t_i64);
ifn!("llvm.ptrauth.sign_generic.i64", fn(t_i64, t_i64) -> t_i64);
ifn!("llvm.ptrauth.blend.i64", fn(t_i64, t_i64) -> t_i64);

if self.sess().instrument_coverage() {
ifn!("llvm.instrprof.increment", fn(ptr, t_i64, t_i32, t_i32) -> void);
}
Expand Down
18 changes: 18 additions & 0 deletions compiler/rustc_codegen_llvm/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,24 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
}
}

sym::ptrauth_sign_key_asia
| sym::ptrauth_sign_key_asib
| sym::ptrauth_sign_key_asda
| sym::ptrauth_sign_key_asdb => {
let key = self.const_i32(match name {
sym::ptrauth_sign_key_asia => 0,
sym::ptrauth_sign_key_asib => 1,
sym::ptrauth_sign_key_asda => 2,
sym::ptrauth_sign_key_asdb => 3,
_ => bug!("Unknown PAuth key"),
});

let value = args[0].immediate();
let discriminator = args[1].immediate();

self.call_intrinsic("llvm.ptrauth.sign.i64", &[value, key, discriminator])
}

sym::raw_eq => {
use abi::Abi::*;
let tp_ty = fn_args.type_at(0);
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_hir_analysis/src/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,11 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
None => bug!("`va_list` language item needed for C-variadic intrinsics"),
},

ptrauth_sign_key_asda
| ptrauth_sign_key_asdb
| ptrauth_sign_key_asia
| ptrauth_sign_key_asib => (0, vec![tcx.types.i64, tcx.types.i64], tcx.types.i64),

sym::nontemporal_store => {
(1, vec![Ty::new_mut_ptr(tcx, param(0)), param(0)], Ty::new_unit(tcx))
}
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,10 @@ symbols! {
ptr_write_bytes,
ptr_write_unaligned,
ptr_write_volatile,
ptrauth_sign_key_asda,
ptrauth_sign_key_asdb,
ptrauth_sign_key_asia,
ptrauth_sign_key_asib,
pub_macro_rules,
pub_restricted,
public,
Expand Down
6 changes: 6 additions & 0 deletions tests/codegen/intrinsics/ptrauth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// compile-flags: -O -C no-prepopulate-passes

#![crate_type = "lib"]
#![feature(core_intrinsics)]

// TODO: Add tests

0 comments on commit 32d34b2

Please sign in to comment.