Skip to content

Commit 15283f6

Browse files
committed
Auto merge of #146338 - CrooseGit:dev/reucru01/AArch64-enable-GCS, r=Urgau,davidtwco
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 e9385f9 + 08020de commit 15283f6

File tree

15 files changed

+44
-18
lines changed

15 files changed

+44
-18
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
@@ -370,7 +370,8 @@ pub(crate) unsafe fn create_module<'ll>(
370370
);
371371
}
372372

373-
if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection {
373+
if let Some(BranchProtection { bti, pac_ret, gcs }) = sess.opts.unstable_opts.branch_protection
374+
{
374375
if sess.target.arch == "aarch64" {
375376
llvm::add_module_flag_u32(
376377
llmod,
@@ -403,6 +404,12 @@ pub(crate) unsafe fn create_module<'ll>(
403404
"sign-return-address-with-bkey",
404405
u32::from(pac_opts.key == PAuthKey::B),
405406
);
407+
llvm::add_module_flag_u32(
408+
llmod,
409+
llvm::ModuleFlagMergeBehavior::Min,
410+
"guarded-control-stack",
411+
gcs.into(),
412+
);
406413
} else {
407414
bug!(
408415
"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
@@ -1621,6 +1621,7 @@ pub struct PacRet {
16211621
pub struct BranchProtection {
16221622
pub bti: bool,
16231623
pub pac_ret: Option<PacRet>,
1624+
pub gcs: bool,
16241625
}
16251626

16261627
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 =
@@ -1905,6 +1905,7 @@ pub mod parse {
19051905
Some(pac) => pac.pc = true,
19061906
_ => return false,
19071907
},
1908+
"gcs" => slot.gcs = true,
19081909
_ => return false,
19091910
};
19101911
}

src/ci/docker/host-aarch64/aarch64-gnu-debug/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM ubuntu:22.04
1+
FROM ubuntu:25.10
22

33
ARG DEBIAN_FRONTEND=noninteractive
44
RUN apt-get update && apt-get install -y --no-install-recommends \

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM ubuntu:25.04
1+
FROM ubuntu:25.10
22

33
ARG DEBIAN_FRONTEND=noninteractive
44

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM ubuntu:22.04
1+
FROM ubuntu:25.10
22

33
ARG DEBIAN_FRONTEND=noninteractive
44
RUN apt-get update && apt-get install -y --no-install-recommends \

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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
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+
//@ [GCS] min-llvm-version: 21
9+
//@ [GCS] ignore-apple (XCode version needs updating)
10+
//@ [GCS] compile-flags: -Z branch-protection=gcs
811
//@ [PACRET] compile-flags: -Z branch-protection=pac-ret,leaf
912
//@ [PAUTHLR_NOP] compile-flags: -Z branch-protection=pac-ret,pc,leaf
1013
//@ [PAUTHLR] compile-flags: -C target-feature=+pauth-lr -Z branch-protection=pac-ret,pc,leaf
@@ -17,6 +20,7 @@
1720
extern crate minicore;
1821
use minicore::*;
1922

23+
// GCS: .aeabi_attribute 2, 1 // Tag_Feature_GCS
2024
// PACRET: hint #25
2125
// PACRET: hint #29
2226
// PAUTHLR_NOP: hint #25

0 commit comments

Comments
 (0)