Skip to content

Commit 857a55b

Browse files
committed
Auto merge of #65497 - choller:master, r=tmiasko
Avoid injecting sanitizer runtimes into staticlibs (#64629). This fixes the remaining issue in `creader.rs` and also fixes the expected test failure. I have explicitly turned the `$(CC)` call into a negative check with the `!` to ensure that this command is really failing (if it is not, then either the runtime is attached to the lib or the lib has not been instrumented and both would be an error). I've also borrowed `program.rs` and the additional `rustc` invocation from @tmiasko 's PR since he pointed out that using `-fsanitize=address` with `$(CC)` for linking could fail if the sanitizer runtimes on the system are incompatible. With this toolchain I was able to compile Firefox locally without any linker errors. I am still seeing races with Rust in TSan but I assume that is because I did not build with `-Z build-std`.
2 parents 7bf377f + a2feb9c commit 857a55b

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/librustc_metadata/creader.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -738,10 +738,10 @@ impl<'a> CrateLoader<'a> {
738738
if !self.sess.crate_types.borrow().iter().all(|ct| {
739739
match *ct {
740740
// Link the runtime
741-
config::CrateType::Staticlib |
742741
config::CrateType::Executable => true,
743742
// This crate will be compiled with the required
744743
// instrumentation pass
744+
config::CrateType::Staticlib |
745745
config::CrateType::Rlib |
746746
config::CrateType::Dylib |
747747
config::CrateType::Cdylib =>

src/test/run-make-fulldeps/sanitizer-staticlib-link/Makefile

+10-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@
44

55
-include ../tools.mk
66

7-
# This test builds a staticlib, then an executable that links to it.
8-
# The staticlib and executable both are compiled with address sanitizer,
9-
# and we assert that a fault in the staticlib is correctly detected.
7+
# This test first builds a staticlib with AddressSanitizer and checks that
8+
# linking it to an executable fails due to the missing sanitizer runtime.
9+
# It then builds an executable linking to the staticlib and checks that
10+
# the fault in the staticlib is detected correctly.
11+
12+
# Note that checking for the link failure actually checks two things at once:
13+
# 1) That the library has the sanitizer intrumentation
14+
# 2) and that library does not have the sanitizer runtime
1015

1116
all:
1217
$(RUSTC) -g -Z sanitizer=address --crate-type staticlib --target $(TARGET) library.rs
13-
$(CC) program.c $(call STATICLIB,library) $(call OUT_EXE,program) $(EXTRACFLAGS) $(EXTRACXXFLAGS)
18+
! $(CC) program.c $(call STATICLIB,library) $(call OUT_EXE,program) $(EXTRACFLAGS) $(EXTRACXXFLAGS)
19+
$(RUSTC) -g -Z sanitizer=address --crate-type bin --target $(TARGET) -L . program.rs
1420
LD_LIBRARY_PATH=$(TMPDIR) $(TMPDIR)/program 2>&1 | $(CGREP) stack-buffer-overflow
1521

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#[link(name = "library")]
2+
extern {
3+
fn overflow();
4+
}
5+
6+
fn main() {
7+
unsafe {
8+
overflow();
9+
}
10+
}

0 commit comments

Comments
 (0)