From 40f0172c6aed0b2fb055c19108d226f8fb410f8c Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Fri, 3 May 2024 14:32:01 +0200 Subject: [PATCH 1/6] Add -Zfixed-x18 Signed-off-by: Alice Ryhl --- compiler/rustc_codegen_llvm/src/llvm_util.rs | 5 +++ compiler/rustc_interface/src/tests.rs | 1 + compiler/rustc_session/src/options.rs | 2 ++ .../src/compiler-flags/fixed-x18.md | 32 +++++++++++++++++++ tests/codegen/fixed-x18.rs | 22 +++++++++++++ 5 files changed, 62 insertions(+) create mode 100644 src/doc/unstable-book/src/compiler-flags/fixed-x18.md create mode 100644 tests/codegen/fixed-x18.rs diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index 5552b38102511..dbce7f43e62c6 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -615,6 +615,11 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec = (None, parse_opt_bool, [TRACKED], "reduce memory use by retaining fewer names within compilation artifacts (LLVM-IR) \ (default: no)"), + fixed_x18: bool = (false, parse_bool, [TRACKED], + "make the x18 register reserved on AArch64 (default: no)"), flatten_format_args: bool = (true, parse_bool, [TRACKED], "flatten nested format_args!() and literals into a simplified format_args!() call \ (default: yes)"), diff --git a/src/doc/unstable-book/src/compiler-flags/fixed-x18.md b/src/doc/unstable-book/src/compiler-flags/fixed-x18.md new file mode 100644 index 0000000000000..8c8bff5fa296d --- /dev/null +++ b/src/doc/unstable-book/src/compiler-flags/fixed-x18.md @@ -0,0 +1,32 @@ +# `fixed-x18` + +This option prevents the compiler from using the x18 register. It is only +supported on aarch64. + +From the [ABI spec][arm-abi]: + +> X18 is the platform register and is reserved for the use of platform ABIs. +> This is an additional temporary register on platforms that don't assign a +> special meaning to it. + +This flag only has an effect when the x18 register would otherwise be considered +a temporary register. When the flag is applied, x18 is always a reserved +register. + +This flag is intended for use with the shadow call stack sanitizer. Generally, +when that sanitizer is enabled, the x18 register is used to store a pointer to +the shadow stack. Enabling this flag prevents the compiler from overwriting the +shadow stack pointer with temporary data, which is necessary for the sanitizer +to work correctly. + +Currently, the `-Zsanitizer=shadow-call-stack` flag is only supported on +platforms that always treat x18 as a reserved register, and the `-Zfixed-x18` +flag is not required to use the sanitizer on such platforms. However, the +sanitizer may be supported on targets where this is not the case in the future. + +It is undefined behavior for `-Zsanitizer=shadow-call-stack` code to call into +code where x18 is a temporary register. On the other hand, when you are *not* +using the shadow call stack sanitizer, compilation units compiled with and +without the `-Zfixed-x18` flag are compatible with each other. + +[arm-abi]: https://developer.arm.com/documentation/den0024/a/The-ABI-for-ARM-64-bit-Architecture/Register-use-in-the-AArch64-Procedure-Call-Standard/Parameters-in-general-purpose-registers diff --git a/tests/codegen/fixed-x18.rs b/tests/codegen/fixed-x18.rs new file mode 100644 index 0000000000000..4997a39a7263d --- /dev/null +++ b/tests/codegen/fixed-x18.rs @@ -0,0 +1,22 @@ +// Test that the `reserve-x18` target feature is (not) emitted when +// the `-Zfixed-x18` flag is (not) set. + +//@ revisions: unset set +//@ needs-llvm-components: aarch64 +//@ compile-flags: --target aarch64-unknown-none +//@ [set] compile-flags: -Zfixed-x18 + +#![crate_type = "lib"] +#![feature(no_core, lang_items)] +#![no_core] + +#[lang = "sized"] +trait Sized {} + +#[no_mangle] +pub fn foo() { + // CHECK: @foo() unnamed_addr #0 + + // unset-NOT: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+reserve-x18{{.*}} } + // set: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+reserve-x18{{.*}} } +} From 518becf5ea30807f98b89002a70e5640e6e8bbf4 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Tue, 14 May 2024 21:09:42 +0200 Subject: [PATCH 2/6] Fail on non-aarch64 targets --- compiler/rustc_codegen_llvm/src/llvm_util.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index dbce7f43e62c6..b3359b816025b 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -617,6 +617,11 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec Date: Wed, 15 May 2024 10:01:55 +0200 Subject: [PATCH 3/6] Use an error struct instead of a panic --- compiler/rustc_codegen_llvm/messages.ftl | 2 ++ compiler/rustc_codegen_llvm/src/errors.rs | 6 +++++ compiler/rustc_codegen_llvm/src/llvm_util.rs | 9 ++++---- tests/ui/abi/fixed_x18.aarch64.stderr | 2 ++ tests/ui/abi/fixed_x18.arm.stderr | 2 ++ tests/ui/abi/fixed_x18.i686.stderr | 2 ++ tests/ui/abi/fixed_x18.riscv32.stderr | 2 ++ tests/ui/abi/fixed_x18.riscv64.stderr | 2 ++ tests/ui/abi/fixed_x18.rs | 23 ++++++++++++++++++++ tests/ui/abi/fixed_x18.x64.stderr | 2 ++ 10 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 tests/ui/abi/fixed_x18.aarch64.stderr create mode 100644 tests/ui/abi/fixed_x18.arm.stderr create mode 100644 tests/ui/abi/fixed_x18.i686.stderr create mode 100644 tests/ui/abi/fixed_x18.riscv32.stderr create mode 100644 tests/ui/abi/fixed_x18.riscv64.stderr create mode 100644 tests/ui/abi/fixed_x18.rs create mode 100644 tests/ui/abi/fixed_x18.x64.stderr diff --git a/compiler/rustc_codegen_llvm/messages.ftl b/compiler/rustc_codegen_llvm/messages.ftl index d14fe0299e64c..1c126e797621b 100644 --- a/compiler/rustc_codegen_llvm/messages.ftl +++ b/compiler/rustc_codegen_llvm/messages.ftl @@ -18,6 +18,8 @@ codegen_llvm_error_creating_import_library = codegen_llvm_error_writing_def_file = Error writing .DEF file: {$error} +codegen_llvm_fixed_x18_invalid_arch = the `-Zfixed-x18` flag is not supported on the `{$arch}` architecture + codegen_llvm_from_llvm_diag = {$message} codegen_llvm_from_llvm_optimization_diag = {$filename}:{$line}:{$column} {$pass_name} ({$kind}): {$message} diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index e15eda7c66c14..9d83dc811633b 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -254,3 +254,9 @@ pub struct MismatchedDataLayout<'a> { pub(crate) struct InvalidTargetFeaturePrefix<'a> { pub feature: &'a str, } + +#[derive(Diagnostic)] +#[diag(codegen_llvm_fixed_x18_invalid_arch)] +pub(crate) struct FixedX18InvalidArch<'a> { + pub arch: &'a str, +} diff --git a/compiler/rustc_codegen_llvm/src/llvm_util.rs b/compiler/rustc_codegen_llvm/src/llvm_util.rs index b3359b816025b..53b9b530e9bd6 100644 --- a/compiler/rustc_codegen_llvm/src/llvm_util.rs +++ b/compiler/rustc_codegen_llvm/src/llvm_util.rs @@ -1,6 +1,6 @@ use crate::back::write::create_informational_target_machine; use crate::errors::{ - InvalidTargetFeaturePrefix, PossibleFeature, TargetFeatureDisableOrEnable, + FixedX18InvalidArch, InvalidTargetFeaturePrefix, PossibleFeature, TargetFeatureDisableOrEnable, UnknownCTargetFeature, UnknownCTargetFeaturePrefix, UnstableCTargetFeature, }; use crate::llvm; @@ -618,11 +618,10 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec Date: Wed, 15 May 2024 12:35:39 +0200 Subject: [PATCH 4/6] Remove fixed_x18.aarch64.stderr --- tests/ui/abi/fixed_x18.aarch64.stderr | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 tests/ui/abi/fixed_x18.aarch64.stderr diff --git a/tests/ui/abi/fixed_x18.aarch64.stderr b/tests/ui/abi/fixed_x18.aarch64.stderr deleted file mode 100644 index 6dc367f9edce6..0000000000000 --- a/tests/ui/abi/fixed_x18.aarch64.stderr +++ /dev/null @@ -1,2 +0,0 @@ -error: the `-Zfixed-x18` flag is not supported on the `x86_64` architecture - From 7677ff2879dda850c4e0622116be7751a77a2810 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Wed, 15 May 2024 13:09:02 +0200 Subject: [PATCH 5/6] Remove aarch64 from revisions list --- tests/ui/abi/fixed_x18.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ui/abi/fixed_x18.rs b/tests/ui/abi/fixed_x18.rs index e62b9c24d223b..6d00cb2709da2 100644 --- a/tests/ui/abi/fixed_x18.rs +++ b/tests/ui/abi/fixed_x18.rs @@ -1,7 +1,7 @@ // This tests that -Zfixed-x18 causes a compilation failure on targets other than aarch64. // Behavior on aarch64 is tested by tests/codegen/fixed-x18.rs. // -//@ revisions: x64 i686 aarch64 arm riscv32 riscv64 +//@ revisions: x64 i686 arm riscv32 riscv64 // //@ compile-flags: -Zfixed-x18 //@ [x64] needs-llvm-components: x86 From 4aafecb1693c30625c3f8d75f428edf3dad22755 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Wed, 29 May 2024 16:58:46 +0200 Subject: [PATCH 6/6] Simplify check for unsupported architectures --- tests/ui/abi/fixed_x18.arm.stderr | 2 -- tests/ui/abi/fixed_x18.i686.stderr | 2 -- tests/ui/abi/fixed_x18.riscv32.stderr | 2 -- tests/ui/abi/fixed_x18.riscv64.stderr | 2 -- tests/ui/abi/fixed_x18.rs | 2 ++ tests/ui/abi/fixed_x18.x64.stderr | 2 -- 6 files changed, 2 insertions(+), 10 deletions(-) delete mode 100644 tests/ui/abi/fixed_x18.arm.stderr delete mode 100644 tests/ui/abi/fixed_x18.i686.stderr delete mode 100644 tests/ui/abi/fixed_x18.riscv32.stderr delete mode 100644 tests/ui/abi/fixed_x18.riscv64.stderr delete mode 100644 tests/ui/abi/fixed_x18.x64.stderr diff --git a/tests/ui/abi/fixed_x18.arm.stderr b/tests/ui/abi/fixed_x18.arm.stderr deleted file mode 100644 index 68728dd9307a8..0000000000000 --- a/tests/ui/abi/fixed_x18.arm.stderr +++ /dev/null @@ -1,2 +0,0 @@ -error: the `-Zfixed-x18` flag is not supported on the `arm` architecture - diff --git a/tests/ui/abi/fixed_x18.i686.stderr b/tests/ui/abi/fixed_x18.i686.stderr deleted file mode 100644 index 458c0679f8439..0000000000000 --- a/tests/ui/abi/fixed_x18.i686.stderr +++ /dev/null @@ -1,2 +0,0 @@ -error: the `-Zfixed-x18` flag is not supported on the `x86` architecture - diff --git a/tests/ui/abi/fixed_x18.riscv32.stderr b/tests/ui/abi/fixed_x18.riscv32.stderr deleted file mode 100644 index e872fb4455b72..0000000000000 --- a/tests/ui/abi/fixed_x18.riscv32.stderr +++ /dev/null @@ -1,2 +0,0 @@ -error: the `-Zfixed-x18` flag is not supported on the `riscv32` architecture - diff --git a/tests/ui/abi/fixed_x18.riscv64.stderr b/tests/ui/abi/fixed_x18.riscv64.stderr deleted file mode 100644 index 7127e5d7056f7..0000000000000 --- a/tests/ui/abi/fixed_x18.riscv64.stderr +++ /dev/null @@ -1,2 +0,0 @@ -error: the `-Zfixed-x18` flag is not supported on the `riscv64` architecture - diff --git a/tests/ui/abi/fixed_x18.rs b/tests/ui/abi/fixed_x18.rs index 6d00cb2709da2..f1ff3e1d53418 100644 --- a/tests/ui/abi/fixed_x18.rs +++ b/tests/ui/abi/fixed_x18.rs @@ -2,6 +2,8 @@ // Behavior on aarch64 is tested by tests/codegen/fixed-x18.rs. // //@ revisions: x64 i686 arm riscv32 riscv64 +//@ error-pattern: the `-Zfixed-x18` flag is not supported +//@ dont-check-compiler-stderr // //@ compile-flags: -Zfixed-x18 //@ [x64] needs-llvm-components: x86 diff --git a/tests/ui/abi/fixed_x18.x64.stderr b/tests/ui/abi/fixed_x18.x64.stderr deleted file mode 100644 index 6dc367f9edce6..0000000000000 --- a/tests/ui/abi/fixed_x18.x64.stderr +++ /dev/null @@ -1,2 +0,0 @@ -error: the `-Zfixed-x18` flag is not supported on the `x86_64` architecture -