Skip to content

Commit e9022da

Browse files
authored
Rollup merge of rust-lang#65241 - tmiasko:no-std-san, r=nikomatsakis
build-std compatible sanitizer support ### Motivation When using `-Z sanitizer=*` feature it is essential that both user code and standard library is instrumented. Otherwise the utility of sanitizer will be limited, or its use will be impractical like in the case of memory sanitizer. The recently introduced cargo feature build-std makes it possible to rebuild standard library with arbitrary rustc flags. Unfortunately, those changes alone do not make it easy to rebuild standard library with sanitizers, since runtimes are dependencies of std that have to be build in specific environment, generally not available outside rustbuild process. Additionally rebuilding them requires presence of llvm-config and compiler-rt sources. The goal of changes proposed here is to make it possible to avoid rebuilding sanitizer runtimes when rebuilding the std, thus making it possible to instrument standard library for use with sanitizer with simple, although verbose command: ``` env CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGS=-Zsanitizer=thread cargo test -Zbuild-std --target x86_64-unknown-linux-gnu ``` ### Implementation * Sanitizer runtimes are no long packed into crates. Instead, libraries build from compiler-rt are used as is, after renaming them into `librusc_rt.*`. * rustc obtains runtimes from target libdir for default sysroot, so that they are not required in custom build sysroots created with build-std. * The runtimes are only linked-in into executables to address issue rust-lang#64629. (in previous design it was hard to avoid linking runtimes into static libraries produced by rustc as demonstrated by sanitizer-staticlib-link test, which still passes despite changes made in rust-lang#64780). * When custom llvm-config is specified during build process, the sanitizer runtimes will be obtained from there instead of begin rebuilding from sources in src/llvm-project/compiler-rt. This should be preferable since runtimes used should match instrumentation passes. For example there have been nine version of address sanitizer ABI. Note this marked as a draft PR, because it is currently untested on OS X (I would appreciate any help there). cc @kennytm, @japaric, @Firstyear, @choller
2 parents da5d7a7 + e42800b commit e9022da

File tree

41 files changed

+338
-640
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+338
-640
lines changed

Cargo.lock

-48
Original file line numberDiff line numberDiff line change
@@ -3412,17 +3412,6 @@ dependencies = [
34123412
"smallvec",
34133413
]
34143414

3415-
[[package]]
3416-
name = "rustc_asan"
3417-
version = "0.0.0"
3418-
dependencies = [
3419-
"alloc",
3420-
"build_helper",
3421-
"cmake",
3422-
"compiler_builtins",
3423-
"core",
3424-
]
3425-
34263415
[[package]]
34273416
name = "rustc_codegen_llvm"
34283417
version = "0.0.0"
@@ -3620,17 +3609,6 @@ dependencies = [
36203609
"cc",
36213610
]
36223611

3623-
[[package]]
3624-
name = "rustc_lsan"
3625-
version = "0.0.0"
3626-
dependencies = [
3627-
"alloc",
3628-
"build_helper",
3629-
"cmake",
3630-
"compiler_builtins",
3631-
"core",
3632-
]
3633-
36343612
[[package]]
36353613
name = "rustc_macros"
36363614
version = "0.1.0"
@@ -3685,17 +3663,6 @@ dependencies = [
36853663
"syntax_pos",
36863664
]
36873665

3688-
[[package]]
3689-
name = "rustc_msan"
3690-
version = "0.0.0"
3691-
dependencies = [
3692-
"alloc",
3693-
"build_helper",
3694-
"cmake",
3695-
"compiler_builtins",
3696-
"core",
3697-
]
3698-
36993666
[[package]]
37003667
name = "rustc_passes"
37013668
version = "0.0.0"
@@ -3809,17 +3776,6 @@ dependencies = [
38093776
"syntax_pos",
38103777
]
38113778

3812-
[[package]]
3813-
name = "rustc_tsan"
3814-
version = "0.0.0"
3815-
dependencies = [
3816-
"alloc",
3817-
"build_helper",
3818-
"cmake",
3819-
"compiler_builtins",
3820-
"core",
3821-
]
3822-
38233779
[[package]]
38243780
name = "rustc_typeck"
38253781
version = "0.0.0"
@@ -4175,10 +4131,6 @@ dependencies = [
41754131
"panic_unwind",
41764132
"profiler_builtins",
41774133
"rand 0.7.0",
4178-
"rustc_asan",
4179-
"rustc_lsan",
4180-
"rustc_msan",
4181-
"rustc_tsan",
41824134
"unwind",
41834135
"wasi",
41844136
]

src/bootstrap/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl Step for Std {
4848
let compiler = builder.compiler(0, builder.config.build);
4949

5050
let mut cargo = builder.cargo(compiler, Mode::Std, target, cargo_subcommand(builder.kind));
51-
std_cargo(builder, &compiler, target, &mut cargo);
51+
std_cargo(builder, target, &mut cargo);
5252

5353
builder.info(&format!("Checking std artifacts ({} -> {})", &compiler.host, target));
5454
run_cargo(builder,

src/bootstrap/compile.rs

+103-36
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl Step for Std {
9494
target_deps.extend(copy_third_party_objects(builder, &compiler, target).into_iter());
9595

9696
let mut cargo = builder.cargo(compiler, Mode::Std, target, "build");
97-
std_cargo(builder, &compiler, target, &mut cargo);
97+
std_cargo(builder, target, &mut cargo);
9898

9999
builder.info(&format!("Building stage{} std artifacts ({} -> {})", compiler.stage,
100100
&compiler.host, target));
@@ -157,13 +157,18 @@ fn copy_third_party_objects(builder: &Builder<'_>, compiler: &Compiler, target:
157157
copy_and_stamp(Path::new(&src), "libunwind.a");
158158
}
159159

160+
if builder.config.sanitizers && compiler.stage != 0 {
161+
// The sanitizers are only copied in stage1 or above,
162+
// to avoid creating dependency on LLVM.
163+
target_deps.extend(copy_sanitizers(builder, &compiler, target));
164+
}
165+
160166
target_deps
161167
}
162168

163169
/// Configure cargo to compile the standard library, adding appropriate env vars
164170
/// and such.
165171
pub fn std_cargo(builder: &Builder<'_>,
166-
compiler: &Compiler,
167172
target: Interned<String>,
168173
cargo: &mut Cargo) {
169174
if let Some(target) = env::var_os("MACOSX_STD_DEPLOYMENT_TARGET") {
@@ -208,21 +213,6 @@ pub fn std_cargo(builder: &Builder<'_>,
208213
let mut features = builder.std_features();
209214
features.push_str(&compiler_builtins_c_feature);
210215

211-
if compiler.stage != 0 && builder.config.sanitizers {
212-
// This variable is used by the sanitizer runtime crates, e.g.
213-
// rustc_lsan, to build the sanitizer runtime from C code
214-
// When this variable is missing, those crates won't compile the C code,
215-
// so we don't set this variable during stage0 where llvm-config is
216-
// missing
217-
// We also only build the runtimes when --enable-sanitizers (or its
218-
// config.toml equivalent) is used
219-
let llvm_config = builder.ensure(native::Llvm {
220-
target: builder.config.build,
221-
});
222-
cargo.env("LLVM_CONFIG", llvm_config);
223-
cargo.env("RUSTC_BUILD_SANITIZERS", "1");
224-
}
225-
226216
cargo.arg("--features").arg(features)
227217
.arg("--manifest-path")
228218
.arg(builder.src.join("src/libtest/Cargo.toml"));
@@ -280,31 +270,108 @@ impl Step for StdLink {
280270
let libdir = builder.sysroot_libdir(target_compiler, target);
281271
let hostdir = builder.sysroot_libdir(target_compiler, compiler.host);
282272
add_to_sysroot(builder, &libdir, &hostdir, &libstd_stamp(builder, compiler, target));
273+
}
274+
}
275+
276+
/// Copies sanitizer runtime libraries into target libdir.
277+
fn copy_sanitizers(builder: &Builder<'_>,
278+
compiler: &Compiler,
279+
target: Interned<String>) -> Vec<PathBuf> {
280+
let mut target_deps = Vec::new();
281+
282+
let sanitizers = supported_sanitizers(target);
283+
if sanitizers.is_empty() {
284+
return target_deps;
285+
}
286+
287+
let llvm_config: PathBuf = builder.ensure(native::Llvm {
288+
target: compiler.host,
289+
});
290+
if builder.config.dry_run {
291+
return target_deps;
292+
}
293+
294+
// The compiler-rt installs sanitizer runtimes into clang resource directory.
295+
let clang_resourcedir = clang_resourcedir(&llvm_config);
296+
let libdir = builder.sysroot_libdir(*compiler, target);
283297

284-
if builder.config.sanitizers && compiler.stage != 0 && target == "x86_64-apple-darwin" {
285-
// The sanitizers are only built in stage1 or above, so the dylibs will
286-
// be missing in stage0 and causes panic. See the `std()` function above
287-
// for reason why the sanitizers are not built in stage0.
288-
copy_apple_sanitizer_dylibs(builder, &builder.native_dir(target), "osx", &libdir);
298+
for (path, name) in &sanitizers {
299+
let src = clang_resourcedir.join(path);
300+
let dst = libdir.join(name);
301+
if !src.exists() {
302+
println!("Ignoring missing runtime: {}", src.display());
303+
continue;
289304
}
305+
builder.copy(&src, &dst);
306+
307+
if target == "x86_64-apple-darwin" {
308+
// Update the library install name reflect the fact it has been renamed.
309+
let status = Command::new("install_name_tool")
310+
.arg("-id")
311+
.arg(format!("@rpath/{}", name))
312+
.arg(&dst)
313+
.status()
314+
.expect("failed to execute `install_name_tool`");
315+
assert!(status.success());
316+
}
317+
318+
target_deps.push(dst);
290319
}
320+
321+
target_deps
291322
}
292323

293-
fn copy_apple_sanitizer_dylibs(
294-
builder: &Builder<'_>,
295-
native_dir: &Path,
296-
platform: &str,
297-
into: &Path,
298-
) {
299-
for &sanitizer in &["asan", "tsan"] {
300-
let filename = format!("lib__rustc__clang_rt.{}_{}_dynamic.dylib", sanitizer, platform);
301-
let mut src_path = native_dir.join(sanitizer);
302-
src_path.push("build");
303-
src_path.push("lib");
304-
src_path.push("darwin");
305-
src_path.push(&filename);
306-
builder.copy(&src_path, &into.join(filename));
324+
/// Returns path to clang's resource directory.
325+
fn clang_resourcedir(llvm_config: &Path) -> PathBuf {
326+
let llvm_version = output(Command::new(&llvm_config).arg("--version"));
327+
let llvm_version = llvm_version.trim();
328+
let llvm_libdir = output(Command::new(&llvm_config).arg("--libdir"));
329+
let llvm_libdir = PathBuf::from(llvm_libdir.trim());
330+
331+
// Determine CLANG_VERSION by stripping LLVM_VERSION_SUFFIX from LLVM_VERSION.
332+
let mut non_digits = 0;
333+
let mut third_non_digit = llvm_version.len();
334+
for (i, c) in llvm_version.char_indices() {
335+
if !c.is_digit(10) {
336+
non_digits += 1;
337+
if non_digits == 3 {
338+
third_non_digit = i;
339+
break;
340+
}
341+
}
342+
}
343+
let clang_version = &llvm_version[..third_non_digit];
344+
llvm_libdir.join("clang").join(&clang_version)
345+
}
346+
347+
/// Returns a list of paths to sanitizer libraries supported on given target,
348+
/// and corresponding names we plan to give them when placed in target libdir.
349+
///
350+
/// Returned paths are relative to clang's resource directory.
351+
fn supported_sanitizers(target: Interned<String>) -> Vec<(PathBuf, String)> {
352+
let sanitizers = &["asan", "lsan", "msan", "tsan"];
353+
let mut result = Vec::new();
354+
match &*target {
355+
"x86_64-apple-darwin" => {
356+
let srcdir = Path::new("lib/darwin");
357+
for s in sanitizers {
358+
let src = format!("libclang_rt.{}_osx_dynamic.dylib", s);
359+
let dst = format!("librustc_rt.{}.dylib", s);
360+
result.push((srcdir.join(src), dst));
361+
}
362+
363+
}
364+
"x86_64-unknown-linux-gnu" => {
365+
let srcdir = Path::new("lib/linux");
366+
for s in sanitizers {
367+
let src = format!("libclang_rt.{}-x86_64.a", s);
368+
let dst = format!("librustc_rt.{}.a", s);
369+
result.push((srcdir.join(src), dst));
370+
}
371+
}
372+
_ => {}
307373
}
374+
result
308375
}
309376

310377
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]

src/bootstrap/dist.rs

-4
Original file line numberDiff line numberDiff line change
@@ -960,10 +960,6 @@ impl Step for Src {
960960
"src/libcore",
961961
"src/libpanic_abort",
962962
"src/libpanic_unwind",
963-
"src/librustc_asan",
964-
"src/librustc_lsan",
965-
"src/librustc_msan",
966-
"src/librustc_tsan",
967963
"src/libstd",
968964
"src/libunwind",
969965
"src/libtest",

src/bootstrap/doc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ impl Step for Std {
458458

459459
let run_cargo_rustdoc_for = |package: &str| {
460460
let mut cargo = builder.cargo(compiler, Mode::Std, target, "rustdoc");
461-
compile::std_cargo(builder, &compiler, target, &mut cargo);
461+
compile::std_cargo(builder, target, &mut cargo);
462462

463463
// Keep a whitelist so we do not build internal stdlib crates, these will be
464464
// build by the rustc step later if enabled.

src/bootstrap/native.rs

+7
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ impl Step for Llvm {
186186
enabled_llvm_projects.push("compiler-rt");
187187
}
188188

189+
if builder.config.sanitizers {
190+
enabled_llvm_projects.push("compiler-rt");
191+
cfg.define("COMPILER_RT_BUILD_SANITIZERS", "ON");
192+
// Avoids building instrumented version of libcxx.
193+
cfg.define("COMPILER_RT_USE_LIBCXX", "OFF");
194+
}
195+
189196
if builder.config.lldb_enabled {
190197
enabled_llvm_projects.push("clang");
191198
enabled_llvm_projects.push("lldb");

src/bootstrap/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1764,7 +1764,7 @@ impl Step for Crate {
17641764
let mut cargo = builder.cargo(compiler, mode, target, test_kind.subcommand());
17651765
match mode {
17661766
Mode::Std => {
1767-
compile::std_cargo(builder, &compiler, target, &mut cargo);
1767+
compile::std_cargo(builder, target, &mut cargo);
17681768
}
17691769
Mode::Rustc => {
17701770
builder.ensure(compile::Rustc { compiler, target });

0 commit comments

Comments
 (0)