Skip to content

Commit 809c245

Browse files
committed
Convert -Zpointer-authentication to target modifier
Also improve the API design of pointer_authentication_functions, by making it return Option<&PointerAuthSchema>, rather than bool.
1 parent 95508ff commit 809c245

17 files changed

Lines changed: 141 additions & 104 deletions

File tree

compiler/rustc_codegen_llvm/src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ pub(crate) fn compile_codegen_unit(
165165
if cfg.elf_got {
166166
cx.add_ptrauth_elf_got_flag();
167167
}
168-
if cx.sess().pointer_authentication_functions() {
168+
if cx.sess().pointer_authentication_functions().is_some() {
169169
cx.add_ptrauth_sign_personality_flag();
170170
}
171171
}

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2040,7 +2040,7 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
20402040
llfn: &'ll Value,
20412041
fn_abi: Option<&FnAbi<'tcx, Ty<'tcx>>>,
20422042
) -> Option<llvm::OperandBundleBox<'ll>> {
2043-
if !self.sess().pointer_authentication_functions() {
2043+
if self.sess().pointer_authentication_functions().is_none() {
20442044
return None;
20452045
}
20462046
// Pointer authentication support is currently limited to extern "C" calls; filter out other

compiler/rustc_codegen_llvm/src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub(crate) fn maybe_sign_fn_ptr<'ll, 'tcx>(
3232
llfn: &'ll llvm::Value,
3333
schema: &PointerAuthSchema,
3434
) -> &'ll llvm::Value {
35-
if !cx.tcx.sess.pointer_authentication_functions() {
35+
if cx.tcx.sess.pointer_authentication_functions().is_none() {
3636
return llfn;
3737
}
3838

compiler/rustc_codegen_llvm/src/consts.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -123,26 +123,8 @@ pub(crate) fn const_alloc_to_llvm<'ll>(
123123
let address_space = cx.tcx.global_alloc(prov.alloc_id()).address_space(cx);
124124
let schema = if cx.sess().pointer_authentication() {
125125
match is_init_fini {
126-
IsInitOrFini::Yes => {
127-
if cx.sess().pointer_authentication_init_fini() {
128-
cx.sess()
129-
.pointer_auth_config
130-
.as_ref()
131-
.and_then(|cfg| cfg.init_fini.as_ref())
132-
} else {
133-
None
134-
}
135-
}
136-
IsInitOrFini::No => {
137-
if cx.sess().pointer_authentication_functions() {
138-
cx.sess()
139-
.pointer_auth_config
140-
.as_ref()
141-
.and_then(|cfg| cfg.function_pointers.as_ref())
142-
} else {
143-
None
144-
}
145-
}
126+
IsInitOrFini::Yes => cx.sess().pointer_authentication_init_fini(),
127+
IsInitOrFini::No => cx.sess().pointer_authentication_functions(),
146128
}
147129
} else {
148130
None

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -981,10 +981,7 @@ impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
981981
ty::List::empty(),
982982
DUMMY_SP,
983983
),
984-
tcx.sess
985-
.pointer_auth_config
986-
.as_ref()
987-
.and_then(|cfg| cfg.function_pointers.as_ref()),
984+
tcx.sess.pointer_authentication_functions(),
988985
),
989986
_ => {
990987
let name = name.unwrap_or("rust_eh_personality");

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -493,10 +493,7 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
493493
return None;
494494
}
495495

496-
let main_llfn = cx.get_fn_addr(
497-
instance,
498-
cx.sess().pointer_auth_config.as_ref().and_then(|cfg| cfg.function_pointers.as_ref()),
499-
);
496+
let main_llfn = cx.get_fn_addr(instance, cx.sess().pointer_authentication_functions());
500497

501498
let entry_fn = create_entry_fn::<Bx>(cx, main_llfn, main_def_id, entry_type);
502499
return Some(entry_fn);
@@ -557,13 +554,8 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
557554
cx.tcx().mk_args(&[main_ret_ty.into()]),
558555
DUMMY_SP,
559556
);
560-
let start_fn = cx.get_fn_addr(
561-
start_instance,
562-
cx.sess()
563-
.pointer_auth_config
564-
.as_ref()
565-
.and_then(|cfg| cfg.function_pointers.as_ref()),
566-
);
557+
let start_fn =
558+
cx.get_fn_addr(start_instance, cx.sess().pointer_authentication_functions());
567559

568560
let i8_ty = cx.type_i8();
569561
let arg_sigpipe = bx.const_u8(sigpipe);

compiler/rustc_codegen_ssa/src/common.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,7 @@ pub(crate) fn build_langcall<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
119119
let instance = ty::Instance::mono(tcx, def_id);
120120
(
121121
bx.fn_abi_of_instance(instance, ty::List::empty()),
122-
bx.get_fn_addr(
123-
instance,
124-
tcx.sess.pointer_auth_config.as_ref().and_then(|cfg| cfg.function_pointers.as_ref()),
125-
),
122+
bx.get_fn_addr(instance, tcx.sess.pointer_authentication_functions()),
126123
instance,
127124
)
128125
}

compiler/rustc_codegen_ssa/src/mir/block.rs

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -686,13 +686,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
686686
}
687687
_ => (
688688
false,
689-
bx.get_fn_addr(
690-
drop_fn,
691-
bx.sess()
692-
.pointer_auth_config
693-
.as_ref()
694-
.and_then(|cfg| cfg.function_pointers.as_ref()),
695-
),
689+
bx.get_fn_addr(drop_fn, bx.sess().pointer_authentication_functions()),
696690
bx.fn_abi_of_instance(drop_fn, ty::List::empty()),
697691
drop_fn,
698692
),
@@ -1110,15 +1104,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
11101104

11111105
(
11121106
None,
1113-
Some(
1114-
bx.get_fn_addr(
1115-
instance,
1116-
bx.sess()
1117-
.pointer_auth_config
1118-
.as_ref()
1119-
.and_then(|cfg| cfg.function_pointers.as_ref()),
1120-
),
1121-
),
1107+
Some(bx.get_fn_addr(
1108+
instance,
1109+
bx.sess().pointer_authentication_functions(),
1110+
)),
11221111
)
11231112
}
11241113
_ => (Some(instance), None),
@@ -1432,13 +1421,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
14321421
}
14331422

14341423
let fn_ptr = match (instance, llfn) {
1435-
(Some(instance), None) => bx.get_fn_addr(
1436-
instance,
1437-
bx.sess()
1438-
.pointer_auth_config
1439-
.as_ref()
1440-
.and_then(|cfg| cfg.function_pointers.as_ref()),
1441-
),
1424+
(Some(instance), None) => {
1425+
bx.get_fn_addr(instance, bx.sess().pointer_authentication_functions())
1426+
}
14421427
(_, Some(llfn)) => llfn,
14431428
_ => span_bug!(fn_span, "no instance or llfn for call"),
14441429
};

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -440,10 +440,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
440440
OperandValue::Immediate(
441441
bx.get_fn_addr(
442442
instance,
443-
bx.sess()
444-
.pointer_auth_config
445-
.as_ref()
446-
.and_then(|cfg| cfg.function_pointers.as_ref()),
443+
bx.sess().pointer_authentication_functions(),
447444
),
448445
)
449446
}
@@ -462,10 +459,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
462459
OperandValue::Immediate(
463460
bx.cx().get_fn_addr(
464461
instance,
465-
bx.sess()
466-
.pointer_auth_config
467-
.as_ref()
468-
.and_then(|cfg| cfg.function_pointers.as_ref()),
462+
bx.sess().pointer_authentication_functions(),
469463
),
470464
)
471465
}
@@ -687,13 +681,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
687681
def: ty::InstanceKind::Shim(ty::ShimKind::ThreadLocal(def_id)),
688682
args: ty::GenericArgs::empty(),
689683
};
690-
let fn_ptr = bx.get_fn_addr(
691-
instance,
692-
bx.sess()
693-
.pointer_auth_config
694-
.as_ref()
695-
.and_then(|cfg| cfg.function_pointers.as_ref()),
696-
);
684+
let fn_ptr =
685+
bx.get_fn_addr(instance, bx.sess().pointer_authentication_functions());
697686
let fn_abi = bx.fn_abi_of_instance(instance, ty::List::empty());
698687
let fn_ty = bx.fn_decl_backend_type(fn_abi);
699688
let fn_attrs = if bx.tcx().def_kind(instance.def_id()).has_codegen_attrs() {

compiler/rustc_session/src/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1602,11 +1602,12 @@ pub struct BranchProtection {
16021602
pub gcs: bool,
16031603
}
16041604

1605-
#[derive(Clone, Copy, Hash, Debug, PartialEq)]
1605+
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)]
16061606
pub enum PointerAuthOption {
16071607
// See <compiler/rustc_session/src/options.rs> and Clang's command line reference:
16081608
// <https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fptrauth-auth-traps>
16091609
// for the origin and meaning of the enum values.
1610+
// tidy-alphabetical-start
16101611
Aarch64JumpTableHardening,
16111612
AuthTraps,
16121613
Calls,
@@ -1620,6 +1621,7 @@ pub enum PointerAuthOption {
16201621
TypeInfoVTPtrDisc,
16211622
VTPtrAddrDisc,
16221623
VTPtrTypeDisc,
1624+
// tidy-alphabetical-end
16231625
}
16241626
impl PointerAuthOption {
16251627
pub fn parse(s: &str) -> Option<Self> {

0 commit comments

Comments
 (0)