about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorJamie Cunliffe <Jamie.Cunliffe@arm.com>2021-12-01 15:56:59 +0000
committerJamie Cunliffe <Jamie.Cunliffe@arm.com>2021-12-01 15:56:59 +0000
commit984ca4689dbceea29bbfcf54c4743b45fccf7ad1 (patch)
tree50e979ae7eed117e44f6bce9a82003fe0270f51c /compiler
parent837cc1687f7c0d35a4e90a2f6bee377b5a2ecfd5 (diff)
downloadrust-984ca4689dbceea29bbfcf54c4743b45fccf7ad1.tar.gz
rust-984ca4689dbceea29bbfcf54c4743b45fccf7ad1.zip
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 <james.mcgregor2@arm.com>
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_codegen_llvm/src/attributes.rs54
-rw-r--r--compiler/rustc_codegen_llvm/src/context.rs42
-rw-r--r--compiler/rustc_codegen_llvm/src/declare.rs4
-rw-r--r--compiler/rustc_interface/src/tests.rs8
-rw-r--r--compiler/rustc_session/src/options.rs9
5 files changed, 28 insertions, 89 deletions
diff --git a/compiler/rustc_codegen_llvm/src/attributes.rs b/compiler/rustc_codegen_llvm/src/attributes.rs
index 768e03b5ef3..8e6329a997f 100644
--- a/compiler/rustc_codegen_llvm/src/attributes.rs
+++ b/compiler/rustc_codegen_llvm/src/attributes.rs
@@ -9,7 +9,7 @@ use rustc_hir::def_id::DefId;
 use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
 use rustc_middle::ty::layout::HasTyCtxt;
 use rustc_middle::ty::{self, TyCtxt};
-use rustc_session::config::{BranchProtection, OptLevel, PAuthKey};
+use rustc_session::config::OptLevel;
 use rustc_session::Session;
 use rustc_target::spec::abi::Abi;
 use rustc_target::spec::{FramePointer, SanitizerSet, StackProbeType, StackProtector};
@@ -203,58 +203,6 @@ pub fn non_lazy_bind(sess: &Session, llfn: &'ll Value) {
     }
 }
 
-pub fn set_branch_protection(sess: &Session, llfn: &'ll Value) {
-    // Setting PAC/BTI function attributes is only necessary for LLVM 11 and earlier.
-    // For LLVM 12 and greater, module-level metadata attributes are set in
-    // `compiler/rustc_codegen_llvm/src/context.rs`.
-    if llvm_util::get_version() >= (12, 0, 0) {
-        return;
-    }
-
-    let BranchProtection { bti, pac_ret: pac } = sess.opts.cg.branch_protection;
-
-    if bti {
-        llvm::AddFunctionAttrString(
-            llfn,
-            llvm::AttributePlace::Function,
-            cstr!("branch-target-enforcement"),
-        );
-    }
-
-    if let Some(pac_opts) = pac {
-        if pac_opts.leaf {
-            llvm::AddFunctionAttrStringValue(
-                llfn,
-                llvm::AttributePlace::Function,
-                cstr!("sign-return-address"),
-                cstr!("non-leaf"),
-            );
-        } else {
-            llvm::AddFunctionAttrStringValue(
-                llfn,
-                llvm::AttributePlace::Function,
-                cstr!("sign-return-address"),
-                cstr!("all"),
-            );
-        }
-
-        match pac_opts.key {
-            PAuthKey::A => llvm::AddFunctionAttrStringValue(
-                llfn,
-                llvm::AttributePlace::Function,
-                cstr!("sign-return-address-key"),
-                cstr!("a_key"),
-            ),
-            PAuthKey::B => llvm::AddFunctionAttrStringValue(
-                llfn,
-                llvm::AttributePlace::Function,
-                cstr!("sign-return-address-key"),
-                cstr!("b_key"),
-            ),
-        }
-    }
-}
-
 pub(crate) fn default_optimisation_attrs(sess: &Session, llfn: &'ll Value) {
     match sess.opts.optimize {
         OptLevel::Size => {
diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs
index da05b5c0cba..0f5487a4447 100644
--- a/compiler/rustc_codegen_llvm/src/context.rs
+++ b/compiler/rustc_codegen_llvm/src/context.rs
@@ -21,7 +21,7 @@ use rustc_middle::ty::layout::{
 };
 use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
 use rustc_middle::{bug, span_bug};
-use rustc_session::config::{BranchProtection, CFGuard, CrateType, DebugInfo, PAuthKey};
+use rustc_session::config::{BranchProtection, CFGuard, CrateType, DebugInfo, PAuthKey, PacRet};
 use rustc_session::Session;
 use rustc_span::source_map::Span;
 use rustc_span::symbol::Symbol;
@@ -243,7 +243,7 @@ pub unsafe fn create_module(
     }
 
     if sess.target.arch == "aarch64" {
-        let BranchProtection { bti, pac_ret: pac } = sess.opts.cg.branch_protection;
+        let BranchProtection { bti, pac_ret: pac } = sess.opts.debugging_opts.branch_protection;
 
         llvm::LLVMRustAddModuleFlag(
             llmod,
@@ -251,27 +251,23 @@ pub unsafe fn create_module(
             bti.into(),
         );
 
-        if let Some(pac_opts) = pac {
-            llvm::LLVMRustAddModuleFlag(llmod, "sign-return-address\0".as_ptr().cast(), 1);
-            llvm::LLVMRustAddModuleFlag(
-                llmod,
-                "sign-return-address-all\0".as_ptr().cast(),
-                pac_opts.leaf.into(),
-            );
-            llvm::LLVMRustAddModuleFlag(
-                llmod,
-                "sign-return-address-with-bkey\0".as_ptr().cast(),
-                if pac_opts.key == PAuthKey::A { 0 } else { 1 },
-            );
-        } else {
-            llvm::LLVMRustAddModuleFlag(llmod, "sign-return-address\0".as_ptr().cast(), 0);
-            llvm::LLVMRustAddModuleFlag(llmod, "sign-return-address-all\0".as_ptr().cast(), 0);
-            llvm::LLVMRustAddModuleFlag(
-                llmod,
-                "sign-return-address-with-bkey\0".as_ptr().cast(),
-                0,
-            );
-        }
+        llvm::LLVMRustAddModuleFlag(
+            llmod,
+            "sign-return-address\0".as_ptr().cast(),
+            pac.is_some().into(),
+        );
+        let pac_opts = pac.unwrap_or(PacRet { leaf: false, key: PAuthKey::A });
+        llvm::LLVMRustAddModuleFlag(
+            llmod,
+            "sign-return-address-all\0".as_ptr().cast(),
+            pac_opts.leaf.into(),
+        );
+        let is_bkey = if pac_opts.key == PAuthKey::A { false } else { true };
+        llvm::LLVMRustAddModuleFlag(
+            llmod,
+            "sign-return-address-with-bkey\0".as_ptr().cast(),
+            is_bkey.into(),
+        );
     }
 
     llmod
diff --git a/compiler/rustc_codegen_llvm/src/declare.rs b/compiler/rustc_codegen_llvm/src/declare.rs
index f4e754b80c9..5db82dd7669 100644
--- a/compiler/rustc_codegen_llvm/src/declare.rs
+++ b/compiler/rustc_codegen_llvm/src/declare.rs
@@ -45,10 +45,6 @@ fn declare_raw_fn(
         llvm::Attribute::NoRedZone.apply_llfn(Function, llfn);
     }
 
-    if cx.tcx.sess.target.arch == "aarch64" {
-        attributes::set_branch_protection(cx.tcx.sess, llfn);
-    }
-
     attributes::default_optimisation_attrs(cx.tcx.sess, llfn);
     attributes::non_lazy_bind(cx.sess(), llfn);
 
diff --git a/compiler/rustc_interface/src/tests.rs b/compiler/rustc_interface/src/tests.rs
index 1ceb11ede19..c9970898d25 100644
--- a/compiler/rustc_interface/src/tests.rs
+++ b/compiler/rustc_interface/src/tests.rs
@@ -567,10 +567,6 @@ fn test_codegen_options_tracking_hash() {
 
     // Make sure that changing a [TRACKED] option changes the hash.
     // This list is in alphabetical order.
-    tracked!(
-        branch_protection,
-        BranchProtection { bti: true, pac_ret: Some(PacRet { leaf: true, key: PAuthKey::B }) }
-    );
     tracked!(code_model, Some(CodeModel::Large));
     tracked!(control_flow_guard, CFGuard::Checks);
     tracked!(debug_assertions, Some(true));
@@ -723,6 +719,10 @@ fn test_debugging_options_tracking_hash() {
     tracked!(asm_comments, true);
     tracked!(assume_incomplete_release, true);
     tracked!(binary_dep_depinfo, true);
+    tracked!(
+        branch_protection,
+        BranchProtection { bti: true, pac_ret: Some(PacRet { leaf: true, key: PAuthKey::B }) }
+    );
     tracked!(chalk, true);
     tracked!(codegen_backend, Some("abc".to_string()));
     tracked!(crate_attr, vec!["abc".to_string()]);
diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs
index 03ce0cc42cd..d3ffae36c67 100644
--- a/compiler/rustc_session/src/options.rs
+++ b/compiler/rustc_session/src/options.rs
@@ -390,7 +390,7 @@ mod desc {
     pub const parse_stack_protector: &str =
         "one of (`none` (default), `basic`, `strong`, or `all`)";
     pub const parse_branch_protection: &str =
-        "a `+` separated combination of `bti`, `b-key`, `pac-ret`, or `leaf`";
+        "a `,` separated combination of `bti`, `b-key`, `pac-ret`, or `leaf`";
 }
 
 mod parse {
@@ -935,7 +935,7 @@ mod parse {
     crate fn parse_branch_protection(slot: &mut BranchProtection, v: Option<&str>) -> bool {
         match v {
             Some(s) => {
-                for opt in s.split('+') {
+                for opt in s.split(',') {
                     match opt {
                         "bti" => slot.bti = true,
                         "pac-ret" if slot.pac_ret.is_none() => {
@@ -953,7 +953,6 @@ mod parse {
                     };
                 }
             }
-
             _ => return false,
         }
         true
@@ -971,8 +970,6 @@ options! {
 
     ar: String = (String::new(), parse_string, [UNTRACKED],
         "this option is deprecated and does nothing"),
-    branch_protection: BranchProtection = (BranchProtection::default(), parse_branch_protection, [TRACKED],
-        "set options for branch target identification and pointer authentication on AArch64"),
     code_model: Option<CodeModel> = (None, parse_code_model, [TRACKED],
         "choose the code model to use (`rustc --print code-models` for details)"),
     codegen_units: Option<usize> = (None, parse_opt_number, [UNTRACKED],
@@ -1101,6 +1098,8 @@ options! {
         (default: no)"),
     borrowck: String = ("migrate".to_string(), parse_string, [UNTRACKED],
         "select which borrowck is used (`mir` or `migrate`) (default: `migrate`)"),
+    branch_protection: BranchProtection = (BranchProtection::default(), parse_branch_protection, [TRACKED],
+        "set options for branch target identification and pointer authentication on AArch64"),
     cgu_partitioning_strategy: Option<String> = (None, parse_opt_string, [TRACKED],
         "the codegen unit partitioning strategy to use"),
     chalk: bool = (false, parse_bool, [TRACKED],