Skip to content

Commit 6fc3880

Browse files
committed
hwaddress: automatically add -Ctarget-feature=tagged-globals
1 parent e4fdb55 commit 6fc3880

6 files changed

Lines changed: 16 additions & 18 deletions

File tree

compiler/rustc_codegen_llvm/src/llvm_util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ fn llvm_features_by_flags(sess: &Session, features: &mut Vec<String>) {
630630
}
631631

632632
target_features::retpoline_features_by_flags(sess, features);
633+
target_features::sanitizer_features_by_flags(sess, features);
633634

634635
// -Zfixed-x18
635636
if sess.opts.unstable_opts.fixed_x18 {

compiler/rustc_codegen_ssa/src/target_features.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_session::Session;
1010
use rustc_session::lint::builtin::AARCH64_SOFTFLOAT_NEON;
1111
use rustc_session::parse::feature_err;
1212
use rustc_span::{Span, Symbol, edit_distance, sym};
13-
use rustc_target::spec::Arch;
13+
use rustc_target::spec::{Arch, SanitizerSet};
1414
use rustc_target::target_features::{RUSTC_SPECIFIC_FEATURES, Stability};
1515
use smallvec::SmallVec;
1616

@@ -460,6 +460,15 @@ pub fn retpoline_features_by_flags(sess: &Session, features: &mut Vec<String>) {
460460
}
461461
}
462462

463+
/// Computes the backend target features to be added to account for sanitizer flags.
464+
pub fn sanitizer_features_by_flags(sess: &Session, features: &mut Vec<String>) {
465+
// It's intentional that this is done only for non-kernel version of hwaddress. This matches
466+
// clang behavior.
467+
if sess.sanitizers().contains(SanitizerSet::HWADDRESS) {
468+
features.push("+tagged-globals".into());
469+
}
470+
}
471+
463472
pub(crate) fn provide(providers: &mut Providers) {
464473
*providers = Providers {
465474
rust_target_features: |tcx, cnum| {

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,6 @@ HWAddressSanitizer is supported on the following targets:
552552
* `aarch64-linux-android`
553553
* `aarch64-unknown-linux-gnu`
554554
555-
HWAddressSanitizer requires `tagged-globals` target feature to instrument
556-
globals. To enable this target feature compile with `-C
557-
target-feature=+tagged-globals`
558-
559555
See the [Clang HWAddressSanitizer documentation][clang-hwasan] for more details.
560556
561557
## Example
@@ -570,9 +566,8 @@ fn main() {
570566
```
571567
572568
```shell
573-
$ rustc main.rs -Zsanitizer=hwaddress -C target-feature=+tagged-globals -C
574-
linker=aarch64-linux-gnu-gcc -C link-arg=-fuse-ld=lld --target
575-
aarch64-unknown-linux-gnu
569+
$ rustc main.rs -Zsanitizer=hwaddress -Clinker=aarch64-linux-gnu-gcc
570+
-Clink-arg=-fuse-ld=lld --target aarch64-unknown-linux-gnu
576571
```
577572
578573
```shell

tests/codegen-llvm/sanitizer/hwasan-vs-khwasan.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extern crate minicore;
1818
// hwasan: @__hwasan_tls
1919
// hwasan: call void @llvm.hwasan.check.memaccess.shortgranules
2020
// hwasan: declare void @__hwasan_init()
21+
// hwasan: attributes #0 {{.*"target-features"=".*\+tagged-globals.*"}}
2122

2223
// The `__hwasan_tls` symbol is unconditionally declared by LLVM's `HWAddressSanitizer` pass.
2324
// However, in kernel mode KHWASAN does not actually use it (because shadow mapping is fixed
@@ -33,6 +34,7 @@ extern crate minicore;
3334
//
3435
// khwasan-NOT: @__hwasan_init
3536
// khwasan: call void @llvm.hwasan.check.memaccess.shortgranules
37+
// khwasan-NOT: attributes #0 {{.*"target-features"=".*\+tagged-globals.*"}}
3638
#[no_mangle]
3739
pub fn test(b: &mut u8) -> u8 {
3840
*b

tests/ui/sanitizer/hwaddress.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@ needs-sanitizer-support
22
//@ needs-sanitizer-hwaddress
33
//
4-
//@ compile-flags: -Z sanitizer=hwaddress -O -g -C target-feature=+tagged-globals -C unsafe-allow-abi-mismatch=sanitizer
4+
//@ compile-flags: -Z sanitizer=hwaddress -O -g -C unsafe-allow-abi-mismatch=sanitizer
55
//
66
//@ run-fail
77
//@ error-pattern: HWAddressSanitizer: tag-mismatch
@@ -15,5 +15,3 @@ fn main() {
1515
let code = unsafe { *xs.offset(4) };
1616
std::process::exit(code);
1717
}
18-
19-
//~? WARN unknown and unstable feature specified for `-Ctarget-feature`: `tagged-globals`

tests/ui/sanitizer/hwaddress.stderr

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)