1 //===-- DataFlowSanitizer.cpp - dynamic data flow analysis ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This file is a part of DataFlowSanitizer, a generalised dynamic data flow
13 /// Unlike other Sanitizer tools, this tool is not designed to detect a specific
14 /// class of bugs on its own. Instead, it provides a generic dynamic data flow
15 /// analysis framework to be used by clients to help detect application-specific
16 /// issues within their own code.
18 /// The analysis is based on automatic propagation of data flow labels (also
19 /// known as taint labels) through a program as it performs computation. Each
20 /// byte of application memory is backed by two bytes of shadow memory which
21 /// hold the label. On Linux/x86_64, memory is laid out as follows:
23 /// +--------------------+ 0x800000000000 (top of memory)
24 /// | application memory |
25 /// +--------------------+ 0x700000008000 (kAppAddr)
29 /// +--------------------+ 0x200200000000 (kUnusedAddr)
31 /// +--------------------+ 0x200000000000 (kUnionTableAddr)
33 /// +--------------------+ 0x000000010000 (kShadowAddr)
34 /// | reserved by kernel |
35 /// +--------------------+ 0x000000000000
37 /// To derive a shadow memory address from an application memory address,
38 /// bits 44-46 are cleared to bring the address into the range
39 /// [0x000000008000,0x100000000000). Then the address is shifted left by 1 to
40 /// account for the double byte representation of shadow labels and move the
41 /// address into the shadow memory range. See the function
42 /// DataFlowSanitizer::getShadowAddress below.
44 /// For more information, please refer to the design document:
45 /// http://clang.llvm.org/docs/DataFlowSanitizerDesign.html
47 #include "llvm/Transforms/Instrumentation.h"
48 #include "llvm/ADT/DenseMap.h"
49 #include "llvm/ADT/DenseSet.h"
50 #include "llvm/ADT/DepthFirstIterator.h"
51 #include "llvm/ADT/StringExtras.h"
52 #include "llvm/ADT/Triple.h"
53 #include "llvm/Analysis/ValueTracking.h"
54 #include "llvm/IR/Dominators.h"
55 #include "llvm/IR/DebugInfo.h"
56 #include "llvm/IR/IRBuilder.h"
57 #include "llvm/IR/InlineAsm.h"
58 #include "llvm/IR/InstVisitor.h"
59 #include "llvm/IR/LLVMContext.h"
60 #include "llvm/IR/MDBuilder.h"
61 #include "llvm/IR/Type.h"
62 #include "llvm/IR/Value.h"
63 #include "llvm/Pass.h"
64 #include "llvm/Support/CommandLine.h"
65 #include "llvm/Support/SpecialCaseList.h"
66 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
67 #include "llvm/Transforms/Utils/Local.h"
75 // The -dfsan-preserve-alignment flag controls whether this pass assumes that
76 // alignment requirements provided by the input IR are correct. For example,
77 // if the input IR contains a load with alignment 8, this flag will cause
78 // the shadow load to have alignment 16. This flag is disabled by default as
79 // we have unfortunately encountered too much code (including Clang itself;
80 // see PR14291) which performs misaligned access.
81 static cl::opt<bool> ClPreserveAlignment(
82 "dfsan-preserve-alignment",
83 cl::desc("respect alignment requirements provided by input IR"), cl::Hidden,
86 // The ABI list files control how shadow parameters are passed. The pass treats
87 // every function labelled "uninstrumented" in the ABI list file as conforming
88 // to the "native" (i.e. unsanitized) ABI. Unless the ABI list contains
89 // additional annotations for those functions, a call to one of those functions
90 // will produce a warning message, as the labelling behaviour of the function is
91 // unknown. The other supported annotations are "functional" and "discard",
92 // which are described below under DataFlowSanitizer::WrapperKind.
93 static cl::list<std::string> ClABIListFiles(
95 cl::desc("File listing native ABI functions and how the pass treats them"),
98 // Controls whether the pass uses IA_Args or IA_TLS as the ABI for instrumented
99 // functions (see DataFlowSanitizer::InstrumentedABI below).
100 static cl::opt<bool> ClArgsABI(
102 cl::desc("Use the argument ABI rather than the TLS ABI"),
105 // Controls whether the pass includes or ignores the labels of pointers in load
107 static cl::opt<bool> ClCombinePointerLabelsOnLoad(
108 "dfsan-combine-pointer-labels-on-load",
109 cl::desc("Combine the label of the pointer with the label of the data when "
110 "loading from memory."),
111 cl::Hidden, cl::init(true));
113 // Controls whether the pass includes or ignores the labels of pointers in
114 // stores instructions.
115 static cl::opt<bool> ClCombinePointerLabelsOnStore(
116 "dfsan-combine-pointer-labels-on-store",
117 cl::desc("Combine the label of the pointer with the label of the data when "
118 "storing in memory."),
119 cl::Hidden, cl::init(false));
121 static cl::opt<bool> ClDebugNonzeroLabels(
122 "dfsan-debug-nonzero-labels",
123 cl::desc("Insert calls to __dfsan_nonzero_label on observing a parameter, "
124 "load or return with a nonzero label"),
129 StringRef GetGlobalTypeString(const GlobalValue &G) {
130 // Types of GlobalVariables are always pointer types.
131 Type *GType = G.getType()->getElementType();
132 // For now we support blacklisting struct types only.
133 if (StructType *SGType = dyn_cast<StructType>(GType)) {
134 if (!SGType->isLiteral())
135 return SGType->getName();
137 return "<unknown type>";
141 std::unique_ptr<SpecialCaseList> SCL;
146 void set(std::unique_ptr<SpecialCaseList> List) { SCL = std::move(List); }
148 /// Returns whether either this function or its source file are listed in the
150 bool isIn(const Function &F, StringRef Category) const {
151 return isIn(*F.getParent(), Category) ||
152 SCL->inSection("fun", F.getName(), Category);
155 /// Returns whether this global alias is listed in the given category.
157 /// If GA aliases a function, the alias's name is matched as a function name
158 /// would be. Similarly, aliases of globals are matched like globals.
159 bool isIn(const GlobalAlias &GA, StringRef Category) const {
160 if (isIn(*GA.getParent(), Category))
163 if (isa<FunctionType>(GA.getType()->getElementType()))
164 return SCL->inSection("fun", GA.getName(), Category);
166 return SCL->inSection("global", GA.getName(), Category) ||
167 SCL->inSection("type", GetGlobalTypeString(GA), Category);
170 /// Returns whether this module is listed in the given category.
171 bool isIn(const Module &M, StringRef Category) const {
172 return SCL->inSection("src", M.getModuleIdentifier(), Category);
176 class DataFlowSanitizer : public ModulePass {
177 friend struct DFSanFunction;
178 friend class DFSanVisitor;
184 /// Which ABI should be used for instrumented functions?
185 enum InstrumentedABI {
186 /// Argument and return value labels are passed through additional
187 /// arguments and by modifying the return type.
190 /// Argument and return value labels are passed through TLS variables
191 /// __dfsan_arg_tls and __dfsan_retval_tls.
195 /// How should calls to uninstrumented functions be handled?
197 /// This function is present in an uninstrumented form but we don't know
198 /// how it should be handled. Print a warning and call the function anyway.
199 /// Don't label the return value.
202 /// This function does not write to (user-accessible) memory, and its return
203 /// value is unlabelled.
206 /// This function does not write to (user-accessible) memory, and the label
207 /// of its return value is the union of the label of its arguments.
210 /// Instead of calling the function, a custom wrapper __dfsw_F is called,
211 /// where F is the name of the function. This function may wrap the
212 /// original function or provide its own implementation. This is similar to
213 /// the IA_Args ABI, except that IA_Args uses a struct return type to
214 /// pass the return value shadow in a register, while WK_Custom uses an
215 /// extra pointer argument to return the shadow. This allows the wrapped
216 /// form of the function type to be expressed in C.
222 IntegerType *ShadowTy;
223 PointerType *ShadowPtrTy;
224 IntegerType *IntptrTy;
225 ConstantInt *ZeroShadow;
226 ConstantInt *ShadowPtrMask;
227 ConstantInt *ShadowPtrMul;
230 void *(*GetArgTLSPtr)();
231 void *(*GetRetvalTLSPtr)();
233 Constant *GetRetvalTLS;
234 FunctionType *DFSanUnionFnTy;
235 FunctionType *DFSanUnionLoadFnTy;
236 FunctionType *DFSanUnimplementedFnTy;
237 FunctionType *DFSanSetLabelFnTy;
238 FunctionType *DFSanNonzeroLabelFnTy;
239 FunctionType *DFSanVarargWrapperFnTy;
240 Constant *DFSanUnionFn;
241 Constant *DFSanCheckedUnionFn;
242 Constant *DFSanUnionLoadFn;
243 Constant *DFSanUnimplementedFn;
244 Constant *DFSanSetLabelFn;
245 Constant *DFSanNonzeroLabelFn;
246 Constant *DFSanVarargWrapperFn;
247 MDNode *ColdCallWeights;
248 DFSanABIList ABIList;
249 DenseMap<Value *, Function *> UnwrappedFnMap;
250 AttributeSet ReadOnlyNoneAttrs;
251 DenseMap<const Function *, DISubprogram *> FunctionDIs;
253 Value *getShadowAddress(Value *Addr, Instruction *Pos);
254 bool isInstrumented(const Function *F);
255 bool isInstrumented(const GlobalAlias *GA);
256 FunctionType *getArgsFunctionType(FunctionType *T);
257 FunctionType *getTrampolineFunctionType(FunctionType *T);
258 FunctionType *getCustomFunctionType(FunctionType *T);
259 InstrumentedABI getInstrumentedABI();
260 WrapperKind getWrapperKind(Function *F);
261 void addGlobalNamePrefix(GlobalValue *GV);
262 Function *buildWrapperFunction(Function *F, StringRef NewFName,
263 GlobalValue::LinkageTypes NewFLink,
264 FunctionType *NewFT);
265 Constant *getOrBuildTrampolineFunction(FunctionType *FT, StringRef FName);
269 const std::vector<std::string> &ABIListFiles = std::vector<std::string>(),
270 void *(*getArgTLS)() = nullptr, void *(*getRetValTLS)() = nullptr);
272 bool doInitialization(Module &M) override;
273 bool runOnModule(Module &M) override;
276 struct DFSanFunction {
277 DataFlowSanitizer &DFS;
280 DataFlowSanitizer::InstrumentedABI IA;
284 AllocaInst *LabelReturnAlloca;
285 DenseMap<Value *, Value *> ValShadowMap;
286 DenseMap<AllocaInst *, AllocaInst *> AllocaShadowMap;
287 std::vector<std::pair<PHINode *, PHINode *> > PHIFixups;
288 DenseSet<Instruction *> SkipInsts;
289 std::vector<Value *> NonZeroChecks;
292 struct CachedCombinedShadow {
296 DenseMap<std::pair<Value *, Value *>, CachedCombinedShadow>
297 CachedCombinedShadows;
298 DenseMap<Value *, std::set<Value *>> ShadowElements;
300 DFSanFunction(DataFlowSanitizer &DFS, Function *F, bool IsNativeABI)
301 : DFS(DFS), F(F), IA(DFS.getInstrumentedABI()),
302 IsNativeABI(IsNativeABI), ArgTLSPtr(nullptr), RetvalTLSPtr(nullptr),
303 LabelReturnAlloca(nullptr) {
305 // FIXME: Need to track down the register allocator issue which causes poor
306 // performance in pathological cases with large numbers of basic blocks.
307 AvoidNewBlocks = F->size() > 1000;
309 Value *getArgTLSPtr();
310 Value *getArgTLS(unsigned Index, Instruction *Pos);
311 Value *getRetvalTLS();
312 Value *getShadow(Value *V);
313 void setShadow(Instruction *I, Value *Shadow);
314 Value *combineShadows(Value *V1, Value *V2, Instruction *Pos);
315 Value *combineOperandShadows(Instruction *Inst);
316 Value *loadShadow(Value *ShadowAddr, uint64_t Size, uint64_t Align,
318 void storeShadow(Value *Addr, uint64_t Size, uint64_t Align, Value *Shadow,
322 class DFSanVisitor : public InstVisitor<DFSanVisitor> {
325 DFSanVisitor(DFSanFunction &DFSF) : DFSF(DFSF) {}
327 void visitOperandShadowInst(Instruction &I);
329 void visitBinaryOperator(BinaryOperator &BO);
330 void visitCastInst(CastInst &CI);
331 void visitCmpInst(CmpInst &CI);
332 void visitGetElementPtrInst(GetElementPtrInst &GEPI);
333 void visitLoadInst(LoadInst &LI);
334 void visitStoreInst(StoreInst &SI);
335 void visitReturnInst(ReturnInst &RI);
336 void visitCallSite(CallSite CS);
337 void visitPHINode(PHINode &PN);
338 void visitExtractElementInst(ExtractElementInst &I);
339 void visitInsertElementInst(InsertElementInst &I);
340 void visitShuffleVectorInst(ShuffleVectorInst &I);
341 void visitExtractValueInst(ExtractValueInst &I);
342 void visitInsertValueInst(InsertValueInst &I);
343 void visitAllocaInst(AllocaInst &I);
344 void visitSelectInst(SelectInst &I);
345 void visitMemSetInst(MemSetInst &I);
346 void visitMemTransferInst(MemTransferInst &I);
351 char DataFlowSanitizer::ID;
352 INITIALIZE_PASS(DataFlowSanitizer, "dfsan",
353 "DataFlowSanitizer: dynamic data flow analysis.", false, false)
356 llvm::createDataFlowSanitizerPass(const std::vector<std::string> &ABIListFiles,
357 void *(*getArgTLS)(),
358 void *(*getRetValTLS)()) {
359 return new DataFlowSanitizer(ABIListFiles, getArgTLS, getRetValTLS);
362 DataFlowSanitizer::DataFlowSanitizer(
363 const std::vector<std::string> &ABIListFiles, void *(*getArgTLS)(),
364 void *(*getRetValTLS)())
365 : ModulePass(ID), GetArgTLSPtr(getArgTLS), GetRetvalTLSPtr(getRetValTLS) {
366 std::vector<std::string> AllABIListFiles(std::move(ABIListFiles));
367 AllABIListFiles.insert(AllABIListFiles.end(), ClABIListFiles.begin(),
368 ClABIListFiles.end());
369 ABIList.set(SpecialCaseList::createOrDie(AllABIListFiles));
372 FunctionType *DataFlowSanitizer::getArgsFunctionType(FunctionType *T) {
373 llvm::SmallVector<Type *, 4> ArgTypes(T->param_begin(), T->param_end());
374 ArgTypes.append(T->getNumParams(), ShadowTy);
376 ArgTypes.push_back(ShadowPtrTy);
377 Type *RetType = T->getReturnType();
378 if (!RetType->isVoidTy())
379 RetType = StructType::get(RetType, ShadowTy, (Type *)nullptr);
380 return FunctionType::get(RetType, ArgTypes, T->isVarArg());
383 FunctionType *DataFlowSanitizer::getTrampolineFunctionType(FunctionType *T) {
384 assert(!T->isVarArg());
385 llvm::SmallVector<Type *, 4> ArgTypes;
386 ArgTypes.push_back(T->getPointerTo());
387 ArgTypes.append(T->param_begin(), T->param_end());
388 ArgTypes.append(T->getNumParams(), ShadowTy);
389 Type *RetType = T->getReturnType();
390 if (!RetType->isVoidTy())
391 ArgTypes.push_back(ShadowPtrTy);
392 return FunctionType::get(T->getReturnType(), ArgTypes, false);
395 FunctionType *DataFlowSanitizer::getCustomFunctionType(FunctionType *T) {
396 llvm::SmallVector<Type *, 4> ArgTypes;
397 for (FunctionType::param_iterator i = T->param_begin(), e = T->param_end();
400 if (isa<PointerType>(*i) && (FT = dyn_cast<FunctionType>(cast<PointerType>(
401 *i)->getElementType()))) {
402 ArgTypes.push_back(getTrampolineFunctionType(FT)->getPointerTo());
403 ArgTypes.push_back(Type::getInt8PtrTy(*Ctx));
405 ArgTypes.push_back(*i);
408 for (unsigned i = 0, e = T->getNumParams(); i != e; ++i)
409 ArgTypes.push_back(ShadowTy);
411 ArgTypes.push_back(ShadowPtrTy);
412 Type *RetType = T->getReturnType();
413 if (!RetType->isVoidTy())
414 ArgTypes.push_back(ShadowPtrTy);
415 return FunctionType::get(T->getReturnType(), ArgTypes, T->isVarArg());
418 bool DataFlowSanitizer::doInitialization(Module &M) {
419 llvm::Triple TargetTriple(M.getTargetTriple());
420 bool IsX86_64 = TargetTriple.getArch() == llvm::Triple::x86_64;
421 bool IsMIPS64 = TargetTriple.getArch() == llvm::Triple::mips64 ||
422 TargetTriple.getArch() == llvm::Triple::mips64el;
424 const DataLayout &DL = M.getDataLayout();
427 Ctx = &M.getContext();
428 ShadowTy = IntegerType::get(*Ctx, ShadowWidth);
429 ShadowPtrTy = PointerType::getUnqual(ShadowTy);
430 IntptrTy = DL.getIntPtrType(*Ctx);
431 ZeroShadow = ConstantInt::getSigned(ShadowTy, 0);
432 ShadowPtrMul = ConstantInt::getSigned(IntptrTy, ShadowWidth / 8);
434 ShadowPtrMask = ConstantInt::getSigned(IntptrTy, ~0x700000000000LL);
436 ShadowPtrMask = ConstantInt::getSigned(IntptrTy, ~0xF000000000LL);
438 report_fatal_error("unsupported triple");
440 Type *DFSanUnionArgs[2] = { ShadowTy, ShadowTy };
442 FunctionType::get(ShadowTy, DFSanUnionArgs, /*isVarArg=*/ false);
443 Type *DFSanUnionLoadArgs[2] = { ShadowPtrTy, IntptrTy };
445 FunctionType::get(ShadowTy, DFSanUnionLoadArgs, /*isVarArg=*/ false);
446 DFSanUnimplementedFnTy = FunctionType::get(
447 Type::getVoidTy(*Ctx), Type::getInt8PtrTy(*Ctx), /*isVarArg=*/false);
448 Type *DFSanSetLabelArgs[3] = { ShadowTy, Type::getInt8PtrTy(*Ctx), IntptrTy };
449 DFSanSetLabelFnTy = FunctionType::get(Type::getVoidTy(*Ctx),
450 DFSanSetLabelArgs, /*isVarArg=*/false);
451 DFSanNonzeroLabelFnTy = FunctionType::get(
452 Type::getVoidTy(*Ctx), None, /*isVarArg=*/false);
453 DFSanVarargWrapperFnTy = FunctionType::get(
454 Type::getVoidTy(*Ctx), Type::getInt8PtrTy(*Ctx), /*isVarArg=*/false);
457 Type *ArgTLSTy = ArrayType::get(ShadowTy, 64);
459 GetArgTLS = ConstantExpr::getIntToPtr(
460 ConstantInt::get(IntptrTy, uintptr_t(GetArgTLSPtr)),
461 PointerType::getUnqual(
462 FunctionType::get(PointerType::getUnqual(ArgTLSTy),
465 if (GetRetvalTLSPtr) {
467 GetRetvalTLS = ConstantExpr::getIntToPtr(
468 ConstantInt::get(IntptrTy, uintptr_t(GetRetvalTLSPtr)),
469 PointerType::getUnqual(
470 FunctionType::get(PointerType::getUnqual(ShadowTy),
474 ColdCallWeights = MDBuilder(*Ctx).createBranchWeights(1, 1000);
478 bool DataFlowSanitizer::isInstrumented(const Function *F) {
479 return !ABIList.isIn(*F, "uninstrumented");
482 bool DataFlowSanitizer::isInstrumented(const GlobalAlias *GA) {
483 return !ABIList.isIn(*GA, "uninstrumented");
486 DataFlowSanitizer::InstrumentedABI DataFlowSanitizer::getInstrumentedABI() {
487 return ClArgsABI ? IA_Args : IA_TLS;
490 DataFlowSanitizer::WrapperKind DataFlowSanitizer::getWrapperKind(Function *F) {
491 if (ABIList.isIn(*F, "functional"))
492 return WK_Functional;
493 if (ABIList.isIn(*F, "discard"))
495 if (ABIList.isIn(*F, "custom"))
501 void DataFlowSanitizer::addGlobalNamePrefix(GlobalValue *GV) {
502 std::string GVName = GV->getName(), Prefix = "dfs$";
503 GV->setName(Prefix + GVName);
505 // Try to change the name of the function in module inline asm. We only do
506 // this for specific asm directives, currently only ".symver", to try to avoid
507 // corrupting asm which happens to contain the symbol name as a substring.
508 // Note that the substitution for .symver assumes that the versioned symbol
509 // also has an instrumented name.
510 std::string Asm = GV->getParent()->getModuleInlineAsm();
511 std::string SearchStr = ".symver " + GVName + ",";
512 size_t Pos = Asm.find(SearchStr);
513 if (Pos != std::string::npos) {
514 Asm.replace(Pos, SearchStr.size(),
515 ".symver " + Prefix + GVName + "," + Prefix);
516 GV->getParent()->setModuleInlineAsm(Asm);
521 DataFlowSanitizer::buildWrapperFunction(Function *F, StringRef NewFName,
522 GlobalValue::LinkageTypes NewFLink,
523 FunctionType *NewFT) {
524 FunctionType *FT = F->getFunctionType();
525 Function *NewF = Function::Create(NewFT, NewFLink, NewFName,
527 NewF->copyAttributesFrom(F);
528 NewF->removeAttributes(
529 AttributeSet::ReturnIndex,
530 AttributeSet::get(F->getContext(), AttributeSet::ReturnIndex,
531 AttributeFuncs::typeIncompatible(NewFT->getReturnType())));
533 BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", NewF);
535 NewF->removeAttributes(
536 AttributeSet::FunctionIndex,
537 AttributeSet().addAttribute(*Ctx, AttributeSet::FunctionIndex,
539 CallInst::Create(DFSanVarargWrapperFn,
540 IRBuilder<>(BB).CreateGlobalStringPtr(F->getName()), "",
542 new UnreachableInst(*Ctx, BB);
544 std::vector<Value *> Args;
545 unsigned n = FT->getNumParams();
546 for (Function::arg_iterator ai = NewF->arg_begin(); n != 0; ++ai, --n)
547 Args.push_back(&*ai);
548 CallInst *CI = CallInst::Create(F, Args, "", BB);
549 if (FT->getReturnType()->isVoidTy())
550 ReturnInst::Create(*Ctx, BB);
552 ReturnInst::Create(*Ctx, CI, BB);
558 Constant *DataFlowSanitizer::getOrBuildTrampolineFunction(FunctionType *FT,
560 FunctionType *FTT = getTrampolineFunctionType(FT);
561 Constant *C = Mod->getOrInsertFunction(FName, FTT);
562 Function *F = dyn_cast<Function>(C);
563 if (F && F->isDeclaration()) {
564 F->setLinkage(GlobalValue::LinkOnceODRLinkage);
565 BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", F);
566 std::vector<Value *> Args;
567 Function::arg_iterator AI = F->arg_begin(); ++AI;
568 for (unsigned N = FT->getNumParams(); N != 0; ++AI, --N)
569 Args.push_back(&*AI);
571 CallInst::Create(&F->getArgumentList().front(), Args, "", BB);
573 if (FT->getReturnType()->isVoidTy())
574 RI = ReturnInst::Create(*Ctx, BB);
576 RI = ReturnInst::Create(*Ctx, CI, BB);
578 DFSanFunction DFSF(*this, F, /*IsNativeABI=*/true);
579 Function::arg_iterator ValAI = F->arg_begin(), ShadowAI = AI; ++ValAI;
580 for (unsigned N = FT->getNumParams(); N != 0; ++ValAI, ++ShadowAI, --N)
581 DFSF.ValShadowMap[ValAI] = ShadowAI;
582 DFSanVisitor(DFSF).visitCallInst(*CI);
583 if (!FT->getReturnType()->isVoidTy())
584 new StoreInst(DFSF.getShadow(RI->getReturnValue()),
585 &F->getArgumentList().back(), RI);
591 bool DataFlowSanitizer::runOnModule(Module &M) {
592 if (ABIList.isIn(M, "skip"))
595 FunctionDIs = makeSubprogramMap(M);
598 Type *ArgTLSTy = ArrayType::get(ShadowTy, 64);
599 ArgTLS = Mod->getOrInsertGlobal("__dfsan_arg_tls", ArgTLSTy);
600 if (GlobalVariable *G = dyn_cast<GlobalVariable>(ArgTLS))
601 G->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
603 if (!GetRetvalTLSPtr) {
604 RetvalTLS = Mod->getOrInsertGlobal("__dfsan_retval_tls", ShadowTy);
605 if (GlobalVariable *G = dyn_cast<GlobalVariable>(RetvalTLS))
606 G->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
609 DFSanUnionFn = Mod->getOrInsertFunction("__dfsan_union", DFSanUnionFnTy);
610 if (Function *F = dyn_cast<Function>(DFSanUnionFn)) {
611 F->addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
612 F->addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
613 F->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
614 F->addAttribute(1, Attribute::ZExt);
615 F->addAttribute(2, Attribute::ZExt);
617 DFSanCheckedUnionFn = Mod->getOrInsertFunction("dfsan_union", DFSanUnionFnTy);
618 if (Function *F = dyn_cast<Function>(DFSanCheckedUnionFn)) {
619 F->addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
620 F->addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
621 F->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
622 F->addAttribute(1, Attribute::ZExt);
623 F->addAttribute(2, Attribute::ZExt);
626 Mod->getOrInsertFunction("__dfsan_union_load", DFSanUnionLoadFnTy);
627 if (Function *F = dyn_cast<Function>(DFSanUnionLoadFn)) {
628 F->addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
629 F->addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
630 F->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
632 DFSanUnimplementedFn =
633 Mod->getOrInsertFunction("__dfsan_unimplemented", DFSanUnimplementedFnTy);
635 Mod->getOrInsertFunction("__dfsan_set_label", DFSanSetLabelFnTy);
636 if (Function *F = dyn_cast<Function>(DFSanSetLabelFn)) {
637 F->addAttribute(1, Attribute::ZExt);
639 DFSanNonzeroLabelFn =
640 Mod->getOrInsertFunction("__dfsan_nonzero_label", DFSanNonzeroLabelFnTy);
641 DFSanVarargWrapperFn = Mod->getOrInsertFunction("__dfsan_vararg_wrapper",
642 DFSanVarargWrapperFnTy);
644 std::vector<Function *> FnsToInstrument;
645 llvm::SmallPtrSet<Function *, 2> FnsWithNativeABI;
646 for (Module::iterator i = M.begin(), e = M.end(); i != e; ++i) {
647 if (!i->isIntrinsic() &&
649 i != DFSanCheckedUnionFn &&
650 i != DFSanUnionLoadFn &&
651 i != DFSanUnimplementedFn &&
652 i != DFSanSetLabelFn &&
653 i != DFSanNonzeroLabelFn &&
654 i != DFSanVarargWrapperFn)
655 FnsToInstrument.push_back(&*i);
658 // Give function aliases prefixes when necessary, and build wrappers where the
659 // instrumentedness is inconsistent.
660 for (Module::alias_iterator i = M.alias_begin(), e = M.alias_end(); i != e;) {
661 GlobalAlias *GA = &*i;
663 // Don't stop on weak. We assume people aren't playing games with the
664 // instrumentedness of overridden weak aliases.
665 if (auto F = dyn_cast<Function>(GA->getBaseObject())) {
666 bool GAInst = isInstrumented(GA), FInst = isInstrumented(F);
667 if (GAInst && FInst) {
668 addGlobalNamePrefix(GA);
669 } else if (GAInst != FInst) {
670 // Non-instrumented alias of an instrumented function, or vice versa.
671 // Replace the alias with a native-ABI wrapper of the aliasee. The pass
672 // below will take care of instrumenting it.
674 buildWrapperFunction(F, "", GA->getLinkage(), F->getFunctionType());
675 GA->replaceAllUsesWith(ConstantExpr::getBitCast(NewF, GA->getType()));
677 GA->eraseFromParent();
678 FnsToInstrument.push_back(NewF);
684 B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
685 ReadOnlyNoneAttrs = AttributeSet::get(*Ctx, AttributeSet::FunctionIndex, B);
687 // First, change the ABI of every function in the module. ABI-listed
688 // functions keep their original ABI and get a wrapper function.
689 for (std::vector<Function *>::iterator i = FnsToInstrument.begin(),
690 e = FnsToInstrument.end();
693 FunctionType *FT = F.getFunctionType();
695 bool IsZeroArgsVoidRet = (FT->getNumParams() == 0 && !FT->isVarArg() &&
696 FT->getReturnType()->isVoidTy());
698 if (isInstrumented(&F)) {
699 // Instrumented functions get a 'dfs$' prefix. This allows us to more
700 // easily identify cases of mismatching ABIs.
701 if (getInstrumentedABI() == IA_Args && !IsZeroArgsVoidRet) {
702 FunctionType *NewFT = getArgsFunctionType(FT);
703 Function *NewF = Function::Create(NewFT, F.getLinkage(), "", &M);
704 NewF->copyAttributesFrom(&F);
705 NewF->removeAttributes(
706 AttributeSet::ReturnIndex,
707 AttributeSet::get(NewF->getContext(), AttributeSet::ReturnIndex,
708 AttributeFuncs::typeIncompatible(NewFT->getReturnType())));
709 for (Function::arg_iterator FArg = F.arg_begin(),
710 NewFArg = NewF->arg_begin(),
711 FArgEnd = F.arg_end();
712 FArg != FArgEnd; ++FArg, ++NewFArg) {
713 FArg->replaceAllUsesWith(NewFArg);
715 NewF->getBasicBlockList().splice(NewF->begin(), F.getBasicBlockList());
717 for (Function::user_iterator UI = F.user_begin(), UE = F.user_end();
719 BlockAddress *BA = dyn_cast<BlockAddress>(*UI);
722 BA->replaceAllUsesWith(
723 BlockAddress::get(NewF, BA->getBasicBlock()));
727 F.replaceAllUsesWith(
728 ConstantExpr::getBitCast(NewF, PointerType::getUnqual(FT)));
732 addGlobalNamePrefix(NewF);
734 addGlobalNamePrefix(&F);
736 } else if (!IsZeroArgsVoidRet || getWrapperKind(&F) == WK_Custom) {
737 // Build a wrapper function for F. The wrapper simply calls F, and is
738 // added to FnsToInstrument so that any instrumentation according to its
739 // WrapperKind is done in the second pass below.
740 FunctionType *NewFT = getInstrumentedABI() == IA_Args
741 ? getArgsFunctionType(FT)
743 Function *NewF = buildWrapperFunction(
744 &F, std::string("dfsw$") + std::string(F.getName()),
745 GlobalValue::LinkOnceODRLinkage, NewFT);
746 if (getInstrumentedABI() == IA_TLS)
747 NewF->removeAttributes(AttributeSet::FunctionIndex, ReadOnlyNoneAttrs);
749 Value *WrappedFnCst =
750 ConstantExpr::getBitCast(NewF, PointerType::getUnqual(FT));
751 F.replaceAllUsesWith(WrappedFnCst);
753 // Patch the pointer to LLVM function in debug info descriptor.
754 auto DI = FunctionDIs.find(&F);
755 if (DI != FunctionDIs.end())
756 DI->second->replaceFunction(&F);
758 UnwrappedFnMap[WrappedFnCst] = &F;
761 if (!F.isDeclaration()) {
762 // This function is probably defining an interposition of an
763 // uninstrumented function and hence needs to keep the original ABI.
764 // But any functions it may call need to use the instrumented ABI, so
765 // we instrument it in a mode which preserves the original ABI.
766 FnsWithNativeABI.insert(&F);
768 // This code needs to rebuild the iterators, as they may be invalidated
769 // by the push_back, taking care that the new range does not include
770 // any functions added by this code.
771 size_t N = i - FnsToInstrument.begin(),
772 Count = e - FnsToInstrument.begin();
773 FnsToInstrument.push_back(&F);
774 i = FnsToInstrument.begin() + N;
775 e = FnsToInstrument.begin() + Count;
777 // Hopefully, nobody will try to indirectly call a vararg
779 } else if (FT->isVarArg()) {
780 UnwrappedFnMap[&F] = &F;
785 for (std::vector<Function *>::iterator i = FnsToInstrument.begin(),
786 e = FnsToInstrument.end();
788 if (!*i || (*i)->isDeclaration())
791 removeUnreachableBlocks(**i);
793 DFSanFunction DFSF(*this, *i, FnsWithNativeABI.count(*i));
795 // DFSanVisitor may create new basic blocks, which confuses df_iterator.
796 // Build a copy of the list before iterating over it.
797 llvm::SmallVector<BasicBlock *, 4> BBList(
798 depth_first(&(*i)->getEntryBlock()));
800 for (llvm::SmallVector<BasicBlock *, 4>::iterator i = BBList.begin(),
803 Instruction *Inst = &(*i)->front();
805 // DFSanVisitor may split the current basic block, changing the current
806 // instruction's next pointer and moving the next instruction to the
807 // tail block from which we should continue.
808 Instruction *Next = Inst->getNextNode();
809 // DFSanVisitor may delete Inst, so keep track of whether it was a
811 bool IsTerminator = isa<TerminatorInst>(Inst);
812 if (!DFSF.SkipInsts.count(Inst))
813 DFSanVisitor(DFSF).visit(Inst);
820 // We will not necessarily be able to compute the shadow for every phi node
821 // until we have visited every block. Therefore, the code that handles phi
822 // nodes adds them to the PHIFixups list so that they can be properly
824 for (std::vector<std::pair<PHINode *, PHINode *> >::iterator
825 i = DFSF.PHIFixups.begin(),
826 e = DFSF.PHIFixups.end();
828 for (unsigned val = 0, n = i->first->getNumIncomingValues(); val != n;
830 i->second->setIncomingValue(
831 val, DFSF.getShadow(i->first->getIncomingValue(val)));
835 // -dfsan-debug-nonzero-labels will split the CFG in all kinds of crazy
836 // places (i.e. instructions in basic blocks we haven't even begun visiting
837 // yet). To make our life easier, do this work in a pass after the main
839 if (ClDebugNonzeroLabels) {
840 for (Value *V : DFSF.NonZeroChecks) {
842 if (Instruction *I = dyn_cast<Instruction>(V))
843 Pos = I->getNextNode();
845 Pos = DFSF.F->getEntryBlock().begin();
846 while (isa<PHINode>(Pos) || isa<AllocaInst>(Pos))
847 Pos = Pos->getNextNode();
848 IRBuilder<> IRB(Pos);
849 Value *Ne = IRB.CreateICmpNE(V, DFSF.DFS.ZeroShadow);
850 BranchInst *BI = cast<BranchInst>(SplitBlockAndInsertIfThen(
851 Ne, Pos, /*Unreachable=*/false, ColdCallWeights));
852 IRBuilder<> ThenIRB(BI);
853 ThenIRB.CreateCall(DFSF.DFS.DFSanNonzeroLabelFn, {});
861 Value *DFSanFunction::getArgTLSPtr() {
865 return ArgTLSPtr = DFS.ArgTLS;
867 IRBuilder<> IRB(F->getEntryBlock().begin());
868 return ArgTLSPtr = IRB.CreateCall(DFS.GetArgTLS, {});
871 Value *DFSanFunction::getRetvalTLS() {
875 return RetvalTLSPtr = DFS.RetvalTLS;
877 IRBuilder<> IRB(F->getEntryBlock().begin());
878 return RetvalTLSPtr = IRB.CreateCall(DFS.GetRetvalTLS, {});
881 Value *DFSanFunction::getArgTLS(unsigned Idx, Instruction *Pos) {
882 IRBuilder<> IRB(Pos);
883 return IRB.CreateConstGEP2_64(getArgTLSPtr(), 0, Idx);
886 Value *DFSanFunction::getShadow(Value *V) {
887 if (!isa<Argument>(V) && !isa<Instruction>(V))
888 return DFS.ZeroShadow;
889 Value *&Shadow = ValShadowMap[V];
891 if (Argument *A = dyn_cast<Argument>(V)) {
893 return DFS.ZeroShadow;
895 case DataFlowSanitizer::IA_TLS: {
896 Value *ArgTLSPtr = getArgTLSPtr();
897 Instruction *ArgTLSPos =
898 DFS.ArgTLS ? &*F->getEntryBlock().begin()
899 : cast<Instruction>(ArgTLSPtr)->getNextNode();
900 IRBuilder<> IRB(ArgTLSPos);
901 Shadow = IRB.CreateLoad(getArgTLS(A->getArgNo(), ArgTLSPos));
904 case DataFlowSanitizer::IA_Args: {
905 unsigned ArgIdx = A->getArgNo() + F->getArgumentList().size() / 2;
906 Function::arg_iterator i = F->arg_begin();
910 assert(Shadow->getType() == DFS.ShadowTy);
914 NonZeroChecks.push_back(Shadow);
916 Shadow = DFS.ZeroShadow;
922 void DFSanFunction::setShadow(Instruction *I, Value *Shadow) {
923 assert(!ValShadowMap.count(I));
924 assert(Shadow->getType() == DFS.ShadowTy);
925 ValShadowMap[I] = Shadow;
928 Value *DataFlowSanitizer::getShadowAddress(Value *Addr, Instruction *Pos) {
929 assert(Addr != RetvalTLS && "Reinstrumenting?");
930 IRBuilder<> IRB(Pos);
931 return IRB.CreateIntToPtr(
933 IRB.CreateAnd(IRB.CreatePtrToInt(Addr, IntptrTy), ShadowPtrMask),
938 // Generates IR to compute the union of the two given shadows, inserting it
939 // before Pos. Returns the computed union Value.
940 Value *DFSanFunction::combineShadows(Value *V1, Value *V2, Instruction *Pos) {
941 if (V1 == DFS.ZeroShadow)
943 if (V2 == DFS.ZeroShadow)
948 auto V1Elems = ShadowElements.find(V1);
949 auto V2Elems = ShadowElements.find(V2);
950 if (V1Elems != ShadowElements.end() && V2Elems != ShadowElements.end()) {
951 if (std::includes(V1Elems->second.begin(), V1Elems->second.end(),
952 V2Elems->second.begin(), V2Elems->second.end())) {
954 } else if (std::includes(V2Elems->second.begin(), V2Elems->second.end(),
955 V1Elems->second.begin(), V1Elems->second.end())) {
958 } else if (V1Elems != ShadowElements.end()) {
959 if (V1Elems->second.count(V2))
961 } else if (V2Elems != ShadowElements.end()) {
962 if (V2Elems->second.count(V1))
966 auto Key = std::make_pair(V1, V2);
968 std::swap(Key.first, Key.second);
969 CachedCombinedShadow &CCS = CachedCombinedShadows[Key];
970 if (CCS.Block && DT.dominates(CCS.Block, Pos->getParent()))
973 IRBuilder<> IRB(Pos);
974 if (AvoidNewBlocks) {
975 CallInst *Call = IRB.CreateCall(DFS.DFSanCheckedUnionFn, {V1, V2});
976 Call->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
977 Call->addAttribute(1, Attribute::ZExt);
978 Call->addAttribute(2, Attribute::ZExt);
980 CCS.Block = Pos->getParent();
983 BasicBlock *Head = Pos->getParent();
984 Value *Ne = IRB.CreateICmpNE(V1, V2);
985 BranchInst *BI = cast<BranchInst>(SplitBlockAndInsertIfThen(
986 Ne, Pos, /*Unreachable=*/false, DFS.ColdCallWeights, &DT));
987 IRBuilder<> ThenIRB(BI);
988 CallInst *Call = ThenIRB.CreateCall(DFS.DFSanUnionFn, {V1, V2});
989 Call->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
990 Call->addAttribute(1, Attribute::ZExt);
991 Call->addAttribute(2, Attribute::ZExt);
993 BasicBlock *Tail = BI->getSuccessor(0);
994 PHINode *Phi = PHINode::Create(DFS.ShadowTy, 2, "", Tail->begin());
995 Phi->addIncoming(Call, Call->getParent());
996 Phi->addIncoming(V1, Head);
1002 std::set<Value *> UnionElems;
1003 if (V1Elems != ShadowElements.end()) {
1004 UnionElems = V1Elems->second;
1006 UnionElems.insert(V1);
1008 if (V2Elems != ShadowElements.end()) {
1009 UnionElems.insert(V2Elems->second.begin(), V2Elems->second.end());
1011 UnionElems.insert(V2);
1013 ShadowElements[CCS.Shadow] = std::move(UnionElems);
1018 // A convenience function which folds the shadows of each of the operands
1019 // of the provided instruction Inst, inserting the IR before Inst. Returns
1020 // the computed union Value.
1021 Value *DFSanFunction::combineOperandShadows(Instruction *Inst) {
1022 if (Inst->getNumOperands() == 0)
1023 return DFS.ZeroShadow;
1025 Value *Shadow = getShadow(Inst->getOperand(0));
1026 for (unsigned i = 1, n = Inst->getNumOperands(); i != n; ++i) {
1027 Shadow = combineShadows(Shadow, getShadow(Inst->getOperand(i)), Inst);
1032 void DFSanVisitor::visitOperandShadowInst(Instruction &I) {
1033 Value *CombinedShadow = DFSF.combineOperandShadows(&I);
1034 DFSF.setShadow(&I, CombinedShadow);
1037 // Generates IR to load shadow corresponding to bytes [Addr, Addr+Size), where
1038 // Addr has alignment Align, and take the union of each of those shadows.
1039 Value *DFSanFunction::loadShadow(Value *Addr, uint64_t Size, uint64_t Align,
1041 if (AllocaInst *AI = dyn_cast<AllocaInst>(Addr)) {
1042 llvm::DenseMap<AllocaInst *, AllocaInst *>::iterator i =
1043 AllocaShadowMap.find(AI);
1044 if (i != AllocaShadowMap.end()) {
1045 IRBuilder<> IRB(Pos);
1046 return IRB.CreateLoad(i->second);
1050 uint64_t ShadowAlign = Align * DFS.ShadowWidth / 8;
1051 SmallVector<Value *, 2> Objs;
1052 GetUnderlyingObjects(Addr, Objs, Pos->getModule()->getDataLayout());
1053 bool AllConstants = true;
1054 for (SmallVector<Value *, 2>::iterator i = Objs.begin(), e = Objs.end();
1056 if (isa<Function>(*i) || isa<BlockAddress>(*i))
1058 if (isa<GlobalVariable>(*i) && cast<GlobalVariable>(*i)->isConstant())
1061 AllConstants = false;
1065 return DFS.ZeroShadow;
1067 Value *ShadowAddr = DFS.getShadowAddress(Addr, Pos);
1070 return DFS.ZeroShadow;
1072 LoadInst *LI = new LoadInst(ShadowAddr, "", Pos);
1073 LI->setAlignment(ShadowAlign);
1077 IRBuilder<> IRB(Pos);
1078 Value *ShadowAddr1 = IRB.CreateGEP(DFS.ShadowTy, ShadowAddr,
1079 ConstantInt::get(DFS.IntptrTy, 1));
1080 return combineShadows(IRB.CreateAlignedLoad(ShadowAddr, ShadowAlign),
1081 IRB.CreateAlignedLoad(ShadowAddr1, ShadowAlign), Pos);
1084 if (!AvoidNewBlocks && Size % (64 / DFS.ShadowWidth) == 0) {
1085 // Fast path for the common case where each byte has identical shadow: load
1086 // shadow 64 bits at a time, fall out to a __dfsan_union_load call if any
1087 // shadow is non-equal.
1088 BasicBlock *FallbackBB = BasicBlock::Create(*DFS.Ctx, "", F);
1089 IRBuilder<> FallbackIRB(FallbackBB);
1090 CallInst *FallbackCall = FallbackIRB.CreateCall(
1091 DFS.DFSanUnionLoadFn,
1092 {ShadowAddr, ConstantInt::get(DFS.IntptrTy, Size)});
1093 FallbackCall->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
1095 // Compare each of the shadows stored in the loaded 64 bits to each other,
1096 // by computing (WideShadow rotl ShadowWidth) == WideShadow.
1097 IRBuilder<> IRB(Pos);
1099 IRB.CreateBitCast(ShadowAddr, Type::getInt64PtrTy(*DFS.Ctx));
1100 Value *WideShadow = IRB.CreateAlignedLoad(WideAddr, ShadowAlign);
1101 Value *TruncShadow = IRB.CreateTrunc(WideShadow, DFS.ShadowTy);
1102 Value *ShlShadow = IRB.CreateShl(WideShadow, DFS.ShadowWidth);
1103 Value *ShrShadow = IRB.CreateLShr(WideShadow, 64 - DFS.ShadowWidth);
1104 Value *RotShadow = IRB.CreateOr(ShlShadow, ShrShadow);
1105 Value *ShadowsEq = IRB.CreateICmpEQ(WideShadow, RotShadow);
1107 BasicBlock *Head = Pos->getParent();
1108 BasicBlock *Tail = Head->splitBasicBlock(Pos);
1110 if (DomTreeNode *OldNode = DT.getNode(Head)) {
1111 std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());
1113 DomTreeNode *NewNode = DT.addNewBlock(Tail, Head);
1114 for (auto Child : Children)
1115 DT.changeImmediateDominator(Child, NewNode);
1118 // In the following code LastBr will refer to the previous basic block's
1119 // conditional branch instruction, whose true successor is fixed up to point
1120 // to the next block during the loop below or to the tail after the final
1122 BranchInst *LastBr = BranchInst::Create(FallbackBB, FallbackBB, ShadowsEq);
1123 ReplaceInstWithInst(Head->getTerminator(), LastBr);
1124 DT.addNewBlock(FallbackBB, Head);
1126 for (uint64_t Ofs = 64 / DFS.ShadowWidth; Ofs != Size;
1127 Ofs += 64 / DFS.ShadowWidth) {
1128 BasicBlock *NextBB = BasicBlock::Create(*DFS.Ctx, "", F);
1129 DT.addNewBlock(NextBB, LastBr->getParent());
1130 IRBuilder<> NextIRB(NextBB);
1131 WideAddr = NextIRB.CreateGEP(Type::getInt64Ty(*DFS.Ctx), WideAddr,
1132 ConstantInt::get(DFS.IntptrTy, 1));
1133 Value *NextWideShadow = NextIRB.CreateAlignedLoad(WideAddr, ShadowAlign);
1134 ShadowsEq = NextIRB.CreateICmpEQ(WideShadow, NextWideShadow);
1135 LastBr->setSuccessor(0, NextBB);
1136 LastBr = NextIRB.CreateCondBr(ShadowsEq, FallbackBB, FallbackBB);
1139 LastBr->setSuccessor(0, Tail);
1140 FallbackIRB.CreateBr(Tail);
1141 PHINode *Shadow = PHINode::Create(DFS.ShadowTy, 2, "", &Tail->front());
1142 Shadow->addIncoming(FallbackCall, FallbackBB);
1143 Shadow->addIncoming(TruncShadow, LastBr->getParent());
1147 IRBuilder<> IRB(Pos);
1148 CallInst *FallbackCall = IRB.CreateCall(
1149 DFS.DFSanUnionLoadFn, {ShadowAddr, ConstantInt::get(DFS.IntptrTy, Size)});
1150 FallbackCall->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
1151 return FallbackCall;
1154 void DFSanVisitor::visitLoadInst(LoadInst &LI) {
1155 auto &DL = LI.getModule()->getDataLayout();
1156 uint64_t Size = DL.getTypeStoreSize(LI.getType());
1158 DFSF.setShadow(&LI, DFSF.DFS.ZeroShadow);
1163 if (ClPreserveAlignment) {
1164 Align = LI.getAlignment();
1166 Align = DL.getABITypeAlignment(LI.getType());
1170 IRBuilder<> IRB(&LI);
1171 Value *Shadow = DFSF.loadShadow(LI.getPointerOperand(), Size, Align, &LI);
1172 if (ClCombinePointerLabelsOnLoad) {
1173 Value *PtrShadow = DFSF.getShadow(LI.getPointerOperand());
1174 Shadow = DFSF.combineShadows(Shadow, PtrShadow, &LI);
1176 if (Shadow != DFSF.DFS.ZeroShadow)
1177 DFSF.NonZeroChecks.push_back(Shadow);
1179 DFSF.setShadow(&LI, Shadow);
1182 void DFSanFunction::storeShadow(Value *Addr, uint64_t Size, uint64_t Align,
1183 Value *Shadow, Instruction *Pos) {
1184 if (AllocaInst *AI = dyn_cast<AllocaInst>(Addr)) {
1185 llvm::DenseMap<AllocaInst *, AllocaInst *>::iterator i =
1186 AllocaShadowMap.find(AI);
1187 if (i != AllocaShadowMap.end()) {
1188 IRBuilder<> IRB(Pos);
1189 IRB.CreateStore(Shadow, i->second);
1194 uint64_t ShadowAlign = Align * DFS.ShadowWidth / 8;
1195 IRBuilder<> IRB(Pos);
1196 Value *ShadowAddr = DFS.getShadowAddress(Addr, Pos);
1197 if (Shadow == DFS.ZeroShadow) {
1198 IntegerType *ShadowTy = IntegerType::get(*DFS.Ctx, Size * DFS.ShadowWidth);
1199 Value *ExtZeroShadow = ConstantInt::get(ShadowTy, 0);
1200 Value *ExtShadowAddr =
1201 IRB.CreateBitCast(ShadowAddr, PointerType::getUnqual(ShadowTy));
1202 IRB.CreateAlignedStore(ExtZeroShadow, ExtShadowAddr, ShadowAlign);
1206 const unsigned ShadowVecSize = 128 / DFS.ShadowWidth;
1207 uint64_t Offset = 0;
1208 if (Size >= ShadowVecSize) {
1209 VectorType *ShadowVecTy = VectorType::get(DFS.ShadowTy, ShadowVecSize);
1210 Value *ShadowVec = UndefValue::get(ShadowVecTy);
1211 for (unsigned i = 0; i != ShadowVecSize; ++i) {
1212 ShadowVec = IRB.CreateInsertElement(
1213 ShadowVec, Shadow, ConstantInt::get(Type::getInt32Ty(*DFS.Ctx), i));
1215 Value *ShadowVecAddr =
1216 IRB.CreateBitCast(ShadowAddr, PointerType::getUnqual(ShadowVecTy));
1218 Value *CurShadowVecAddr =
1219 IRB.CreateConstGEP1_32(ShadowVecTy, ShadowVecAddr, Offset);
1220 IRB.CreateAlignedStore(ShadowVec, CurShadowVecAddr, ShadowAlign);
1221 Size -= ShadowVecSize;
1223 } while (Size >= ShadowVecSize);
1224 Offset *= ShadowVecSize;
1227 Value *CurShadowAddr =
1228 IRB.CreateConstGEP1_32(DFS.ShadowTy, ShadowAddr, Offset);
1229 IRB.CreateAlignedStore(Shadow, CurShadowAddr, ShadowAlign);
1235 void DFSanVisitor::visitStoreInst(StoreInst &SI) {
1236 auto &DL = SI.getModule()->getDataLayout();
1237 uint64_t Size = DL.getTypeStoreSize(SI.getValueOperand()->getType());
1242 if (ClPreserveAlignment) {
1243 Align = SI.getAlignment();
1245 Align = DL.getABITypeAlignment(SI.getValueOperand()->getType());
1250 Value* Shadow = DFSF.getShadow(SI.getValueOperand());
1251 if (ClCombinePointerLabelsOnStore) {
1252 Value *PtrShadow = DFSF.getShadow(SI.getPointerOperand());
1253 Shadow = DFSF.combineShadows(Shadow, PtrShadow, &SI);
1255 DFSF.storeShadow(SI.getPointerOperand(), Size, Align, Shadow, &SI);
1258 void DFSanVisitor::visitBinaryOperator(BinaryOperator &BO) {
1259 visitOperandShadowInst(BO);
1262 void DFSanVisitor::visitCastInst(CastInst &CI) { visitOperandShadowInst(CI); }
1264 void DFSanVisitor::visitCmpInst(CmpInst &CI) { visitOperandShadowInst(CI); }
1266 void DFSanVisitor::visitGetElementPtrInst(GetElementPtrInst &GEPI) {
1267 visitOperandShadowInst(GEPI);
1270 void DFSanVisitor::visitExtractElementInst(ExtractElementInst &I) {
1271 visitOperandShadowInst(I);
1274 void DFSanVisitor::visitInsertElementInst(InsertElementInst &I) {
1275 visitOperandShadowInst(I);
1278 void DFSanVisitor::visitShuffleVectorInst(ShuffleVectorInst &I) {
1279 visitOperandShadowInst(I);
1282 void DFSanVisitor::visitExtractValueInst(ExtractValueInst &I) {
1283 visitOperandShadowInst(I);
1286 void DFSanVisitor::visitInsertValueInst(InsertValueInst &I) {
1287 visitOperandShadowInst(I);
1290 void DFSanVisitor::visitAllocaInst(AllocaInst &I) {
1291 bool AllLoadsStores = true;
1292 for (User *U : I.users()) {
1293 if (isa<LoadInst>(U))
1296 if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
1297 if (SI->getPointerOperand() == &I)
1301 AllLoadsStores = false;
1304 if (AllLoadsStores) {
1305 IRBuilder<> IRB(&I);
1306 DFSF.AllocaShadowMap[&I] = IRB.CreateAlloca(DFSF.DFS.ShadowTy);
1308 DFSF.setShadow(&I, DFSF.DFS.ZeroShadow);
1311 void DFSanVisitor::visitSelectInst(SelectInst &I) {
1312 Value *CondShadow = DFSF.getShadow(I.getCondition());
1313 Value *TrueShadow = DFSF.getShadow(I.getTrueValue());
1314 Value *FalseShadow = DFSF.getShadow(I.getFalseValue());
1316 if (isa<VectorType>(I.getCondition()->getType())) {
1319 DFSF.combineShadows(
1320 CondShadow, DFSF.combineShadows(TrueShadow, FalseShadow, &I), &I));
1323 if (TrueShadow == FalseShadow) {
1324 ShadowSel = TrueShadow;
1327 SelectInst::Create(I.getCondition(), TrueShadow, FalseShadow, "", &I);
1329 DFSF.setShadow(&I, DFSF.combineShadows(CondShadow, ShadowSel, &I));
1333 void DFSanVisitor::visitMemSetInst(MemSetInst &I) {
1334 IRBuilder<> IRB(&I);
1335 Value *ValShadow = DFSF.getShadow(I.getValue());
1336 IRB.CreateCall(DFSF.DFS.DFSanSetLabelFn,
1337 {ValShadow, IRB.CreateBitCast(I.getDest(), Type::getInt8PtrTy(
1339 IRB.CreateZExtOrTrunc(I.getLength(), DFSF.DFS.IntptrTy)});
1342 void DFSanVisitor::visitMemTransferInst(MemTransferInst &I) {
1343 IRBuilder<> IRB(&I);
1344 Value *DestShadow = DFSF.DFS.getShadowAddress(I.getDest(), &I);
1345 Value *SrcShadow = DFSF.DFS.getShadowAddress(I.getSource(), &I);
1346 Value *LenShadow = IRB.CreateMul(
1348 ConstantInt::get(I.getLength()->getType(), DFSF.DFS.ShadowWidth / 8));
1350 if (ClPreserveAlignment) {
1351 AlignShadow = IRB.CreateMul(I.getAlignmentCst(),
1352 ConstantInt::get(I.getAlignmentCst()->getType(),
1353 DFSF.DFS.ShadowWidth / 8));
1355 AlignShadow = ConstantInt::get(I.getAlignmentCst()->getType(),
1356 DFSF.DFS.ShadowWidth / 8);
1358 Type *Int8Ptr = Type::getInt8PtrTy(*DFSF.DFS.Ctx);
1359 DestShadow = IRB.CreateBitCast(DestShadow, Int8Ptr);
1360 SrcShadow = IRB.CreateBitCast(SrcShadow, Int8Ptr);
1361 IRB.CreateCall(I.getCalledValue(), {DestShadow, SrcShadow, LenShadow,
1362 AlignShadow, I.getVolatileCst()});
1365 void DFSanVisitor::visitReturnInst(ReturnInst &RI) {
1366 if (!DFSF.IsNativeABI && RI.getReturnValue()) {
1368 case DataFlowSanitizer::IA_TLS: {
1369 Value *S = DFSF.getShadow(RI.getReturnValue());
1370 IRBuilder<> IRB(&RI);
1371 IRB.CreateStore(S, DFSF.getRetvalTLS());
1374 case DataFlowSanitizer::IA_Args: {
1375 IRBuilder<> IRB(&RI);
1376 Type *RT = DFSF.F->getFunctionType()->getReturnType();
1378 IRB.CreateInsertValue(UndefValue::get(RT), RI.getReturnValue(), 0);
1380 IRB.CreateInsertValue(InsVal, DFSF.getShadow(RI.getReturnValue()), 1);
1381 RI.setOperand(0, InsShadow);
1388 void DFSanVisitor::visitCallSite(CallSite CS) {
1389 Function *F = CS.getCalledFunction();
1390 if ((F && F->isIntrinsic()) || isa<InlineAsm>(CS.getCalledValue())) {
1391 visitOperandShadowInst(*CS.getInstruction());
1395 // Calls to this function are synthesized in wrappers, and we shouldn't
1397 if (F == DFSF.DFS.DFSanVarargWrapperFn)
1400 assert(!(cast<FunctionType>(
1401 CS.getCalledValue()->getType()->getPointerElementType())->isVarArg() &&
1402 dyn_cast<InvokeInst>(CS.getInstruction())));
1404 IRBuilder<> IRB(CS.getInstruction());
1406 DenseMap<Value *, Function *>::iterator i =
1407 DFSF.DFS.UnwrappedFnMap.find(CS.getCalledValue());
1408 if (i != DFSF.DFS.UnwrappedFnMap.end()) {
1409 Function *F = i->second;
1410 switch (DFSF.DFS.getWrapperKind(F)) {
1411 case DataFlowSanitizer::WK_Warning: {
1412 CS.setCalledFunction(F);
1413 IRB.CreateCall(DFSF.DFS.DFSanUnimplementedFn,
1414 IRB.CreateGlobalStringPtr(F->getName()));
1415 DFSF.setShadow(CS.getInstruction(), DFSF.DFS.ZeroShadow);
1418 case DataFlowSanitizer::WK_Discard: {
1419 CS.setCalledFunction(F);
1420 DFSF.setShadow(CS.getInstruction(), DFSF.DFS.ZeroShadow);
1423 case DataFlowSanitizer::WK_Functional: {
1424 CS.setCalledFunction(F);
1425 visitOperandShadowInst(*CS.getInstruction());
1428 case DataFlowSanitizer::WK_Custom: {
1429 // Don't try to handle invokes of custom functions, it's too complicated.
1430 // Instead, invoke the dfsw$ wrapper, which will in turn call the __dfsw_
1432 if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) {
1433 FunctionType *FT = F->getFunctionType();
1434 FunctionType *CustomFT = DFSF.DFS.getCustomFunctionType(FT);
1435 std::string CustomFName = "__dfsw_";
1436 CustomFName += F->getName();
1438 DFSF.DFS.Mod->getOrInsertFunction(CustomFName, CustomFT);
1439 if (Function *CustomFn = dyn_cast<Function>(CustomF)) {
1440 CustomFn->copyAttributesFrom(F);
1442 // Custom functions returning non-void will write to the return label.
1443 if (!FT->getReturnType()->isVoidTy()) {
1444 CustomFn->removeAttributes(AttributeSet::FunctionIndex,
1445 DFSF.DFS.ReadOnlyNoneAttrs);
1449 std::vector<Value *> Args;
1451 CallSite::arg_iterator i = CS.arg_begin();
1452 for (unsigned n = FT->getNumParams(); n != 0; ++i, --n) {
1453 Type *T = (*i)->getType();
1454 FunctionType *ParamFT;
1455 if (isa<PointerType>(T) &&
1456 (ParamFT = dyn_cast<FunctionType>(
1457 cast<PointerType>(T)->getElementType()))) {
1458 std::string TName = "dfst";
1459 TName += utostr(FT->getNumParams() - n);
1461 TName += F->getName();
1462 Constant *T = DFSF.DFS.getOrBuildTrampolineFunction(ParamFT, TName);
1465 IRB.CreateBitCast(*i, Type::getInt8PtrTy(*DFSF.DFS.Ctx)));
1472 for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
1473 Args.push_back(DFSF.getShadow(*i));
1475 if (FT->isVarArg()) {
1476 auto *LabelVATy = ArrayType::get(DFSF.DFS.ShadowTy,
1477 CS.arg_size() - FT->getNumParams());
1478 auto *LabelVAAlloca = new AllocaInst(LabelVATy, "labelva",
1479 DFSF.F->getEntryBlock().begin());
1481 for (unsigned n = 0; i != CS.arg_end(); ++i, ++n) {
1482 auto LabelVAPtr = IRB.CreateStructGEP(LabelVATy, LabelVAAlloca, n);
1483 IRB.CreateStore(DFSF.getShadow(*i), LabelVAPtr);
1486 Args.push_back(IRB.CreateStructGEP(LabelVATy, LabelVAAlloca, 0));
1489 if (!FT->getReturnType()->isVoidTy()) {
1490 if (!DFSF.LabelReturnAlloca) {
1491 DFSF.LabelReturnAlloca =
1492 new AllocaInst(DFSF.DFS.ShadowTy, "labelreturn",
1493 DFSF.F->getEntryBlock().begin());
1495 Args.push_back(DFSF.LabelReturnAlloca);
1498 for (i = CS.arg_begin() + FT->getNumParams(); i != CS.arg_end(); ++i)
1501 CallInst *CustomCI = IRB.CreateCall(CustomF, Args);
1502 CustomCI->setCallingConv(CI->getCallingConv());
1503 CustomCI->setAttributes(CI->getAttributes());
1505 if (!FT->getReturnType()->isVoidTy()) {
1506 LoadInst *LabelLoad = IRB.CreateLoad(DFSF.LabelReturnAlloca);
1507 DFSF.setShadow(CustomCI, LabelLoad);
1510 CI->replaceAllUsesWith(CustomCI);
1511 CI->eraseFromParent();
1519 FunctionType *FT = cast<FunctionType>(
1520 CS.getCalledValue()->getType()->getPointerElementType());
1521 if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_TLS) {
1522 for (unsigned i = 0, n = FT->getNumParams(); i != n; ++i) {
1523 IRB.CreateStore(DFSF.getShadow(CS.getArgument(i)),
1524 DFSF.getArgTLS(i, CS.getInstruction()));
1528 Instruction *Next = nullptr;
1529 if (!CS.getType()->isVoidTy()) {
1530 if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
1531 if (II->getNormalDest()->getSinglePredecessor()) {
1532 Next = II->getNormalDest()->begin();
1535 SplitEdge(II->getParent(), II->getNormalDest(), &DFSF.DT);
1536 Next = NewBB->begin();
1539 Next = CS->getNextNode();
1542 if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_TLS) {
1543 IRBuilder<> NextIRB(Next);
1544 LoadInst *LI = NextIRB.CreateLoad(DFSF.getRetvalTLS());
1545 DFSF.SkipInsts.insert(LI);
1546 DFSF.setShadow(CS.getInstruction(), LI);
1547 DFSF.NonZeroChecks.push_back(LI);
1551 // Do all instrumentation for IA_Args down here to defer tampering with the
1552 // CFG in a way that SplitEdge may be able to detect.
1553 if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_Args) {
1554 FunctionType *NewFT = DFSF.DFS.getArgsFunctionType(FT);
1556 IRB.CreateBitCast(CS.getCalledValue(), PointerType::getUnqual(NewFT));
1557 std::vector<Value *> Args;
1559 CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1560 for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
1564 for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
1565 Args.push_back(DFSF.getShadow(*i));
1567 if (FT->isVarArg()) {
1568 unsigned VarArgSize = CS.arg_size() - FT->getNumParams();
1569 ArrayType *VarArgArrayTy = ArrayType::get(DFSF.DFS.ShadowTy, VarArgSize);
1570 AllocaInst *VarArgShadow =
1571 new AllocaInst(VarArgArrayTy, "", DFSF.F->getEntryBlock().begin());
1572 Args.push_back(IRB.CreateConstGEP2_32(VarArgArrayTy, VarArgShadow, 0, 0));
1573 for (unsigned n = 0; i != e; ++i, ++n) {
1576 IRB.CreateConstGEP2_32(VarArgArrayTy, VarArgShadow, 0, n));
1582 if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
1583 NewCS = IRB.CreateInvoke(Func, II->getNormalDest(), II->getUnwindDest(),
1586 NewCS = IRB.CreateCall(Func, Args);
1588 NewCS.setCallingConv(CS.getCallingConv());
1589 NewCS.setAttributes(CS.getAttributes().removeAttributes(
1590 *DFSF.DFS.Ctx, AttributeSet::ReturnIndex,
1591 AttributeFuncs::typeIncompatible(NewCS.getInstruction()->getType())));
1594 ExtractValueInst *ExVal =
1595 ExtractValueInst::Create(NewCS.getInstruction(), 0, "", Next);
1596 DFSF.SkipInsts.insert(ExVal);
1597 ExtractValueInst *ExShadow =
1598 ExtractValueInst::Create(NewCS.getInstruction(), 1, "", Next);
1599 DFSF.SkipInsts.insert(ExShadow);
1600 DFSF.setShadow(ExVal, ExShadow);
1601 DFSF.NonZeroChecks.push_back(ExShadow);
1603 CS.getInstruction()->replaceAllUsesWith(ExVal);
1606 CS.getInstruction()->eraseFromParent();
1610 void DFSanVisitor::visitPHINode(PHINode &PN) {
1612 PHINode::Create(DFSF.DFS.ShadowTy, PN.getNumIncomingValues(), "", &PN);
1614 // Give the shadow phi node valid predecessors to fool SplitEdge into working.
1615 Value *UndefShadow = UndefValue::get(DFSF.DFS.ShadowTy);
1616 for (PHINode::block_iterator i = PN.block_begin(), e = PN.block_end(); i != e;
1618 ShadowPN->addIncoming(UndefShadow, *i);
1621 DFSF.PHIFixups.push_back(std::make_pair(&PN, ShadowPN));
1622 DFSF.setShadow(&PN, ShadowPN);