Skip to content

Commit 5986dd8

Browse files
committed
Auto merge of #79883 - frewsxcv:frewsxcv-san, r=shepmaster
Enable ASan, TSan, UBSan for aarch64-apple-darwin. I confirmed ASan, TSan, UBSan all work for me locally with `clang` on my new Macbook Air. ~This requires rust-lang/llvm-project#86
2 parents f6b6d5c + d482de3 commit 5986dd8

File tree

8 files changed

+73
-18
lines changed

8 files changed

+73
-18
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ fn link_sanitizer_runtime(sess: &Session, linker: &mut dyn Linker, name: &str) {
895895
.unwrap_or_default();
896896

897897
match sess.opts.target_triple.triple() {
898-
"x86_64-apple-darwin" => {
898+
"aarch64-apple-darwin" | "x86_64-apple-darwin" => {
899899
// On Apple platforms, the sanitizer is always built as a dylib, and
900900
// LLVM will link to `@rpath/*.dylib`, so we need to specify an
901901
// rpath to the library as well (the rpath should be absolute, see

compiler/rustc_session/src/session.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1522,18 +1522,24 @@ fn validate_commandline_args_with_session_available(sess: &Session) {
15221522
}
15231523

15241524
const ASAN_SUPPORTED_TARGETS: &[&str] = &[
1525+
"aarch64-apple-darwin",
15251526
"aarch64-fuchsia",
15261527
"aarch64-unknown-linux-gnu",
15271528
"x86_64-apple-darwin",
15281529
"x86_64-fuchsia",
15291530
"x86_64-unknown-freebsd",
15301531
"x86_64-unknown-linux-gnu",
15311532
];
1532-
const LSAN_SUPPORTED_TARGETS: &[&str] =
1533-
&["aarch64-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu"];
1533+
const LSAN_SUPPORTED_TARGETS: &[&str] = &[
1534+
"aarch64-apple-darwin",
1535+
"aarch64-unknown-linux-gnu",
1536+
"x86_64-apple-darwin",
1537+
"x86_64-unknown-linux-gnu",
1538+
];
15341539
const MSAN_SUPPORTED_TARGETS: &[&str] =
15351540
&["aarch64-unknown-linux-gnu", "x86_64-unknown-freebsd", "x86_64-unknown-linux-gnu"];
15361541
const TSAN_SUPPORTED_TARGETS: &[&str] = &[
1542+
"aarch64-apple-darwin",
15371543
"aarch64-unknown-linux-gnu",
15381544
"x86_64-apple-darwin",
15391545
"x86_64-unknown-freebsd",

src/bootstrap/compile.rs

+27-9
Original file line numberDiff line numberDiff line change
@@ -356,15 +356,12 @@ fn copy_sanitizers(
356356
let dst = libdir.join(&runtime.name);
357357
builder.copy(&runtime.path, &dst);
358358

359-
if target == "x86_64-apple-darwin" {
360-
// Update the library install name reflect the fact it has been renamed.
361-
let status = Command::new("install_name_tool")
362-
.arg("-id")
363-
.arg(format!("@rpath/{}", runtime.name))
364-
.arg(&dst)
365-
.status()
366-
.expect("failed to execute `install_name_tool`");
367-
assert!(status.success());
359+
if target == "x86_64-apple-darwin" || target == "aarch64-apple-darwin" {
360+
// Update the library’s install name to reflect that it has has been renamed.
361+
apple_darwin_update_library_name(&dst, &format!("@rpath/{}", &runtime.name));
362+
// Upon renaming the install name, the code signature of the file will invalidate,
363+
// so we will sign it again.
364+
apple_darwin_sign_file(&dst);
368365
}
369366

370367
target_deps.push(dst);
@@ -373,6 +370,27 @@ fn copy_sanitizers(
373370
target_deps
374371
}
375372

373+
fn apple_darwin_update_library_name(library_path: &Path, new_name: &str) {
374+
let status = Command::new("install_name_tool")
375+
.arg("-id")
376+
.arg(new_name)
377+
.arg(library_path)
378+
.status()
379+
.expect("failed to execute `install_name_tool`");
380+
assert!(status.success());
381+
}
382+
383+
fn apple_darwin_sign_file(file_path: &Path) {
384+
let status = Command::new("codesign")
385+
.arg("-f") // Force to rewrite the existing signature
386+
.arg("-s")
387+
.arg("-")
388+
.arg(file_path)
389+
.status()
390+
.expect("failed to execute `codesign`");
391+
assert!(status.success());
392+
}
393+
376394
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
377395
pub struct StartupObjects {
378396
pub compiler: Compiler,

src/bootstrap/native.rs

+1
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,7 @@ fn supported_sanitizers(
802802
};
803803

804804
match &*target.triple {
805+
"aarch64-apple-darwin" => darwin_libs("osx", &["asan", "lsan", "tsan"]),
805806
"aarch64-fuchsia" => common_libs("fuchsia", "aarch64", &["asan"]),
806807
"aarch64-unknown-linux-gnu" => {
807808
common_libs("linux", "aarch64", &["asan", "lsan", "msan", "tsan"])

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

+26-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ with runtime flag `ASAN_OPTIONS=detect_leaks=1` on macOS.
3131

3232
AddressSanitizer is supported on the following targets:
3333

34+
* `aarch64-apple-darwin`
35+
* `aarch64-fuchsia`
36+
* `aarch64-unknown-linux-gnu`
3437
* `x86_64-apple-darwin`
38+
* `x86_64-fuchsia`
39+
* `x86_64-unknown-freebsd`
3540
* `x86_64-unknown-linux-gnu`
3641

3742
AddressSanitizer works with non-instrumented code although it will impede its
@@ -169,10 +174,26 @@ Shadow byte legend (one shadow byte represents 8 application bytes):
169174
==39249==ABORTING
170175
```
171176
177+
# LeakSanitizer
178+
179+
LeakSanitizer is run-time memory leak detector.
180+
181+
LeakSanitizer is supported on the following targets:
182+
183+
* `aarch64-apple-darwin`
184+
* `aarch64-unknown-linux-gnu`
185+
* `x86_64-apple-darwin`
186+
* `x86_64-unknown-linux-gnu`
187+
172188
# MemorySanitizer
173189
174-
MemorySanitizer is detector of uninitialized reads. It is only supported on the
175-
`x86_64-unknown-linux-gnu` target.
190+
MemorySanitizer is detector of uninitialized reads.
191+
192+
MemorySanitizer is supported on the following targets:
193+
194+
* `aarch64-unknown-linux-gnu`
195+
* `x86_64-unknown-freebsd`
196+
* `x86_64-unknown-linux-gnu`
176197
177198
MemorySanitizer requires all program code to be instrumented. C/C++ dependencies
178199
need to be recompiled using Clang with `-fsanitize=memory` option. Failing to
@@ -219,7 +240,10 @@ $ cargo run -Zbuild-std --target x86_64-unknown-linux-gnu
219240
ThreadSanitizer is a data race detection tool. It is supported on the following
220241
targets:
221242
243+
* `aarch64-apple-darwin`
244+
* `aarch64-unknown-linux-gnu`
222245
* `x86_64-apple-darwin`
246+
* `x86_64-unknown-freebsd`
223247
* `x86_64-unknown-linux-gnu`
224248
225249
To work correctly ThreadSanitizer needs to be "aware" of all synchronization

src/test/codegen/sanitizer-no-sanitize.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Verifies that no_sanitze attribute can be used to
1+
// Verifies that no_sanitize attribute can be used to
22
// selectively disable sanitizer instrumentation.
33
//
44
// needs-sanitizer-address
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: `-Zsanitizer=leak` only works with targets: aarch64-unknown-linux-gnu, x86_64-apple-darwin, x86_64-unknown-linux-gnu
1+
error: `-Zsanitizer=leak` only works with targets: aarch64-apple-darwin, aarch64-unknown-linux-gnu, x86_64-apple-darwin, x86_64-unknown-linux-gnu
22

33
error: aborting due to previous error
44

src/tools/compiletest/src/util.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ const ARCH_TABLE: &[(&str, &str)] = &[
8282
];
8383

8484
pub const ASAN_SUPPORTED_TARGETS: &[&str] = &[
85+
"aarch64-apple-darwin",
8586
"aarch64-fuchsia",
8687
"aarch64-unknown-linux-gnu",
8788
"x86_64-apple-darwin",
@@ -90,13 +91,18 @@ pub const ASAN_SUPPORTED_TARGETS: &[&str] = &[
9091
"x86_64-unknown-linux-gnu",
9192
];
9293

93-
pub const LSAN_SUPPORTED_TARGETS: &[&str] =
94-
&["aarch64-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu"];
94+
pub const LSAN_SUPPORTED_TARGETS: &[&str] = &[
95+
"aarch64-apple-darwin",
96+
"aarch64-unknown-linux-gnu",
97+
"x86_64-apple-darwin",
98+
"x86_64-unknown-linux-gnu",
99+
];
95100

96101
pub const MSAN_SUPPORTED_TARGETS: &[&str] =
97102
&["aarch64-unknown-linux-gnu", "x86_64-unknown-freebsd", "x86_64-unknown-linux-gnu"];
98103

99104
pub const TSAN_SUPPORTED_TARGETS: &[&str] = &[
105+
"aarch64-apple-darwin",
100106
"aarch64-unknown-linux-gnu",
101107
"x86_64-apple-darwin",
102108
"x86_64-unknown-freebsd",

0 commit comments

Comments
 (0)