Revert D138095 Use InernalAlloc in DemangleCXXABI

Broke 2/3 tests on macOS which seem to be related to
`free(demangled_name)` in DemangleCXXABI.
This commit is contained in:
Fangrui Song 2022-11-22 16:29:24 -08:00
parent 655d857325
commit 06c74b5e73
4 changed files with 14 additions and 23 deletions

View File

@ -149,8 +149,8 @@ static void CheckODRViolationViaIndicator(const Global *g) {
if (g->odr_indicator == l->g->odr_indicator && if (g->odr_indicator == l->g->odr_indicator &&
(flags()->detect_odr_violation >= 2 || g->size != l->g->size) && (flags()->detect_odr_violation >= 2 || g->size != l->g->size) &&
!IsODRViolationSuppressed(g->name)) !IsODRViolationSuppressed(g->name))
ReportODRViolation(g, FindRegistrationSite(g), l->g, ReportODRViolation(g, FindRegistrationSite(g),
FindRegistrationSite(l->g)); l->g, FindRegistrationSite(l->g));
} }
} }

View File

@ -49,17 +49,12 @@ const char *DemangleCXXABI(const char *name) {
// FIXME: __cxa_demangle aggressively insists on allocating memory. // FIXME: __cxa_demangle aggressively insists on allocating memory.
// There's not much we can do about that, short of providing our // There's not much we can do about that, short of providing our
// own demangler (libc++abi's implementation could be adapted so that // own demangler (libc++abi's implementation could be adapted so that
// it does not allocate). For now, we just call it anyway, and use // it does not allocate). For now, we just call it anyway, and we leak
// InternalAlloc to prevent lsan error. // the returned value.
if (&__cxxabiv1::__cxa_demangle) { if (&__cxxabiv1::__cxa_demangle)
if (char *demangled_name = __cxxabiv1::__cxa_demangle(name, 0, 0, 0)) { if (const char *demangled_name =
size_t size = internal_strlen(demangled_name) + 1; __cxxabiv1::__cxa_demangle(name, 0, 0, 0))
char *buf = (char *)InternalAlloc(size); return demangled_name;
internal_memcpy(buf, demangled_name, size);
free(demangled_name);
return buf;
}
}
return name; return name;
} }

View File

@ -2264,12 +2264,11 @@ bool ModuleAddressSanitizer::InstrumentGlobals(IRBuilder<> &IRB, Module &M,
if (G->hasSanitizerMetadata()) if (G->hasSanitizerMetadata())
MD = G->getSanitizerMetadata(); MD = G->getSanitizerMetadata();
// The runtime library tries demangling symbol names in the descriptor but // TODO: Symbol names in the descriptor can be demangled by the runtime
// functionality like __cxa_demangle may be unavailable (e.g. // library. This could save ~0.4% of VM size for a private large binary.
// -static-libstdc++). So we demangle the symbol names here. std::string NameForGlobal = llvm::demangle(G->getName().str());
std::string NameForGlobal = G->getName().str();
GlobalVariable *Name = GlobalVariable *Name =
createPrivateGlobalForString(M, llvm::demangle(NameForGlobal), createPrivateGlobalForString(M, NameForGlobal,
/*AllowMerging*/ true, kAsanGenPrefix); /*AllowMerging*/ true, kAsanGenPrefix);
Type *Ty = G->getValueType(); Type *Ty = G->getValueType();

View File

@ -13,20 +13,17 @@ target triple = "x86_64-unknown-linux-gnu"
@a = dso_local global [2 x i32] zeroinitializer, align 4 @a = dso_local global [2 x i32] zeroinitializer, align 4
@b = private 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 @c = internal global [2 x i32] zeroinitializer, align 4
@_ZL1d = unnamed_addr global [2 x i32] zeroinitializer, align 4 @d = 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 that we generate internal alias and odr indicator symbols for global to be protected.
; CHECK-NOINDICATOR-NOT: __odr_asan_gen_a ; CHECK-NOINDICATOR-NOT: __odr_asan_gen_a
; CHECK-NOALIAS-NOT: private alias ; 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: @__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: @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: @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: @2 = private alias { [2 x i32], [24 x i8] }, ptr @c
; CHECK-ALIAS: @3 = private alias { [2 x i32], [24 x i8] }, ptr @_ZL1d ; CHECK-ALIAS: @3 = private alias { [2 x i32], [24 x i8] }, ptr @d
; Function Attrs: nounwind sanitize_address uwtable ; Function Attrs: nounwind sanitize_address uwtable
define i32 @foo(i32 %M) #0 { define i32 @foo(i32 %M) #0 {