Skip to content

Commit 3075644

Browse files
authored
Rollup merge of #128348 - dingxiangfei2009:allow-shadow-call-stack-sanitizer, r=tmandry
Unconditionally allow shadow call-stack sanitizer for AArch64 It is possible to do so whenever `-Z fixed-x18` is applied. cc ``@Darksonn`` for context The reasoning is that, as soon as reservation on `x18` is forced through the flag `fixed-x18`, on AArch64 the option to instrument with [Shadow Call Stack sanitizer](https://clang.llvm.org/docs/ShadowCallStack.html) is then applicable regardless of the target configuration. At the every least, we would like to relax the restriction on specifically `aarch64-unknonw-none`. For this option, we can include a documentation change saying that users of compiled objects need to ensure that they are linked to runtime with Shadow Call Stack instrumentation support. Related: #121972
2 parents d2b5aa6 + b368dcb commit 3075644

File tree

6 files changed

+55
-2
lines changed

6 files changed

+55
-2
lines changed

compiler/rustc_session/src/session.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,12 @@ fn validate_commandline_args_with_session_available(sess: &Session) {
11881188

11891189
// Sanitizers can only be used on platforms that we know have working sanitizer codegen.
11901190
let supported_sanitizers = sess.target.options.supported_sanitizers;
1191-
let unsupported_sanitizers = sess.opts.unstable_opts.sanitizer - supported_sanitizers;
1191+
let mut unsupported_sanitizers = sess.opts.unstable_opts.sanitizer - supported_sanitizers;
1192+
// Niche: if `fixed-x18`, or effectively switching on `reserved-x18` flag, is enabled
1193+
// we should allow Shadow Call Stack sanitizer.
1194+
if sess.opts.unstable_opts.fixed_x18 && sess.target.arch == "aarch64" {
1195+
unsupported_sanitizers -= SanitizerSet::SHADOWCALLSTACK;
1196+
}
11921197
match unsupported_sanitizers.into_iter().count() {
11931198
0 => {}
11941199
1 => {

src/doc/rustc/src/platform-support/android.md

+5
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,8 @@ Currently the `riscv64-linux-android` target requires the following architecture
6161
* `Zba` (address calculation instructions)
6262
* `Zbb` (base instructions)
6363
* `Zbs` (single-bit instructions)
64+
65+
### aarch64-linux-android on Nightly compilers
66+
67+
As soon as `-Zfixed-x18` compiler flag is supplied, the [`ShadowCallStack` sanitizer](https://releases.llvm.org/7.0.1/tools/clang/docs/ShadowCallStack.html)
68+
instrumentation is also made avaiable by supplying the second compiler flag `-Zsanitizer=shadow-call-stack`.

src/doc/unstable-book/src/compiler-flags/fixed-x18.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# `fixed-x18`
22

33
This option prevents the compiler from using the x18 register. It is only
4-
supported on aarch64.
4+
supported on `aarch64`.
55

66
From the [ABI spec][arm-abi]:
77

@@ -23,6 +23,11 @@ Currently, the `-Zsanitizer=shadow-call-stack` flag is only supported on
2323
platforms that always treat x18 as a reserved register, and the `-Zfixed-x18`
2424
flag is not required to use the sanitizer on such platforms. However, the
2525
sanitizer may be supported on targets where this is not the case in the future.
26+
One way to do so now on Nightly compilers is to explicitly supply this `-Zfixed-x18`
27+
flag with `aarch64` targets, so that the sanitizer is available for instrumentation
28+
on targets like `aarch64-unknown-none`, for instance. However, discretion is still
29+
required to make sure that the runtime support is in place for this sanitizer
30+
to be effective.
2631

2732
It is undefined behavior for `-Zsanitizer=shadow-call-stack` code to call into
2833
code where x18 is a temporary register. On the other hand, when you are *not*

src/doc/unstable-book/src/compiler-flags/sanitizer.md

+4
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,10 @@ A runtime must be provided by the application or operating system.
787787
788788
See the [Clang ShadowCallStack documentation][clang-scs] for more details.
789789
790+
* `aarch64-unknown-none`
791+
792+
In addition to support from a runtime by the application or operating system, the `-Zfixed-x18` flag is also mandatory.
793+
790794
# ThreadSanitizer
791795
792796
ThreadSanitizer is a data race detection tool. It is supported on the following
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ revisions: aarch64 android
2+
//@[aarch64] compile-flags: --target aarch64-unknown-none -Zfixed-x18 -Zsanitizer=shadow-call-stack
3+
//@[aarch64] needs-llvm-components: aarch64
4+
//@[android] compile-flags: --target aarch64-linux-android -Zsanitizer=shadow-call-stack
5+
//@[android] needs-llvm-components: aarch64
6+
7+
#![allow(internal_features)]
8+
#![crate_type = "rlib"]
9+
#![feature(no_core, lang_items)]
10+
#![no_core]
11+
12+
#[lang = "sized"]
13+
trait Sized {}
14+
15+
// CHECK: ; Function Attrs:{{.*}}shadowcallstack
16+
#[no_mangle]
17+
pub fn foo() {}
18+
19+
// CHECK: attributes #0 = {{.*}}shadowcallstack{{.*}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ compile-flags: --target aarch64-unknown-none -Zsanitizer=shadow-call-stack
2+
//@ error-pattern: shadow-call-stack sanitizer is not supported for this target
3+
//@ dont-check-compiler-stderr
4+
//@ needs-llvm-components: aarch64
5+
6+
#![allow(internal_features)]
7+
#![crate_type = "rlib"]
8+
#![feature(no_core, lang_items)]
9+
#![no_core]
10+
11+
#[lang = "sized"]
12+
trait Sized {}
13+
14+
#[no_mangle]
15+
pub fn foo() {}

0 commit comments

Comments
 (0)