From 297a1830228a0cdc61fa2a0014ace3817497982e Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Tue, 22 Nov 2022 16:47:33 -0800 Subject: [PATCH] [asan] Don't demangle __odr_asan_gen_* symbols This relands the ODR indicator part of D138095 (reverted by 06c74b5e7367b41e9b4ea3d74c971aace5681fb8): a `__odr_asan_gen_*` symbol should use a mangled name as its associated symbol does. --- llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp | 9 +++++---- llvm/test/Instrumentation/AddressSanitizer/local_alias.ll | 7 +++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp index ff05454..a4d4cc7 100644 --- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -2264,11 +2264,12 @@ bool ModuleAddressSanitizer::InstrumentGlobals(IRBuilder<> &IRB, Module &M, if (G->hasSanitizerMetadata()) MD = G->getSanitizerMetadata(); - // TODO: Symbol names in the descriptor can be demangled by the runtime - // library. This could save ~0.4% of VM size for a private large binary. - std::string NameForGlobal = llvm::demangle(G->getName().str()); + // The runtime library tries demangling symbol names in the descriptor but + // functionality like __cxa_demangle may be unavailable (e.g. + // -static-libstdc++). So we demangle the symbol names here. + std::string NameForGlobal = G->getName().str(); GlobalVariable *Name = - createPrivateGlobalForString(M, NameForGlobal, + createPrivateGlobalForString(M, llvm::demangle(NameForGlobal), /*AllowMerging*/ true, kAsanGenPrefix); Type *Ty = G->getValueType(); diff --git a/llvm/test/Instrumentation/AddressSanitizer/local_alias.ll b/llvm/test/Instrumentation/AddressSanitizer/local_alias.ll index b772d5c..ada8120 100644 --- a/llvm/test/Instrumentation/AddressSanitizer/local_alias.ll +++ b/llvm/test/Instrumentation/AddressSanitizer/local_alias.ll @@ -13,17 +13,20 @@ target triple = "x86_64-unknown-linux-gnu" @a = dso_local global [2 x i32] zeroinitializer, align 4 @b = private global [2 x i32] zeroinitializer, align 4 @c = internal global [2 x i32] zeroinitializer, align 4 -@d = unnamed_addr global [2 x i32] zeroinitializer, align 4 +@_ZL1d = unnamed_addr global [2 x i32] zeroinitializer, align 4 ; Check that we generate internal alias and odr indicator symbols for global to be protected. ; CHECK-NOINDICATOR-NOT: __odr_asan_gen_a ; CHECK-NOALIAS-NOT: private alias +; CHECK-INDICATOR: @___asan_gen_.1 = private unnamed_addr constant [2 x i8] c"a\00", align 1 ; CHECK-INDICATOR: @__odr_asan_gen_a = global i8 0, align 1 +; CHECK-INDICATOR: @___asan_gen_.4 = private unnamed_addr constant [2 x i8] c"d\00", align 1 +; CHECK-INDICATOR: @__odr_asan_gen__ZL1d = global i8 0, align 1 ; CHECK-ALIAS: @0 = private alias { [2 x i32], [24 x i8] }, ptr @a ; CHECK-ALIAS: @1 = private alias { [2 x i32], [24 x i8] }, ptr @b ; CHECK-ALIAS: @2 = private alias { [2 x i32], [24 x i8] }, ptr @c -; CHECK-ALIAS: @3 = private alias { [2 x i32], [24 x i8] }, ptr @d +; CHECK-ALIAS: @3 = private alias { [2 x i32], [24 x i8] }, ptr @_ZL1d ; Function Attrs: nounwind sanitize_address uwtable define i32 @foo(i32 %M) #0 { -- 2.7.4