Skip to content

Commit 975f7df

Browse files
committed
compiletest: Add directives to detect sanitizer support
Add needs-sanitizer-{address,leak,memory,thread} directive indicating that test requires target with support for specific sanitizer. This is an addition to the existing needs-sanitizer-support directive indicating that test requires a sanitizer runtime library.
1 parent 449e8ea commit 975f7df

20 files changed

+75
-33
lines changed

src/test/codegen/sanitizer-memory-track-orgins.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// Verifies that MemorySanitizer track-origins level can be controlled
22
// with -Zsanitizer-memory-track-origins option.
33
//
4-
// needs-sanitizer-support
5-
// only-linux
6-
// only-x86_64
4+
// needs-sanitizer-memory
75
// revisions:MSAN-0 MSAN-1 MSAN-2 MSAN-1-LTO MSAN-2-LTO
86
//
97
//[MSAN-0] compile-flags: -Zsanitizer=memory

src/test/codegen/sanitizer-no-sanitize-inlining.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
// Verifies that no_sanitize attribute prevents inlining when
22
// given sanitizer is enabled, but has no effect on inlining otherwise.
33
//
4-
// needs-sanitizer-support
5-
// only-x86_64
6-
//
4+
// needs-sanitizer-address
5+
// needs-sanitizer-leak
76
// revisions: ASAN LSAN
8-
//
97
//[ASAN] compile-flags: -Zsanitizer=address -C opt-level=3 -Z mir-opt-level=3
108
//[LSAN] compile-flags: -Zsanitizer=leak -C opt-level=3 -Z mir-opt-level=3
119

src/test/codegen/sanitizer-no-sanitize.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Verifies that no_sanitze attribute can be used to
22
// selectively disable sanitizer instrumentation.
33
//
4-
// needs-sanitizer-support
4+
// needs-sanitizer-address
55
// compile-flags: -Zsanitizer=address
66

77
#![crate_type="lib"]

src/test/codegen/sanitizer-recover.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// Verifies that AddressSanitizer and MemorySanitizer
22
// recovery mode can be enabled with -Zsanitizer-recover.
33
//
4-
// needs-sanitizer-support
5-
// only-linux
6-
// only-x86_64
4+
// needs-sanitizer-address
5+
// needs-sanitizer-memory
76
// revisions:ASAN ASAN-RECOVER MSAN MSAN-RECOVER MSAN-RECOVER-LTO
87
// no-prefer-dynamic
98
//

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# needs-sanitizer-support
2-
# only-x86_64
3-
# only-linux
2+
# needs-sanitizer-address
43

54
-include ../tools.mk
65

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# needs-sanitizer-support
2-
# only-x86_64
3-
# only-linux
2+
# needs-sanitizer-address
43

54
-include ../tools.mk
65

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# needs-sanitizer-support
2-
# only-x86_64
3-
# only-linux
2+
# needs-sanitizer-address
43

54
-include ../tools.mk
65

src/test/rustdoc/sanitizer-option.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// needs-sanitizer-support
2+
// needs-sanitizer-address
23
// compile-flags: --test -Z sanitizer=address
34
//
45
// #43031: Verify that rustdoc passes `-Z` options to rustc. Use an extern

src/test/ui/sanitize/address.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// needs-sanitizer-support
2-
// only-x86_64
2+
// needs-sanitizer-address
33
//
44
// compile-flags: -Z sanitizer=address -O -g
55
//

src/test/ui/sanitize/badfree.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// needs-sanitizer-support
2-
// only-x86_64
2+
// needs-sanitizer-address
33
//
44
// compile-flags: -Z sanitizer=address -O
55
//

src/test/ui/sanitize/cfg.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// the `#[cfg(sanitize = "option")]` attribute is configured.
33

44
// needs-sanitizer-support
5-
// only-linux
6-
// only-x86_64
5+
// needs-sanitizer-address
6+
// needs-sanitizer-leak
7+
// needs-sanitizer-memory
8+
// needs-sanitizer-thread
79
// check-pass
810
// revisions: address leak memory thread
911
//[address]compile-flags: -Zsanitizer=address --cfg address

src/test/ui/sanitize/issue-72154-lifetime-markers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// miscompilation which was subsequently detected by AddressSanitizer as UB.
55
//
66
// needs-sanitizer-support
7-
// only-x86_64
7+
// needs-sanitizer-address
88
//
99
// compile-flags: -Copt-level=0 -Zsanitizer=address
1010
// run-pass

src/test/ui/sanitize/leak.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// needs-sanitizer-support
2-
// only-x86_64
2+
// needs-sanitizer-leak
33
//
44
// compile-flags: -Z sanitizer=leak -O
55
//

src/test/ui/sanitize/memory.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// needs-sanitizer-support
2-
// only-linux
3-
// only-x86_64
2+
// needs-sanitizer-memory
43
//
54
// compile-flags: -Z sanitizer=memory -Zsanitizer-memory-track-origins -O
65
//

src/test/ui/sanitize/new-llvm-pass-manager-thin-lto.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//
55
// min-llvm-version 9.0
66
// needs-sanitizer-support
7-
// only-x86_64
7+
// needs-sanitizer-address
88
//
99
// no-prefer-dynamic
1010
// revisions: opt0 opt1

src/test/ui/sanitize/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// would occasionally fail, making test flaky.
1212
//
1313
// needs-sanitizer-support
14-
// only-x86_64
14+
// needs-sanitizer-thread
1515
//
1616
// compile-flags: -Z sanitizer=thread -O
1717
//

src/test/ui/sanitize/use-after-scope.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// needs-sanitizer-support
2-
// only-x86_64
2+
// needs-sanitizer-address
33
//
44
// compile-flags: -Zsanitizer=address
55
// run-fail

src/tools/compiletest/src/header.rs

+23-5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ impl EarlyProps {
4343
let mut props = EarlyProps::default();
4444
let rustc_has_profiler_support = env::var_os("RUSTC_PROFILER_SUPPORT").is_some();
4545
let rustc_has_sanitizer_support = env::var_os("RUSTC_SANITIZER_SUPPORT").is_some();
46+
let has_asan = util::ASAN_SUPPORTED_TARGETS.contains(&&*config.target);
47+
let has_lsan = util::LSAN_SUPPORTED_TARGETS.contains(&&*config.target);
48+
let has_msan = util::MSAN_SUPPORTED_TARGETS.contains(&&*config.target);
49+
let has_tsan = util::TSAN_SUPPORTED_TARGETS.contains(&&*config.target);
4650

4751
iter_header(testfile, None, rdr, &mut |ln| {
4852
// we should check if any only-<platform> exists and if it exists
@@ -74,7 +78,25 @@ impl EarlyProps {
7478
props.ignore = true;
7579
}
7680

77-
if !rustc_has_sanitizer_support && config.parse_needs_sanitizer_support(ln) {
81+
if !rustc_has_sanitizer_support
82+
&& config.parse_name_directive(ln, "needs-sanitizer-support")
83+
{
84+
props.ignore = true;
85+
}
86+
87+
if !has_asan && config.parse_name_directive(ln, "needs-sanitizer-address") {
88+
props.ignore = true;
89+
}
90+
91+
if !has_lsan && config.parse_name_directive(ln, "needs-sanitizer-leak") {
92+
props.ignore = true;
93+
}
94+
95+
if !has_msan && config.parse_name_directive(ln, "needs-sanitizer-memory") {
96+
props.ignore = true;
97+
}
98+
99+
if !has_tsan && config.parse_name_directive(ln, "needs-sanitizer-thread") {
78100
props.ignore = true;
79101
}
80102

@@ -829,10 +851,6 @@ impl Config {
829851
self.parse_name_directive(line, "needs-profiler-support")
830852
}
831853

832-
fn parse_needs_sanitizer_support(&self, line: &str) -> bool {
833-
self.parse_name_directive(line, "needs-sanitizer-support")
834-
}
835-
836854
/// Parses a name-value directive which contains config-specific information, e.g., `ignore-x86`
837855
/// or `normalize-stderr-32bit`.
838856
fn parse_cfg_name_directive(&self, line: &str, prefix: &str) -> ParsedNameDirective {

src/tools/compiletest/src/header/tests.rs

+19
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,22 @@ fn debugger() {
195195
config.debugger = Some(Debugger::Lldb);
196196
assert!(parse_rs(&config, "// ignore-lldb").ignore);
197197
}
198+
199+
#[test]
200+
fn sanitizers() {
201+
let mut config = config();
202+
203+
// Target that supports all sanitizers:
204+
config.target = "x86_64-unknown-linux-gnu".to_owned();
205+
assert!(!parse_rs(&config, "// needs-sanitizer-address").ignore);
206+
assert!(!parse_rs(&config, "// needs-sanitizer-leak").ignore);
207+
assert!(!parse_rs(&config, "// needs-sanitizer-memory").ignore);
208+
assert!(!parse_rs(&config, "// needs-sanitizer-thread").ignore);
209+
210+
// Target that doesn't support sanitizers:
211+
config.target = "wasm32-unknown-emscripten".to_owned();
212+
assert!(parse_rs(&config, "// needs-sanitizer-address").ignore);
213+
assert!(parse_rs(&config, "// needs-sanitizer-leak").ignore);
214+
assert!(parse_rs(&config, "// needs-sanitizer-memory").ignore);
215+
assert!(parse_rs(&config, "// needs-sanitizer-thread").ignore);
216+
}

src/tools/compiletest/src/util.rs

+11
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ const ARCH_TABLE: &'static [(&'static str, &'static str)] = &[
8181
("xcore", "xcore"),
8282
];
8383

84+
pub const ASAN_SUPPORTED_TARGETS: &'static [&'static str] =
85+
&["aarch64-fuchsia", "x86_64-apple-darwin", "x86_64-fuchsia", "x86_64-unknown-linux-gnu"];
86+
87+
pub const LSAN_SUPPORTED_TARGETS: &'static [&'static str] =
88+
&["x86_64-apple-darwin", "x86_64-unknown-linux-gnu"];
89+
90+
pub const MSAN_SUPPORTED_TARGETS: &'static [&'static str] = &["x86_64-unknown-linux-gnu"];
91+
92+
pub const TSAN_SUPPORTED_TARGETS: &'static [&'static str] =
93+
&["x86_64-apple-darwin", "x86_64-unknown-linux-gnu"];
94+
8495
pub fn matches_os(triple: &str, name: &str) -> bool {
8596
// For the wasm32 bare target we ignore anything also ignored on emscripten
8697
// and then we also recognize `wasm32-bare` as the os for the target

0 commit comments

Comments
 (0)