Skip to content

Commit d2f21b8

Browse files
committed
Adds AArch64 GCS support
- Adds option to rustc config to enable GCS - Passes `guarded-control-stack` flag to llvm if enabled
1 parent f6df223 commit d2f21b8

File tree

6 files changed

+18
-4
lines changed

6 files changed

+18
-4
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

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

394394
// For non-naked functions, set branch protection attributes on aarch64.
395-
if let Some(BranchProtection { bti, pac_ret }) =
395+
if let Some(BranchProtection { bti, pac_ret, gcs }) =
396396
cx.sess().opts.unstable_opts.branch_protection
397397
{
398398
assert!(cx.sess().target.arch == "aarch64");
399399
if bti {
400400
to_add.push(llvm::CreateAttrString(cx.llcx, "branch-target-enforcement"));
401401
}
402+
if gcs {
403+
to_add.push(llvm::CreateAttrString(cx.llcx, "guarded-control-stack"));
404+
}
402405
if let Some(PacRet { leaf, pc, key }) = pac_ret {
403406
if pc {
404407
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
@@ -386,7 +386,8 @@ pub(crate) unsafe fn create_module<'ll>(
386386
);
387387
}
388388

389-
if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection {
389+
if let Some(BranchProtection { bti, pac_ret, gcs }) = sess.opts.unstable_opts.branch_protection
390+
{
390391
if sess.target.arch == "aarch64" {
391392
llvm::add_module_flag_u32(
392393
llmod,
@@ -419,6 +420,12 @@ pub(crate) unsafe fn create_module<'ll>(
419420
"sign-return-address-with-bkey",
420421
u32::from(pac_opts.key == PAuthKey::B),
421422
);
423+
llvm::add_module_flag_u32(
424+
llmod,
425+
llvm::ModuleFlagMergeBehavior::Min,
426+
"guarded-control-stack",
427+
gcs.into(),
428+
);
422429
} else {
423430
bug!(
424431
"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
@@ -799,7 +799,7 @@ mod desc {
799799
pub(crate) const parse_polonius: &str = "either no value or `legacy` (the default), or `next`";
800800
pub(crate) const parse_stack_protector: &str =
801801
"one of (`none` (default), `basic`, `strong`, or `all`)";
802-
pub(crate) const parse_branch_protection: &str = "a `,` separated combination of `bti`, `pac-ret`, followed by a combination of `pc`, `b-key`, or `leaf`";
802+
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)";
803803
pub(crate) const parse_proc_macro_execution_strategy: &str =
804804
"one of supported execution strategies (`same-thread`, or `cross-thread`)";
805805
pub(crate) const parse_remap_path_scope: &str =
@@ -1836,6 +1836,7 @@ pub mod parse {
18361836
Some(pac) => pac.pc = true,
18371837
_ => return false,
18381838
},
1839+
"gcs" => slot.gcs = true,
18391840
_ => return false,
18401841
};
18411842
}

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

0 commit comments

Comments
 (0)