From: Kostya Serebryany Date: Mon, 24 Nov 2014 18:49:53 +0000 (+0000) Subject: [asan/coverage] change the way asan coverage instrumentation is done: instead of... X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=4cadd4afa0126ff7165e8cc8d0562fea76328945;p=platform%2Fupstream%2Fllvm.git [asan/coverage] change the way asan coverage instrumentation is done: instead of setting the guard to 1 in the generated code, pass the pointer to guard to __sanitizer_cov and set it there. No user-visible functionality change expected llvm-svn: 222675 --- diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep.cc b/compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep.cc index bd98adb..af8f943 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep.cc @@ -12,14 +12,14 @@ // // Compiler instrumentation: // For every interesting basic block the compiler injects the following code: -// if (*Guard) { -// __sanitizer_cov(); -// *Guard = 1; +// if (Guard) { +// __sanitizer_cov(&Guard); // } // It's fine to call __sanitizer_cov more than once for a given block. // // Run-time: // - __sanitizer_cov(): record that we've executed the PC (GET_CALLER_PC). +// and atomically set Guard to 1. // - __sanitizer_cov_dump: dump the coverage data to disk. // For every module of the current process that has coverage data // this will create a file module_name.PID.sancov. The file format is simple: @@ -65,7 +65,7 @@ class CoverageData { void BeforeFork(); void AfterFork(int child_pid); void Extend(uptr npcs); - void Add(uptr pc); + void Add(uptr pc, u8 *guard); void IndirCall(uptr caller, uptr callee, uptr callee_cache[], uptr cache_size); void DumpCallerCalleePairs(); @@ -230,15 +230,19 @@ void CoverageData::Extend(uptr npcs) { atomic_store(&pc_array_size, size, memory_order_release); } -// Simply add the pc into the vector under lock. If the function is called more -// than once for a given PC it will be inserted multiple times, which is fine. -void CoverageData::Add(uptr pc) { +// Atomically add the pc to the vector. The atomically set the guard to 1. +// If the function is called more than once for a given PC it will +// be inserted multiple times, which is fine. +void CoverageData::Add(uptr pc, u8 *guard) { if (!pc_array) return; uptr idx = atomic_fetch_add(&pc_array_index, 1, memory_order_relaxed); CHECK_LT(idx * sizeof(uptr), atomic_load(&pc_array_size, memory_order_acquire)); pc_array[idx] = pc; atomic_fetch_add(&coverage_counter, 1, memory_order_relaxed); + // Set the guard. + atomic_uint8_t *atomic_guard = reinterpret_cast(guard); + atomic_store(atomic_guard, 1, memory_order_relaxed); } // Registers a pair caller=>callee. @@ -537,8 +541,9 @@ void CovAfterFork(int child_pid) { } // namespace __sanitizer extern "C" { -SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov() { - coverage_data.Add(StackTrace::GetPreviousInstructionPc(GET_CALLER_PC())); +SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov(u8 *guard) { + coverage_data.Add(StackTrace::GetPreviousInstructionPc(GET_CALLER_PC()), + guard); } SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_indir_call16(uptr callee, uptr callee_cache16[]) { diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h b/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h index d77ca8f..97eef74 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h @@ -120,7 +120,7 @@ extern "C" { SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_dump(); SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_init(); - SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov(); + SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov(__sanitizer::u8 *guard); SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_annotate_contiguous_container(const void *beg, const void *end, diff --git a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp index f882072..60d7f9f 100644 --- a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp +++ b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp @@ -13,9 +13,8 @@ // We create a Guard boolean variable with the same linkage // as the function and inject this code into the entry block (CoverageLevel=1) // or all blocks (CoverageLevel>=2): -// if (*Guard) { -// __sanitizer_cov(); -// *Guard = 1; +// if (Guard) { +// __sanitizer_cov(&Guard); // } // The accesses to Guard are atomic. The rest of the logic is // in __sanitizer_cov (it's fine to call it more than once). @@ -132,6 +131,7 @@ bool SanitizerCoverageModule::runOnModule(Module &M) { DataLayoutPass *DLP = &getAnalysis(); IntptrTy = Type::getIntNTy(*C, DLP->getDataLayout().getPointerSizeInBits()); Type *VoidTy = Type::getVoidTy(*C); + IRBuilder<> IRB(*C); Function *CtorFunc = Function::Create(FunctionType::get(VoidTy, false), @@ -139,8 +139,8 @@ bool SanitizerCoverageModule::runOnModule(Module &M) { ReturnInst::Create(*C, BasicBlock::Create(*C, "", CtorFunc)); appendToGlobalCtors(M, CtorFunc, kSanCtorAndDtorPriority); - SanCovFunction = - checkInterfaceFunction(M.getOrInsertFunction(kSanCovName, VoidTy, nullptr)); + SanCovFunction = checkInterfaceFunction( + M.getOrInsertFunction(kSanCovName, VoidTy, IRB.getInt8PtrTy(), nullptr)); SanCovIndirCallFunction = checkInterfaceFunction(M.getOrInsertFunction( kSanCovIndirCallName, VoidTy, IntptrTy, IntptrTy, nullptr)); SanCovModuleInit = checkInterfaceFunction(M.getOrInsertFunction( @@ -157,7 +157,7 @@ bool SanitizerCoverageModule::runOnModule(Module &M) { for (auto &F : M) runOnFunction(F); - IRBuilder<> IRB(CtorFunc->getEntryBlock().getTerminator()); + IRB.SetInsertPoint(CtorFunc->getEntryBlock().getTerminator()); IRB.CreateCall(SanCovModuleInit, ConstantInt::get(IntptrTy, SanCovFunction->getNumUses())); return true; @@ -279,10 +279,7 @@ void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F, IRB.SetInsertPoint(Ins); IRB.SetCurrentDebugLocation(EntryLoc); // __sanitizer_cov gets the PC of the instruction using GET_CALLER_PC. - IRB.CreateCall(SanCovFunction); - StoreInst *Store = IRB.CreateStore(ConstantInt::get(Int8Ty, 1), Guard); - Store->setAtomic(Monotonic); - Store->setAlignment(1); + IRB.CreateCall(SanCovFunction, Guard); } char SanitizerCoverageModule::ID = 0; diff --git a/llvm/test/Instrumentation/SanitizerCoverage/coverage-dbg.ll b/llvm/test/Instrumentation/SanitizerCoverage/coverage-dbg.ll index eea93b8..3520f8a 100644 --- a/llvm/test/Instrumentation/SanitizerCoverage/coverage-dbg.ll +++ b/llvm/test/Instrumentation/SanitizerCoverage/coverage-dbg.ll @@ -15,7 +15,7 @@ ; and add sanitize_address to @_ZN1A1fEv ; Test that __sanitizer_cov call has !dbg pointing to the opening { of A::f(). -; CHECK: call void @__sanitizer_cov(), !dbg [[A:!.*]] +; CHECK: call void @__sanitizer_cov(i8*{{.*}}), !dbg [[A:!.*]] ; CHECK: [[A]] = metadata !{i32 6, i32 0, metadata !{{.*}}, null} diff --git a/llvm/test/Instrumentation/SanitizerCoverage/coverage.ll b/llvm/test/Instrumentation/SanitizerCoverage/coverage.ll index da0498d..2d9a56a 100644 --- a/llvm/test/Instrumentation/SanitizerCoverage/coverage.ll +++ b/llvm/test/Instrumentation/SanitizerCoverage/coverage.ll @@ -36,9 +36,9 @@ entry: ; CHECK1: %0 = load atomic i8* @__sancov_gen_cov_foo monotonic, align 1 ; CHECK1: %1 = icmp eq i8 0, %0 ; CHECK1: br i1 %1, label %2, label %3 -; CHECK1: call void @__sanitizer_cov +; CHECK1: call void @__sanitizer_cov(i8*{{.*}}) ; CHECK1-NOT: call void @__sanitizer_cov -; CHECK1: store atomic i8 1, i8* @__sancov_gen_cov_foo monotonic, align 1 +; CHECK1: ret void ; CHECK1-LABEL: define internal void @sancov.module_ctor ; CHECK1-NOT: ret diff --git a/llvm/test/Instrumentation/SanitizerCoverage/coverage2-dbg.ll b/llvm/test/Instrumentation/SanitizerCoverage/coverage2-dbg.ll index 9b26329..f351fe1 100644 --- a/llvm/test/Instrumentation/SanitizerCoverage/coverage2-dbg.ll +++ b/llvm/test/Instrumentation/SanitizerCoverage/coverage2-dbg.ll @@ -17,9 +17,9 @@ target triple = "x86_64-unknown-linux-gnu" ; Check that __sanitizer_cov call has !dgb pointing to the beginning ; of appropriate basic blocks. ; CHECK-LABEL:_Z3fooPi -; CHECK: call void @__sanitizer_cov(), !dbg [[A:!.*]] -; CHECK: call void @__sanitizer_cov(), !dbg [[B:!.*]] -; CHECK: call void @__sanitizer_cov(), !dbg [[C:!.*]] +; CHECK: call void @__sanitizer_cov(i8*{{.*}}), !dbg [[A:!.*]] +; CHECK: call void @__sanitizer_cov(i8*{{.*}}), !dbg [[B:!.*]] +; CHECK: call void @__sanitizer_cov(i8*{{.*}}), !dbg [[C:!.*]] ; CHECK: ret void ; CHECK: [[A]] = metadata !{i32 1, i32 0, metadata !{{.*}}, null} ; CHECK: [[B]] = metadata !{i32 3, i32 5, metadata !{{.*}}, null}