]> git.lizzy.rs Git - rust.git/commitdiff
Rename sanitizer runtime libraries on OSX
authorAlex Crichton <alex@alexcrichton.com>
Sat, 29 Sep 2018 19:37:12 +0000 (12:37 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Sat, 29 Sep 2018 21:29:05 +0000 (14:29 -0700)
Currently we ship sanitizer libraries as they're built, but these names
unfortunately conflict with the names of the sanitizer libraries
installed on the system. If a crate, for example, links in C code that
wants to use the system sanitizer and the Rust code doesn't use
sanitizers at all, then using `cargo` may accidentally pull in the
Rust-installed sanitizer library due to a conflict in names.

This change is intended to be entirely transparent for Rust users of
sanitizers, it should only hopefully improve our story with other users!

Closes #54134

src/bootstrap/compile.rs
src/build_helper/lib.rs
src/librustc_asan/build.rs
src/librustc_tsan/build.rs

index 608f2c982c2a52485abfd8751054f189a759cf5f..7d235743c2c46834fc3a8ebfaabfa5f719b081be 100644 (file)
@@ -249,7 +249,7 @@ fn run(self, builder: &Builder) {
 
 fn copy_apple_sanitizer_dylibs(builder: &Builder, native_dir: &Path, platform: &str, into: &Path) {
     for &sanitizer in &["asan", "tsan"] {
-        let filename = format!("libclang_rt.{}_{}_dynamic.dylib", sanitizer, platform);
+        let filename = format!("lib__rustc__clang_rt.{}_{}_dynamic.dylib", sanitizer, platform);
         let mut src_path = native_dir.join(sanitizer);
         src_path.push("build");
         src_path.push("lib");
index 1cbb8e49bfa15e0579ebeb0e3b37aa0d0bda10db..ec94f57861dbeb1efce4a827795c0dfb954f73d9 100644 (file)
@@ -178,6 +178,37 @@ pub struct NativeLibBoilerplate {
     pub out_dir: PathBuf,
 }
 
+impl NativeLibBoilerplate {
+    /// On OSX we don't want to ship the exact filename that compiler-rt builds.
+    /// This conflicts with the system and ours is likely a wildly different
+    /// version, so they can't be substituted.
+    ///
+    /// As a result, we rename it here but we need to also use
+    /// `install_name_tool` on OSX to rename the commands listed inside of it to
+    /// ensure it's linked against correctly.
+    pub fn fixup_sanitizer_lib_name(&self, sanitizer_name: &str) {
+        if env::var("TARGET").unwrap() != "x86_64-apple-darwin" {
+            return
+        }
+
+        let dir = self.out_dir.join("build/lib/darwin");
+        let name = format!("clang_rt.{}_osx_dynamic", sanitizer_name);
+        let src = dir.join(&format!("lib{}.dylib", name));
+        let new_name = format!("lib__rustc__{}.dylib", name);
+        let dst = dir.join(&new_name);
+
+        println!("{} => {}", src.display(), dst.display());
+        fs::rename(&src, &dst).unwrap();
+        let status = Command::new("install_name_tool")
+            .arg("-id")
+            .arg(format!("@rpath/{}", new_name))
+            .arg(&dst)
+            .status()
+            .expect("failed to execute `install_name_tool`");
+        assert!(status.success());
+    }
+}
+
 impl Drop for NativeLibBoilerplate {
     fn drop(&mut self) {
         if !thread::panicking() {
@@ -229,7 +260,7 @@ pub fn native_lib_boilerplate(
 pub fn sanitizer_lib_boilerplate(sanitizer_name: &str)
     -> Result<(NativeLibBoilerplate, String), ()>
 {
-    let (link_name, search_path, dynamic) = match &*env::var("TARGET").unwrap() {
+    let (link_name, search_path, apple) = match &*env::var("TARGET").unwrap() {
         "x86_64-unknown-linux-gnu" => (
             format!("clang_rt.{}-x86_64", sanitizer_name),
             "build/lib/linux",
@@ -242,8 +273,8 @@ pub fn sanitizer_lib_boilerplate(sanitizer_name: &str)
         ),
         _ => return Err(()),
     };
-    let to_link = if dynamic {
-        format!("dylib={}", link_name)
+    let to_link = if apple {
+        format!("dylib=__rustc__{}", link_name)
     } else {
         format!("static={}", link_name)
     };
index b8614c520e7cfa601857b4f6017d44a3e58ccdfe..a5905df57c8cfd62f9624adb2bc030ab39d76cd6 100644 (file)
@@ -31,6 +31,7 @@ fn main() {
             .out_dir(&native.out_dir)
             .build_target(&target)
             .build();
+        native.fixup_sanitizer_lib_name("asan");
     }
     println!("cargo:rerun-if-env-changed=LLVM_CONFIG");
 }
index 38595478c74337edde1bcc413704c37e190211b3..d23c71e2c1286cf9fdecf0eff3948e2a9fa70dc4 100644 (file)
@@ -31,6 +31,7 @@ fn main() {
             .out_dir(&native.out_dir)
             .build_target(&target)
             .build();
+        native.fixup_sanitizer_lib_name("tsan");
     }
     println!("cargo:rerun-if-env-changed=LLVM_CONFIG");
 }