Skip to content

Commit b315760

Browse files
committed
Rename sanitizer runtime libraries on OSX
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
1 parent 9653f79 commit b315760

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

src/bootstrap/compile.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ impl Step for StdLink {
249249

250250
fn copy_apple_sanitizer_dylibs(builder: &Builder, native_dir: &Path, platform: &str, into: &Path) {
251251
for &sanitizer in &["asan", "tsan"] {
252-
let filename = format!("libclang_rt.{}_{}_dynamic.dylib", sanitizer, platform);
252+
let filename = format!("lib__rustc__clang_rt.{}_{}_dynamic.dylib", sanitizer, platform);
253253
let mut src_path = native_dir.join(sanitizer);
254254
src_path.push("build");
255255
src_path.push("lib");

src/build_helper/lib.rs

+34-3
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,37 @@ pub struct NativeLibBoilerplate {
178178
pub out_dir: PathBuf,
179179
}
180180

181+
impl NativeLibBoilerplate {
182+
/// On OSX we don't want to ship the exact filename that compiler-rt builds.
183+
/// This conflicts with the system and ours is likely a wildly different
184+
/// version, so they can't be substituted.
185+
///
186+
/// As a result, we rename it here but we need to also use
187+
/// `install_name_tool` on OSX to rename the commands listed inside of it to
188+
/// ensure it's linked against correctly.
189+
pub fn fixup_sanitizer_lib_name(&self, sanitizer_name: &str) {
190+
if env::var("TARGET").unwrap() != "x86_64-apple-darwin" {
191+
return
192+
}
193+
194+
let dir = self.out_dir.join("build/lib/darwin");
195+
let name = format!("clang_rt.{}_osx_dynamic", sanitizer_name);
196+
let src = dir.join(&format!("lib{}.dylib", name));
197+
let new_name = format!("lib__rustc__{}.dylib", name);
198+
let dst = dir.join(&new_name);
199+
200+
println!("{} => {}", src.display(), dst.display());
201+
fs::rename(&src, &dst).unwrap();
202+
let status = Command::new("install_name_tool")
203+
.arg("-id")
204+
.arg(format!("@rpath/{}", new_name))
205+
.arg(&dst)
206+
.status()
207+
.expect("failed to execute `install_name_tool`");
208+
assert!(status.success());
209+
}
210+
}
211+
181212
impl Drop for NativeLibBoilerplate {
182213
fn drop(&mut self) {
183214
if !thread::panicking() {
@@ -229,7 +260,7 @@ pub fn native_lib_boilerplate(
229260
pub fn sanitizer_lib_boilerplate(sanitizer_name: &str)
230261
-> Result<(NativeLibBoilerplate, String), ()>
231262
{
232-
let (link_name, search_path, dynamic) = match &*env::var("TARGET").unwrap() {
263+
let (link_name, search_path, apple) = match &*env::var("TARGET").unwrap() {
233264
"x86_64-unknown-linux-gnu" => (
234265
format!("clang_rt.{}-x86_64", sanitizer_name),
235266
"build/lib/linux",
@@ -242,8 +273,8 @@ pub fn sanitizer_lib_boilerplate(sanitizer_name: &str)
242273
),
243274
_ => return Err(()),
244275
};
245-
let to_link = if dynamic {
246-
format!("dylib={}", link_name)
276+
let to_link = if apple {
277+
format!("dylib=__rustc__{}", link_name)
247278
} else {
248279
format!("static={}", link_name)
249280
};

src/librustc_asan/build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ fn main() {
3131
.out_dir(&native.out_dir)
3232
.build_target(&target)
3333
.build();
34+
native.fixup_sanitizer_lib_name("asan");
3435
}
3536
println!("cargo:rerun-if-env-changed=LLVM_CONFIG");
3637
}

src/librustc_tsan/build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ fn main() {
3131
.out_dir(&native.out_dir)
3232
.build_target(&target)
3333
.build();
34+
native.fixup_sanitizer_lib_name("tsan");
3435
}
3536
println!("cargo:rerun-if-env-changed=LLVM_CONFIG");
3637
}

0 commit comments

Comments
 (0)