From 5940c193159a40fc9ebecc0de1482ba3d0a7a2dc Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Wed, 9 Dec 2020 23:53:53 -0500 Subject: [PATCH 1/9] Enable ASan, TSan, UBSan for aarch64-apple-darwin. --- compiler/rustc_codegen_ssa/src/back/link.rs | 3 ++- compiler/rustc_session/src/session.rs | 10 ++++++++-- src/bootstrap/native.rs | 1 + src/tools/compiletest/src/util.rs | 10 ++++++++-- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 70cf876a08a..0f4408a8fc6 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -819,7 +819,8 @@ fn link_sanitizer_runtime(sess: &Session, linker: &mut dyn Linker, name: &str) { .unwrap_or_default(); match sess.opts.target_triple.triple() { - "x86_64-apple-darwin" => { + "aarch64-apple-darwin" + | "x86_64-apple-darwin" => { // On Apple platforms, the sanitizer is always built as a dylib, and // LLVM will link to `@rpath/*.dylib`, so we need to specify an // rpath to the library as well (the rpath should be absolute, see diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 4e269f3172c..6ca9050339f 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -1520,6 +1520,7 @@ fn validate_commandline_args_with_session_available(sess: &Session) { } const ASAN_SUPPORTED_TARGETS: &[&str] = &[ + "aarch64-apple-darwin", "aarch64-fuchsia", "aarch64-unknown-linux-gnu", "x86_64-apple-darwin", @@ -1527,11 +1528,16 @@ fn validate_commandline_args_with_session_available(sess: &Session) { "x86_64-unknown-freebsd", "x86_64-unknown-linux-gnu", ]; - const LSAN_SUPPORTED_TARGETS: &[&str] = - &["aarch64-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu"]; + const LSAN_SUPPORTED_TARGETS: &[&str] = &[ + "aarch64-apple-darwin", + "aarch64-unknown-linux-gnu", + "x86_64-apple-darwin", + "x86_64-unknown-linux-gnu", + ]; const MSAN_SUPPORTED_TARGETS: &[&str] = &["aarch64-unknown-linux-gnu", "x86_64-unknown-freebsd", "x86_64-unknown-linux-gnu"]; const TSAN_SUPPORTED_TARGETS: &[&str] = &[ + "aarch64-apple-darwin", "aarch64-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-unknown-freebsd", diff --git a/src/bootstrap/native.rs b/src/bootstrap/native.rs index d716b23af60..6412df3fd90 100644 --- a/src/bootstrap/native.rs +++ b/src/bootstrap/native.rs @@ -802,6 +802,7 @@ fn supported_sanitizers( }; match &*target.triple { + "aarch64-apple-darwin" => darwin_libs("osx", &["asan", "lsan", "tsan"]), "aarch64-fuchsia" => common_libs("fuchsia", "aarch64", &["asan"]), "aarch64-unknown-linux-gnu" => { common_libs("linux", "aarch64", &["asan", "lsan", "msan", "tsan"]) diff --git a/src/tools/compiletest/src/util.rs b/src/tools/compiletest/src/util.rs index 4f77e719fba..1647df8044c 100644 --- a/src/tools/compiletest/src/util.rs +++ b/src/tools/compiletest/src/util.rs @@ -82,6 +82,7 @@ const ARCH_TABLE: &[(&str, &str)] = &[ ]; pub const ASAN_SUPPORTED_TARGETS: &[&str] = &[ + "aarch64-apple-darwin", "aarch64-fuchsia", "aarch64-unknown-linux-gnu", "x86_64-apple-darwin", @@ -90,13 +91,18 @@ pub const ASAN_SUPPORTED_TARGETS: &[&str] = &[ "x86_64-unknown-linux-gnu", ]; -pub const LSAN_SUPPORTED_TARGETS: &[&str] = - &["aarch64-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu"]; +pub const LSAN_SUPPORTED_TARGETS: &[&str] = &[ + "aarch64-apple-darwin", + "aarch64-unknown-linux-gnu", + "x86_64-apple-darwin", + "x86_64-unknown-linux-gnu", +]; pub const MSAN_SUPPORTED_TARGETS: &[&str] = &["aarch64-unknown-linux-gnu", "x86_64-unknown-freebsd", "x86_64-unknown-linux-gnu"]; pub const TSAN_SUPPORTED_TARGETS: &[&str] = &[ + "aarch64-apple-darwin", "aarch64-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-unknown-freebsd", From 74207767c0fb0ad3d5df8be95f1b750d5c2c0d84 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Thu, 10 Dec 2020 00:18:30 -0500 Subject: [PATCH 2/9] rustfmt --- compiler/rustc_codegen_ssa/src/back/link.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 0f4408a8fc6..46febf049c3 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -819,8 +819,7 @@ fn link_sanitizer_runtime(sess: &Session, linker: &mut dyn Linker, name: &str) { .unwrap_or_default(); match sess.opts.target_triple.triple() { - "aarch64-apple-darwin" - | "x86_64-apple-darwin" => { + "aarch64-apple-darwin" | "x86_64-apple-darwin" => { // On Apple platforms, the sanitizer is always built as a dylib, and // LLVM will link to `@rpath/*.dylib`, so we need to specify an // rpath to the library as well (the rpath should be absolute, see From d1911dd075d11239e69e3cc896a5e50a1108f2f5 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Thu, 10 Dec 2020 00:18:43 -0500 Subject: [PATCH 3/9] If "aarch64-apple-darwin", copy sanitizers. --- src/bootstrap/compile.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs index cdad1cb4d49..6d1ad27a67a 100644 --- a/src/bootstrap/compile.rs +++ b/src/bootstrap/compile.rs @@ -355,7 +355,7 @@ fn copy_sanitizers( let dst = libdir.join(&runtime.name); builder.copy(&runtime.path, &dst); - if target == "x86_64-apple-darwin" { + if target == "x86_64-apple-darwin" || target == "aarch64-apple-darwin" { // Update the library install name reflect the fact it has been renamed. let status = Command::new("install_name_tool") .arg("-id") From 01029e2abcc76c04e7ed7917c07f13a73b804b60 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Thu, 10 Dec 2020 09:02:30 -0500 Subject: [PATCH 4/9] Fix sanitizer test output matching --- src/test/ui/sanitize/unsupported-target.stderr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/sanitize/unsupported-target.stderr b/src/test/ui/sanitize/unsupported-target.stderr index f5961a11b1f..093678707fb 100644 --- a/src/test/ui/sanitize/unsupported-target.stderr +++ b/src/test/ui/sanitize/unsupported-target.stderr @@ -1,4 +1,4 @@ -error: `-Zsanitizer=leak` only works with targets: aarch64-unknown-linux-gnu, x86_64-apple-darwin, x86_64-unknown-linux-gnu +error: `-Zsanitizer=leak` only works with targets: aarch64-apple-darwin, aarch64-unknown-linux-gnu, x86_64-apple-darwin, x86_64-unknown-linux-gnu error: aborting due to previous error From d5173334465b9d58f3a15d59bd496af1c34f6f45 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Thu, 10 Dec 2020 17:54:06 -0500 Subject: [PATCH 5/9] Use fork of llvm-project that includes Apple Silicon fix --- .gitmodules | 4 ++-- src/llvm-project | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitmodules b/.gitmodules index 984113151de..1f850bc3300 100644 --- a/.gitmodules +++ b/.gitmodules @@ -36,8 +36,8 @@ url = https://github.com/rust-lang/edition-guide.git [submodule "src/llvm-project"] path = src/llvm-project - url = https://github.com/rust-lang/llvm-project.git - branch = rustc/11.0-2020-10-12 + url = https://github.com/frewsxcv/llvm-project.git + branch = apple-silicon-sanitizers [submodule "src/doc/embedded-book"] path = src/doc/embedded-book url = https://github.com/rust-embedded/book.git diff --git a/src/llvm-project b/src/llvm-project index 8d78ad13896..dcceb131c59 160000 --- a/src/llvm-project +++ b/src/llvm-project @@ -1 +1 @@ -Subproject commit 8d78ad13896b955f630714f386a95ed91b237e3d +Subproject commit dcceb131c59e48e49b4b9af09094d8daed4e6a9b From 25bfa153710c24ed2f7f6fa0dd3c0758a1c27973 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Thu, 10 Dec 2020 18:51:30 -0500 Subject: [PATCH 6/9] Manually code-sign after running install_name_tool --- src/bootstrap/compile.rs | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs index 6d1ad27a67a..3cfb2f2836d 100644 --- a/src/bootstrap/compile.rs +++ b/src/bootstrap/compile.rs @@ -356,14 +356,11 @@ fn copy_sanitizers( builder.copy(&runtime.path, &dst); if target == "x86_64-apple-darwin" || target == "aarch64-apple-darwin" { - // Update the library install name reflect the fact it has been renamed. - let status = Command::new("install_name_tool") - .arg("-id") - .arg(format!("@rpath/{}", runtime.name)) - .arg(&dst) - .status() - .expect("failed to execute `install_name_tool`"); - assert!(status.success()); + // Update the library’s install name to reflect that it has has been renamed. + apple_darwin_update_library_name(&dst, &format!("@rpath/{}", &runtime.name)); + // Upon renaming the install name, the code signature of the file will invalidate, + // so we will sign it again. + apple_darwin_sign_file(&dst); } target_deps.push(dst); @@ -372,6 +369,27 @@ fn copy_sanitizers( target_deps } +fn apple_darwin_update_library_name(library_path: &Path, new_name: &str) { + let status = Command::new("install_name_tool") + .arg("-id") + .arg(new_name) + .arg(library_path) + .status() + .expect("failed to execute `install_name_tool`"); + assert!(status.success()); +} + +fn apple_darwin_sign_file(file_path: &Path) { + let status = Command::new("codesign") + .arg("-f") // Force to rewrite the existing signature + .arg("-s") + .arg("-") + .arg(file_path) + .status() + .expect("failed to execute `codesign`"); + assert!(status.success()); +} + #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub struct StartupObjects { pub compiler: Compiler, From 5ab1602a1772320758c6d9dedd8fd1cc82eff119 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Thu, 10 Dec 2020 19:06:29 -0500 Subject: [PATCH 7/9] Fix typo in code comment --- src/test/codegen/sanitizer-no-sanitize.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/codegen/sanitizer-no-sanitize.rs b/src/test/codegen/sanitizer-no-sanitize.rs index 1b2b18822e6..fb9d249da03 100644 --- a/src/test/codegen/sanitizer-no-sanitize.rs +++ b/src/test/codegen/sanitizer-no-sanitize.rs @@ -1,4 +1,4 @@ -// Verifies that no_sanitze attribute can be used to +// Verifies that no_sanitize attribute can be used to // selectively disable sanitizer instrumentation. // // needs-sanitizer-address From 7ee606f5142af7af0edec77abf34a40f5bae19d6 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Thu, 10 Dec 2020 19:11:58 -0500 Subject: [PATCH 8/9] Update sanitizer supported docs to reflect reality --- .../src/compiler-flags/sanitizer.md | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/doc/unstable-book/src/compiler-flags/sanitizer.md b/src/doc/unstable-book/src/compiler-flags/sanitizer.md index 93908e9190e..d03d5c75014 100644 --- a/src/doc/unstable-book/src/compiler-flags/sanitizer.md +++ b/src/doc/unstable-book/src/compiler-flags/sanitizer.md @@ -31,7 +31,12 @@ with runtime flag `ASAN_OPTIONS=detect_leaks=1` on macOS. AddressSanitizer is supported on the following targets: +* `aarch64-apple-darwin` +* `aarch64-fuchsia` +* `aarch64-unknown-linux-gnu` * `x86_64-apple-darwin` +* `x86_64-fuchsia` +* `x86_64-unknown-freebsd` * `x86_64-unknown-linux-gnu` 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): ==39249==ABORTING ``` +# LeakSanitizer + +LeakSanitizer is run-time memory leak detector. + +LeakSanitizer is supported on the following targets: + +* `aarch64-apple-darwin` +* `aarch64-unknown-linux-gnu` +* `x86_64-apple-darwin` +* `x86_64-unknown-linux-gnu` + # MemorySanitizer -MemorySanitizer is detector of uninitialized reads. It is only supported on the -`x86_64-unknown-linux-gnu` target. +MemorySanitizer is detector of uninitialized reads. + +MemorySanitizer is supported on the following targets: + +* `aarch64-unknown-linux-gnu` +* `x86_64-unknown-freebsd` +* `x86_64-unknown-linux-gnu` MemorySanitizer requires all program code to be instrumented. C/C++ dependencies 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 ThreadSanitizer is a data race detection tool. It is supported on the following targets: +* `aarch64-apple-darwin` +* `aarch64-unknown-linux-gnu` * `x86_64-apple-darwin` +* `x86_64-unknown-freebsd` * `x86_64-unknown-linux-gnu` To work correctly ThreadSanitizer needs to be "aware" of all synchronization From 26cc060756d0456b17fdc53ac5d34e7f7bdc873d Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Fri, 11 Dec 2020 09:26:29 -0500 Subject: [PATCH 9/9] Switch back to rust-lang/llvm-project now that the patch has merged --- .gitmodules | 4 ++-- src/llvm-project | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitmodules b/.gitmodules index 1f850bc3300..984113151de 100644 --- a/.gitmodules +++ b/.gitmodules @@ -36,8 +36,8 @@ url = https://github.com/rust-lang/edition-guide.git [submodule "src/llvm-project"] path = src/llvm-project - url = https://github.com/frewsxcv/llvm-project.git - branch = apple-silicon-sanitizers + url = https://github.com/rust-lang/llvm-project.git + branch = rustc/11.0-2020-10-12 [submodule "src/doc/embedded-book"] path = src/doc/embedded-book url = https://github.com/rust-embedded/book.git diff --git a/src/llvm-project b/src/llvm-project index dcceb131c59..d5c516edf78 160000 --- a/src/llvm-project +++ b/src/llvm-project @@ -1 +1 @@ -Subproject commit dcceb131c59e48e49b4b9af09094d8daed4e6a9b +Subproject commit d5c516edf7897b377ac39ccc5d7ca52a95124408