diff --git a/compiler-rt/lib/msan/msan.cpp b/compiler-rt/lib/msan/msan.cpp --- a/compiler-rt/lib/msan/msan.cpp +++ b/compiler-rt/lib/msan/msan.cpp @@ -584,24 +584,21 @@ // 'descr' is created at compile time and contains '----' in the beginning. // When we see descr for the first time we replace '----' with a uniq id // and set the origin to (id | (31-th bit)). -void __msan_set_alloca_origin(void *a, uptr size, char *descr) { +void __msan_set_alloca_origin(void *a, uptr size, u32 *id_ptr, char *descr) { __msan_set_alloca_origin4( - a, size, descr, + a, size, id_ptr, descr, StackTrace::GetPreviousInstructionPc( reinterpret_cast(__builtin_return_address(0)))); } -void __msan_set_alloca_origin4(void *a, uptr size, char *descr, uptr pc) { - static const u32 dash = '-'; - static const u32 first_timer = - dash + (dash << 8) + (dash << 16) + (dash << 24); - u32 *id_ptr = (u32*)descr; - bool print = false; // Previously: internal_strstr(descr + 4, "AllocaTOTest") != 0; +void __msan_set_alloca_origin4(void *a, uptr size, u32 *id_ptr, char *descr, + uptr pc) { + bool print = false; u32 id = *id_ptr; - if (id == first_timer || id == 0) { + if (id == 0) { u32 idx = atomic_fetch_add(&NumStackOriginDescrs, 1, memory_order_relaxed); CHECK_LT(idx, kNumStackOriginDescrs); - StackOriginDescr[idx] = descr + 4; + StackOriginDescr[idx] = descr; #if SANITIZER_PPC64V1 // On PowerPC64 ELFv1, the address of a function actually points to a // three-doubleword data structure with the first field containing @@ -613,10 +610,10 @@ id = Origin::CreateStackOrigin(idx).raw_id(); *id_ptr = id; if (print) - Printf("First time: idx=%d id=%d %s 0x%zx \n", idx, id, descr + 4, pc); + Printf("First time: idx=%d id=%d %s 0x%zx \n", idx, id, descr, pc); } if (print) - Printf("__msan_set_alloca_origin: descr=%s id=%x\n", descr + 4, id); + Printf("__msan_set_alloca_origin: descr=%s id=%x\n", descr, id); __msan_set_origin(a, size, id); } diff --git a/compiler-rt/lib/msan/msan_interface_internal.h b/compiler-rt/lib/msan/msan_interface_internal.h --- a/compiler-rt/lib/msan/msan_interface_internal.h +++ b/compiler-rt/lib/msan/msan_interface_internal.h @@ -105,9 +105,10 @@ SANITIZER_INTERFACE_ATTRIBUTE void __msan_set_origin(const void *a, uptr size, u32 origin); SANITIZER_INTERFACE_ATTRIBUTE -void __msan_set_alloca_origin(void *a, uptr size, char *descr); +void __msan_set_alloca_origin(void *a, uptr size, u32 *id_ptr, char *descr); SANITIZER_INTERFACE_ATTRIBUTE -void __msan_set_alloca_origin4(void *a, uptr size, char *descr, uptr pc); +void __msan_set_alloca_origin4(void *a, uptr size, u32 *id_ptr, char *descr, + uptr pc); SANITIZER_INTERFACE_ATTRIBUTE u32 __msan_chain_origin(u32 id); SANITIZER_INTERFACE_ATTRIBUTE diff --git a/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp b/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp --- a/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp @@ -686,7 +686,6 @@ OS << ">"; } -#ifdef kda_COMMENTED_OUT /// Create a non-const global initialized with the given string. /// /// Creates a writable global for Str so that we can pass it to the @@ -695,10 +694,9 @@ static GlobalVariable *createPrivateNonConstGlobalForString(Module &M, StringRef Str) { Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str); - return new GlobalVariable(M, StrConst->getType(), /*isConstant=*/false, + return new GlobalVariable(M, StrConst->getType(), /*isConstant=*/true, GlobalValue::PrivateLinkage, StrConst, ""); } -#endif /* kda_COMMENTED_OUT */ /// Create KMSAN API callbacks. void MemorySanitizer::createKernelApi(Module &M) { @@ -829,7 +827,7 @@ MsanSetAllocaOriginFn = M.getOrInsertFunction( "__msan_set_alloca_origin", IRB.getVoidTy(), IRB.getInt8PtrTy(), IntptrTy, - IRB.getInt8PtrTy()); + IRB.getInt8PtrTy(), IRB.getInt8PtrTy()); MsanPoisonStackFn = M.getOrInsertFunction("__msan_poison_stack", IRB.getVoidTy(), IRB.getInt8PtrTy(), IntptrTy); @@ -3869,19 +3867,7 @@ "_msphi_o")); } - Value *getLocalVarOrigin(AllocaInst &I) { -#ifdef kda_COMMENTED_OUT - SmallString<2048> StackDescriptionStorage; - raw_svector_ostream StackDescription(StackDescriptionStorage); - // We create a string with a description of the stack allocation and - // pass it into __msan_set_alloca_origin. - // It will be printed by the run-time if stack-originated UMR is found. - // The first 4 bytes of the string are set to '----' and will be replaced - // by __msan_va_arg_overflow_size_tls at the first call. - StackDescription << "----" << I.getName(); - return createPrivateNonConstGlobalForString(*F.getParent(), - StackDescription.str()); -#endif /* kda_COMMENTED_OUT */ + Value *getLocalVarIdptr(AllocaInst &I) { ConstantInt *IntConst = ConstantInt::get(Type::getInt32Ty((*F.getParent()).getContext()), 0); return new GlobalVariable(*F.getParent(), IntConst->getType(), @@ -3889,6 +3875,16 @@ IntConst); } + Value *getLocalVarDescription(AllocaInst &I) { + SmallString<2048> StackDescriptionStorage; + raw_svector_ostream StackDescription(StackDescriptionStorage); + // Create a string with a description of the stack allocation and + // pass it into __msan_set_alloca_origin. + StackDescription << I.getName(); + return createPrivateNonConstGlobalForString(*F.getParent(), + StackDescription.str()); + } + void poisonAllocaUserspace(AllocaInst &I, IRBuilder<> &IRB, Value *Len) { if (PoisonStack && ClPoisonStackWithCall) { IRB.CreateCall(MS.MsanPoisonStackFn, @@ -3903,19 +3899,23 @@ } if (PoisonStack && MS.TrackOrigins) { - Value *Origin = getLocalVarOrigin(I); + Value *Idptr = getLocalVarIdptr(I); + Value *Descr = getLocalVarDescription(I); IRB.CreateCall(MS.MsanSetAllocaOriginFn, {IRB.CreatePointerCast(&I, IRB.getInt8PtrTy()), Len, - IRB.CreatePointerCast(Origin, IRB.getInt8PtrTy())}); + IRB.CreatePointerCast(Idptr, IRB.getInt8PtrTy()), + IRB.CreatePointerCast(Descr, IRB.getInt8PtrTy())}); } } void poisonAllocaKmsan(AllocaInst &I, IRBuilder<> &IRB, Value *Len) { - Value *Origin = getLocalVarOrigin(I); + Value *Idptr = getLocalVarIdptr(I); + Value *Descr = getLocalVarDescription(I); if (PoisonStack) { IRB.CreateCall(MS.MsanPoisonAllocaFn, {IRB.CreatePointerCast(&I, IRB.getInt8PtrTy()), Len, - IRB.CreatePointerCast(Origin, IRB.getInt8PtrTy())}); + IRB.CreatePointerCast(Idptr, IRB.getInt8PtrTy()), + IRB.CreatePointerCast(Descr, IRB.getInt8PtrTy())}); } else { IRB.CreateCall(MS.MsanUnpoisonAllocaFn, {IRB.CreatePointerCast(&I, IRB.getInt8PtrTy()), Len});