Skip to content

Commit

Permalink
Auto merge of rust-lang#105421 - jacobbramley:jb/branch-prot-check, r…
Browse files Browse the repository at this point in the history
…=nagisa

Check AArch64 branch-protection earlier in the pipeline.

As suggested in rust-lang#93516.

r? `@nagisa`
  • Loading branch information
bors committed Dec 17, 2022
2 parents 49c2279 + 73d374f commit aef17b7
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 18 deletions.
10 changes: 6 additions & 4 deletions compiler/rustc_codegen_llvm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -281,9 +280,7 @@ 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 {
if sess.target.arch == "aarch64" {
llvm::LLVMRustAddModuleFlag(
llmod,
llvm::LLVMModFlagBehavior::Error,
Expand All @@ -309,6 +306,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."
);
}
}

Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_codegen_llvm/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ pub(crate) struct SymbolAlreadyDefined<'a> {
pub symbol_name: &'a str,
}

#[derive(Diagnostic)]
#[diag(codegen_llvm_branch_protection_requires_aarch64)]
pub(crate) struct BranchProtectionRequiresAArch64;

#[derive(Diagnostic)]
#[diag(codegen_llvm_invalid_minimum_alignment)]
pub(crate) struct InvalidMinimumAlignment {
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ codegen_llvm_instrument_coverage_requires_llvm_12 =
codegen_llvm_symbol_already_defined =
symbol `{$symbol_name}` is already defined
codegen_llvm_branch_protection_requires_aarch64 =
-Zbranch-protection is only supported on aarch64
codegen_llvm_invalid_minimum_alignment =
invalid minimum global alignment: {$err}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/session.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ session_unsupported_dwarf_version = requested DWARF version {$dwarf_version} is
session_target_stack_protector_not_supported = `-Z stack-protector={$stack_protector}` is not supported for target {$target_triple} and will be ignored
session_branch_protection_requires_aarch64 = `-Zbranch-protection` is only supported on aarch64
session_split_debuginfo_unstable_platform = `-Csplit-debuginfo={$debuginfo}` is unstable on this platform
session_file_is_not_writeable = output file {$file} is not writeable -- check its permissions
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_session/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ pub struct StackProtectorNotSupportedForTarget<'a> {
pub target_triple: &'a TargetTriple,
}

#[derive(Diagnostic)]
#[diag(session_branch_protection_requires_aarch64)]
pub(crate) struct BranchProtectionRequiresAArch64;

#[derive(Diagnostic)]
#[diag(session_split_debuginfo_unstable_platform)]
pub struct SplitDebugInfoUnstablePlatform {
Expand Down
12 changes: 8 additions & 4 deletions compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use crate::code_stats::CodeStats;
pub use crate::code_stats::{DataTypeKind, FieldInfo, SizeKind, VariantInfo};
use crate::config::{self, CrateType, InstrumentCoverage, OptLevel, OutputType, SwitchWithOptPath};
use crate::errors::{
CannotEnableCrtStaticLinux, CannotMixAndMatchSanitizers, LinkerPluginToWindowsNotSupported,
NotCircumventFeature, ProfileSampleUseFileDoesNotExist, ProfileUseFileDoesNotExist,
SanitizerCfiEnabled, SanitizerNotSupported, SanitizersNotSupported, SkippingConstChecks,
SplitDebugInfoUnstablePlatform, StackProtectorNotSupportedForTarget,
BranchProtectionRequiresAArch64, CannotEnableCrtStaticLinux, CannotMixAndMatchSanitizers,
LinkerPluginToWindowsNotSupported, NotCircumventFeature, ProfileSampleUseFileDoesNotExist,
ProfileUseFileDoesNotExist, SanitizerCfiEnabled, SanitizerNotSupported, SanitizersNotSupported,
SkippingConstChecks, SplitDebugInfoUnstablePlatform, StackProtectorNotSupportedForTarget,
TargetRequiresUnwindTables, UnleashedFeatureHelp, UnstableVirtualFunctionElimination,
UnsupportedDwarfVersion,
};
Expand Down Expand Up @@ -1565,6 +1565,10 @@ fn validate_commandline_args_with_session_available(sess: &Session) {
}
}

if sess.opts.unstable_opts.branch_protection.is_some() && sess.target.arch != "aarch64" {
sess.emit_err(BranchProtectionRequiresAArch64);
}

if let Some(dwarf_version) = sess.opts.unstable_opts.dwarf_version {
if dwarf_version > 5 {
sess.emit_err(UnsupportedDwarfVersion { dwarf_version });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# `branch-protection`

This option lets you enable branch authentication instructions on AArch64.
This option is ignored for non-AArch64 architectures.
This option is only accepted when targeting AArch64 architectures.
It takes some combination of the following values, separated by a `,`.

- `pac-ret` - Enable pointer authentication for non-leaf functions.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: -Zbranch-protection is only supported on aarch64
error: `-Zbranch-protection` is only supported on aarch64

error: aborting due to previous error

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// [BADFLAGS] check-fail
// [BADFLAGS] needs-llvm-components: aarch64
// [BADTARGET] compile-flags: --target=x86_64-unknown-linux-gnu -Zbranch-protection=bti
// [BADTARGET] build-fail
// [BADTARGET] check-fail
// [BADTARGET] needs-llvm-components: x86

#![crate_type = "lib"]
Expand Down

0 comments on commit aef17b7

Please sign in to comment.