Skip to content

Commit 41cd328

Browse files
committed
Run Sanitizers and Valgrind on tests as part of CI
Unfortunately we can't yet run MemorySanitizer on `cargo test` as there is a known memory bug in rustc's test runner. [1] [1]: rust-lang/rust#39610
1 parent 1293771 commit 41cd328

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

.buildbot.sh

+5
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@ export PATH=`pwd`/.cargo/bin/:$PATH
1313
rustup component add --toolchain nightly rustfmt-preview || cargo +nightly install --force rustfmt-nightly
1414

1515
cargo +nightly fmt --all -- --check
16+
1617
cargo test
18+
19+
# Run gc tests with sanitizers
20+
SANITIZERS="thread" cargo test --test gc_tests --target x86_64-unknown-linux-gnu
21+
VALGRIND="true" cargo test --test gc_tests --target x86_64-unknown-linux-gnu

gc_tests/run_tests.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ use tempdir::TempDir;
1515
static CRATE_NAME: &'static str = "gcmalloc";
1616

1717
fn main() {
18+
let sanitizers: Vec<String> = match env::var("SANITIZERS") {
19+
Ok(ref s) => s.split(';').map(|s| s.to_string()).collect(),
20+
Err(_) => Vec::new(),
21+
};
22+
23+
let valgrind: bool = match env::var("VALGRIND") {
24+
Ok(ref s) => s == "true",
25+
Err(_) => false,
26+
};
27+
1828
// We grab the rlibs from `target/<debug | release>/` but in order
1929
// for them to exist here, they must have been moved from `deps/`.
2030
// Simply running `cargo test` will not do this, instead, we must
@@ -65,6 +75,10 @@ fn main() {
6575
let lib_arg = [CRATE_NAME, "=", lib_path.to_str().unwrap()].concat();
6676
let deps_arg = ["dependency=", deps_path.to_str().unwrap()].concat();
6777

78+
let san_flags = sanitizers.iter().map(|x| format!("-Zsanitizer={}", x));
79+
80+
compiler.args(san_flags);
81+
6882
compiler.args(&[
6983
"--edition=2018",
7084
"-o",
@@ -78,7 +92,25 @@ fn main() {
7892
lib_arg.as_str(),
7993
]);
8094

81-
let runtime = Command::new(exe);
95+
assert!(
96+
!(valgrind && !sanitizers.is_empty()),
97+
"Valgrind can't be used on code compiled with sanitizers"
98+
);
99+
100+
let mut runtime;
101+
if valgrind {
102+
let suppr_file = PathBuf::from("gc_tests/valgrind.supp");
103+
let suppressions = ["--suppressions=", suppr_file.to_str().unwrap()].concat();
104+
runtime = Command::new("valgrind");
105+
runtime.args(&[
106+
"--error-exitcode=1",
107+
suppressions.as_str(),
108+
exe.to_str().unwrap(),
109+
]);
110+
} else {
111+
runtime = Command::new(exe);
112+
};
113+
82114
vec![("Compiler", compiler), ("Run-time", runtime)]
83115
})
84116
.run();

gc_tests/tests/alloc_info_oom.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Run-time:
22
// status: error
3-
// stderr: Allocation failed: Metadata list full
3+
// stderr:
4+
// ...
5+
// Allocation failed: Metadata list full
6+
// ...
47

58
extern crate gcmalloc;
69

gc_tests/valgrind.supp

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
<uninitialized_stack>
3+
Memcheck:Cond
4+
...
5+
fun:scan_stack
6+
}
7+

src/alloc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ impl AllocMetadata {
397397
}
398398

399399
/// Removes the metadata associated with an allocation.
400+
#[cfg(test)]
400401
pub(crate) fn remove(ptr: usize) {
401402
ALLOC_LOCK.lock();
402403

0 commit comments

Comments
 (0)