LLVM 24.0.0git
SanitizerCoverage.cpp
Go to the documentation of this file.
1//===-- SanitizerCoverage.cpp - coverage instrumentation for sanitizers ---===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Coverage instrumentation done on LLVM IR level, works with Sanitizers.
10//
11//===----------------------------------------------------------------------===//
12
15#include "llvm/ADT/ArrayRef.h"
19#include "llvm/IR/Constant.h"
20#include "llvm/IR/Constants.h"
21#include "llvm/IR/DataLayout.h"
22#include "llvm/IR/Dominators.h"
24#include "llvm/IR/Function.h"
26#include "llvm/IR/IRBuilder.h"
28#include "llvm/IR/Intrinsics.h"
29#include "llvm/IR/LLVMContext.h"
30#include "llvm/IR/MDBuilder.h"
31#include "llvm/IR/Module.h"
32#include "llvm/IR/Type.h"
40
41using namespace llvm;
42
43#define DEBUG_TYPE "sancov"
44
45const char SanCovTracePCIndirName[] = "__sanitizer_cov_trace_pc_indir";
46const char SanCovTracePCName[] = "__sanitizer_cov_trace_pc";
47const char SanCovTracePCEntryName[] = "__sanitizer_cov_trace_pc_entry";
48const char SanCovTracePCExitName[] = "__sanitizer_cov_trace_pc_exit";
49const char SanCovTraceCmp1[] = "__sanitizer_cov_trace_cmp1";
50const char SanCovTraceCmp2[] = "__sanitizer_cov_trace_cmp2";
51const char SanCovTraceCmp4[] = "__sanitizer_cov_trace_cmp4";
52const char SanCovTraceCmp8[] = "__sanitizer_cov_trace_cmp8";
53const char SanCovTraceConstCmp1[] = "__sanitizer_cov_trace_const_cmp1";
54const char SanCovTraceConstCmp2[] = "__sanitizer_cov_trace_const_cmp2";
55const char SanCovTraceConstCmp4[] = "__sanitizer_cov_trace_const_cmp4";
56const char SanCovTraceConstCmp8[] = "__sanitizer_cov_trace_const_cmp8";
57const char SanCovLoad1[] = "__sanitizer_cov_load1";
58const char SanCovLoad2[] = "__sanitizer_cov_load2";
59const char SanCovLoad4[] = "__sanitizer_cov_load4";
60const char SanCovLoad8[] = "__sanitizer_cov_load8";
61const char SanCovLoad16[] = "__sanitizer_cov_load16";
62const char SanCovStore1[] = "__sanitizer_cov_store1";
63const char SanCovStore2[] = "__sanitizer_cov_store2";
64const char SanCovStore4[] = "__sanitizer_cov_store4";
65const char SanCovStore8[] = "__sanitizer_cov_store8";
66const char SanCovStore16[] = "__sanitizer_cov_store16";
67const char SanCovTraceDiv4[] = "__sanitizer_cov_trace_div4";
68const char SanCovTraceDiv8[] = "__sanitizer_cov_trace_div8";
69const char SanCovTraceGep[] = "__sanitizer_cov_trace_gep";
70const char SanCovTraceSwitchName[] = "__sanitizer_cov_trace_switch";
72 "sancov.module_ctor_trace_pc_guard";
74 "sancov.module_ctor_8bit_counters";
75const char SanCovModuleCtorBoolFlagName[] = "sancov.module_ctor_bool_flag";
77
78const char SanCovTracePCGuardName[] = "__sanitizer_cov_trace_pc_guard";
79const char SanCovTracePCGuardInitName[] = "__sanitizer_cov_trace_pc_guard_init";
80const char SanCov8bitCountersInitName[] = "__sanitizer_cov_8bit_counters_init";
81const char SanCovBoolFlagInitName[] = "__sanitizer_cov_bool_flag_init";
82const char SanCovPCsInitName[] = "__sanitizer_cov_pcs_init";
83const char SanCovCFsInitName[] = "__sanitizer_cov_cfs_init";
84
85const char SanCovGuardsSectionName[] = "sancov_guards";
86const char SanCovCountersSectionName[] = "sancov_cntrs";
87const char SanCovBoolFlagSectionName[] = "sancov_bools";
88const char SanCovPCsSectionName[] = "sancov_pcs";
89const char SanCovCFsSectionName[] = "sancov_cfs";
90const char SanCovCallbackGateSectionName[] = "sancov_gate";
91
92const char SanCovStackDepthCallbackName[] = "__sanitizer_cov_stack_depth";
93const char SanCovLowestStackName[] = "__sancov_lowest_stack";
94const char SanCovCallbackGateName[] = "__sancov_should_track";
95
96namespace {
97
98SanitizerCoverageOptions getOptions(int LegacyCoverageLevel) {
100 switch (LegacyCoverageLevel) {
101 case 0:
103 break;
104 case 1:
106 break;
107 case 2:
109 break;
110 case 3:
112 break;
113 case 4:
115 Res.IndirectCalls = true;
116 break;
117 }
118 return Res;
119}
120
121SanitizerCoverageOptions overrideFromCL(const InstrumentationOptions &Opts,
123 // Sets CoverageType and IndirectCalls.
124 SanitizerCoverageOptions CLOpts = getOptions(Opts.sanitizer_coverage_level);
125 Options.CoverageType = std::max(Options.CoverageType, CLOpts.CoverageType);
126 Options.IndirectCalls |= CLOpts.IndirectCalls;
127 Options.TraceCmp |= Opts.sanitizer_coverage_trace_compares;
128 Options.TraceDiv |= Opts.sanitizer_coverage_trace_divs;
129 Options.TraceGep |= Opts.sanitizer_coverage_trace_geps;
130 Options.TracePC |= Opts.sanitizer_coverage_trace_pc;
131 Options.TracePCEntryExit |= Opts.sanitizer_coverage_trace_pc_entry_exit;
132 Options.TracePCGuard |= Opts.sanitizer_coverage_trace_pc_guard;
133 Options.Inline8bitCounters |= Opts.sanitizer_coverage_inline_8bit_counters;
134 Options.InlineBoolFlag |= Opts.sanitizer_coverage_inline_bool_flag;
135 Options.PCTable |= Opts.sanitizer_coverage_pc_table;
136 Options.NoPrune |= !Opts.sanitizer_coverage_prune_blocks;
137 Options.StackDepth |= Opts.sanitizer_coverage_stack_depth;
138 Options.StackDepthCallbackMin =
139 std::max(Options.StackDepthCallbackMin,
140 Opts.sanitizer_coverage_stack_depth_callback_min);
141 Options.TraceLoads |= Opts.sanitizer_coverage_trace_loads;
142 Options.TraceStores |= Opts.sanitizer_coverage_trace_stores;
143 Options.GatedCallbacks |= Opts.sanitizer_coverage_gated_trace_callbacks;
144 if (!Options.TracePCGuard && !Options.TracePC && !Options.TracePCEntryExit &&
145 !Options.Inline8bitCounters && !Options.StackDepth &&
146 !Options.InlineBoolFlag && !Options.TraceLoads && !Options.TraceStores)
147 Options.TracePCGuard = true; // TracePCGuard is default.
148 Options.CollectControlFlow |= Opts.sanitizer_coverage_control_flow;
149 return Options;
150}
151
152class ModuleSanitizerCoverage {
153public:
154 using DomTreeCallback = function_ref<const DominatorTree &(Function &F)>;
155 using PostDomTreeCallback =
157
158 ModuleSanitizerCoverage(const InstrumentationOptions &Opts, Module &M,
159 DomTreeCallback DTCallback,
160 PostDomTreeCallback PDTCallback,
162 const SpecialCaseList *Allowlist,
163 const SpecialCaseList *Blocklist)
164 : Opts(Opts), M(M), DTCallback(DTCallback), PDTCallback(PDTCallback),
165 Options(Options), Allowlist(Allowlist), Blocklist(Blocklist) {}
166
167 bool instrumentModule();
168
169private:
170 void createFunctionControlFlow(Function &F);
171 void instrumentFunction(Function &F);
172 void InjectCoverageForIndirectCalls(Function &F,
173 ArrayRef<Instruction *> IndirCalls);
174 void InjectTraceForCmp(Function &F, ArrayRef<Instruction *> CmpTraceTargets,
175 Value *&FunctionGateCmp);
176 void InjectTraceForDiv(Function &F,
177 ArrayRef<BinaryOperator *> DivTraceTargets);
178 void InjectTraceForGep(Function &F,
179 ArrayRef<GetElementPtrInst *> GepTraceTargets);
180 void InjectTraceForLoadsAndStores(Function &F, ArrayRef<LoadInst *> Loads,
181 ArrayRef<StoreInst *> Stores);
182 void InjectTraceForExits(Function &F);
183 void InjectTraceForSwitch(Function &F,
184 ArrayRef<Instruction *> SwitchTraceTargets,
185 Value *&FunctionGateCmp);
186 bool InjectCoverage(Function &F, ArrayRef<BasicBlock *> AllBlocks,
187 Value *&FunctionGateCmp, bool IsLeafFunc);
188 GlobalVariable *CreateFunctionLocalArrayInSection(size_t NumElements,
189 Function &F, Type *Ty,
190 const char *Section);
191 GlobalVariable *CreatePCArray(Function &F, ArrayRef<BasicBlock *> AllBlocks);
192 void CreateFunctionLocalArrays(Function &F, ArrayRef<BasicBlock *> AllBlocks);
193 Instruction *CreateGateBranch(Function &F, Value *&FunctionGateCmp,
194 Instruction *I);
195 Value *CreateFunctionLocalGateCmp(IRBuilder<> &IRB);
196 void InjectCoverageAtBlock(Function &F, BasicBlock &BB, size_t Idx,
197 Value *&FunctionGateCmp, bool IsLeafFunc);
198 Function *CreateInitCallsForSections(Module &M, const char *CtorName,
199 const char *InitFunctionName, Type *Ty,
200 const char *Section);
201 std::pair<Value *, Value *> CreateSecStartEnd(Module &M, const char *Section,
202 Type *Ty);
203
204 std::string getSectionName(const std::string &Section) const;
205 std::string getSectionStart(const std::string &Section) const;
206 std::string getSectionEnd(const std::string &Section) const;
207
208 const InstrumentationOptions &Opts;
209 Module &M;
210 DomTreeCallback DTCallback;
211 PostDomTreeCallback PDTCallback;
212
213 FunctionCallee SanCovStackDepthCallback;
214 FunctionCallee SanCovTracePCIndir;
215 FunctionCallee SanCovTracePC, SanCovTracePCGuard;
216 FunctionCallee SanCovTracePCEntry, SanCovTracePCExit;
217 std::array<FunctionCallee, 4> SanCovTraceCmpFunction;
218 std::array<FunctionCallee, 4> SanCovTraceConstCmpFunction;
219 std::array<FunctionCallee, 5> SanCovLoadFunction;
220 std::array<FunctionCallee, 5> SanCovStoreFunction;
221 std::array<FunctionCallee, 2> SanCovTraceDivFunction;
222 FunctionCallee SanCovTraceGepFunction;
223 FunctionCallee SanCovTraceSwitchFunction;
224 GlobalVariable *SanCovLowestStack;
225 GlobalVariable *SanCovCallbackGate;
226 Type *PtrTy, *IntptrTy, *Int64Ty, *Int32Ty, *Int16Ty, *Int8Ty, *Int1Ty;
227 Module *CurModule;
228 Triple TargetTriple;
229 LLVMContext *C;
230 const DataLayout *DL;
231
232 GlobalVariable *FunctionGuardArray; // for trace-pc-guard.
233 GlobalVariable *Function8bitCounterArray; // for inline-8bit-counters.
234 GlobalVariable *FunctionBoolArray; // for inline-bool-flag.
235 GlobalVariable *FunctionPCsArray; // for pc-table.
236 GlobalVariable *FunctionCFsArray; // for control flow table
237 SmallVector<GlobalValue *, 20> GlobalsToAppendToUsed;
238 SmallVector<GlobalValue *, 20> GlobalsToAppendToCompilerUsed;
239
241
242 const SpecialCaseList *Allowlist;
243 const SpecialCaseList *Blocklist;
244};
245} // namespace
246
249 const std::vector<std::string> &AllowlistFiles,
250 const std::vector<std::string> &BlocklistFiles)
251 : Options(std::move(Options)),
252 VFS(VFS ? std::move(VFS) : vfs::getRealFileSystem()) {
253 if (AllowlistFiles.size() > 0)
254 Allowlist = SpecialCaseList::createOrDie(AllowlistFiles, *this->VFS);
255 if (BlocklistFiles.size() > 0)
256 Blocklist = SpecialCaseList::createOrDie(BlocklistFiles, *this->VFS);
257}
258
261 auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
262 auto DTCallback = [&FAM](Function &F) -> const DominatorTree & {
263 return FAM.getResult<DominatorTreeAnalysis>(F);
264 };
265 auto PDTCallback = [&FAM](Function &F) -> const PostDominatorTree & {
266 return FAM.getResult<PostDominatorTreeAnalysis>(F);
267 };
268 const InstrumentationOptions &Opts = InstrumentationOptions::Global;
269 ModuleSanitizerCoverage ModuleSancov(Opts, M, DTCallback, PDTCallback,
270 overrideFromCL(Opts, Options),
271 Allowlist.get(), Blocklist.get());
272 if (!ModuleSancov.instrumentModule())
273 return PreservedAnalyses::all();
274
276 // GlobalsAA is considered stateless and does not get invalidated unless
277 // explicitly invalidated; PreservedAnalyses::none() is not enough. Sanitizers
278 // make changes that require GlobalsAA to be invalidated.
279 PA.abandon<GlobalsAA>();
280 return PA;
281}
282
283std::pair<Value *, Value *>
284ModuleSanitizerCoverage::CreateSecStartEnd(Module &M, const char *Section,
285 Type *Ty) {
286 // Use ExternalWeak so that if all sections are discarded due to section
287 // garbage collection, the linker will not report undefined symbol errors.
288 // Windows defines the start/stop symbols in compiler-rt so no need for
289 // ExternalWeak.
290 GlobalValue::LinkageTypes Linkage = TargetTriple.isOSBinFormatCOFF()
293 GlobalVariable *SecStart = new GlobalVariable(M, Ty, false, Linkage, nullptr,
294 getSectionStart(Section));
296 GlobalVariable *SecEnd = new GlobalVariable(M, Ty, false, Linkage, nullptr,
297 getSectionEnd(Section));
299 if (!TargetTriple.isOSBinFormatCOFF())
300 return std::make_pair(SecStart, SecEnd);
301
302 // Account for the fact that on windows-msvc __start_* symbols actually
303 // point to a uint64_t before the start of the array.
305 SecStart, ConstantInt::get(IntptrTy, sizeof(uint64_t)));
306 return std::make_pair(GEP, SecEnd);
307}
308
309Function *ModuleSanitizerCoverage::CreateInitCallsForSections(
310 Module &M, const char *CtorName, const char *InitFunctionName, Type *Ty,
311 const char *Section) {
312 if (Opts.sanitizer_coverage_drop_ctors)
313 return nullptr;
314 auto SecStartEnd = CreateSecStartEnd(M, Section, Ty);
315 auto SecStart = SecStartEnd.first;
316 auto SecEnd = SecStartEnd.second;
317 Function *CtorFunc;
318 std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions(
319 M, CtorName, InitFunctionName, {PtrTy, PtrTy}, {SecStart, SecEnd});
320 assert(CtorFunc->getName() == CtorName);
321
322 if (TargetTriple.supportsCOMDAT()) {
323 // Use comdat to dedup CtorFunc.
324 CtorFunc->setComdat(M.getOrInsertComdat(CtorName));
325 appendToGlobalCtors(M, CtorFunc, SanCtorAndDtorPriority, CtorFunc);
326 } else {
328 }
329
330 if (TargetTriple.isOSBinFormatCOFF()) {
331 // In COFF files, if the contructors are set as COMDAT (they are because
332 // COFF supports COMDAT) and the linker flag /OPT:REF (strip unreferenced
333 // functions and data) is used, the constructors get stripped. To prevent
334 // this, give the constructors weak ODR linkage and ensure the linker knows
335 // to include the sancov constructor. This way the linker can deduplicate
336 // the constructors but always leave one copy.
338 }
339 return CtorFunc;
340}
341
342bool ModuleSanitizerCoverage::instrumentModule() {
344 return false;
345 if (Allowlist &&
346 !Allowlist->inSection("coverage", "src", M.getSourceFileName()))
347 return false;
348 if (Blocklist &&
349 Blocklist->inSection("coverage", "src", M.getSourceFileName()))
350 return false;
351 C = &(M.getContext());
352 DL = &M.getDataLayout();
353 CurModule = &M;
354 TargetTriple = M.getTargetTriple();
355 FunctionGuardArray = nullptr;
356 Function8bitCounterArray = nullptr;
357 FunctionBoolArray = nullptr;
358 FunctionPCsArray = nullptr;
359 FunctionCFsArray = nullptr;
360 IntptrTy = Type::getIntNTy(*C, DL->getPointerSizeInBits());
361 PtrTy = PointerType::getUnqual(*C);
362 Type *VoidTy = Type::getVoidTy(*C);
363 IRBuilder<> IRB(M);
364 Int64Ty = IRB.getInt64Ty();
365 Int32Ty = IRB.getInt32Ty();
366 Int16Ty = IRB.getInt16Ty();
367 Int8Ty = IRB.getInt8Ty();
368 Int1Ty = IRB.getInt1Ty();
369
370 SanCovTracePCIndir =
371 M.getOrInsertFunction(SanCovTracePCIndirName, VoidTy, IntptrTy);
372 // Make sure smaller parameters are zero-extended to i64 if required by the
373 // target ABI.
374 AttributeList SanCovTraceCmpZeroExtAL;
375 SanCovTraceCmpZeroExtAL =
376 SanCovTraceCmpZeroExtAL.addParamAttribute(*C, 0, Attribute::ZExt);
377 SanCovTraceCmpZeroExtAL =
378 SanCovTraceCmpZeroExtAL.addParamAttribute(*C, 1, Attribute::ZExt);
379
380 SanCovTraceCmpFunction[0] =
381 M.getOrInsertFunction(SanCovTraceCmp1, SanCovTraceCmpZeroExtAL, VoidTy,
382 IRB.getInt8Ty(), IRB.getInt8Ty());
383 SanCovTraceCmpFunction[1] =
384 M.getOrInsertFunction(SanCovTraceCmp2, SanCovTraceCmpZeroExtAL, VoidTy,
385 IRB.getInt16Ty(), IRB.getInt16Ty());
386 SanCovTraceCmpFunction[2] =
387 M.getOrInsertFunction(SanCovTraceCmp4, SanCovTraceCmpZeroExtAL, VoidTy,
388 IRB.getInt32Ty(), IRB.getInt32Ty());
389 SanCovTraceCmpFunction[3] =
390 M.getOrInsertFunction(SanCovTraceCmp8, VoidTy, Int64Ty, Int64Ty);
391
392 SanCovTraceConstCmpFunction[0] = M.getOrInsertFunction(
393 SanCovTraceConstCmp1, SanCovTraceCmpZeroExtAL, VoidTy, Int8Ty, Int8Ty);
394 SanCovTraceConstCmpFunction[1] = M.getOrInsertFunction(
395 SanCovTraceConstCmp2, SanCovTraceCmpZeroExtAL, VoidTy, Int16Ty, Int16Ty);
396 SanCovTraceConstCmpFunction[2] = M.getOrInsertFunction(
397 SanCovTraceConstCmp4, SanCovTraceCmpZeroExtAL, VoidTy, Int32Ty, Int32Ty);
398 SanCovTraceConstCmpFunction[3] =
399 M.getOrInsertFunction(SanCovTraceConstCmp8, VoidTy, Int64Ty, Int64Ty);
400
401 // Loads.
402 SanCovLoadFunction[0] = M.getOrInsertFunction(SanCovLoad1, VoidTy, PtrTy);
403 SanCovLoadFunction[1] = M.getOrInsertFunction(SanCovLoad2, VoidTy, PtrTy);
404 SanCovLoadFunction[2] = M.getOrInsertFunction(SanCovLoad4, VoidTy, PtrTy);
405 SanCovLoadFunction[3] = M.getOrInsertFunction(SanCovLoad8, VoidTy, PtrTy);
406 SanCovLoadFunction[4] = M.getOrInsertFunction(SanCovLoad16, VoidTy, PtrTy);
407 // Stores.
408 SanCovStoreFunction[0] = M.getOrInsertFunction(SanCovStore1, VoidTy, PtrTy);
409 SanCovStoreFunction[1] = M.getOrInsertFunction(SanCovStore2, VoidTy, PtrTy);
410 SanCovStoreFunction[2] = M.getOrInsertFunction(SanCovStore4, VoidTy, PtrTy);
411 SanCovStoreFunction[3] = M.getOrInsertFunction(SanCovStore8, VoidTy, PtrTy);
412 SanCovStoreFunction[4] = M.getOrInsertFunction(SanCovStore16, VoidTy, PtrTy);
413
414 {
415 AttributeList AL;
416 AL = AL.addParamAttribute(*C, 0, Attribute::ZExt);
417 SanCovTraceDivFunction[0] =
418 M.getOrInsertFunction(SanCovTraceDiv4, AL, VoidTy, IRB.getInt32Ty());
419 }
420 SanCovTraceDivFunction[1] =
421 M.getOrInsertFunction(SanCovTraceDiv8, VoidTy, Int64Ty);
422 SanCovTraceGepFunction =
423 M.getOrInsertFunction(SanCovTraceGep, VoidTy, IntptrTy);
424 SanCovTraceSwitchFunction =
425 M.getOrInsertFunction(SanCovTraceSwitchName, VoidTy, Int64Ty, PtrTy);
426
427 SanCovLowestStack = M.getOrInsertGlobal(SanCovLowestStackName, IntptrTy);
428 if (SanCovLowestStack->getValueType() != IntptrTy) {
429 C->emitError(StringRef("'") + SanCovLowestStackName +
430 "' should not be declared by the user");
431 return true;
432 }
433 SanCovLowestStack->setThreadLocalMode(
435 if (Options.StackDepth && !SanCovLowestStack->isDeclaration())
436 SanCovLowestStack->setInitializer(Constant::getAllOnesValue(IntptrTy));
437
438 if (Options.GatedCallbacks) {
439 if (!Options.TracePCGuard && !Options.TraceCmp) {
440 C->emitError("'sanitizer-coverage-gated-trace-callbacks' is only "
441 "supported with trace-pc-guard or trace-cmp");
442 return true;
443 }
444
445 SanCovCallbackGate = cast<GlobalVariable>(
446 M.getOrInsertGlobal(SanCovCallbackGateName, Int64Ty));
447 SanCovCallbackGate->setSection(
449 SanCovCallbackGate->setInitializer(Constant::getNullValue(Int64Ty));
450 SanCovCallbackGate->setLinkage(GlobalVariable::LinkOnceAnyLinkage);
451 SanCovCallbackGate->setVisibility(GlobalVariable::HiddenVisibility);
452 appendToCompilerUsed(M, SanCovCallbackGate);
453 }
454
455 SanCovTracePC = M.getOrInsertFunction(SanCovTracePCName, VoidTy);
456 SanCovTracePCEntry = M.getOrInsertFunction(SanCovTracePCEntryName, VoidTy);
457 SanCovTracePCExit = M.getOrInsertFunction(SanCovTracePCExitName, VoidTy);
458 SanCovTracePCGuard =
459 M.getOrInsertFunction(SanCovTracePCGuardName, VoidTy, PtrTy);
460
461 SanCovStackDepthCallback =
462 M.getOrInsertFunction(SanCovStackDepthCallbackName, VoidTy);
463
464 for (auto &F : M)
465 instrumentFunction(F);
466
467 Function *Ctor = nullptr;
468
469 if (FunctionGuardArray)
470 Ctor = CreateInitCallsForSections(M, SanCovModuleCtorTracePcGuardName,
473 if (Function8bitCounterArray)
474 Ctor = CreateInitCallsForSections(M, SanCovModuleCtor8bitCountersName,
477 if (FunctionBoolArray) {
478 Ctor = CreateInitCallsForSections(M, SanCovModuleCtorBoolFlagName,
481 }
482 if (Ctor && Options.PCTable) {
483 auto SecStartEnd = CreateSecStartEnd(M, SanCovPCsSectionName, IntptrTy);
484 FunctionCallee InitFunction =
486 IRBuilder<> IRBCtor(Ctor->getEntryBlock().getTerminator());
487 IRBCtor.CreateCall(InitFunction, {SecStartEnd.first, SecStartEnd.second});
488 }
489
490 if (Ctor && Options.CollectControlFlow) {
491 auto SecStartEnd = CreateSecStartEnd(M, SanCovCFsSectionName, IntptrTy);
492 FunctionCallee InitFunction =
494 IRBuilder<> IRBCtor(Ctor->getEntryBlock().getTerminator());
495 IRBCtor.CreateCall(InitFunction, {SecStartEnd.first, SecStartEnd.second});
496 }
497
498 appendToUsed(M, GlobalsToAppendToUsed);
499 appendToCompilerUsed(M, GlobalsToAppendToCompilerUsed);
500 return true;
501}
502
503// True if block has successors and it dominates all of them.
504static bool isFullDominator(const BasicBlock *BB, const DominatorTree &DT) {
505 if (succ_empty(BB))
506 return false;
507
508 return llvm::all_of(successors(BB), [&](const BasicBlock *SUCC) {
509 return DT.dominates(BB, SUCC);
510 });
511}
512
513// True if block has predecessors and it postdominates all of them.
514static bool isFullPostDominator(const BasicBlock *BB,
515 const PostDominatorTree &PDT) {
516 if (pred_empty(BB))
517 return false;
518
519 return llvm::all_of(predecessors(BB), [&](const BasicBlock *PRED) {
520 return PDT.dominates(BB, PRED);
521 });
522}
523
524static bool shouldInstrumentBlock(const Function &F, const BasicBlock *BB,
525 const DominatorTree &DT,
526 const PostDominatorTree &PDT,
528 // Don't insert coverage for blocks containing nothing but unreachable: we
529 // will never call __sanitizer_cov() for them, so counting them in
530 // NumberOfInstrumentedBlocks() might complicate calculation of code coverage
531 // percentage. Also, unreachable instructions frequently have no debug
532 // locations.
534 return false;
535
536 // Don't insert coverage into blocks without a valid insertion point
537 // (catchswitch blocks).
538 if (BB->getFirstInsertionPt() == BB->end())
539 return false;
540
541 if (Options.NoPrune || &F.getEntryBlock() == BB)
542 return true;
543
545 &F.getEntryBlock() != BB)
546 return false;
547
548 // Do not instrument full dominators, or full post-dominators with multiple
549 // predecessors.
550 return !isFullDominator(BB, DT) &&
551 !(isFullPostDominator(BB, PDT) && !BB->getSinglePredecessor());
552}
553
554// Returns true iff From->To is a backedge.
555// A twist here is that we treat From->To as a backedge if
556// * To dominates From or
557// * To->UniqueSuccessor dominates From
558static bool IsBackEdge(BasicBlock *From, BasicBlock *To,
559 const DominatorTree &DT) {
560 if (DT.dominates(To, From))
561 return true;
562 if (auto Next = To->getUniqueSuccessor())
563 if (DT.dominates(Next, From))
564 return true;
565 return false;
566}
567
568// Prunes uninteresting Cmp instrumentation:
569// * CMP instructions that feed into loop backedge branch.
570//
571// Note that Cmp pruning is controlled by the same flag as the
572// BB pruning.
573static bool IsInterestingCmp(ICmpInst *CMP, const DominatorTree &DT,
575 if (!Options.NoPrune)
576 if (CMP->hasOneUse())
577 if (auto BR = dyn_cast<CondBrInst>(CMP->user_back()))
578 for (BasicBlock *B : BR->successors())
579 if (IsBackEdge(BR->getParent(), B, DT))
580 return false;
581 return true;
582}
583
584void ModuleSanitizerCoverage::instrumentFunction(Function &F) {
585 if (F.empty())
586 return;
587 if (F.getName().contains(".module_ctor"))
588 return; // Should not instrument sanitizer init functions.
589 if (F.getName().starts_with("__sanitizer_"))
590 return; // Don't instrument __sanitizer_* callbacks.
591 // Don't touch available_externally functions, their actual body is elewhere.
592 if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage)
593 return;
594 // Don't instrument MSVC CRT configuration helpers. They may run before normal
595 // initialization.
596 if (F.getName() == "__local_stdio_printf_options" ||
597 F.getName() == "__local_stdio_scanf_options")
598 return;
599 if (isa<UnreachableInst>(F.getEntryBlock().getTerminator()))
600 return;
601 // Don't instrument functions using SEH for now. Splitting basic blocks like
602 // we do for coverage breaks WinEHPrepare.
603 // FIXME: Remove this when SEH no longer uses landingpad pattern matching.
604 if (F.hasPersonalityFn() &&
606 return;
607 if (Allowlist && !Allowlist->inSection("coverage", "fun", F.getName()))
608 return;
609 if (Blocklist && Blocklist->inSection("coverage", "fun", F.getName()))
610 return;
611 // Do not apply any instrumentation for naked functions.
612 if (F.hasFnAttribute(Attribute::Naked))
613 return;
614 if (F.hasFnAttribute(Attribute::NoSanitizeCoverage))
615 return;
616 if (F.hasFnAttribute(Attribute::DisableSanitizerInstrumentation))
617 return;
618 if (Options.CoverageType >= SanitizerCoverageOptions::SCK_Edge) {
620 F, CriticalEdgeSplittingOptions().setIgnoreUnreachableDests());
621 }
623 SmallVector<BasicBlock *, 16> BlocksToInstrument;
624 SmallVector<Instruction *, 8> CmpTraceTargets;
625 SmallVector<Instruction *, 8> SwitchTraceTargets;
626 SmallVector<BinaryOperator *, 8> DivTraceTargets;
630
631 const DominatorTree &DT = DTCallback(F);
632 const PostDominatorTree &PDT = PDTCallback(F);
633 bool IsLeafFunc = true;
634
635 for (auto &BB : F) {
636 if (shouldInstrumentBlock(F, &BB, DT, PDT, Options))
637 BlocksToInstrument.push_back(&BB);
638 for (auto &Inst : BB) {
639 if (Options.IndirectCalls) {
640 CallBase *CB = dyn_cast<CallBase>(&Inst);
641 if (CB && CB->isIndirectCall())
642 IndirCalls.push_back(&Inst);
643 }
644 if (Options.TraceCmp) {
645 if (ICmpInst *CMP = dyn_cast<ICmpInst>(&Inst))
646 if (IsInterestingCmp(CMP, DT, Options))
647 CmpTraceTargets.push_back(&Inst);
648 if (isa<SwitchInst>(&Inst))
649 SwitchTraceTargets.push_back(&Inst);
650 }
651 if (Options.TraceDiv)
653 if (BO->getOpcode() == Instruction::SDiv ||
654 BO->getOpcode() == Instruction::UDiv)
655 DivTraceTargets.push_back(BO);
656 if (Options.TraceGep)
658 GepTraceTargets.push_back(GEP);
659 if (Options.TraceLoads)
660 if (LoadInst *LI = dyn_cast<LoadInst>(&Inst))
661 Loads.push_back(LI);
662 if (Options.TraceStores)
663 if (StoreInst *SI = dyn_cast<StoreInst>(&Inst))
664 Stores.push_back(SI);
665 if (Options.StackDepth)
666 if (isa<InvokeInst>(Inst) ||
667 (isa<CallInst>(Inst) && !isa<IntrinsicInst>(Inst)))
668 IsLeafFunc = false;
669 }
670 }
671
672 if (Options.CollectControlFlow)
673 createFunctionControlFlow(F);
674
675 Value *FunctionGateCmp = nullptr;
676 InjectCoverage(F, BlocksToInstrument, FunctionGateCmp, IsLeafFunc);
677 InjectCoverageForIndirectCalls(F, IndirCalls);
678 InjectTraceForCmp(F, CmpTraceTargets, FunctionGateCmp);
679 InjectTraceForSwitch(F, SwitchTraceTargets, FunctionGateCmp);
680 InjectTraceForDiv(F, DivTraceTargets);
681 InjectTraceForGep(F, GepTraceTargets);
682 InjectTraceForLoadsAndStores(F, Loads, Stores);
683
684 if (Options.TracePCEntryExit)
685 InjectTraceForExits(F);
686}
687
688GlobalVariable *ModuleSanitizerCoverage::CreateFunctionLocalArrayInSection(
689 size_t NumElements, Function &F, Type *Ty, const char *Section) {
690 ArrayType *ArrayTy = ArrayType::get(Ty, NumElements);
691 auto Array = new GlobalVariable(
692 *CurModule, ArrayTy, false, GlobalVariable::PrivateLinkage,
693 Constant::getNullValue(ArrayTy), "__sancov_gen_");
694
695 // sancov_pcs parallels the other arrays, so they must be retained or
696 // discarded together. Put them in F's comdat to tie them to F for linker GC
697 // benefit. Outside ELF (nodeduplicate), a new comdat for an interposable F
698 // could prevail over a strong definition (COFF: the weak external becomes a
699 // COMDAT definition; Wasm: comdats deduplicate first). noipa doesn't affect
700 // symbol resolution.
701 if (TargetTriple.supportsCOMDAT() &&
702 (F.hasComdat() || TargetTriple.isOSBinFormatELF() ||
703 !F.isInterposable(/*CheckNoIPA=*/false)))
704 if (auto Comdat = getOrCreateFunctionComdat(F, TargetTriple))
705 Array->setComdat(Comdat);
706 Array->setSection(getSectionName(Section));
707 Array->setAlignment(Align(DL->getTypeStoreSize(Ty).getFixedValue()));
708
709 // Optimizers (e.g. GlobalOpt/ConstantMerge) may not discard the arrays as a
710 // unit, so retain them in the compiler; without a comdat, in the linker too.
711 if (Array->hasComdat())
712 GlobalsToAppendToCompilerUsed.push_back(Array);
713 else
714 GlobalsToAppendToUsed.push_back(Array);
715
716 return Array;
717}
718
720ModuleSanitizerCoverage::CreatePCArray(Function &F,
721 ArrayRef<BasicBlock *> AllBlocks) {
722 size_t N = AllBlocks.size();
723 assert(N);
725 IRBuilder<> IRB(&*F.getEntryBlock().getFirstInsertionPt());
726 for (size_t i = 0; i < N; i++) {
727 if (&F.getEntryBlock() == AllBlocks[i]) {
728 PCs.push_back((Constant *)IRB.CreatePointerCast(&F, PtrTy));
729 PCs.push_back(
730 (Constant *)IRB.CreateIntToPtr(ConstantInt::get(IntptrTy, 1), PtrTy));
731 } else {
732 PCs.push_back((Constant *)IRB.CreatePointerCast(
733 BlockAddress::get(AllBlocks[i]), PtrTy));
735 }
736 }
737 auto *PCArray =
738 CreateFunctionLocalArrayInSection(N * 2, F, PtrTy, SanCovPCsSectionName);
739 PCArray->setInitializer(
740 ConstantArray::get(ArrayType::get(PtrTy, N * 2), PCs));
741 PCArray->setConstant(true);
742
743 return PCArray;
744}
745
746void ModuleSanitizerCoverage::CreateFunctionLocalArrays(
747 Function &F, ArrayRef<BasicBlock *> AllBlocks) {
748 if (Options.TracePCGuard)
749 FunctionGuardArray = CreateFunctionLocalArrayInSection(
750 AllBlocks.size(), F, Int32Ty, SanCovGuardsSectionName);
751
752 if (Options.Inline8bitCounters)
753 Function8bitCounterArray = CreateFunctionLocalArrayInSection(
754 AllBlocks.size(), F, Int8Ty, SanCovCountersSectionName);
755 if (Options.InlineBoolFlag)
756 FunctionBoolArray = CreateFunctionLocalArrayInSection(
757 AllBlocks.size(), F, Int1Ty, SanCovBoolFlagSectionName);
758
759 if (Options.PCTable)
760 FunctionPCsArray = CreatePCArray(F, AllBlocks);
761}
762
763Value *ModuleSanitizerCoverage::CreateFunctionLocalGateCmp(IRBuilder<> &IRB) {
764 auto Load = IRB.CreateLoad(Int64Ty, SanCovCallbackGate);
765 Load->setNoSanitizeMetadata();
766 auto Cmp = IRB.CreateIsNotNull(Load);
767 Cmp->setName("sancov gate cmp");
768 return Cmp;
769}
770
771Instruction *ModuleSanitizerCoverage::CreateGateBranch(Function &F,
772 Value *&FunctionGateCmp,
773 Instruction *IP) {
774 if (!FunctionGateCmp) {
775 // Create this in the entry block
776 BasicBlock &BB = F.getEntryBlock();
778 IP = PrepareToSplitEntryBlock(BB, IP);
779 IRBuilder<> EntryIRB(&*IP);
780 FunctionGateCmp = CreateFunctionLocalGateCmp(EntryIRB);
781 }
782 // Set the branch weights in order to minimize the price paid when the
783 // gate is turned off, allowing the default enablement of this
784 // instrumentation with as little of a performance cost as possible
785 auto Weights = MDBuilder(*C).createBranchWeights(1, 100000);
786 return SplitBlockAndInsertIfThen(FunctionGateCmp, IP, false, Weights);
787}
788
789bool ModuleSanitizerCoverage::InjectCoverage(Function &F,
790 ArrayRef<BasicBlock *> AllBlocks,
791 Value *&FunctionGateCmp,
792 bool IsLeafFunc) {
793 if (AllBlocks.empty())
794 return false;
795 CreateFunctionLocalArrays(F, AllBlocks);
796 for (size_t i = 0, N = AllBlocks.size(); i < N; i++)
797 InjectCoverageAtBlock(F, *AllBlocks[i], i, FunctionGateCmp, IsLeafFunc);
798
799 return true;
800}
801
802// On every indirect call we call a run-time function
803// __sanitizer_cov_indir_call* with two parameters:
804// - callee address,
805// - global cache array that contains CacheSize pointers (zero-initialized).
806// The cache is used to speed up recording the caller-callee pairs.
807// The address of the caller is passed implicitly via caller PC.
808// CacheSize is encoded in the name of the run-time function.
809void ModuleSanitizerCoverage::InjectCoverageForIndirectCalls(
810 Function &F, ArrayRef<Instruction *> IndirCalls) {
811 if (IndirCalls.empty())
812 return;
813 assert(Options.TracePC || Options.TracePCEntryExit || Options.TracePCGuard ||
814 Options.Inline8bitCounters || Options.InlineBoolFlag);
815 for (auto *I : IndirCalls) {
817 CallBase &CB = cast<CallBase>(*I);
819 if (isa<InlineAsm>(Callee))
820 continue;
821 IRB.CreateCall(SanCovTracePCIndir, IRB.CreatePointerCast(Callee, IntptrTy));
822 }
823}
824
825// For every switch statement we insert a call:
826// __sanitizer_cov_trace_switch(CondValue,
827// {NumCases, ValueSizeInBits, Case0Value, Case1Value, Case2Value, ... })
828
829void ModuleSanitizerCoverage::InjectTraceForSwitch(
830 Function &F, ArrayRef<Instruction *> SwitchTraceTargets,
831 Value *&FunctionGateCmp) {
832 for (auto *I : SwitchTraceTargets) {
835 SmallVector<Constant *, 16> Initializers;
836 Value *Cond = SI->getCondition();
837 if (Cond->getType()->getScalarSizeInBits() >
838 Int64Ty->getScalarSizeInBits())
839 continue;
840 Initializers.push_back(ConstantInt::get(Int64Ty, SI->getNumCases()));
841 Initializers.push_back(
842 ConstantInt::get(Int64Ty, Cond->getType()->getScalarSizeInBits()));
843 if (Cond->getType()->getScalarSizeInBits() <
844 Int64Ty->getScalarSizeInBits())
845 Cond = IRB.CreateIntCast(Cond, Int64Ty, false);
846 for (auto It : SI->cases()) {
847 ConstantInt *C = It.getCaseValue();
848 if (C->getType()->getScalarSizeInBits() < 64)
849 C = ConstantInt::get(C->getContext(), C->getValue().zext(64));
850 Initializers.push_back(C);
851 }
852 llvm::sort(drop_begin(Initializers, 2),
853 [](const Constant *A, const Constant *B) {
854 return cast<ConstantInt>(A)->getLimitedValue() <
855 cast<ConstantInt>(B)->getLimitedValue();
856 });
857 ArrayType *ArrayOfInt64Ty = ArrayType::get(Int64Ty, Initializers.size());
859 *CurModule, ArrayOfInt64Ty, false, GlobalVariable::InternalLinkage,
860 ConstantArray::get(ArrayOfInt64Ty, Initializers),
861 "__sancov_gen_cov_switch_values");
862 if (Options.GatedCallbacks) {
863 auto GateBranch = CreateGateBranch(F, FunctionGateCmp, I);
864 IRBuilder<> GateIRB(GateBranch);
865 GateIRB.CreateCall(SanCovTraceSwitchFunction, {Cond, GV});
866 } else {
867 IRB.CreateCall(SanCovTraceSwitchFunction, {Cond, GV});
868 }
869 }
870 }
871}
872
873void ModuleSanitizerCoverage::InjectTraceForDiv(
874 Function &, ArrayRef<BinaryOperator *> DivTraceTargets) {
875 for (auto *BO : DivTraceTargets) {
877 Value *A1 = BO->getOperand(1);
878 if (isa<ConstantInt>(A1))
879 continue;
880 if (!A1->getType()->isIntegerTy())
881 continue;
882 uint64_t TypeSize = DL->getTypeStoreSizeInBits(A1->getType());
883 int CallbackIdx = TypeSize == 32 ? 0 : TypeSize == 64 ? 1 : -1;
884 if (CallbackIdx < 0)
885 continue;
886 auto Ty = Type::getIntNTy(*C, TypeSize);
887 IRB.CreateCall(SanCovTraceDivFunction[CallbackIdx],
888 {IRB.CreateIntCast(A1, Ty, true)});
889 }
890}
891
892void ModuleSanitizerCoverage::InjectTraceForGep(
893 Function &, ArrayRef<GetElementPtrInst *> GepTraceTargets) {
894 for (auto *GEP : GepTraceTargets) {
896 for (Use &Idx : GEP->indices())
897 if (!isa<ConstantInt>(Idx) && Idx->getType()->isIntegerTy())
898 IRB.CreateCall(SanCovTraceGepFunction,
899 {IRB.CreateIntCast(Idx, IntptrTy, true)});
900 }
901}
902
903void ModuleSanitizerCoverage::InjectTraceForLoadsAndStores(
905 auto CallbackIdx = [&](Type *ElementTy) -> int {
906 uint64_t TypeSize = DL->getTypeStoreSizeInBits(ElementTy);
907 return TypeSize == 8 ? 0
908 : TypeSize == 16 ? 1
909 : TypeSize == 32 ? 2
910 : TypeSize == 64 ? 3
911 : TypeSize == 128 ? 4
912 : -1;
913 };
914 for (auto *LI : Loads) {
916 auto Ptr = LI->getPointerOperand();
917 int Idx = CallbackIdx(LI->getType());
918 if (Idx < 0)
919 continue;
920 IRB.CreateCall(SanCovLoadFunction[Idx], Ptr);
921 }
922 for (auto *SI : Stores) {
924 auto Ptr = SI->getPointerOperand();
925 int Idx = CallbackIdx(SI->getValueOperand()->getType());
926 if (Idx < 0)
927 continue;
928 IRB.CreateCall(SanCovStoreFunction[Idx], Ptr);
929 }
930}
931
932void ModuleSanitizerCoverage::InjectTraceForExits(Function &F) {
933 EscapeEnumerator EE(F, "sancov_exit");
934 while (IRBuilder<> *AtExit = EE.Next()) {
936 AtExit->CreateCall(SanCovTracePCExit, {})
937 ->setTailCallKind(CallInst::TCK_NoTail);
938 }
939}
940
941void ModuleSanitizerCoverage::InjectTraceForCmp(
942 Function &F, ArrayRef<Instruction *> CmpTraceTargets,
943 Value *&FunctionGateCmp) {
944 for (auto *I : CmpTraceTargets) {
945 if (ICmpInst *ICMP = dyn_cast<ICmpInst>(I)) {
946 InstrumentationIRBuilder IRB(ICMP);
947 Value *A0 = ICMP->getOperand(0);
948 Value *A1 = ICMP->getOperand(1);
949 if (!A0->getType()->isIntegerTy())
950 continue;
951 uint64_t TypeSize = DL->getTypeStoreSizeInBits(A0->getType());
952 int CallbackIdx = TypeSize == 8 ? 0
953 : TypeSize == 16 ? 1
954 : TypeSize == 32 ? 2
955 : TypeSize == 64 ? 3
956 : -1;
957 if (CallbackIdx < 0)
958 continue;
959 // __sanitizer_cov_trace_cmp((type_size << 32) | predicate, A0, A1);
960 auto CallbackFunc = SanCovTraceCmpFunction[CallbackIdx];
961 bool FirstIsConst = isa<ConstantInt>(A0);
962 bool SecondIsConst = isa<ConstantInt>(A1);
963 // If both are const, then we don't need such a comparison.
964 if (FirstIsConst && SecondIsConst)
965 continue;
966 // If only one is const, then make it the first callback argument.
967 if (FirstIsConst || SecondIsConst) {
968 CallbackFunc = SanCovTraceConstCmpFunction[CallbackIdx];
969 if (SecondIsConst)
970 std::swap(A0, A1);
971 }
972
973 auto Ty = Type::getIntNTy(*C, TypeSize);
974 if (Options.GatedCallbacks) {
975 auto GateBranch = CreateGateBranch(F, FunctionGateCmp, I);
976 IRBuilder<> GateIRB(GateBranch);
977 GateIRB.CreateCall(CallbackFunc, {GateIRB.CreateIntCast(A0, Ty, true),
978 GateIRB.CreateIntCast(A1, Ty, true)});
979 } else {
980 IRB.CreateCall(CallbackFunc, {IRB.CreateIntCast(A0, Ty, true),
981 IRB.CreateIntCast(A1, Ty, true)});
982 }
983 }
984 }
985}
986
987void ModuleSanitizerCoverage::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
988 size_t Idx,
989 Value *&FunctionGateCmp,
990 bool IsLeafFunc) {
992 bool IsEntryBB = &BB == &F.getEntryBlock();
993 DebugLoc EntryLoc;
994 if (IsEntryBB) {
995 if (auto SP = F.getSubprogram())
996 EntryLoc = DILocation::get(SP->getContext(), SP->getScopeLine(), 0, SP);
997 // Keep static allocas and llvm.localescape calls in the entry block. Even
998 // if we aren't splitting the block, it's nice for allocas to be before
999 // calls.
1000 IP = PrepareToSplitEntryBlock(BB, IP);
1001 }
1002
1003 InstrumentationIRBuilder IRB(&*IP);
1004 if (EntryLoc)
1005 IRB.SetCurrentDebugLocation(EntryLoc);
1006 if (Options.TracePC || (IsEntryBB && Options.TracePCEntryExit)) {
1007 FunctionCallee Callee = IsEntryBB && Options.TracePCEntryExit
1008 ? SanCovTracePCEntry
1009 : SanCovTracePC;
1010 IRB.CreateCall(Callee)
1011 ->setCannotMerge(); // gets the PC using GET_CALLER_PC.
1012 }
1013 if (Options.TracePCGuard) {
1014 auto GuardPtr = IRB.CreateConstInBoundsGEP2_64(
1015 FunctionGuardArray->getValueType(), FunctionGuardArray, 0, Idx);
1016 if (Options.GatedCallbacks) {
1017 Instruction *I = &*IP;
1018 auto GateBranch = CreateGateBranch(F, FunctionGateCmp, I);
1019 IRBuilder<> GateIRB(GateBranch);
1020 GateIRB.CreateCall(SanCovTracePCGuard, GuardPtr)->setCannotMerge();
1021 } else {
1022 IRB.CreateCall(SanCovTracePCGuard, GuardPtr)->setCannotMerge();
1023 }
1024 }
1025 if (Options.Inline8bitCounters) {
1026 auto CounterPtr = IRB.CreateGEP(
1027 Function8bitCounterArray->getValueType(), Function8bitCounterArray,
1028 {ConstantInt::get(IntptrTy, 0), ConstantInt::get(IntptrTy, Idx)});
1029 auto Load = IRB.CreateLoad(Int8Ty, CounterPtr);
1030 auto Inc = IRB.CreateAdd(Load, ConstantInt::get(Int8Ty, 1));
1031 auto Store = IRB.CreateStore(Inc, CounterPtr);
1032 Load->setNoSanitizeMetadata();
1033 Store->setNoSanitizeMetadata();
1034 }
1035 if (Options.InlineBoolFlag) {
1036 auto FlagPtr = IRB.CreateGEP(
1037 FunctionBoolArray->getValueType(), FunctionBoolArray,
1038 {ConstantInt::get(IntptrTy, 0), ConstantInt::get(IntptrTy, Idx)});
1039 auto Load = IRB.CreateLoad(Int1Ty, FlagPtr);
1040 auto ThenTerm = SplitBlockAndInsertIfThen(
1041 IRB.CreateIsNull(Load), &*IP, false,
1043 InstrumentationIRBuilder ThenIRB(ThenTerm);
1044 auto Store = ThenIRB.CreateStore(ConstantInt::getTrue(Int1Ty), FlagPtr);
1045 if (EntryLoc)
1046 Store->setDebugLoc(EntryLoc);
1047 Load->setNoSanitizeMetadata();
1048 Store->setNoSanitizeMetadata();
1049 }
1050 if (Options.StackDepth && IsEntryBB && !IsLeafFunc) {
1051 Module *M = F.getParent();
1052 const DataLayout &DL = M->getDataLayout();
1053
1054 if (Options.StackDepthCallbackMin) {
1055 // In callback mode, only add call when stack depth reaches minimum.
1056 int EstimatedStackSize = 0;
1057 // If dynamic alloca found, always add call.
1058 bool HasDynamicAlloc = false;
1059 // Find an insertion point after last "alloca".
1060 llvm::Instruction *InsertBefore = nullptr;
1061
1062 // Examine all allocas in the basic block. since we're too early
1063 // to have results from Intrinsic::frameaddress, we have to manually
1064 // estimate the stack size.
1065 for (auto &I : BB) {
1066 if (auto *AI = dyn_cast<AllocaInst>(&I)) {
1067 // Move potential insertion point past the "alloca".
1068 InsertBefore = AI->getNextNode();
1069
1070 // Make an estimate on the stack usage.
1071 if (auto AllocaSize = AI->getAllocationSize(DL)) {
1072 if (AllocaSize->isFixed())
1073 EstimatedStackSize += AllocaSize->getFixedValue();
1074 else
1075 HasDynamicAlloc = true;
1076 } else {
1077 HasDynamicAlloc = true;
1078 }
1079 }
1080 }
1081
1082 if (HasDynamicAlloc ||
1083 EstimatedStackSize >= Options.StackDepthCallbackMin) {
1084 if (InsertBefore)
1085 IRB.SetInsertPoint(InsertBefore);
1086 auto Call = IRB.CreateCall(SanCovStackDepthCallback);
1087 if (EntryLoc)
1088 Call->setDebugLoc(EntryLoc);
1090 }
1091 } else {
1092 // Check stack depth. If it's the deepest so far, record it.
1093 auto FrameAddrPtr = IRB.CreateIntrinsic(
1094 Intrinsic::frameaddress, IRB.getPtrTy(DL.getAllocaAddrSpace()),
1095 {Constant::getNullValue(Int32Ty)});
1096 auto FrameAddrInt = IRB.CreatePtrToInt(FrameAddrPtr, IntptrTy);
1097 auto LowestStack = IRB.CreateLoad(IntptrTy, SanCovLowestStack);
1098 auto IsStackLower = IRB.CreateICmpULT(FrameAddrInt, LowestStack);
1099 auto ThenTerm = SplitBlockAndInsertIfThen(
1100 IsStackLower, &*IP, false,
1102 InstrumentationIRBuilder ThenIRB(ThenTerm);
1103 auto Store = ThenIRB.CreateStore(FrameAddrInt, SanCovLowestStack);
1104 if (EntryLoc)
1105 Store->setDebugLoc(EntryLoc);
1106 LowestStack->setNoSanitizeMetadata();
1107 Store->setNoSanitizeMetadata();
1108 }
1109 }
1110}
1111
1112std::string
1113ModuleSanitizerCoverage::getSectionName(const std::string &Section) const {
1114 if (TargetTriple.isOSBinFormatCOFF()) {
1115 if (Section == SanCovCountersSectionName)
1116 return ".SCOV$CM";
1117 if (Section == SanCovBoolFlagSectionName)
1118 return ".SCOV$BM";
1119 if (Section == SanCovPCsSectionName)
1120 return ".SCOVP$M";
1121 return ".SCOV$GM"; // For SanCovGuardsSectionName.
1122 }
1123 if (TargetTriple.isOSBinFormatMachO())
1124 return "__DATA,__" + Section;
1125 return "__" + Section;
1126}
1127
1128std::string
1129ModuleSanitizerCoverage::getSectionStart(const std::string &Section) const {
1130 if (TargetTriple.isOSBinFormatMachO())
1131 return "\1section$start$__DATA$__" + Section;
1132 return "__start___" + Section;
1133}
1134
1135std::string
1136ModuleSanitizerCoverage::getSectionEnd(const std::string &Section) const {
1137 if (TargetTriple.isOSBinFormatMachO())
1138 return "\1section$end$__DATA$__" + Section;
1139 return "__stop___" + Section;
1140}
1141
1142void ModuleSanitizerCoverage::createFunctionControlFlow(Function &F) {
1144 IRBuilder<> IRB(&*F.getEntryBlock().getFirstInsertionPt());
1145
1146 for (auto &BB : F) {
1147 // blockaddress can not be used on function's entry block.
1148 if (&BB == &F.getEntryBlock())
1149 CFs.push_back((Constant *)IRB.CreatePointerCast(&F, PtrTy));
1150 else
1151 CFs.push_back(
1152 (Constant *)IRB.CreatePointerCast(BlockAddress::get(&BB), PtrTy));
1153
1154 for (auto SuccBB : successors(&BB)) {
1155 assert(SuccBB != &F.getEntryBlock());
1156 CFs.push_back(
1157 (Constant *)IRB.CreatePointerCast(BlockAddress::get(SuccBB), PtrTy));
1158 }
1159
1161
1162 for (auto &Inst : BB) {
1163 if (CallBase *CB = dyn_cast<CallBase>(&Inst)) {
1164 if (CB->isIndirectCall()) {
1165 // TODO(navidem): handle indirect calls, for now mark its existence.
1167 ConstantInt::getAllOnesValue(IntptrTy), PtrTy));
1168 } else {
1169 auto CalledF = CB->getCalledFunction();
1170 if (CalledF && !CalledF->isIntrinsic())
1171 CFs.push_back((Constant *)IRB.CreatePointerCast(CalledF, PtrTy));
1172 }
1173 }
1174 }
1175
1177 }
1178
1179 FunctionCFsArray = CreateFunctionLocalArrayInSection(CFs.size(), F, PtrTy,
1181 FunctionCFsArray->setInitializer(
1182 ConstantArray::get(ArrayType::get(PtrTy, CFs.size()), CFs));
1183 FunctionCFsArray->setConstant(true);
1184}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This is the interface for a simple mod/ref and alias analysis over globals.
Hexagon Common GEP
Module.h This file contains the declarations for the Module class.
static LVOptions Options
Definition LVOptions.cpp:25
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
static cl::opt< bool > SplitAllCriticalEdges("phi-elim-split-all-critical-edges", cl::init(false), cl::Hidden, cl::desc("Split all critical edges during " "PHI elimination"))
FunctionAnalysisManager FAM
ModuleAnalysisManager MAM
if(PassOpts->AAPipeline)
const SmallVectorImpl< MachineOperand > & Cond
const char SanCovCFsSectionName[]
static bool isFullPostDominator(const BasicBlock *BB, const PostDominatorTree &PDT)
const char SanCovTraceConstCmp4[]
const char SanCovBoolFlagSectionName[]
const char SanCov8bitCountersInitName[]
const char SanCovTracePCEntryName[]
const char SanCovLoad8[]
const char SanCovTraceSwitchName[]
const char SanCovTraceCmp1[]
const char SanCovModuleCtorTracePcGuardName[]
const char SanCovCountersSectionName[]
const char SanCovPCsInitName[]
const char SanCovTracePCGuardName[]
const char SanCovModuleCtor8bitCountersName[]
const char SanCovTracePCGuardInitName[]
const char SanCovTraceDiv4[]
static const uint64_t SanCtorAndDtorPriority
const char SanCovBoolFlagInitName[]
const char SanCovTraceGep[]
const char SanCovLoad16[]
const char SanCovTraceConstCmp8[]
const char SanCovGuardsSectionName[]
const char SanCovStore1[]
const char SanCovTraceConstCmp2[]
const char SanCovTraceConstCmp1[]
static bool IsBackEdge(BasicBlock *From, BasicBlock *To, const DominatorTree &DT)
const char SanCovCallbackGateName[]
const char SanCovTraceDiv8[]
const char SanCovLoad4[]
const char SanCovStackDepthCallbackName[]
const char SanCovCFsInitName[]
const char SanCovStore2[]
const char SanCovPCsSectionName[]
const char SanCovLoad1[]
const char SanCovTracePCExitName[]
static bool isFullDominator(const BasicBlock *BB, const DominatorTree &DT)
const char SanCovTraceCmp8[]
const char SanCovCallbackGateSectionName[]
const char SanCovStore16[]
static bool IsInterestingCmp(ICmpInst *CMP, const DominatorTree &DT, const SanitizerCoverageOptions &Options)
static bool shouldInstrumentBlock(const Function &F, const BasicBlock *BB, const DominatorTree &DT, const PostDominatorTree &PDT, const SanitizerCoverageOptions &Options)
const char SanCovModuleCtorBoolFlagName[]
const char SanCovTraceCmp2[]
const char SanCovStore8[]
const char SanCovTracePCName[]
const char SanCovStore4[]
const char SanCovLoad2[]
const char SanCovTraceCmp4[]
const char SanCovLowestStackName[]
const char SanCovTracePCIndirName[]
This file defines the SmallVector class.
Defines the virtual file system interface vfs::FileSystem.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
Class to represent array types.
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:459
LLVM_ABI const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
LLVM_ABI const BasicBlock * getUniqueSuccessor() const
Return the successor of this block if it has a unique successor.
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
LLVM_ABI InstListType::const_iterator getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp=true) const
Returns a pointer to the first instruction in this block that is not a PHINode, a debug intrinsic,...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
static LLVM_ABI BlockAddress * get(Function *F, BasicBlock *BB)
Return a BlockAddress for the specified function and basic block.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
void setCannotMerge()
Value * getCalledOperand() const
static LLVM_ABI Constant * get(ArrayType *T, ArrayRef< Constant * > V)
static Constant * getPtrAdd(Constant *Ptr, Constant *Offset, GEPNoWrapFlags NW=GEPNoWrapFlags::none(), std::optional< ConstantRange > InRange=std::nullopt, Type *OnlyIfReduced=nullptr)
Create a getelementptr i8, ptr, offset constant expression.
Definition Constants.h:1518
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
A debug info location.
Definition DebugLoc.h:126
Analysis pass which computes a DominatorTree.
Definition Dominators.h:241
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
EscapeEnumerator - This is a little algorithm to find all escape points from a function so that "fina...
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
const BasicBlock & getEntryBlock() const
Definition Function.h:794
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
LLVM_ABI void setComdat(Comdat *C)
Definition Globals.cpp:287
void setLinkage(LinkageTypes LT)
@ HiddenVisibility
The GV is hidden.
Definition GlobalValue.h:69
void setVisibility(VisibilityTypes V)
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition GlobalValue.h:52
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition GlobalValue.h:61
@ InternalLinkage
Rename collisions when linking (static functions).
Definition GlobalValue.h:60
@ LinkOnceAnyLinkage
Keep one copy of function when linking (inline)
Definition GlobalValue.h:55
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition GlobalValue.h:58
@ ExternalLinkage
Externally visible function.
Definition GlobalValue.h:53
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition GlobalValue.h:54
@ ExternalWeakLinkage
ExternalWeak linkage description.
Definition GlobalValue.h:62
Analysis pass providing a never-invalidated alias analysis result.
This instruction compares its operands according to the predicate given to the constructor.
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:2407
Value * CreatePointerCast(Value *V, Type *DestTy, const Twine &Name="")
Definition IRBuilder.h:2306
Value * CreateIntToPtr(Value *V, Type *DestTy, const Twine &Name="")
Definition IRBuilder.h:2247
void SetCurrentDebugLocation(const DebugLoc &L)
Set location information used by debugging information.
Definition IRBuilder.h:238
Value * CreateGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="", GEPNoWrapFlags NW=GEPNoWrapFlags::none())
Definition IRBuilder.h:2020
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
Definition IRBuilder.h:1916
LLVMContext & getContext() const
Definition IRBuilder.h:181
LLVM_ABI Value * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > OverloadTypes, ArrayRef< Value * > Args, FMFSource FMFSource={}, const Twine &Name="", ArrayRef< OperandBundleDef > OpBundles={}, function_ref< void(CallInst *)> SetFn=[](CallInst *) {})
Variant to create a possibly constant-folded intrinsic.
Value * CreateConstInBoundsGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, uint64_t Idx1, const Twine &Name="")
Definition IRBuilder.h:2085
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition IRBuilder.h:1934
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition IRBuilder.h:1427
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
Definition IRBuilder.h:2242
Value * CreateIsNotNull(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg != 0.
Definition IRBuilder.h:2772
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args={}, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition IRBuilder.h:2570
PointerType * getPtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer.
Definition IRBuilder.h:574
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition IRBuilder.h:2332
Value * CreateIsNull(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg == 0.
Definition IRBuilder.h:2767
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition IRBuilder.h:199
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2918
Instruction * user_back()
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
A smart pointer to a reference-counted object that inherits from RefCountedBase or ThreadSafeRefCount...
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
An instruction for reading from memory.
LLVM_ABI MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight, bool IsExpected=false)
Return metadata containing two branch weights.
Definition MDBuilder.cpp:38
LLVM_ABI MDNode * createUnlikelyBranchWeights()
Return metadata containing two branch weights, with significant bias towards false destination.
Definition MDBuilder.cpp:48
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1579
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
static PointerType * getUnqual(LLVMContext &C)
This constructs an opaque pointer to an object in the default address space (address space zero).
Analysis pass which computes a PostDominatorTree.
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
LLVM_ABI bool dominates(const Instruction *I1, const Instruction *I2) const
Return true if I1 dominates I2.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & abandon()
Mark an analysis as abandoned.
Definition Analysis.h:171
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
LLVM_ABI SanitizerCoveragePass(SanitizerCoverageOptions Options=SanitizerCoverageOptions(), IntrusiveRefCntPtr< vfs::FileSystem > VFS=nullptr, const std::vector< std::string > &AllowlistFiles={}, const std::vector< std::string > &BlocklistFiles={})
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This is a utility class used to parse user-provided text files with "special case lists" for code san...
static LLVM_ABI std::unique_ptr< SpecialCaseList > createOrDie(const std::vector< std::string > &Paths, llvm::vfs::FileSystem &FS)
Parses the special case list entries from files.
An instruction for storing to memory.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Multiway switch.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:272
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:252
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:303
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:441
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
An efficient, type-erasing, non-owning reference to a callable.
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition ilist_node.h:348
CallInst * Call
static constexpr const StringLiteral & getSectionName(DebugSectionKind SectionKind)
Return the name of the section.
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1755
bool succ_empty(const Instruction *I)
Definition CFG.h:141
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
auto successors(const MachineBasicBlock *BB)
@ Load
The value being inserted comes from a load (InsertElement only).
@ Store
The extracted value is stored (ExtractElement only).
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
LLVM_ABI FunctionCallee declareSanitizerInitFunction(Module &M, StringRef InitName, ArrayRef< Type * > InitArgTypes, bool Weak=false)
LLVM_ABI std::pair< Function *, FunctionCallee > createSanitizerCtorAndInitFunctions(Module &M, StringRef CtorName, StringRef InitName, ArrayRef< Type * > InitArgTypes, ArrayRef< Value * > InitArgs, StringRef VersionCheckName=StringRef(), bool Weak=false)
Creates sanitizer constructor function, and calls sanitizer's init function from it.
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1652
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
LLVM_ABI Comdat * getOrCreateFunctionComdat(Function &F, Triple &T)
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.compiler.used list.
bool isAsynchronousEHPersonality(EHPersonality Pers)
Returns true if this personality function catches asynchronous exceptions.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1933
LLVM_ABI void appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Append F to the list of global ctors of module M with the given Priority.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto predecessors(const MachineBasicBlock *BB)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
Definition InstrProf.h:147
bool pred_empty(const BasicBlock *BB)
Definition CFG.h:107
LLVM_ABI BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB, BasicBlock::iterator IP)
Instrumentation passes often insert conditional checks into entry blocks.
LLVM_ABI Instruction * SplitBlockAndInsertIfThen(Value *Cond, BasicBlock::iterator SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
LLVM_ABI void appendToUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.used list.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:880
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Option class for critical edge splitting.
static void ensureDebugInfo(IRBuilder<> &IRB, const Function &F)
enum llvm::SanitizerCoverageOptions::Type CoverageType