From 49f3c0b73689eed49df9ef822f9fd437db93ce9e Mon Sep 17 00:00:00 2001 From: Jacob Bramley Date: Mon, 21 Nov 2022 16:05:51 +0000 Subject: Check AArch64 branch-protection earlier in the pipeline. As suggested in #93516. --- compiler/rustc_codegen_llvm/src/context.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'compiler/rustc_codegen_llvm/src/context.rs') diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs index 4dcc7cd5447..a22a67ad7d3 100644 --- a/compiler/rustc_codegen_llvm/src/context.rs +++ b/compiler/rustc_codegen_llvm/src/context.rs @@ -3,7 +3,6 @@ use crate::back::write::to_llvm_code_model; use crate::callee::get_fn; use crate::coverageinfo; use crate::debuginfo; -use crate::errors::BranchProtectionRequiresAArch64; use crate::llvm; use crate::llvm_util; use crate::type_::Type; @@ -275,10 +274,9 @@ pub unsafe fn create_module<'ll>( } } - if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection { - if sess.target.arch != "aarch64" { - sess.emit_err(BranchProtectionRequiresAArch64); - } else { + // AArch64-only options (checked in rustc_session). + if sess.target.arch == "aarch64" { + if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection { llvm::LLVMRustAddModuleFlag( llmod, llvm::LLVMModFlagBehavior::Error, -- cgit 1.4.1-3-g733a5 From 73d374f3e7d5fe9635a23937cdc2f7e1d1c29224 Mon Sep 17 00:00:00 2001 From: Jacob Bramley Date: Tue, 13 Dec 2022 17:04:02 +0000 Subject: bug! if branch-protection makes it to non-AArch64 codegen. --- compiler/rustc_codegen_llvm/src/context.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'compiler/rustc_codegen_llvm/src/context.rs') diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs index a22a67ad7d3..f6fcdc90cb6 100644 --- a/compiler/rustc_codegen_llvm/src/context.rs +++ b/compiler/rustc_codegen_llvm/src/context.rs @@ -274,9 +274,8 @@ pub unsafe fn create_module<'ll>( } } - // AArch64-only options (checked in rustc_session). - if sess.target.arch == "aarch64" { - if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection { + if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection { + if sess.target.arch == "aarch64" { llvm::LLVMRustAddModuleFlag( llmod, llvm::LLVMModFlagBehavior::Error, @@ -302,6 +301,11 @@ pub unsafe fn create_module<'ll>( "sign-return-address-with-bkey\0".as_ptr().cast(), u32::from(pac_opts.key == PAuthKey::B), ); + } else { + bug!( + "branch-protection used on non-AArch64 target; \ + this should be checked in rustc_session." + ); } } -- cgit 1.4.1-3-g733a5 From 55c4164fffb8c4389727cff554dd77012c624087 Mon Sep 17 00:00:00 2001 From: Arvind Mukund Date: Mon, 19 Dec 2022 19:06:30 -0800 Subject: Correct ModFlagBehavior for Aarch64 on LLVM-15 When building with Fat LTO and BTI enabled on aarch64, the BTI is set to `Module::Min` for alloc shim but is set to `Module::Error` for the crate. This was fine when we were using LLVM-14 but LLVM-15 changes it's behaviour to support for compiling with different `mbranch-protection` flags. Refer: https://github.com/rust-lang/llvm-project/commit/b0343a38a5910e980bb031e4014655d77cd0c162 --- compiler/rustc_codegen_llvm/src/context.rs | 8 ++++---- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'compiler/rustc_codegen_llvm/src/context.rs') diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs index f3bff5d5716..9c7d3dabd6f 100644 --- a/compiler/rustc_codegen_llvm/src/context.rs +++ b/compiler/rustc_codegen_llvm/src/context.rs @@ -283,26 +283,26 @@ pub unsafe fn create_module<'ll>( if sess.target.arch == "aarch64" { llvm::LLVMRustAddModuleFlag( llmod, - llvm::LLVMModFlagBehavior::Error, + llvm::LLVMModFlagBehavior::Min, "branch-target-enforcement\0".as_ptr().cast(), bti.into(), ); llvm::LLVMRustAddModuleFlag( llmod, - llvm::LLVMModFlagBehavior::Error, + llvm::LLVMModFlagBehavior::Min, "sign-return-address\0".as_ptr().cast(), pac_ret.is_some().into(), ); let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, key: PAuthKey::A }); llvm::LLVMRustAddModuleFlag( llmod, - llvm::LLVMModFlagBehavior::Error, + llvm::LLVMModFlagBehavior::Min, "sign-return-address-all\0".as_ptr().cast(), pac_opts.leaf.into(), ); llvm::LLVMRustAddModuleFlag( llmod, - llvm::LLVMModFlagBehavior::Error, + llvm::LLVMModFlagBehavior::Min, "sign-return-address-with-bkey\0".as_ptr().cast(), u32::from(pac_opts.key == PAuthKey::B), ); diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index e61dbe8b8fc..6c78966a98d 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -79,6 +79,7 @@ pub enum LLVMModFlagBehavior { Append = 5, AppendUnique = 6, Max = 7, + Min = 8, } // Consts for the LLVM CallConv type, pre-cast to usize. -- cgit 1.4.1-3-g733a5 From 5480ac540cdb88a1512334c5680ccf7cd76a38c8 Mon Sep 17 00:00:00 2001 From: Arvind Mukund Date: Tue, 20 Dec 2022 11:44:12 -0800 Subject: Use `Error` behavior for LLVM versions prior to 15 CI fails when building with LLVM-13. This raises unknown behavior constant `8` from IRVerifier. --- compiler/rustc_codegen_llvm/src/context.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'compiler/rustc_codegen_llvm/src/context.rs') diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs index 9c7d3dabd6f..d9ccba07a34 100644 --- a/compiler/rustc_codegen_llvm/src/context.rs +++ b/compiler/rustc_codegen_llvm/src/context.rs @@ -280,29 +280,35 @@ pub unsafe fn create_module<'ll>( } if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection { + let behavior = if llvm_version >= (15, 0, 0) { + llvm::LLVMModFlagBehavior::Min + } else { + llvm::LLVMModFlagBehavior::Error + }; + if sess.target.arch == "aarch64" { llvm::LLVMRustAddModuleFlag( llmod, - llvm::LLVMModFlagBehavior::Min, + behavior, "branch-target-enforcement\0".as_ptr().cast(), bti.into(), ); llvm::LLVMRustAddModuleFlag( llmod, - llvm::LLVMModFlagBehavior::Min, + behavior, "sign-return-address\0".as_ptr().cast(), pac_ret.is_some().into(), ); let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, key: PAuthKey::A }); llvm::LLVMRustAddModuleFlag( llmod, - llvm::LLVMModFlagBehavior::Min, + behavior, "sign-return-address-all\0".as_ptr().cast(), pac_opts.leaf.into(), ); llvm::LLVMRustAddModuleFlag( llmod, - llvm::LLVMModFlagBehavior::Min, + behavior, "sign-return-address-with-bkey\0".as_ptr().cast(), u32::from(pac_opts.key == PAuthKey::B), ); -- cgit 1.4.1-3-g733a5