Skip to content

Commit dd65613

Browse files
authored
Rollup merge of rust-lang#146338 - CrooseGit:dev/reucru01/AArch64-enable-GCS, r=Urgau
Extends AArch64 branch protection support to include GCS Extends existing support for AArch64 branch protection to include support for [Guarded Control Stacks](https://community.arm.com/arm-community-blogs/b/architectures-and-processors-blog/posts/arm-a-profile-architecture-2022#guarded-control-stack-gcs:~:text=Extraction%20or%20tracking.-,Guarded%20Control%20Stack%20(GCS),-With%20the%202022).
2 parents 917580c + 661289b commit dd65613

File tree

13 files changed

+37
-16
lines changed

13 files changed

+37
-16
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,16 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
407407
to_add.extend(sanitize_attrs(cx, codegen_fn_attrs.no_sanitize));
408408

409409
// For non-naked functions, set branch protection attributes on aarch64.
410-
if let Some(BranchProtection { bti, pac_ret }) =
410+
if let Some(BranchProtection { bti, pac_ret, gcs }) =
411411
cx.sess().opts.unstable_opts.branch_protection
412412
{
413413
assert!(cx.sess().target.arch == "aarch64");
414414
if bti {
415415
to_add.push(llvm::CreateAttrString(cx.llcx, "branch-target-enforcement"));
416416
}
417+
if gcs {
418+
to_add.push(llvm::CreateAttrString(cx.llcx, "guarded-control-stack"));
419+
}
417420
if let Some(PacRet { leaf, pc, key }) = pac_ret {
418421
if pc {
419422
to_add.push(llvm::CreateAttrString(cx.llcx, "branch-protection-pauth-lr"));

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,8 @@ pub(crate) unsafe fn create_module<'ll>(
399399
);
400400
}
401401

402-
if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection {
402+
if let Some(BranchProtection { bti, pac_ret, gcs }) = sess.opts.unstable_opts.branch_protection
403+
{
403404
if sess.target.arch == "aarch64" {
404405
llvm::add_module_flag_u32(
405406
llmod,
@@ -432,6 +433,12 @@ pub(crate) unsafe fn create_module<'ll>(
432433
"sign-return-address-with-bkey",
433434
u32::from(pac_opts.key == PAuthKey::B),
434435
);
436+
llvm::add_module_flag_u32(
437+
llmod,
438+
llvm::ModuleFlagMergeBehavior::Min,
439+
"guarded-control-stack",
440+
gcs.into(),
441+
);
435442
} else {
436443
bug!(
437444
"branch-protection used on non-AArch64 target; \

compiler/rustc_interface/src/tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,8 @@ fn test_unstable_options_tracking_hash() {
772772
branch_protection,
773773
Some(BranchProtection {
774774
bti: true,
775-
pac_ret: Some(PacRet { leaf: true, pc: true, key: PAuthKey::B })
775+
pac_ret: Some(PacRet { leaf: true, pc: true, key: PAuthKey::B }),
776+
gcs: true,
776777
})
777778
);
778779
tracked!(codegen_backend, Some("abc".to_string()));

compiler/rustc_session/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,6 +1613,7 @@ pub struct PacRet {
16131613
pub struct BranchProtection {
16141614
pub bti: bool,
16151615
pub pac_ret: Option<PacRet>,
1616+
pub gcs: bool,
16161617
}
16171618

16181619
pub(crate) const fn default_lib_output() -> CrateType {

compiler/rustc_session/src/options.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ mod desc {
866866
pub(crate) const parse_polonius: &str = "either no value or `legacy` (the default), or `next`";
867867
pub(crate) const parse_stack_protector: &str =
868868
"one of (`none` (default), `basic`, `strong`, or `all`)";
869-
pub(crate) const parse_branch_protection: &str = "a `,` separated combination of `bti`, `pac-ret`, followed by a combination of `pc`, `b-key`, or `leaf`";
869+
pub(crate) const parse_branch_protection: &str = "a `,` separated combination of `bti`, `gcs`, `pac-ret`, (optionally with `pc`, `b-key`, `leaf` if `pac-ret` is set)";
870870
pub(crate) const parse_proc_macro_execution_strategy: &str =
871871
"one of supported execution strategies (`same-thread`, or `cross-thread`)";
872872
pub(crate) const parse_remap_path_scope: &str =
@@ -1903,6 +1903,7 @@ pub mod parse {
19031903
Some(pac) => pac.pc = true,
19041904
_ => return false,
19051905
},
1906+
"gcs" => slot.gcs = true,
19061907
_ => return false,
19071908
};
19081909
}

src/ci/docker/host-aarch64/aarch64-gnu-llvm-19/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ARG DEBIAN_FRONTEND=noninteractive
44

55
RUN apt-get update && apt-get install -y --no-install-recommends \
66
bzip2 \
7-
g++ \
7+
g++-15 \
88
make \
99
ninja-build \
1010
file \

src/doc/unstable-book/src/compiler-flags/branch-protection.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ It takes some combination of the following values, separated by a `,`.
1313
- `leaf` - Enable pointer authentication for all functions, including leaf functions.
1414
- `b-key` - Sign return addresses with key B, instead of the default key A.
1515
- `bti` - Enable branch target identification.
16+
- `gcs` - Enable guarded control stack support.
1617

1718
`leaf`, `b-key` and `pc` are only valid if `pac-ret` was previously specified.
1819
For example, `-Z branch-protection=bti,pac-ret,leaf` is valid, but

tests/assembly-llvm/aarch64-pointer-auth.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// Test that PAC instructions are emitted when branch-protection is specified.
22

33
//@ add-core-stubs
4-
//@ revisions: PACRET PAUTHLR_NOP PAUTHLR
4+
//@ revisions: GCS PACRET PAUTHLR_NOP PAUTHLR
55
//@ assembly-output: emit-asm
66
//@ needs-llvm-components: aarch64
77
//@ compile-flags: --target aarch64-unknown-linux-gnu
8+
//@ min-llvm-version: 20
9+
//@ [GCS] compile-flags: -Z branch-protection=gcs
810
//@ [PACRET] compile-flags: -Z branch-protection=pac-ret,leaf
911
//@ [PAUTHLR_NOP] compile-flags: -Z branch-protection=pac-ret,pc,leaf
1012
//@ [PAUTHLR] compile-flags: -C target-feature=+pauth-lr -Z branch-protection=pac-ret,pc,leaf
@@ -17,6 +19,7 @@
1719
extern crate minicore;
1820
use minicore::*;
1921

22+
// GCS: .aeabi_attribute 2, 1 // Tag_Feature_GCS
2023
// PACRET: hint #25
2124
// PACRET: hint #29
2225
// PAUTHLR_NOP: hint #25

tests/codegen-llvm/branch-protection.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Test that the correct module flags are emitted with different branch protection flags.
22

33
//@ add-core-stubs
4-
//@ revisions: BTI PACRET LEAF BKEY PAUTHLR PAUTHLR_BKEY PAUTHLR_LEAF PAUTHLR_BTI NONE
4+
//@ revisions: BTI GCS PACRET LEAF BKEY PAUTHLR PAUTHLR_BKEY PAUTHLR_LEAF PAUTHLR_BTI NONE
55
//@ needs-llvm-components: aarch64
66
//@ [BTI] compile-flags: -Z branch-protection=bti
7+
//@ [GCS] compile-flags: -Z branch-protection=gcs
78
//@ [PACRET] compile-flags: -Z branch-protection=pac-ret
89
//@ [LEAF] compile-flags: -Z branch-protection=pac-ret,leaf
910
//@ [BKEY] compile-flags: -Z branch-protection=pac-ret,b-key
@@ -32,6 +33,9 @@ pub fn test() {}
3233
// BTI: !"sign-return-address-all", i32 0
3334
// BTI: !"sign-return-address-with-bkey", i32 0
3435

36+
// GCS: attributes [[ATTR]] = {{.*}} "guarded-control-stack"
37+
// GCS: !"guarded-control-stack", i32 1
38+
3539
// PACRET: attributes [[ATTR]] = {{.*}} "sign-return-address"="non-leaf"
3640
// PACRET-SAME: "sign-return-address-key"="a_key"
3741
// PACRET: !"branch-target-enforcement", i32 0

tests/run-make/pointer-auth-link-with-c-lto-clang/rmake.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// `-Z branch protection` is an unstable compiler feature which adds pointer-authentication
22
// code (PAC), a useful hashing measure for verifying that pointers have not been modified.
33
// This test checks that compilation and execution is successful when this feature is activated,
4-
// with some of its possible extra arguments (bti, pac-ret, leaf) when doing LTO.
4+
// with some of its possible extra arguments (bti, gcs, pac-ret, leaf) when doing LTO.
55
// See https://github.com/rust-lang/rust/pull/88354
66

77
//@ needs-force-clang-based-tests
@@ -19,7 +19,7 @@ fn main() {
1919
clang()
2020
.arg("-v")
2121
.lto("thin")
22-
.arg("-mbranch-protection=bti+pac-ret+b-key+leaf")
22+
.arg("-mbranch-protection=bti+gcs+pac-ret+b-key+leaf")
2323
.arg("-c")
2424
.out_exe("test.o")
2525
.input("test.c")
@@ -30,7 +30,7 @@ fn main() {
3030
.opt_level("2")
3131
.linker(&env_var("CLANG"))
3232
.link_arg("-fuse-ld=lld")
33-
.arg("-Zbranch-protection=bti,pac-ret,leaf")
33+
.arg("-Zbranch-protection=bti,gcs,pac-ret,leaf")
3434
.input("test.rs")
3535
.output("test.bin")
3636
.run();

0 commit comments

Comments
 (0)