Skip to content

Commit 6d20335

Browse files
authored
Rollup merge of #99207 - 5225225:msan-eager-checks, r=jackh726
Enable eager checks for memory sanitizer Fixes #99179
2 parents 7200da0 + 16525cc commit 6d20335

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

compiler/rustc_codegen_llvm/src/abi.rs

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_target::abi::call::ArgAbi;
1919
pub use rustc_target::abi::call::*;
2020
use rustc_target::abi::{self, HasDataLayout, Int};
2121
pub use rustc_target::spec::abi::Abi;
22+
use rustc_target::spec::SanitizerSet;
2223

2324
use libc::c_uint;
2425
use smallvec::SmallVec;
@@ -90,6 +91,13 @@ fn get_attrs<'ll>(this: &ArgAttributes, cx: &CodegenCx<'ll, '_>) -> SmallVec<[&'
9091
if regular.contains(ArgAttribute::NoAliasMutRef) && should_use_mutable_noalias(cx) {
9192
attrs.push(llvm::AttributeKind::NoAlias.create_attr(cx.llcx));
9293
}
94+
} else if cx.tcx.sess.opts.unstable_opts.sanitizer.contains(SanitizerSet::MEMORY) {
95+
// If we're not optimising, *but* memory sanitizer is on, emit noundef, since it affects
96+
// memory sanitizer's behavior.
97+
98+
if regular.contains(ArgAttribute::NoUndef) {
99+
attrs.push(llvm::AttributeKind::NoUndef.create_attr(cx.llcx));
100+
}
93101
}
94102

95103
attrs

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

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

136136
return wrap(createMemorySanitizerLegacyPassPass(
137-
MemorySanitizerOptions{TrackOrigins, Recover, CompileKernel}));
137+
#if LLVM_VERSION_GE(14, 0)
138+
MemorySanitizerOptions{TrackOrigins, Recover, CompileKernel, /*EagerChecks=*/true}
139+
#else
140+
MemorySanitizerOptions{TrackOrigins, Recover, CompileKernel}
141+
#endif
142+
));
138143
#else
139144
report_fatal_error("Legacy PM not supported with LLVM 15");
140145
#endif
@@ -930,10 +935,18 @@ LLVMRustOptimizeWithNewPassManager(
930935

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

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

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

src/test/ui/sanitize/memory.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// needs-sanitizer-support
22
// needs-sanitizer-memory
33
//
4-
// compile-flags: -Z sanitizer=memory -Zsanitizer-memory-track-origins -O
4+
// revisions: unoptimized optimized
5+
//
6+
// [optimized]compile-flags: -Z sanitizer=memory -Zsanitizer-memory-track-origins -O
7+
// [unoptimized]compile-flags: -Z sanitizer=memory -Zsanitizer-memory-track-origins
58
//
69
// run-fail
710
// error-pattern: MemorySanitizer: use-of-uninitialized-value
@@ -22,9 +25,9 @@ use std::mem::MaybeUninit;
2225
#[inline(never)]
2326
#[no_mangle]
2427
fn random() -> [isize; 32] {
25-
let r = unsafe { MaybeUninit::uninit().assume_init() };
28+
let r = MaybeUninit::uninit();
2629
// Avoid optimizing everything out.
27-
black_box(r)
30+
unsafe { std::intrinsics::volatile_load(r.as_ptr()) }
2831
}
2932

3033
#[inline(never)]
@@ -39,6 +42,6 @@ fn xor(a: &[isize]) -> isize {
3942

4043
#[start]
4144
fn main(_: isize, _: *const *const u8) -> isize {
42-
let r = random();
45+
let r = black_box(random as fn() -> [isize; 32])();
4346
xor(&r)
4447
}

0 commit comments

Comments
 (0)