llvm.org GIT mirror llvm / 4b89914
Reapply "[asan] Poor man's coverage that works with ASan" I was able to successfully run a bootstrapped LTO build of clang with r194701, so this change does not seem to be the cause of our failing buildbots. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@194789 91177308-0d34-0410-b5e6-96231b3b80d8 Bob Wilson 7 years ago
2 changed file(s) with 65 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
7676 static const char *const kAsanPoisonGlobalsName = "__asan_before_dynamic_init";
7777 static const char *const kAsanUnpoisonGlobalsName = "__asan_after_dynamic_init";
7878 static const char *const kAsanInitName = "__asan_init_v3";
79 static const char *const kAsanCovName = "__sanitizer_cov";
7980 static const char *const kAsanHandleNoReturnName = "__asan_handle_no_return";
8081 static const char *const kAsanMappingOffsetName = "__asan_mapping_offset";
8182 static const char *const kAsanMappingScaleName = "__asan_mapping_scale";
133134 // This flag may need to be replaced with -f[no]asan-globals.
134135 static cl::opt ClGlobals("asan-globals",
135136 cl::desc("Handle global objects"), cl::Hidden, cl::init(true));
137 static cl::opt ClCoverage("asan-coverage",
138 cl::desc("ASan coverage"), cl::Hidden, cl::init(false));
136139 static cl::opt ClInitializers("asan-initialization-order",
137140 cl::desc("Handle C++ initializer order"), cl::Hidden, cl::init(false));
138141 static cl::opt ClMemIntrin("asan-memintrin",
323326 bool LooksLikeCodeInBug11395(Instruction *I);
324327 void FindDynamicInitializers(Module &M);
325328 bool GlobalIsLinkerInitialized(GlobalVariable *G);
329 bool InjectCoverage(Function &F);
326330
327331 bool CheckInitOrder;
328332 bool CheckUseAfterReturn;
338342 Function *AsanCtorFunction;
339343 Function *AsanInitFunction;
340344 Function *AsanHandleNoReturnFunc;
345 Function *AsanCovFunction;
341346 OwningPtr BL;
342347 // This array is indexed by AccessIsWrite and log2(AccessSize).
343348 Function *AsanErrorCallback[2][kNumberOfAccessSizes];
10841089
10851090 AsanHandleNoReturnFunc = checkInterfaceFunction(M.getOrInsertFunction(
10861091 kAsanHandleNoReturnName, IRB.getVoidTy(), NULL));
1092 AsanCovFunction = checkInterfaceFunction(M.getOrInsertFunction(
1093 kAsanCovName, IRB.getVoidTy(), IntptrTy, NULL));
10871094 // We insert an empty inline asm after __asan_report* to avoid callback merge.
10881095 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
10891096 StringRef(""), StringRef(""),
11531160 return true;
11541161 }
11551162 return false;
1163 }
1164
1165 // Poor man's coverage that works with ASan.
1166 // We create a Guard boolean variable with the same linkage
1167 // as the function and inject this code into the entry block:
1168 // if (*Guard) {
1169 // __sanitizer_cov(&F);
1170 // *Guard = 1;
1171 // }
1172 // The accesses to Guard are atomic. The rest of the logic is
1173 // in __sanitizer_cov (it's fine to call it more than once).
1174 //
1175 // This coverage implementation provides very limited data:
1176 // it only tells if a given function was ever executed.
1177 // No counters, no per-basic-block or per-edge data.
1178 // But for many use cases this is what we need and the added slowdown
1179 // is negligible. This simple implementation will probably be obsoleted
1180 // by the upcoming Clang-based coverage implementation.
1181 // By having it here and now we hope to
1182 // a) get the functionality to users earlier and
1183 // b) collect usage statistics to help improve Clang coverage design.
1184 bool AddressSanitizer::InjectCoverage(Function &F) {
1185 if (!ClCoverage) return false;
1186 IRBuilder<> IRB(F.getEntryBlock().getFirstInsertionPt());
1187 Type *Int8Ty = IRB.getInt8Ty();
1188 GlobalVariable *Guard = new GlobalVariable(
1189 *F.getParent(), Int8Ty, false, F.getLinkage(),
1190 Constant::getNullValue(Int8Ty), "__asan_gen_cov_" + F.getName());
1191 LoadInst *Load = IRB.CreateLoad(Guard);
1192 Load->setAtomic(Monotonic);
1193 Load->setAlignment(1);
1194 Value *Cmp = IRB.CreateICmpEQ(Constant::getNullValue(Int8Ty), Load);
1195 Instruction *Ins = SplitBlockAndInsertIfThen(cast(Cmp), false);
1196 IRB.SetInsertPoint(Ins);
1197 // We pass &F to __sanitizer_cov. We could avoid this and rely on
1198 // GET_CALLER_PC, but having the PC of the first instruction is just nice.
1199 IRB.CreateCall(AsanCovFunction, IRB.CreatePointerCast(&F, IntptrTy));
1200 StoreInst *Store = IRB.CreateStore(ConstantInt::get(Int8Ty, 1), Guard);
1201 Store->setAtomic(Monotonic);
1202 Store->setAlignment(1);
1203 return true;
11561204 }
11571205
11581206 bool AddressSanitizer::runOnFunction(Function &F) {
12501298 }
12511299
12521300 bool res = NumInstrumented > 0 || ChangedStack || !NoReturnCalls.empty();
1301
1302 if (InjectCoverage(F))
1303 res = true;
1304
12531305 DEBUG(dbgs() << "ASAN done instrumenting: " << res << " " << F << "\n");
12541306
12551307 if (ClKeepUninstrumented) {
0 ; RUN: opt < %s -asan -asan-coverage=1 -S | FileCheck %s
1 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
2 target triple = "x86_64-unknown-linux-gnu"
3 define i32 @foo(i32* %a) sanitize_address {
4 entry:
5 ret i32 0
6 }
7 ; CHECK: define i32 @foo(i32* %a) #0 {
8 ; CHECK: %0 = load atomic i8* @__asan_gen_cov_foo monotonic, align 1
9 ; CHECK: %1 = icmp eq i8 0, %0
10 ; CHECK: br i1 %1, label %2, label %3
11 ; CHECK: call void @__sanitizer_cov(i64 ptrtoint (i32 (i32*)* @foo to i64))
12 ; CHECK: store atomic i8 1, i8* @__asan_gen_cov_foo monotonic, align 1