[ASan] Collect unmangled names of global variables in Clang to print them in error reports.

Currently ASan instrumentation pass creates a string with global name
for each instrumented global (to include global names in the error report). Global
name is already mangled at this point, and we may not be able to demangle it
at runtime (e.g. there is no __cxa_demangle on Android).

Instead, create a string with fully qualified global name in Clang, and pass it
to ASan instrumentation pass in llvm.asan.globals metadata. If there is no metadata
for some global, ASan will use the original algorithm.

This fixes https://code.google.com/p/address-sanitizer/issues/detail?id=264.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@212872 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Alexey Samsonov 2014-07-12 00:42:52 +00:00
parent cc6a418600
commit b2d259060d
4 changed files with 39 additions and 20 deletions

View File

@ -216,8 +216,11 @@ namespace {
class GlobalsMetadata {
public:
struct Entry {
Entry() : SourceLoc(nullptr), IsDynInit(false), IsBlacklisted(false) {}
Entry()
: SourceLoc(nullptr), Name(nullptr), IsDynInit(false),
IsBlacklisted(false) {}
GlobalVariable *SourceLoc;
GlobalVariable *Name;
bool IsDynInit;
bool IsBlacklisted;
};
@ -232,7 +235,7 @@ class GlobalsMetadata {
return;
for (auto MDN : Globals->operands()) {
// Metadata node contains the global and the fields of "Entry".
assert(MDN->getNumOperands() == 4);
assert(MDN->getNumOperands() == 5);
Value *V = MDN->getOperand(0);
// The optimizer may optimize away a global entirely.
if (!V)
@ -246,9 +249,14 @@ class GlobalsMetadata {
E.SourceLoc = GVLoc;
addSourceLocationGlobal(GVLoc);
}
ConstantInt *IsDynInit = cast<ConstantInt>(MDN->getOperand(2));
if (Value *Name = MDN->getOperand(2)) {
GlobalVariable *GVName = cast<GlobalVariable>(Name);
E.Name = GVName;
InstrumentationGlobals.insert(GVName);
}
ConstantInt *IsDynInit = cast<ConstantInt>(MDN->getOperand(3));
E.IsDynInit |= IsDynInit->isOne();
ConstantInt *IsBlacklisted = cast<ConstantInt>(MDN->getOperand(3));
ConstantInt *IsBlacklisted = cast<ConstantInt>(MDN->getOperand(4));
E.IsBlacklisted |= IsBlacklisted->isOne();
}
}
@ -1049,6 +1057,14 @@ bool AddressSanitizerModule::InstrumentGlobals(IRBuilder<> &IRB, Module &M) {
for (size_t i = 0; i < n; i++) {
static const uint64_t kMaxGlobalRedzone = 1 << 18;
GlobalVariable *G = GlobalsToChange[i];
auto MD = GlobalsMD.get(G);
// Create string holding the global name unless it was provided by
// the metadata.
GlobalVariable *Name =
MD.Name ? MD.Name : createPrivateGlobalForString(M, G->getName(),
/*AllowMerging*/ true);
PointerType *PtrTy = cast<PointerType>(G->getType());
Type *Ty = PtrTy->getElementType();
uint64_t SizeInBytes = DL->getTypeAllocSize(Ty);
@ -1070,9 +1086,6 @@ bool AddressSanitizerModule::InstrumentGlobals(IRBuilder<> &IRB, Module &M) {
NewTy, G->getInitializer(),
Constant::getNullValue(RightRedZoneTy), NULL);
GlobalVariable *Name =
createPrivateGlobalForString(M, G->getName(), /*AllowMerging*/true);
// Create a new global variable with enough space for a redzone.
GlobalValue::LinkageTypes Linkage = G->getLinkage();
if (G->isConstant() && Linkage == GlobalValue::PrivateLinkage)
@ -1092,8 +1105,6 @@ bool AddressSanitizerModule::InstrumentGlobals(IRBuilder<> &IRB, Module &M) {
NewGlobal->takeName(G);
G->eraseFromParent();
auto MD = GlobalsMD.get(G);
Initializers[i] = ConstantStruct::get(
GlobalStructTy, ConstantExpr::getPointerCast(NewGlobal, IntptrTy),
ConstantInt::get(IntptrTy, SizeInBytes),

View File

@ -18,12 +18,20 @@ target triple = "x86_64-unknown-linux-gnu"
@.asan_loc_descr2 = private unnamed_addr constant { [22 x i8]*, i32, i32 } { [22 x i8]* @.str1, i32 12, i32 14 }
@.asan_loc_descr4 = private unnamed_addr constant { [22 x i8]*, i32, i32 } { [22 x i8]* @.str1, i32 14, i32 25 }
; Global names:
@.str2 = private unnamed_addr constant [7 x i8] c"global\00", align 1
@.str3 = private unnamed_addr constant [16 x i8] c"dyn_init_global\00", align 1
@.str4 = private unnamed_addr constant [11 x i8] c"static_var\00", align 1
@.str5 = private unnamed_addr constant [17 x i8] c"<string literal>\00", align 1
; Check that globals were instrumented, but sanitizer location descriptors weren't:
; CHECK: @global = global { i32, [60 x i8] } zeroinitializer, align 32
; CHECK: @.str = internal unnamed_addr constant { [14 x i8], [50 x i8] } { [14 x i8] c"Hello, world!\00", [50 x i8] zeroinitializer }, align 32
; CHECK: @.asan_loc_descr = private unnamed_addr constant { [22 x i8]*, i32, i32 } { [22 x i8]* @.str1, i32 5, i32 5 }
; CHECK: @.str2 = private unnamed_addr constant [7 x i8] c"global\00", align 1
; Check that location decriptors were passed into __asan_register_globals:
; Check that location decriptors and global names were passed into __asan_register_globals:
; CHECK: i64 ptrtoint ([7 x i8]* @.str2 to i64)
; CHECK: i64 ptrtoint ({ [22 x i8]*, i32, i32 }* @.asan_loc_descr to i64)
; Function Attrs: nounwind sanitize_address
@ -55,9 +63,9 @@ attributes #1 = { nounwind sanitize_address "less-precise-fpmad"="false" "no-fra
!llvm.asan.globals = !{!0, !1, !2, !3, !4}
!llvm.ident = !{!5}
!0 = metadata !{i32* @global, { [22 x i8]*, i32, i32 }* @.asan_loc_descr, i1 false, i1 false}
!1 = metadata !{i32* @dyn_init_global, { [22 x i8]*, i32, i32 }* @.asan_loc_descr1, i1 true, i1 false}
!2 = metadata !{i32* @blacklisted_global, null, i1 false, i1 true}
!3 = metadata !{i32* @_ZZ4funcvE10static_var, { [22 x i8]*, i32, i32 }* @.asan_loc_descr2, i1 false, i1 false}
!4 = metadata !{[14 x i8]* @.str, { [22 x i8]*, i32, i32 }* @.asan_loc_descr4, i1 false, i1 false}
!0 = metadata !{i32* @global, { [22 x i8]*, i32, i32 }* @.asan_loc_descr, [7 x i8]* @.str2, i1 false, i1 false}
!1 = metadata !{i32* @dyn_init_global, { [22 x i8]*, i32, i32 }* @.asan_loc_descr1, [16 x i8]* @.str3, i1 true, i1 false}
!2 = metadata !{i32* @blacklisted_global, null, null, i1 false, i1 true}
!3 = metadata !{i32* @_ZZ4funcvE10static_var, { [22 x i8]*, i32, i32 }* @.asan_loc_descr2, [11 x i8]* @.str4, i1 false, i1 false}
!4 = metadata !{[14 x i8]* @.str, { [22 x i8]*, i32, i32 }* @.asan_loc_descr4, [17 x i8]* @.str5, i1 false, i1 false}
!5 = metadata !{metadata !"clang version 3.5.0 (211282)"}

View File

@ -69,7 +69,7 @@ entry:
!llvm.asan.globals = !{!0}
!0 = metadata !{[10 x i32]* @GlobDy, null, i1 true, i1 false}
!0 = metadata !{[10 x i32]* @GlobDy, null, null, i1 true, i1 false}
; CHECK-LABEL: define internal void @asan.module_ctor
; CHECK-NOT: ret

View File

@ -7,10 +7,10 @@ target triple = "x86_64-unknown-linux-gnu"
@YYY = global i32 0, align 4 ; W/o dynamic initializer.
; Clang will emit the following metadata identifying @xxx as dynamically
; initialized.
!0 = metadata !{i32* @xxx, null, i1 true, i1 false}
!1 = metadata !{i32* @XXX, null, i1 true, i1 false}
!2 = metadata !{i32* @yyy, null, i1 false, i1 false}
!3 = metadata !{i32* @YYY, null, i1 false, i1 false}
!0 = metadata !{i32* @xxx, null, null, i1 true, i1 false}
!1 = metadata !{i32* @XXX, null, null, i1 true, i1 false}
!2 = metadata !{i32* @yyy, null, null, i1 false, i1 false}
!3 = metadata !{i32* @YYY, null, null, i1 false, i1 false}
!llvm.asan.globals = !{!0, !1, !2, !3}
define i32 @initializer() uwtable {