Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_codegen_llvm/src/llvm_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ fn llvm_features_by_flags(sess: &Session, features: &mut Vec<String>) {
}

target_features::retpoline_features_by_flags(sess, features);
target_features::sanitizer_features_by_flags(sess, features);

// -Zfixed-x18
if sess.opts.unstable_opts.fixed_x18 {
Expand Down
11 changes: 10 additions & 1 deletion compiler/rustc_codegen_ssa/src/target_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_session::Session;
use rustc_session::lint::builtin::AARCH64_SOFTFLOAT_NEON;
use rustc_session::parse::feature_err;
use rustc_span::{Span, Symbol, edit_distance, sym};
use rustc_target::spec::Arch;
use rustc_target::spec::{Arch, SanitizerSet};
use rustc_target::target_features::{RUSTC_SPECIFIC_FEATURES, Stability};
use smallvec::SmallVec;

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

/// Computes the backend target features to be added to account for sanitizer flags.
pub fn sanitizer_features_by_flags(sess: &Session, features: &mut Vec<String>) {
// It's intentional that this is done only for non-kernel version of hwaddress. This matches
// clang behavior.
if sess.sanitizers().contains(SanitizerSet::HWADDRESS) {
features.push("+tagged-globals".into());
}
}

pub(crate) fn provide(providers: &mut Providers) {
*providers = Providers {
rust_target_features: |tcx, cnum| {
Expand Down
9 changes: 2 additions & 7 deletions src/doc/unstable-book/src/compiler-flags/sanitizer.md
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,6 @@ HWAddressSanitizer is supported on the following targets:
* `aarch64-linux-android`
* `aarch64-unknown-linux-gnu`

HWAddressSanitizer requires `tagged-globals` target feature to instrument
globals. To enable this target feature compile with `-C
target-feature=+tagged-globals`

See the [Clang HWAddressSanitizer documentation][clang-hwasan] for more details.

## Example
Expand All @@ -570,9 +566,8 @@ fn main() {
```

```shell
$ rustc main.rs -Zsanitizer=hwaddress -C target-feature=+tagged-globals -C
linker=aarch64-linux-gnu-gcc -C link-arg=-fuse-ld=lld --target
aarch64-unknown-linux-gnu
$ rustc main.rs -Zsanitizer=hwaddress -Clinker=aarch64-linux-gnu-gcc
-Clink-arg=-fuse-ld=lld --target aarch64-unknown-linux-gnu
```

```shell
Expand Down
2 changes: 2 additions & 0 deletions tests/codegen-llvm/sanitizer/hwasan-vs-khwasan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extern crate minicore;
// hwasan: @__hwasan_tls
// hwasan: call void @llvm.hwasan.check.memaccess.shortgranules
// hwasan: declare void @__hwasan_init()
// hwasan: attributes #0 {{.*"target-features"=".*\+tagged-globals.*"}}

// The `__hwasan_tls` symbol is unconditionally declared by LLVM's `HWAddressSanitizer` pass.
// However, in kernel mode KHWASAN does not actually use it (because shadow mapping is fixed
Expand All @@ -33,6 +34,7 @@ extern crate minicore;
//
// khwasan-NOT: @__hwasan_init
// khwasan: call void @llvm.hwasan.check.memaccess.shortgranules
// khwasan-NOT: attributes #0 {{.*"target-features"=".*\+tagged-globals.*"}}
#[no_mangle]
pub fn test(b: &mut u8) -> u8 {
*b
Expand Down
4 changes: 1 addition & 3 deletions tests/ui/sanitizer/hwaddress.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@ needs-sanitizer-support
//@ needs-sanitizer-hwaddress
//
//@ compile-flags: -Z sanitizer=hwaddress -O -g -C target-feature=+tagged-globals -C unsafe-allow-abi-mismatch=sanitizer
//@ compile-flags: -Z sanitizer=hwaddress -O -g -C unsafe-allow-abi-mismatch=sanitizer
//
//@ run-fail
//@ error-pattern: HWAddressSanitizer: tag-mismatch
Expand All @@ -15,5 +15,3 @@ fn main() {
let code = unsafe { *xs.offset(4) };
std::process::exit(code);
}

//~? WARN unknown and unstable feature specified for `-Ctarget-feature`: `tagged-globals`
7 changes: 0 additions & 7 deletions tests/ui/sanitizer/hwaddress.stderr

This file was deleted.

Loading