Skip to content

Commit 66dcf5d

Browse files
committed
Enable eager checks for memory sanitizer
1 parent 2fbc08e commit 66dcf5d

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,12 @@ extern "C" LLVMPassRef LLVMRustCreateMemorySanitizerPass(int TrackOrigins, bool
131131
const bool CompileKernel = false;
132132

133133
return wrap(createMemorySanitizerLegacyPassPass(
134-
MemorySanitizerOptions{TrackOrigins, Recover, CompileKernel}));
134+
#if LLVM_VERSION_GE(14, 0)
135+
MemorySanitizerOptions{TrackOrigins, Recover, CompileKernel, /*EagerChecks=*/true}
136+
#else
137+
MemorySanitizerOptions{TrackOrigins, Recover, CompileKernel}
138+
#endif
139+
));
135140
#else
136141
report_fatal_error("Legacy PM not supported with LLVM 15");
137142
#endif
@@ -931,10 +936,18 @@ LLVMRustOptimizeWithNewPassManager(
931936

932937
if (SanitizerOptions) {
933938
if (SanitizerOptions->SanitizeMemory) {
939+
#if LLVM_VERSION_GE(14, 0)
940+
MemorySanitizerOptions Options(
941+
SanitizerOptions->SanitizeMemoryTrackOrigins,
942+
SanitizerOptions->SanitizeMemoryRecover,
943+
/*CompileKernel=*/false,
944+
/*EagerChecks=*/true);
945+
#else
934946
MemorySanitizerOptions Options(
935947
SanitizerOptions->SanitizeMemoryTrackOrigins,
936948
SanitizerOptions->SanitizeMemoryRecover,
937949
/*CompileKernel=*/false);
950+
#endif
938951
OptimizerLastEPCallbacks.push_back(
939952
[Options](ModulePassManager &MPM, OptimizationLevel Level) {
940953
#if LLVM_VERSION_GE(14, 0)

src/test/ui/sanitize/memory-eager.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// needs-sanitizer-support
2+
// needs-sanitizer-memory
3+
// min-llvm-version: 14.0.0
4+
//
5+
// compile-flags: -Z sanitizer=memory -Zsanitizer-memory-track-origins -O
6+
//
7+
// run-fail
8+
// error-pattern: MemorySanitizer: use-of-uninitialized-value
9+
// error-pattern: Uninitialized value was created by an allocation
10+
// error-pattern: in the stack frame of function 'random'
11+
//
12+
// This test case intentionally limits the usage of the std,
13+
// since it will be linked with an uninstrumented version of it.
14+
15+
#![feature(core_intrinsics)]
16+
#![feature(start)]
17+
#![feature(bench_black_box)]
18+
19+
use std::hint::black_box;
20+
use std::mem::MaybeUninit;
21+
22+
#[inline(never)]
23+
#[no_mangle]
24+
#[allow(invalid_value)]
25+
fn random() -> char {
26+
let r = unsafe { MaybeUninit::uninit().assume_init() };
27+
// Avoid optimizing everything out.
28+
black_box(r)
29+
}
30+
31+
#[start]
32+
fn main(_: isize, _: *const *const u8) -> isize {
33+
random();
34+
0
35+
}

src/test/ui/sanitize/memory.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ use std::mem::MaybeUninit;
2121
#[inline(never)]
2222
#[no_mangle]
2323
fn random() -> [isize; 32] {
24-
let r = unsafe { MaybeUninit::uninit().assume_init() };
24+
let r = MaybeUninit::uninit();
2525
// Avoid optimizing everything out.
26-
black_box(r)
26+
unsafe { std::intrinsics::volatile_load(r.as_ptr()) }
2727
}
2828

2929
#[inline(never)]
@@ -38,6 +38,6 @@ fn xor(a: &[isize]) -> isize {
3838

3939
#[start]
4040
fn main(_: isize, _: *const *const u8) -> isize {
41-
let r = random();
41+
let r = black_box(random as fn() -> [isize; 32])();
4242
xor(&r)
4343
}

0 commit comments

Comments
 (0)