From 73b584275a485991493be8e2670649b9a14695bf Mon Sep 17 00:00:00 2001 From: Lukasz Anforowicz Date: Tue, 8 Aug 2023 18:07:25 +0000 Subject: [PATCH] Mark `___asan_globals_registered` as an exported symbol for LTO Fixes https://github.com/rust-lang/rust/issues/113404 --- .../src/back/symbol_export.rs | 4 +++ .../address-sanitizer-globals-tracking.rs | 36 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 tests/codegen/sanitizer/address-sanitizer-globals-tracking.rs diff --git a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs index bc4aa910917cb..671fd48487e7b 100644 --- a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs +++ b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs @@ -265,6 +265,10 @@ fn exported_symbols_provider_local( externally_injected_weak_symbols.push("__msan_track_origins"); } } + if tcx.sess.opts.unstable_opts.sanitizer.contains(SanitizerSet::ADDRESS) { + // Similar to profiling, preserve weak asan symbols during LTO. + externally_injected_weak_symbols.push("___asan_globals_registered"); + } symbols.extend(externally_injected_weak_symbols.into_iter().map(|sym| { let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, sym)); ( diff --git a/tests/codegen/sanitizer/address-sanitizer-globals-tracking.rs b/tests/codegen/sanitizer/address-sanitizer-globals-tracking.rs new file mode 100644 index 0000000000000..ec1028d8bd9eb --- /dev/null +++ b/tests/codegen/sanitizer/address-sanitizer-globals-tracking.rs @@ -0,0 +1,36 @@ +// Verifies that AddressSanitizer symbols show up as expected in LLVM IR +// with -Zsanitizer (DO NOT SUBMIT: add ASAN (no LTO) and ASAN-LTO2 (lto=fat) tests). +// +// Notes about the `compile-flags` below: +// +// * The original issue only reproed with LTO - this is why this angle has +// extra test coverage via different `revisions` +// * To observe the failure/repro at LLVM-IR level we need to use `staticlib` +// which necessitates `-C prefer-dynamic=false` - without the latter flag, +// we would have run into "cannot prefer dynamic linking when performing LTO". +// +// needs-sanitizer-address +// +// revisions:ASAN ASAN-LTO +//[ASAN] compile-flags: -Zsanitizer=address +//[ASAN-LTO] compile-flags: -Zsanitizer=address -C prefer-dynamic=false -C lto + +#![crate_type="staticlib"] + +// The test below mimics `CACHED_POW10` from `library/core/src/num/flt2dec/strategy/grisu.rs` which +// (because of incorrect handling of `___asan_globals_registered` during LTO) was incorrectly +// reported as an ODR violation in https://crbug.com/1459233#c1. Before this bug was fixed, +// `___asan_globals_registered` would show up as `internal global i64`. +// +// See https://github.com/rust-lang/rust/issues/113404 for more discussion. +// +// CHECK: @___asan_globals_registered = common hidden global i64 0 +// CHECK: @__start_asan_globals = extern_weak hidden global i64 +// CHECK: @__stop_asan_globals = extern_weak hidden global i64 +#[no_mangle] +pub static CACHED_POW10: [(u64, i16, i16); 4] = [ + (0xe61acf033d1a45df, -1087, -308), + (0xab70fe17c79ac6ca, -1060, -300), + (0xff77b1fcbebcdc4f, -1034, -292), + (0xbe5691ef416bd60c, -1007, -284), +];