Skip to content

Commit 775a936

Browse files
author
Jorge Aparicio
committed
build/test the sanitizers only when --enable-sanitizers is used
1 parent 9af6aa3 commit 775a936

File tree

11 files changed

+21
-65
lines changed

11 files changed

+21
-65
lines changed

configure

+1
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ opt codegen-tests 1 "run the src/test/codegen tests"
649649
opt option-checking 1 "complain about unrecognized options in this configure script"
650650
opt ninja 0 "build LLVM using the Ninja generator (for MSVC, requires building in the correct environment)"
651651
opt vendor 0 "enable usage of vendored Rust crates"
652+
opt sanitizers 0 "build the sanitizer runtimes (asan, lsan, msan, tsan)"
652653

653654
# Optimization and debugging options. These may be overridden by the release channel, etc.
654655
opt_nosave optimize 1 "build optimized rust code"

src/bootstrap/check.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ pub fn compiletest(build: &Build,
236236
cmd.env("RUSTC_BOOTSTRAP", "1");
237237
build.add_rust_test_threads(&mut cmd);
238238

239+
if build.config.sanitizers {
240+
cmd.env("SANITIZER_SUPPORT", "1");
241+
}
242+
239243
cmd.arg("--adb-path").arg("adb");
240244
cmd.arg("--adb-test-dir").arg(ADB_TEST_DIR);
241245
if target.contains("android") {
@@ -332,10 +336,7 @@ pub fn krate(build: &Build,
332336
krate: Option<&str>) {
333337
let (name, path, features, root) = match mode {
334338
Mode::Libstd => {
335-
("libstd",
336-
"src/rustc/std_shim",
337-
build.std_features(),
338-
"std_shim")
339+
("libstd", "src/rustc/std_shim", build.std_features(), "std_shim")
339340
}
340341
Mode::Libtest => {
341342
("libtest", "src/rustc/test_shim", String::new(), "test_shim")

src/bootstrap/compile.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,14 @@ pub fn std(build: &Build, target: &str, compiler: &Compiler) {
5252
features.push_str(" force_alloc_system");
5353
}
5454

55-
if compiler.stage != 0 && !build.system_llvm(target) {
55+
if compiler.stage != 0 && build.config.sanitizers {
5656
// This variable is used by the sanitizer runtime crates, e.g.
5757
// rustc_lsan, to build the sanitizer runtime from C code
5858
// When this variable is missing, those crates won't compile the C code,
5959
// so we don't set this variable during stage0 where llvm-config is
6060
// missing
61-
// We also don't build the runtimes when compiling against system llvm
62-
// because some distributions ship llvm packages that have a directory
63-
// layout different from the one that the runtime's build system expects
61+
// We also only build the runtimes when --enable-sanitizers (or its
62+
// config.toml equivalent) is used
6463
cargo.env("LLVM_CONFIG", build.llvm_config(target));
6564
}
6665
cargo.arg("--features").arg(features)

src/bootstrap/config.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub struct Config {
4848
pub target_config: HashMap<String, Target>,
4949
pub full_bootstrap: bool,
5050
pub extended: bool,
51+
pub sanitizers: bool,
5152

5253
// llvm codegen options
5354
pub llvm_assertions: bool,
@@ -108,8 +109,6 @@ pub struct Config {
108109
/// Per-target configuration stored in the global configuration structure.
109110
#[derive(Default)]
110111
pub struct Target {
111-
// `true` if compiling against system LLVM or a pre-built LLVM
112-
pub system_llvm: bool,
113112
pub llvm_config: Option<PathBuf>,
114113
pub jemalloc: Option<PathBuf>,
115114
pub cc: Option<PathBuf>,
@@ -150,6 +149,7 @@ struct Build {
150149
python: Option<String>,
151150
full_bootstrap: Option<bool>,
152151
extended: Option<bool>,
152+
sanitizers: Option<bool>,
153153
}
154154

155155
/// TOML representation of various global install decisions.
@@ -294,6 +294,7 @@ impl Config {
294294
set(&mut config.vendor, build.vendor);
295295
set(&mut config.full_bootstrap, build.full_bootstrap);
296296
set(&mut config.extended, build.extended);
297+
set(&mut config.sanitizers, build.sanitizers);
297298

298299
if let Some(ref install) = toml.install {
299300
config.prefix = install.prefix.clone().map(PathBuf::from);
@@ -437,6 +438,7 @@ impl Config {
437438
("VENDOR", self.vendor),
438439
("FULL_BOOTSTRAP", self.full_bootstrap),
439440
("EXTENDED", self.extended),
441+
("SANITIZERS", self.sanitizers),
440442
}
441443

442444
match key {
@@ -514,7 +516,6 @@ impl Config {
514516
.or_insert(Target::default());
515517
let root = parse_configure_path(value);
516518
target.llvm_config = Some(push_exe_path(root, &["bin", "llvm-config"]));
517-
target.system_llvm = true;
518519
}
519520
"CFG_JEMALLOC_ROOT" if value.len() > 0 => {
520521
let target = self.target_config.entry(self.build.clone())

src/bootstrap/config.toml.example

+3
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@
124124
# disabled by default.
125125
#extended = false
126126

127+
# Build the sanitizer runtimes
128+
#sanitizers = false
129+
127130
# =============================================================================
128131
# General install configuration options
129132
# =============================================================================

src/bootstrap/lib.rs

-4
Original file line numberDiff line numberDiff line change
@@ -717,10 +717,6 @@ impl Build {
717717
}
718718
}
719719

720-
fn system_llvm(&self, target: &str) -> bool {
721-
self.config.target_config.get(target).map(|t| t.system_llvm).unwrap_or(false)
722-
}
723-
724720
/// Returns the path to `FileCheck` binary for the specified target
725721
fn llvm_filecheck(&self, target: &str) -> PathBuf {
726722
let target_config = self.config.target_config.get(target);

src/librustc_llvm/ffi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub enum Attribute {
127127
ZExt = 18,
128128
InReg = 19,
129129
SanitizeThread = 20,
130-
SanitizeAddress = 21,
130+
SanitizeAddress = 21,
131131
SanitizeMemory = 22,
132132
}
133133

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
-include ../tools.mk
22

33
# NOTE the address sanitizer only supports x86_64 linux
4-
ifndef IS_WINDOWS
5-
ifeq ($(shell uname),Linux)
6-
ifeq ($(shell uname -m),x86_64)
4+
ifdef SANITIZER_SUPPORT
75
all:
86
$(RUSTC) -g -Z sanitizer=address -Z print-link-args overflow.rs | grep -q librustc_asan
97
$(TMPDIR)/overflow 2>&1 | grep -q stack-buffer-overflow
108
else
119
all:
1210

1311
endif
14-
else
15-
all:
16-
17-
endif
18-
else
19-
all:
20-
21-
endif
+1-14
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
11
-include ../tools.mk
22

3-
# NOTE the leak sanitizer only supports x86_64 linux
4-
# Also, this particular sanitizer sometimes doesn't work so we are not going to
5-
# run the binary
6-
ifndef IS_WINDOWS
7-
ifeq ($(shell uname),Linux)
8-
ifeq ($(shell uname -m),x86_64)
3+
ifdef SANITIZER_SUPPORT
94
all:
105
$(RUSTC) -C opt-level=1 -g -Z sanitizer=leak -Z print-link-args leak.rs | grep -q librustc_lsan
116
$(TMPDIR)/leak 2>&1 | grep -q 'detected memory leaks'
127
else
138
all:
149

1510
endif
16-
else
17-
all:
18-
19-
endif
20-
else
21-
all:
22-
23-
endif
+1-12
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
-include ../tools.mk
22

3-
# NOTE the memory sanitizer only supports x86_64 linux
4-
ifndef IS_WINDOWS
5-
ifeq ($(shell uname),Linux)
6-
ifeq ($(shell uname -m),x86_64)
3+
ifdef SANITIZER_SUPPORT
74
all:
85
$(RUSTC) -g -Z sanitizer=memory -Z print-link-args uninit.rs | grep -q librustc_msan
96
$(TMPDIR)/uninit 2>&1 | grep -q use-of-uninitialized-value
107
else
118
all:
129

1310
endif
14-
else
15-
all:
16-
17-
endif
18-
else
19-
all:
20-
21-
endif
+1-12
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
-include ../tools.mk
22

3-
# NOTE the leak sanitizer only supports x86_64 linux
4-
ifndef IS_WINDOWS
5-
ifeq ($(shell uname),Linux)
6-
ifeq ($(shell uname -m),x86_64)
3+
ifdef SANITIZER_SUPPORT
74
all:
85
$(RUSTC) -g -Z sanitizer=thread -Z print-link-args racy.rs | grep -q librustc_tsan
96
$(TMPDIR)/racy 2>&1 | grep -q 'data race'
107
else
118
all:
129

1310
endif
14-
else
15-
all:
16-
17-
endif
18-
else
19-
all:
20-
21-
endif

0 commit comments

Comments
 (0)