llvm.org GIT mirror llvm / 19b3038
[sanitizer-coverage] add a feature sanitizer-coverage-create-pc-table=1 (works with trace-pc-guard and inline-8bit-counters) that adds a static table of instrumented PCs to be used at run-time git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@309335 91177308-0d34-0410-b5e6-96231b3b80d8 Kostya Serebryany 2 years ago
2 changed file(s) with 104 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
5656 "__sanitizer_cov_trace_pc_guard";
5757 static const char *const SanCovTracePCGuardInitName =
5858 "__sanitizer_cov_trace_pc_guard_init";
59 static const char *const SanCov8bitCountersInitName =
59 static const char *const SanCov8bitCountersInitName =
6060 "__sanitizer_cov_8bit_counters_init";
61 static const char *const SanCovPCsInitName = "__sanitizer_cov_pcs_init";
6162
6263 static const char *const SanCovGuardsSectionName = "sancov_guards";
6364 static const char *const SanCovCountersSectionName = "sancov_cntrs";
65 static const char *const SanCovPCsSectionName = "sancov_pcs";
6466
6567 static cl::opt ClCoverageLevel(
6668 "sanitizer-coverage-level",
7678 cl::desc("pc tracing with a guard"),
7779 cl::Hidden, cl::init(false));
7880
79 static cl::opt ClInline8bitCounters("sanitizer-coverage-inline-8bit-counters",
80 cl::desc("increments 8-bit counter for every edge"),
81 cl::Hidden, cl::init(false));
81 // If true, we create a global variable that contains PCs of all instrumented
82 // BBs, put this global into a named section, and pass this section's bounds
83 // to __sanitizer_cov_pcs_init.
84 // This way the coverage instrumentation does not need to acquire the PCs
85 // at run-time. Works with trace-pc-guard and inline-8bit-counters.
86 static cl::opt ClCreatePCTable("sanitizer-coverage-create-pc-table",
87 cl::desc("create a static PC table"),
88 cl::Hidden, cl::init(false));
89
90 static cl::opt
91 ClInline8bitCounters("sanitizer-coverage-inline-8bit-counters",
92 cl::desc("increments 8-bit counter for every edge"),
93 cl::Hidden, cl::init(false));
8294
8395 static cl::opt
8496 ClCMPTracing("sanitizer-coverage-trace-compares",
171183 GlobalVariable *CreateFunctionLocalArrayInSection(size_t NumElements,
172184 Function &F, Type *Ty,
173185 const char *Section);
174 void CreateFunctionLocalArrays(size_t NumGuards, Function &F);
186 void CreateFunctionLocalArrays(Function &F, ArrayRef AllBlocks);
187 void CreatePCArray(Function &F, ArrayRef AllBlocks);
175188 void InjectCoverageAtBlock(Function &F, BasicBlock &BB, size_t Idx);
176 void CreateInitCallForSection(Module &M, const char *InitFunctionName,
177 Type *Ty, const std::string &Section);
189 Function *CreateInitCallsForSections(Module &M, const char *InitFunctionName,
190 Type *Ty, const char *Section);
191 std::pair
192 CreateSecStartEnd(Module &M, const char *Section, Type *Ty);
178193
179194 void SetNoSanitizeMetadata(Instruction *I) {
180195 I->setMetadata(I->getModule()->getMDKindID("nosanitize"),
200215
201216 GlobalVariable *FunctionGuardArray; // for trace-pc-guard.
202217 GlobalVariable *Function8bitCounterArray; // for inline-8bit-counters.
218 GlobalVariable *FunctionPCsArray; // for create-pc-table.
203219
204220 SanitizerCoverageOptions Options;
205221 };
206222
207223 } // namespace
208224
209 void SanitizerCoverageModule::CreateInitCallForSection(
210 Module &M, const char *InitFunctionName, Type *Ty,
211 const std::string &Section) {
212 IRBuilder<> IRB(M.getContext());
213 Function *CtorFunc;
225 std::pair
226 SanitizerCoverageModule::CreateSecStartEnd(Module &M, const char *Section,
227 Type *Ty) {
214228 GlobalVariable *SecStart =
215229 new GlobalVariable(M, Ty, false, GlobalVariable::ExternalLinkage, nullptr,
216230 getSectionStart(Section));
220234 nullptr, getSectionEnd(Section));
221235 SecEnd->setVisibility(GlobalValue::HiddenVisibility);
222236
237 return std::make_pair(SecStart, SecEnd);
238 }
239
240
241 Function *SanitizerCoverageModule::CreateInitCallsForSections(
242 Module &M, const char *InitFunctionName, Type *Ty,
243 const char *Section) {
244 IRBuilder<> IRB(M.getContext());
245 auto SecStartEnd = CreateSecStartEnd(M, Section, Ty);
246 auto SecStart = SecStartEnd.first;
247 auto SecEnd = SecStartEnd.second;
248 Function *CtorFunc;
223249 std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions(
224250 M, SanCovModuleCtorName, InitFunctionName, {Ty, Ty},
225251 {IRB.CreatePointerCast(SecStart, Ty), IRB.CreatePointerCast(SecEnd, Ty)});
231257 } else {
232258 appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority);
233259 }
260 return CtorFunc;
234261 }
235262
236263 bool SanitizerCoverageModule::runOnModule(Module &M) {
242269 TargetTriple = Triple(M.getTargetTriple());
243270 FunctionGuardArray = nullptr;
244271 Function8bitCounterArray = nullptr;
272 FunctionPCsArray = nullptr;
245273 IntptrTy = Type::getIntNTy(*C, DL->getPointerSizeInBits());
246274 IntptrPtrTy = PointerType::getUnqual(IntptrTy);
247275 Type *VoidTy = Type::getVoidTy(*C);
304332 for (auto &F : M)
305333 runOnFunction(F);
306334
335 Function *Ctor = nullptr;
336
307337 if (FunctionGuardArray)
308 CreateInitCallForSection(M, SanCovTracePCGuardInitName, Int32PtrTy,
309 SanCovGuardsSectionName);
338 Ctor = CreateInitCallsForSections(M, SanCovTracePCGuardInitName, Int32PtrTy,
339 SanCovGuardsSectionName);
310340 if (Function8bitCounterArray)
311 CreateInitCallForSection(M, SanCov8bitCountersInitName, Int8PtrTy,
312 SanCovCountersSectionName);
313
341 Ctor = CreateInitCallsForSections(M, SanCov8bitCountersInitName, Int8PtrTy,
342 SanCovCountersSectionName);
343 if (Ctor && ClCreatePCTable) {
344 auto SecStartEnd = CreateSecStartEnd(M, SanCovPCsSectionName, Int8PtrTy);
345 Function *InitFunction = declareSanitizerInitFunction(
346 M, SanCovPCsInitName, {Int8PtrTy, Int8PtrTy});
347 IRBuilder<> IRBCtor(Ctor->getEntryBlock().getTerminator());
348 IRBCtor.CreateCall(InitFunction,
349 {IRB.CreatePointerCast(SecStartEnd.first, Int8PtrTy),
350 IRB.CreatePointerCast(SecStartEnd.second, Int8PtrTy)});
351 }
314352 return true;
315353 }
316354
449487 Array->setSection(getSectionName(Section));
450488 return Array;
451489 }
452 void SanitizerCoverageModule::CreateFunctionLocalArrays(size_t NumGuards,
453 Function &F) {
490
491 void SanitizerCoverageModule::CreatePCArray(Function &F,
492 ArrayRef AllBlocks) {
493 size_t N = AllBlocks.size();
494 assert(N);
495 assert(&F.getEntryBlock() == AllBlocks[0]);
496 SmallVector PCs;
497 IRBuilder<> IRB(&*F.getEntryBlock().getFirstInsertionPt());
498 PCs.push_back((Constant *)IRB.CreatePointerCast(&F, Int8PtrTy));
499 for (size_t i = 1; i < N; i++)
500 PCs.push_back(BlockAddress::get(AllBlocks[i]));
501 FunctionPCsArray =
502 CreateFunctionLocalArrayInSection(N, F, Int8PtrTy, SanCovPCsSectionName);
503 FunctionPCsArray->setInitializer(
504 ConstantArray::get(ArrayType::get(Int8PtrTy, N), PCs));
505 FunctionPCsArray->setConstant(true);
506 FunctionPCsArray->setAlignment(DL->getPointerSize());
507 }
508
509 void SanitizerCoverageModule::CreateFunctionLocalArrays(
510 Function &F, ArrayRef AllBlocks) {
454511 if (Options.TracePCGuard)
455512 FunctionGuardArray = CreateFunctionLocalArrayInSection(
456 NumGuards, F, Int32Ty, SanCovGuardsSectionName);
513 AllBlocks.size(), F, Int32Ty, SanCovGuardsSectionName);
457514 if (Options.Inline8bitCounters)
458515 Function8bitCounterArray = CreateFunctionLocalArrayInSection(
459 NumGuards, F, Int8Ty, SanCovCountersSectionName);
516 AllBlocks.size(), F, Int8Ty, SanCovCountersSectionName);
517 if (ClCreatePCTable)
518 CreatePCArray(F, AllBlocks);
460519 }
461520
462521 bool SanitizerCoverageModule::InjectCoverage(Function &F,
463522 ArrayRef AllBlocks) {
464523 if (AllBlocks.empty()) return false;
465 CreateFunctionLocalArrays(AllBlocks.size(), F);
524 CreateFunctionLocalArrays(F, AllBlocks);
466525 for (size_t i = 0, N = AllBlocks.size(); i < N; i++)
467526 InjectCoverageAtBlock(F, *AllBlocks[i], i);
468527 return true;
0 ; Test -sanitizer-coverage-create-pc-table=1
1 ; RUN: opt < %s -sancov -sanitizer-coverage-level=3 -sanitizer-coverage-trace-pc-guard -sanitizer-coverage-create-pc-table=1 -S | FileCheck %s
2 ; RUN: opt < %s -sancov -sanitizer-coverage-level=3 -sanitizer-coverage-inline-8bit-counters -sanitizer-coverage-create-pc-table=1 -S | FileCheck %s
3
4 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"
5 target triple = "x86_64-unknown-linux-gnu"
6 define void @foo(i32* %a) sanitize_address {
7 entry:
8 %tobool = icmp eq i32* %a, null
9 br i1 %tobool, label %if.end, label %if.then
10
11 if.then: ; preds = %entry
12 store i32 0, i32* %a, align 4
13 br label %if.end
14
15 if.end: ; preds = %entry, %if.then
16 ret void
17 }
18
19 ; CHECK: private constant [3 x i8*] [{{.*}}@foo{{.*}}blockaddress{{.*}}blockaddress{{.*}}], section "__sancov_pcs", align 8
20 ; CHECK: define internal void @sancov.module_ctor
21 ; CHECK: call void @__sanitizer_cov
22 ; CHECK: call void @__sanitizer_cov_pcs_init