Skip to content

Commit 0dcea65

Browse files
authored
Rollup merge of rust-lang#68410 - tmiasko:msan-lto, r=varkor
Export weak symbols used by MemorySanitizer Export weak symbols defined by MemorySanitizer instrumentation, which are used to implement `-Zsanitizer-memory-track-origins` and `-Zsanitizer-recover=memory`. Previously, when using fat LTO, they would internalized and eliminated. Fixes rust-lang#68367.
2 parents 5801824 + d8c661a commit 0dcea65

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed

src/librustc_codegen_ssa/back/symbol_export.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33

44
use rustc::middle::codegen_fn_attrs::CodegenFnAttrFlags;
55
use rustc::middle::exported_symbols::{metadata_symbol_name, ExportedSymbol, SymbolExportLevel};
6-
use rustc::session::config;
6+
use rustc::session::config::{self, Sanitizer};
77
use rustc::ty::query::Providers;
88
use rustc::ty::subst::SubstsRef;
99
use rustc::ty::Instance;
@@ -206,6 +206,16 @@ fn exported_symbols_provider_local(
206206
}));
207207
}
208208

209+
if let Some(Sanitizer::Memory) = tcx.sess.opts.debugging_opts.sanitizer {
210+
// Similar to profiling, preserve weak msan symbol during LTO.
211+
const MSAN_WEAK_SYMBOLS: [&str; 2] = ["__msan_track_origins", "__msan_keep_going"];
212+
213+
symbols.extend(MSAN_WEAK_SYMBOLS.iter().map(|sym| {
214+
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(sym));
215+
(exported_symbol, SymbolExportLevel::C)
216+
}));
217+
}
218+
209219
if tcx.sess.crate_types.borrow().contains(&config::CrateType::Dylib) {
210220
let symbol_name = metadata_symbol_name(tcx);
211221
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(&symbol_name));

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@
44
// needs-sanitizer-support
55
// only-linux
66
// only-x86_64
7-
// revisions:MSAN-0 MSAN-1 MSAN-2
7+
// revisions:MSAN-0 MSAN-1 MSAN-2 MSAN-1-LTO MSAN-2-LTO
88
//
99
//[MSAN-0] compile-flags: -Zsanitizer=memory
1010
//[MSAN-1] compile-flags: -Zsanitizer=memory -Zsanitizer-memory-track-origins=1
1111
//[MSAN-2] compile-flags: -Zsanitizer=memory -Zsanitizer-memory-track-origins
12+
//[MSAN-1-LTO] compile-flags: -Zsanitizer=memory -Zsanitizer-memory-track-origins=1 -C lto=fat
13+
//[MSAN-2-LTO] compile-flags: -Zsanitizer=memory -Zsanitizer-memory-track-origins -C lto=fat
1214

1315
#![crate_type="lib"]
1416

1517
// MSAN-0-NOT: @__msan_track_origins
1618
// MSAN-1: @__msan_track_origins = weak_odr local_unnamed_addr constant i32 1
1719
// MSAN-2: @__msan_track_origins = weak_odr local_unnamed_addr constant i32 2
20+
// MSAN-1-LTO: @__msan_track_origins = weak_odr local_unnamed_addr constant i32 1
21+
// MSAN-2-LTO: @__msan_track_origins = weak_odr local_unnamed_addr constant i32 2
1822
//
1923
// MSAN-0-LABEL: define void @copy(
2024
// MSAN-1-LABEL: define void @copy(

src/test/codegen/sanitizer-recover.rs

+36-20
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,47 @@
44
// needs-sanitizer-support
55
// only-linux
66
// only-x86_64
7-
// revisions:ASAN ASAN-RECOVER MSAN MSAN-RECOVER
7+
// revisions:ASAN ASAN-RECOVER MSAN MSAN-RECOVER MSAN-RECOVER-LTO
8+
// no-prefer-dynamic
89
//
9-
//[ASAN] compile-flags: -Zsanitizer=address
10-
//[ASAN-RECOVER] compile-flags: -Zsanitizer=address -Zsanitizer-recover=address
11-
//[MSAN] compile-flags: -Zsanitizer=memory
12-
//[MSAN-RECOVER] compile-flags: -Zsanitizer=memory -Zsanitizer-recover=memory
13-
14-
#![crate_type="lib"]
10+
//[ASAN] compile-flags: -Zsanitizer=address
11+
//[ASAN-RECOVER] compile-flags: -Zsanitizer=address -Zsanitizer-recover=address
12+
//[MSAN] compile-flags: -Zsanitizer=memory
13+
//[MSAN-RECOVER] compile-flags: -Zsanitizer=memory -Zsanitizer-recover=memory
14+
//[MSAN-RECOVER-LTO] compile-flags: -Zsanitizer=memory -Zsanitizer-recover=memory -C lto=fat
15+
//
16+
// MSAN-NOT: @__msan_keep_going
17+
// MSAN-RECOVER: @__msan_keep_going = weak_odr {{.*}} constant i32 1
18+
// MSAN-RECOVER-LTO: @__msan_keep_going = weak_odr {{.*}} constant i32 1
1519

16-
// ASAN-LABEL: define i32 @penguin(
20+
// ASAN-LABEL: define i32 @penguin(
21+
// ASAN: call void @__asan_report_load4(i64 %0)
22+
// ASAN: unreachable
23+
// ASAN: }
24+
//
1725
// ASAN-RECOVER-LABEL: define i32 @penguin(
18-
// MSAN-LABEL: define i32 @penguin(
26+
// ASAN-RECOVER: call void @__asan_report_load4_noabort(
27+
// ASAN-RECOVER-NOT: unreachable
28+
// ASAN: }
29+
//
30+
// MSAN-LABEL: define i32 @penguin(
31+
// MSAN: call void @__msan_warning_noreturn()
32+
// MSAN: unreachable
33+
// MSAN: }
34+
//
1935
// MSAN-RECOVER-LABEL: define i32 @penguin(
36+
// MSAN-RECOVER: call void @__msan_warning()
37+
// MSAN-RECOVER-NOT: unreachable
38+
// MSAN-RECOVER: }
39+
//
40+
// MSAN-RECOVER-LTO-LABEL: define i32 @penguin(
41+
// MSAN-RECOVER-LTO: call void @__msan_warning()
42+
// MSAN-RECOVER-LTO-NOT: unreachable
43+
// MSAN-RECOVER-LTO: }
44+
//
2045
#[no_mangle]
2146
pub fn penguin(p: &mut i32) -> i32 {
22-
// ASAN: call void @__asan_report_load4(i64 %0)
23-
// ASAN: unreachable
24-
//
25-
// ASAN-RECOVER: call void @__asan_report_load4_noabort(
26-
// ASAN-RECOVER-NOT: unreachable
27-
//
28-
// MSAN: call void @__msan_warning_noreturn()
29-
// MSAN: unreachable
30-
//
31-
// MSAN-RECOVER: call void @__msan_warning()
32-
// MSAN-RECOVER-NOT: unreachable
3347
*p
3448
}
49+
50+
fn main() {}

0 commit comments

Comments
 (0)