Skip to content

Commit 984ca46

Browse files
Review comments
- Changed the separator from '+' to ','. - Moved the branch protection options from -C to -Z. - Additional test for incorrect branch-protection option. - Remove LLVM < 12 code. - Style fixes. Co-authored-by: James McGregor <[email protected]>
1 parent 837cc16 commit 984ca46

File tree

12 files changed

+57
-120
lines changed

12 files changed

+57
-120
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

+1-53
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hir::def_id::DefId;
99
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
1010
use rustc_middle::ty::layout::HasTyCtxt;
1111
use rustc_middle::ty::{self, TyCtxt};
12-
use rustc_session::config::{BranchProtection, OptLevel, PAuthKey};
12+
use rustc_session::config::OptLevel;
1313
use rustc_session::Session;
1414
use rustc_target::spec::abi::Abi;
1515
use rustc_target::spec::{FramePointer, SanitizerSet, StackProbeType, StackProtector};
@@ -203,58 +203,6 @@ pub fn non_lazy_bind(sess: &Session, llfn: &'ll Value) {
203203
}
204204
}
205205

206-
pub fn set_branch_protection(sess: &Session, llfn: &'ll Value) {
207-
// Setting PAC/BTI function attributes is only necessary for LLVM 11 and earlier.
208-
// For LLVM 12 and greater, module-level metadata attributes are set in
209-
// `compiler/rustc_codegen_llvm/src/context.rs`.
210-
if llvm_util::get_version() >= (12, 0, 0) {
211-
return;
212-
}
213-
214-
let BranchProtection { bti, pac_ret: pac } = sess.opts.cg.branch_protection;
215-
216-
if bti {
217-
llvm::AddFunctionAttrString(
218-
llfn,
219-
llvm::AttributePlace::Function,
220-
cstr!("branch-target-enforcement"),
221-
);
222-
}
223-
224-
if let Some(pac_opts) = pac {
225-
if pac_opts.leaf {
226-
llvm::AddFunctionAttrStringValue(
227-
llfn,
228-
llvm::AttributePlace::Function,
229-
cstr!("sign-return-address"),
230-
cstr!("non-leaf"),
231-
);
232-
} else {
233-
llvm::AddFunctionAttrStringValue(
234-
llfn,
235-
llvm::AttributePlace::Function,
236-
cstr!("sign-return-address"),
237-
cstr!("all"),
238-
);
239-
}
240-
241-
match pac_opts.key {
242-
PAuthKey::A => llvm::AddFunctionAttrStringValue(
243-
llfn,
244-
llvm::AttributePlace::Function,
245-
cstr!("sign-return-address-key"),
246-
cstr!("a_key"),
247-
),
248-
PAuthKey::B => llvm::AddFunctionAttrStringValue(
249-
llfn,
250-
llvm::AttributePlace::Function,
251-
cstr!("sign-return-address-key"),
252-
cstr!("b_key"),
253-
),
254-
}
255-
}
256-
}
257-
258206
pub(crate) fn default_optimisation_attrs(sess: &Session, llfn: &'ll Value) {
259207
match sess.opts.optimize {
260208
OptLevel::Size => {

compiler/rustc_codegen_llvm/src/context.rs

+19-23
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_middle::ty::layout::{
2121
};
2222
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
2323
use rustc_middle::{bug, span_bug};
24-
use rustc_session::config::{BranchProtection, CFGuard, CrateType, DebugInfo, PAuthKey};
24+
use rustc_session::config::{BranchProtection, CFGuard, CrateType, DebugInfo, PAuthKey, PacRet};
2525
use rustc_session::Session;
2626
use rustc_span::source_map::Span;
2727
use rustc_span::symbol::Symbol;
@@ -243,35 +243,31 @@ pub unsafe fn create_module(
243243
}
244244

245245
if sess.target.arch == "aarch64" {
246-
let BranchProtection { bti, pac_ret: pac } = sess.opts.cg.branch_protection;
246+
let BranchProtection { bti, pac_ret: pac } = sess.opts.debugging_opts.branch_protection;
247247

248248
llvm::LLVMRustAddModuleFlag(
249249
llmod,
250250
"branch-target-enforcement\0".as_ptr().cast(),
251251
bti.into(),
252252
);
253253

254-
if let Some(pac_opts) = pac {
255-
llvm::LLVMRustAddModuleFlag(llmod, "sign-return-address\0".as_ptr().cast(), 1);
256-
llvm::LLVMRustAddModuleFlag(
257-
llmod,
258-
"sign-return-address-all\0".as_ptr().cast(),
259-
pac_opts.leaf.into(),
260-
);
261-
llvm::LLVMRustAddModuleFlag(
262-
llmod,
263-
"sign-return-address-with-bkey\0".as_ptr().cast(),
264-
if pac_opts.key == PAuthKey::A { 0 } else { 1 },
265-
);
266-
} else {
267-
llvm::LLVMRustAddModuleFlag(llmod, "sign-return-address\0".as_ptr().cast(), 0);
268-
llvm::LLVMRustAddModuleFlag(llmod, "sign-return-address-all\0".as_ptr().cast(), 0);
269-
llvm::LLVMRustAddModuleFlag(
270-
llmod,
271-
"sign-return-address-with-bkey\0".as_ptr().cast(),
272-
0,
273-
);
274-
}
254+
llvm::LLVMRustAddModuleFlag(
255+
llmod,
256+
"sign-return-address\0".as_ptr().cast(),
257+
pac.is_some().into(),
258+
);
259+
let pac_opts = pac.unwrap_or(PacRet { leaf: false, key: PAuthKey::A });
260+
llvm::LLVMRustAddModuleFlag(
261+
llmod,
262+
"sign-return-address-all\0".as_ptr().cast(),
263+
pac_opts.leaf.into(),
264+
);
265+
let is_bkey = if pac_opts.key == PAuthKey::A { false } else { true };
266+
llvm::LLVMRustAddModuleFlag(
267+
llmod,
268+
"sign-return-address-with-bkey\0".as_ptr().cast(),
269+
is_bkey.into(),
270+
);
275271
}
276272

277273
llmod

compiler/rustc_codegen_llvm/src/declare.rs

-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ fn declare_raw_fn(
4545
llvm::Attribute::NoRedZone.apply_llfn(Function, llfn);
4646
}
4747

48-
if cx.tcx.sess.target.arch == "aarch64" {
49-
attributes::set_branch_protection(cx.tcx.sess, llfn);
50-
}
51-
5248
attributes::default_optimisation_attrs(cx.tcx.sess, llfn);
5349
attributes::non_lazy_bind(cx.sess(), llfn);
5450

compiler/rustc_interface/src/tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,6 @@ fn test_codegen_options_tracking_hash() {
567567

568568
// Make sure that changing a [TRACKED] option changes the hash.
569569
// This list is in alphabetical order.
570-
tracked!(
571-
branch_protection,
572-
BranchProtection { bti: true, pac_ret: Some(PacRet { leaf: true, key: PAuthKey::B }) }
573-
);
574570
tracked!(code_model, Some(CodeModel::Large));
575571
tracked!(control_flow_guard, CFGuard::Checks);
576572
tracked!(debug_assertions, Some(true));
@@ -723,6 +719,10 @@ fn test_debugging_options_tracking_hash() {
723719
tracked!(asm_comments, true);
724720
tracked!(assume_incomplete_release, true);
725721
tracked!(binary_dep_depinfo, true);
722+
tracked!(
723+
branch_protection,
724+
BranchProtection { bti: true, pac_ret: Some(PacRet { leaf: true, key: PAuthKey::B }) }
725+
);
726726
tracked!(chalk, true);
727727
tracked!(codegen_backend, Some("abc".to_string()));
728728
tracked!(crate_attr, vec!["abc".to_string()]);

compiler/rustc_session/src/options.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ mod desc {
390390
pub const parse_stack_protector: &str =
391391
"one of (`none` (default), `basic`, `strong`, or `all`)";
392392
pub const parse_branch_protection: &str =
393-
"a `+` separated combination of `bti`, `b-key`, `pac-ret`, or `leaf`";
393+
"a `,` separated combination of `bti`, `b-key`, `pac-ret`, or `leaf`";
394394
}
395395

396396
mod parse {
@@ -935,7 +935,7 @@ mod parse {
935935
crate fn parse_branch_protection(slot: &mut BranchProtection, v: Option<&str>) -> bool {
936936
match v {
937937
Some(s) => {
938-
for opt in s.split('+') {
938+
for opt in s.split(',') {
939939
match opt {
940940
"bti" => slot.bti = true,
941941
"pac-ret" if slot.pac_ret.is_none() => {
@@ -953,7 +953,6 @@ mod parse {
953953
};
954954
}
955955
}
956-
957956
_ => return false,
958957
}
959958
true
@@ -971,8 +970,6 @@ options! {
971970

972971
ar: String = (String::new(), parse_string, [UNTRACKED],
973972
"this option is deprecated and does nothing"),
974-
branch_protection: BranchProtection = (BranchProtection::default(), parse_branch_protection, [TRACKED],
975-
"set options for branch target identification and pointer authentication on AArch64"),
976973
code_model: Option<CodeModel> = (None, parse_code_model, [TRACKED],
977974
"choose the code model to use (`rustc --print code-models` for details)"),
978975
codegen_units: Option<usize> = (None, parse_opt_number, [UNTRACKED],
@@ -1101,6 +1098,8 @@ options! {
11011098
(default: no)"),
11021099
borrowck: String = ("migrate".to_string(), parse_string, [UNTRACKED],
11031100
"select which borrowck is used (`mir` or `migrate`) (default: `migrate`)"),
1101+
branch_protection: BranchProtection = (BranchProtection::default(), parse_branch_protection, [TRACKED],
1102+
"set options for branch target identification and pointer authentication on AArch64"),
11041103
cgu_partitioning_strategy: Option<String> = (None, parse_opt_string, [TRACKED],
11051104
"the codegen unit partitioning strategy to use"),
11061105
chalk: bool = (false, parse_bool, [TRACKED],

src/doc/rustc/src/codegen-options/index.md

-23
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,6 @@ a version of this list for your exact compiler by running `rustc -C help`.
77

88
This option is deprecated and does nothing.
99

10-
## branch-protection
11-
12-
This option lets you enable branch authentication instructions on AArch64.
13-
This option is ignored for non-AArch64 architectures.
14-
It takes some combination of the following values, separated by a `+`.
15-
16-
- `pac-ret` - Enable pointer authentication for non-leaf functions.
17-
- `leaf` - Enable pointer authentication for all functions, including leaf functions.
18-
- `b-key` - Sign return addresses with key B, instead of the default key A.
19-
- `bti` - Enable branch target identification.
20-
21-
`leaf` and `b-key` are only valid if `pac-ret` was previously specified.
22-
For example, `-C branch-protection=bti+pac-ret+leaf` is valid, but
23-
`-C branch-protection=bti+leaf+pac-ret` is not.
24-
25-
Repeated values are ignored.
26-
For example, `-C branch-protection=pac-ret+leaf+pac-ret` is equivalent to
27-
`-C branch-protection=pac-ret+leaf`.
28-
29-
Rust's standard library does not ship with BTI or pointer authentication enabled by default. \
30-
In Cargo projects the standard library can be recompiled with pointer authentication using the nightly
31-
[build-std](https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#build-std) feature.
32-
3310
## code-model
3411

3512
This option lets you choose which code model to use. \
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# `branch-protection`
2+
3+
This option lets you enable branch authentication instructions on AArch64.
4+
This option is ignored for non-AArch64 architectures.
5+
It takes some combination of the following values, separated by a `,`.
6+
7+
- `pac-ret` - Enable pointer authentication for non-leaf functions.
8+
- `leaf` - Enable pointer authentication for all functions, including leaf functions.
9+
- `b-key` - Sign return addresses with key B, instead of the default key A.
10+
- `bti` - Enable branch target identification.
11+
12+
`leaf` and `b-key` are only valid if `pac-ret` was previously specified.
13+
For example, `-Z branch-protection=bti,pac-ret,leaf` is valid, but
14+
`-Z branch-protection=bti,leaf,pac-ret` is not.
15+
16+
Rust's standard library does not ship with BTI or pointer authentication enabled by default.
17+
In Cargo projects the standard library can be recompiled with pointer authentication using the nightly
18+
[build-std](https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#build-std) feature.

src/test/assembly/aarch64-pointer-auth.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// min-llvm-version: 10.0.1
44
// assembly-output: emit-asm
55
// compile-flags: --target aarch64-unknown-linux-gnu
6-
// compile-flags: -C branch-protection=pac-ret+leaf
6+
// compile-flags: -Z branch-protection=pac-ret,leaf
77
// needs-llvm-components: aarch64
88

99
#![feature(no_core, lang_items)]

src/test/codegen/branch-protection.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
// revisions: bti pac-ret leaf b-key
44
// min-llvm-version: 12.0.0
55
// needs-llvm-components: aarch64
6-
// [bti] compile-flags: -C branch-protection=bti
7-
// [pac-ret] compile-flags: -C branch-protection=pac-ret
8-
// [leaf] compile-flags: -C branch-protection=pac-ret+leaf
9-
// [b-key] compile-flags: -C branch-protection=pac-ret+b-key
6+
// [bti] compile-flags: -Z branch-protection=bti
7+
// [pac-ret] compile-flags: -Z branch-protection=pac-ret
8+
// [leaf] compile-flags: -Z branch-protection=pac-ret,leaf
9+
// [b-key] compile-flags: -Z branch-protection=pac-ret,b-key
1010
// compile-flags: --target aarch64-unknown-linux-gnu
1111

1212
#![crate_type = "lib"]

src/test/run-make-fulldeps/pointer-auth-link-with-c/Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
all:
66
$(COMPILE_OBJ) $(TMPDIR)/test.o test.c
77
$(AR) rcs $(TMPDIR)/libtest.a $(TMPDIR)/test.o
8-
$(RUSTC) -C branch-protection=bti+pac-ret+leaf test.rs
8+
$(RUSTC) -Z branch-protection=bti,pac-ret,leaf test.rs
99
$(call RUN,test)
1010

1111
$(COMPILE_OBJ) $(TMPDIR)/test.o test.c -mbranch-protection=bti+pac-ret+leaf
1212
$(AR) rcs $(TMPDIR)/libtest.a $(TMPDIR)/test.o
13-
$(RUSTC) -C branch-protection=bti+pac-ret+leaf test.rs
14-
$(call RUN,test)
13+
$(RUSTC) -Z branch-protection=bti,pac-ret,leaf test.rs
14+
$(call RUN,test)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// compile-flags: -Z branch-protection=leaf
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: incorrect value `leaf` for debugging option `branch-protection` - a `,` separated combination of `bti`, `b-key`, `pac-ret`, or `leaf` was expected
2+

0 commit comments

Comments
 (0)