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/Analysis/ValueTracking.h"
52 #include "llvm/IR/InlineAsm.h"
53 #include "llvm/IR/IRBuilder.h"
54 #include "llvm/IR/LLVMContext.h"
55 #include "llvm/IR/MDBuilder.h"
56 #include "llvm/IR/Type.h"
57 #include "llvm/IR/Value.h"
58 #include "llvm/InstVisitor.h"
59 #include "llvm/Pass.h"
60 #include "llvm/Support/CommandLine.h"
61 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
62 #include "llvm/Transforms/Utils/Local.h"
63 #include "llvm/Transforms/Utils/SpecialCaseList.h"
68 // The -dfsan-preserve-alignment flag controls whether this pass assumes that
69 // alignment requirements provided by the input IR are correct. For example,
70 // if the input IR contains a load with alignment 8, this flag will cause
71 // the shadow load to have alignment 16. This flag is disabled by default as
72 // we have unfortunately encountered too much code (including Clang itself;
73 // see PR14291) which performs misaligned access.
74 static cl::opt<bool> ClPreserveAlignment(
75 "dfsan-preserve-alignment",
76 cl::desc("respect alignment requirements provided by input IR"), cl::Hidden,
79 // The ABI list file controls how shadow parameters are passed. The pass treats
80 // every function labelled "uninstrumented" in the ABI list file as conforming
81 // to the "native" (i.e. unsanitized) ABI. Unless the ABI list contains
82 // additional annotations for those functions, a call to one of those functions
83 // will produce a warning message, as the labelling behaviour of the function is
84 // unknown. The other supported annotations are "functional" and "discard",
85 // which are described below under DataFlowSanitizer::WrapperKind.
86 static cl::opt<std::string> ClABIListFile(
88 cl::desc("File listing native ABI functions and how the pass treats them"),
91 // Controls whether the pass uses IA_Args or IA_TLS as the ABI for instrumented
92 // functions (see DataFlowSanitizer::InstrumentedABI below).
93 static cl::opt<bool> ClArgsABI(
95 cl::desc("Use the argument ABI rather than the TLS ABI"),
100 class DataFlowSanitizer : public ModulePass {
101 friend struct DFSanFunction;
102 friend class DFSanVisitor;
108 /// Which ABI should be used for instrumented functions?
109 enum InstrumentedABI {
110 /// Argument and return value labels are passed through additional
111 /// arguments and by modifying the return type.
114 /// Argument and return value labels are passed through TLS variables
115 /// __dfsan_arg_tls and __dfsan_retval_tls.
119 /// How should calls to uninstrumented functions be handled?
121 /// This function is present in an uninstrumented form but we don't know
122 /// how it should be handled. Print a warning and call the function anyway.
123 /// Don't label the return value.
126 /// This function does not write to (user-accessible) memory, and its return
127 /// value is unlabelled.
130 /// This function does not write to (user-accessible) memory, and the label
131 /// of its return value is the union of the label of its arguments.
134 /// Instead of calling the function, a custom wrapper __dfsw_F is called,
135 /// where F is the name of the function. This function may wrap the
136 /// original function or provide its own implementation. This is similar to
137 /// the IA_Args ABI, except that IA_Args uses a struct return type to
138 /// pass the return value shadow in a register, while WK_Custom uses an
139 /// extra pointer argument to return the shadow. This allows the wrapped
140 /// form of the function type to be expressed in C.
147 IntegerType *ShadowTy;
148 PointerType *ShadowPtrTy;
149 IntegerType *IntptrTy;
150 ConstantInt *ZeroShadow;
151 ConstantInt *ShadowPtrMask;
152 ConstantInt *ShadowPtrMul;
155 void *(*GetArgTLSPtr)();
156 void *(*GetRetvalTLSPtr)();
158 Constant *GetRetvalTLS;
159 FunctionType *DFSanUnionFnTy;
160 FunctionType *DFSanUnionLoadFnTy;
161 FunctionType *DFSanUnimplementedFnTy;
162 Constant *DFSanUnionFn;
163 Constant *DFSanUnionLoadFn;
164 Constant *DFSanUnimplementedFn;
165 MDNode *ColdCallWeights;
166 OwningPtr<SpecialCaseList> ABIList;
167 DenseMap<Value *, Function *> UnwrappedFnMap;
168 AttributeSet ReadOnlyNoneAttrs;
170 Value *getShadowAddress(Value *Addr, Instruction *Pos);
171 Value *combineShadows(Value *V1, Value *V2, Instruction *Pos);
172 bool isInstrumented(Function *F);
173 FunctionType *getArgsFunctionType(FunctionType *T);
174 FunctionType *getCustomFunctionType(FunctionType *T);
175 InstrumentedABI getInstrumentedABI();
176 WrapperKind getWrapperKind(Function *F);
179 DataFlowSanitizer(StringRef ABIListFile = StringRef(),
180 void *(*getArgTLS)() = 0, void *(*getRetValTLS)() = 0);
182 bool doInitialization(Module &M);
183 bool runOnModule(Module &M);
186 struct DFSanFunction {
187 DataFlowSanitizer &DFS;
189 DataFlowSanitizer::InstrumentedABI IA;
193 AllocaInst *LabelReturnAlloca;
194 DenseMap<Value *, Value *> ValShadowMap;
195 DenseMap<AllocaInst *, AllocaInst *> AllocaShadowMap;
196 std::vector<std::pair<PHINode *, PHINode *> > PHIFixups;
197 DenseSet<Instruction *> SkipInsts;
199 DFSanFunction(DataFlowSanitizer &DFS, Function *F, bool IsNativeABI)
200 : DFS(DFS), F(F), IA(DFS.getInstrumentedABI()),
201 IsNativeABI(IsNativeABI), ArgTLSPtr(0), RetvalTLSPtr(0),
202 LabelReturnAlloca(0) {}
203 Value *getArgTLSPtr();
204 Value *getArgTLS(unsigned Index, Instruction *Pos);
205 Value *getRetvalTLS();
206 Value *getShadow(Value *V);
207 void setShadow(Instruction *I, Value *Shadow);
208 Value *combineOperandShadows(Instruction *Inst);
209 Value *loadShadow(Value *ShadowAddr, uint64_t Size, uint64_t Align,
211 void storeShadow(Value *Addr, uint64_t Size, uint64_t Align, Value *Shadow,
215 class DFSanVisitor : public InstVisitor<DFSanVisitor> {
218 DFSanVisitor(DFSanFunction &DFSF) : DFSF(DFSF) {}
220 void visitOperandShadowInst(Instruction &I);
222 void visitBinaryOperator(BinaryOperator &BO);
223 void visitCastInst(CastInst &CI);
224 void visitCmpInst(CmpInst &CI);
225 void visitGetElementPtrInst(GetElementPtrInst &GEPI);
226 void visitLoadInst(LoadInst &LI);
227 void visitStoreInst(StoreInst &SI);
228 void visitReturnInst(ReturnInst &RI);
229 void visitCallSite(CallSite CS);
230 void visitPHINode(PHINode &PN);
231 void visitExtractElementInst(ExtractElementInst &I);
232 void visitInsertElementInst(InsertElementInst &I);
233 void visitShuffleVectorInst(ShuffleVectorInst &I);
234 void visitExtractValueInst(ExtractValueInst &I);
235 void visitInsertValueInst(InsertValueInst &I);
236 void visitAllocaInst(AllocaInst &I);
237 void visitSelectInst(SelectInst &I);
238 void visitMemTransferInst(MemTransferInst &I);
243 char DataFlowSanitizer::ID;
244 INITIALIZE_PASS(DataFlowSanitizer, "dfsan",
245 "DataFlowSanitizer: dynamic data flow analysis.", false, false)
247 ModulePass *llvm::createDataFlowSanitizerPass(StringRef ABIListFile,
248 void *(*getArgTLS)(),
249 void *(*getRetValTLS)()) {
250 return new DataFlowSanitizer(ABIListFile, getArgTLS, getRetValTLS);
253 DataFlowSanitizer::DataFlowSanitizer(StringRef ABIListFile,
254 void *(*getArgTLS)(),
255 void *(*getRetValTLS)())
256 : ModulePass(ID), GetArgTLSPtr(getArgTLS), GetRetvalTLSPtr(getRetValTLS),
257 ABIList(SpecialCaseList::createOrDie(ABIListFile.empty() ? ClABIListFile
261 FunctionType *DataFlowSanitizer::getArgsFunctionType(FunctionType *T) {
262 llvm::SmallVector<Type *, 4> ArgTypes;
263 std::copy(T->param_begin(), T->param_end(), std::back_inserter(ArgTypes));
264 for (unsigned i = 0, e = T->getNumParams(); i != e; ++i)
265 ArgTypes.push_back(ShadowTy);
267 ArgTypes.push_back(ShadowPtrTy);
268 Type *RetType = T->getReturnType();
269 if (!RetType->isVoidTy())
270 RetType = StructType::get(RetType, ShadowTy, (Type *)0);
271 return FunctionType::get(RetType, ArgTypes, T->isVarArg());
274 FunctionType *DataFlowSanitizer::getCustomFunctionType(FunctionType *T) {
275 assert(!T->isVarArg());
276 llvm::SmallVector<Type *, 4> ArgTypes;
277 std::copy(T->param_begin(), T->param_end(), std::back_inserter(ArgTypes));
278 for (unsigned i = 0, e = T->getNumParams(); i != e; ++i)
279 ArgTypes.push_back(ShadowTy);
280 Type *RetType = T->getReturnType();
281 if (!RetType->isVoidTy())
282 ArgTypes.push_back(ShadowPtrTy);
283 return FunctionType::get(T->getReturnType(), ArgTypes, false);
286 bool DataFlowSanitizer::doInitialization(Module &M) {
287 DL = getAnalysisIfAvailable<DataLayout>();
292 Ctx = &M.getContext();
293 ShadowTy = IntegerType::get(*Ctx, ShadowWidth);
294 ShadowPtrTy = PointerType::getUnqual(ShadowTy);
295 IntptrTy = DL->getIntPtrType(*Ctx);
296 ZeroShadow = ConstantInt::getSigned(ShadowTy, 0);
297 ShadowPtrMask = ConstantInt::getSigned(IntptrTy, ~0x700000000000LL);
298 ShadowPtrMul = ConstantInt::getSigned(IntptrTy, ShadowWidth / 8);
300 Type *DFSanUnionArgs[2] = { ShadowTy, ShadowTy };
302 FunctionType::get(ShadowTy, DFSanUnionArgs, /*isVarArg=*/ false);
303 Type *DFSanUnionLoadArgs[2] = { ShadowPtrTy, IntptrTy };
305 FunctionType::get(ShadowTy, DFSanUnionLoadArgs, /*isVarArg=*/ false);
306 DFSanUnimplementedFnTy = FunctionType::get(
307 Type::getVoidTy(*Ctx), Type::getInt8PtrTy(*Ctx), /*isVarArg=*/false);
310 Type *ArgTLSTy = ArrayType::get(ShadowTy, 64);
312 GetArgTLS = ConstantExpr::getIntToPtr(
313 ConstantInt::get(IntptrTy, uintptr_t(GetArgTLSPtr)),
314 PointerType::getUnqual(
315 FunctionType::get(PointerType::getUnqual(ArgTLSTy), (Type *)0)));
317 if (GetRetvalTLSPtr) {
319 GetRetvalTLS = ConstantExpr::getIntToPtr(
320 ConstantInt::get(IntptrTy, uintptr_t(GetRetvalTLSPtr)),
321 PointerType::getUnqual(
322 FunctionType::get(PointerType::getUnqual(ShadowTy), (Type *)0)));
325 ColdCallWeights = MDBuilder(*Ctx).createBranchWeights(1, 1000);
329 bool DataFlowSanitizer::isInstrumented(Function *F) {
330 return !ABIList->isIn(*F, "uninstrumented");
333 DataFlowSanitizer::InstrumentedABI DataFlowSanitizer::getInstrumentedABI() {
334 return ClArgsABI ? IA_Args : IA_TLS;
337 DataFlowSanitizer::WrapperKind DataFlowSanitizer::getWrapperKind(Function *F) {
338 if (ABIList->isIn(*F, "functional"))
339 return WK_Functional;
340 if (ABIList->isIn(*F, "discard"))
342 if (ABIList->isIn(*F, "custom"))
348 bool DataFlowSanitizer::runOnModule(Module &M) {
352 if (ABIList->isIn(M, "skip"))
356 Type *ArgTLSTy = ArrayType::get(ShadowTy, 64);
357 ArgTLS = Mod->getOrInsertGlobal("__dfsan_arg_tls", ArgTLSTy);
358 if (GlobalVariable *G = dyn_cast<GlobalVariable>(ArgTLS))
359 G->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
361 if (!GetRetvalTLSPtr) {
362 RetvalTLS = Mod->getOrInsertGlobal("__dfsan_retval_tls", ShadowTy);
363 if (GlobalVariable *G = dyn_cast<GlobalVariable>(RetvalTLS))
364 G->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
367 DFSanUnionFn = Mod->getOrInsertFunction("__dfsan_union", DFSanUnionFnTy);
368 if (Function *F = dyn_cast<Function>(DFSanUnionFn)) {
369 F->addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
370 F->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
371 F->addAttribute(1, Attribute::ZExt);
372 F->addAttribute(2, Attribute::ZExt);
375 Mod->getOrInsertFunction("__dfsan_union_load", DFSanUnionLoadFnTy);
376 if (Function *F = dyn_cast<Function>(DFSanUnionLoadFn)) {
377 F->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
379 DFSanUnimplementedFn =
380 Mod->getOrInsertFunction("__dfsan_unimplemented", DFSanUnimplementedFnTy);
382 std::vector<Function *> FnsToInstrument;
383 llvm::SmallPtrSet<Function *, 2> FnsWithNativeABI;
384 for (Module::iterator i = M.begin(), e = M.end(); i != e; ++i) {
385 if (!i->isIntrinsic() &&
387 i != DFSanUnionLoadFn &&
388 i != DFSanUnimplementedFn)
389 FnsToInstrument.push_back(&*i);
393 B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
394 ReadOnlyNoneAttrs = AttributeSet::get(*Ctx, AttributeSet::FunctionIndex, B);
396 // First, change the ABI of every function in the module. ABI-listed
397 // functions keep their original ABI and get a wrapper function.
398 for (std::vector<Function *>::iterator i = FnsToInstrument.begin(),
399 e = FnsToInstrument.end();
402 FunctionType *FT = F.getFunctionType();
404 if (FT->getNumParams() == 0 && !FT->isVarArg() &&
405 FT->getReturnType()->isVoidTy())
408 if (isInstrumented(&F)) {
409 if (getInstrumentedABI() == IA_Args) {
410 FunctionType *NewFT = getArgsFunctionType(FT);
411 Function *NewF = Function::Create(NewFT, F.getLinkage(), "", &M);
412 NewF->copyAttributesFrom(&F);
413 NewF->removeAttributes(
414 AttributeSet::ReturnIndex,
415 AttributeFuncs::typeIncompatible(NewFT->getReturnType(),
416 AttributeSet::ReturnIndex));
417 for (Function::arg_iterator FArg = F.arg_begin(),
418 NewFArg = NewF->arg_begin(),
419 FArgEnd = F.arg_end();
420 FArg != FArgEnd; ++FArg, ++NewFArg) {
421 FArg->replaceAllUsesWith(NewFArg);
423 NewF->getBasicBlockList().splice(NewF->begin(), F.getBasicBlockList());
425 for (Function::use_iterator ui = F.use_begin(), ue = F.use_end();
427 BlockAddress *BA = dyn_cast<BlockAddress>(ui.getUse().getUser());
430 BA->replaceAllUsesWith(
431 BlockAddress::get(NewF, BA->getBasicBlock()));
435 F.replaceAllUsesWith(
436 ConstantExpr::getBitCast(NewF, PointerType::getUnqual(FT)));
441 // Hopefully, nobody will try to indirectly call a vararg
443 } else if (FT->isVarArg()) {
444 UnwrappedFnMap[&F] = &F;
447 // Build a wrapper function for F. The wrapper simply calls F, and is
448 // added to FnsToInstrument so that any instrumentation according to its
449 // WrapperKind is done in the second pass below.
450 FunctionType *NewFT = getInstrumentedABI() == IA_Args
451 ? getArgsFunctionType(FT)
454 Function::Create(NewFT, GlobalValue::LinkOnceODRLinkage,
455 std::string("dfsw$") + F.getName(), &M);
456 NewF->copyAttributesFrom(&F);
457 NewF->removeAttributes(
458 AttributeSet::ReturnIndex,
459 AttributeFuncs::typeIncompatible(NewFT->getReturnType(),
460 AttributeSet::ReturnIndex));
461 if (getInstrumentedABI() == IA_TLS)
462 NewF->removeAttributes(AttributeSet::FunctionIndex,
465 BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", NewF);
466 std::vector<Value *> Args;
467 unsigned n = FT->getNumParams();
468 for (Function::arg_iterator ai = NewF->arg_begin(); n != 0; ++ai, --n)
469 Args.push_back(&*ai);
470 CallInst *CI = CallInst::Create(&F, Args, "", BB);
471 if (FT->getReturnType()->isVoidTy())
472 ReturnInst::Create(*Ctx, BB);
474 ReturnInst::Create(*Ctx, CI, BB);
476 Value *WrappedFnCst =
477 ConstantExpr::getBitCast(NewF, PointerType::getUnqual(FT));
478 F.replaceAllUsesWith(WrappedFnCst);
479 UnwrappedFnMap[WrappedFnCst] = &F;
482 if (!F.isDeclaration()) {
483 // This function is probably defining an interposition of an
484 // uninstrumented function and hence needs to keep the original ABI.
485 // But any functions it may call need to use the instrumented ABI, so
486 // we instrument it in a mode which preserves the original ABI.
487 FnsWithNativeABI.insert(&F);
489 // This code needs to rebuild the iterators, as they may be invalidated
490 // by the push_back, taking care that the new range does not include
491 // any functions added by this code.
492 size_t N = i - FnsToInstrument.begin(),
493 Count = e - FnsToInstrument.begin();
494 FnsToInstrument.push_back(&F);
495 i = FnsToInstrument.begin() + N;
496 e = FnsToInstrument.begin() + Count;
501 for (std::vector<Function *>::iterator i = FnsToInstrument.begin(),
502 e = FnsToInstrument.end();
504 if (!*i || (*i)->isDeclaration())
507 removeUnreachableBlocks(**i);
509 DFSanFunction DFSF(*this, *i, FnsWithNativeABI.count(*i));
511 // DFSanVisitor may create new basic blocks, which confuses df_iterator.
512 // Build a copy of the list before iterating over it.
513 llvm::SmallVector<BasicBlock *, 4> BBList;
514 std::copy(df_begin(&(*i)->getEntryBlock()), df_end(&(*i)->getEntryBlock()),
515 std::back_inserter(BBList));
517 for (llvm::SmallVector<BasicBlock *, 4>::iterator i = BBList.begin(),
520 Instruction *Inst = &(*i)->front();
522 // DFSanVisitor may split the current basic block, changing the current
523 // instruction's next pointer and moving the next instruction to the
524 // tail block from which we should continue.
525 Instruction *Next = Inst->getNextNode();
526 // DFSanVisitor may delete Inst, so keep track of whether it was a
528 bool IsTerminator = isa<TerminatorInst>(Inst);
529 if (!DFSF.SkipInsts.count(Inst))
530 DFSanVisitor(DFSF).visit(Inst);
537 // We will not necessarily be able to compute the shadow for every phi node
538 // until we have visited every block. Therefore, the code that handles phi
539 // nodes adds them to the PHIFixups list so that they can be properly
541 for (std::vector<std::pair<PHINode *, PHINode *> >::iterator
542 i = DFSF.PHIFixups.begin(),
543 e = DFSF.PHIFixups.end();
545 for (unsigned val = 0, n = i->first->getNumIncomingValues(); val != n;
547 i->second->setIncomingValue(
548 val, DFSF.getShadow(i->first->getIncomingValue(val)));
556 Value *DFSanFunction::getArgTLSPtr() {
560 return ArgTLSPtr = DFS.ArgTLS;
562 IRBuilder<> IRB(F->getEntryBlock().begin());
563 return ArgTLSPtr = IRB.CreateCall(DFS.GetArgTLS);
566 Value *DFSanFunction::getRetvalTLS() {
570 return RetvalTLSPtr = DFS.RetvalTLS;
572 IRBuilder<> IRB(F->getEntryBlock().begin());
573 return RetvalTLSPtr = IRB.CreateCall(DFS.GetRetvalTLS);
576 Value *DFSanFunction::getArgTLS(unsigned Idx, Instruction *Pos) {
577 IRBuilder<> IRB(Pos);
578 return IRB.CreateConstGEP2_64(getArgTLSPtr(), 0, Idx);
581 Value *DFSanFunction::getShadow(Value *V) {
582 if (!isa<Argument>(V) && !isa<Instruction>(V))
583 return DFS.ZeroShadow;
584 Value *&Shadow = ValShadowMap[V];
586 if (Argument *A = dyn_cast<Argument>(V)) {
588 return DFS.ZeroShadow;
590 case DataFlowSanitizer::IA_TLS: {
591 Value *ArgTLSPtr = getArgTLSPtr();
592 Instruction *ArgTLSPos =
593 DFS.ArgTLS ? &*F->getEntryBlock().begin()
594 : cast<Instruction>(ArgTLSPtr)->getNextNode();
595 IRBuilder<> IRB(ArgTLSPos);
596 Shadow = IRB.CreateLoad(getArgTLS(A->getArgNo(), ArgTLSPos));
599 case DataFlowSanitizer::IA_Args: {
600 unsigned ArgIdx = A->getArgNo() + F->getArgumentList().size() / 2;
601 Function::arg_iterator i = F->arg_begin();
605 assert(Shadow->getType() == DFS.ShadowTy);
610 Shadow = DFS.ZeroShadow;
616 void DFSanFunction::setShadow(Instruction *I, Value *Shadow) {
617 assert(!ValShadowMap.count(I));
618 assert(Shadow->getType() == DFS.ShadowTy);
619 ValShadowMap[I] = Shadow;
622 Value *DataFlowSanitizer::getShadowAddress(Value *Addr, Instruction *Pos) {
623 assert(Addr != RetvalTLS && "Reinstrumenting?");
624 IRBuilder<> IRB(Pos);
625 return IRB.CreateIntToPtr(
627 IRB.CreateAnd(IRB.CreatePtrToInt(Addr, IntptrTy), ShadowPtrMask),
632 // Generates IR to compute the union of the two given shadows, inserting it
633 // before Pos. Returns the computed union Value.
634 Value *DataFlowSanitizer::combineShadows(Value *V1, Value *V2,
636 if (V1 == ZeroShadow)
638 if (V2 == ZeroShadow)
642 IRBuilder<> IRB(Pos);
643 BasicBlock *Head = Pos->getParent();
644 Value *Ne = IRB.CreateICmpNE(V1, V2);
645 Instruction *NeInst = dyn_cast<Instruction>(Ne);
647 BranchInst *BI = cast<BranchInst>(SplitBlockAndInsertIfThen(
648 NeInst, /*Unreachable=*/ false, ColdCallWeights));
649 IRBuilder<> ThenIRB(BI);
650 CallInst *Call = ThenIRB.CreateCall2(DFSanUnionFn, V1, V2);
651 Call->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
652 Call->addAttribute(1, Attribute::ZExt);
653 Call->addAttribute(2, Attribute::ZExt);
655 BasicBlock *Tail = BI->getSuccessor(0);
656 PHINode *Phi = PHINode::Create(ShadowTy, 2, "", Tail->begin());
657 Phi->addIncoming(Call, Call->getParent());
658 Phi->addIncoming(ZeroShadow, Head);
667 // A convenience function which folds the shadows of each of the operands
668 // of the provided instruction Inst, inserting the IR before Inst. Returns
669 // the computed union Value.
670 Value *DFSanFunction::combineOperandShadows(Instruction *Inst) {
671 if (Inst->getNumOperands() == 0)
672 return DFS.ZeroShadow;
674 Value *Shadow = getShadow(Inst->getOperand(0));
675 for (unsigned i = 1, n = Inst->getNumOperands(); i != n; ++i) {
676 Shadow = DFS.combineShadows(Shadow, getShadow(Inst->getOperand(i)), Inst);
681 void DFSanVisitor::visitOperandShadowInst(Instruction &I) {
682 Value *CombinedShadow = DFSF.combineOperandShadows(&I);
683 DFSF.setShadow(&I, CombinedShadow);
686 // Generates IR to load shadow corresponding to bytes [Addr, Addr+Size), where
687 // Addr has alignment Align, and take the union of each of those shadows.
688 Value *DFSanFunction::loadShadow(Value *Addr, uint64_t Size, uint64_t Align,
690 if (AllocaInst *AI = dyn_cast<AllocaInst>(Addr)) {
691 llvm::DenseMap<AllocaInst *, AllocaInst *>::iterator i =
692 AllocaShadowMap.find(AI);
693 if (i != AllocaShadowMap.end()) {
694 IRBuilder<> IRB(Pos);
695 return IRB.CreateLoad(i->second);
699 uint64_t ShadowAlign = Align * DFS.ShadowWidth / 8;
700 SmallVector<Value *, 2> Objs;
701 GetUnderlyingObjects(Addr, Objs, DFS.DL);
702 bool AllConstants = true;
703 for (SmallVector<Value *, 2>::iterator i = Objs.begin(), e = Objs.end();
705 if (isa<Function>(*i) || isa<BlockAddress>(*i))
707 if (isa<GlobalVariable>(*i) && cast<GlobalVariable>(*i)->isConstant())
710 AllConstants = false;
714 return DFS.ZeroShadow;
716 Value *ShadowAddr = DFS.getShadowAddress(Addr, Pos);
719 return DFS.ZeroShadow;
721 LoadInst *LI = new LoadInst(ShadowAddr, "", Pos);
722 LI->setAlignment(ShadowAlign);
726 IRBuilder<> IRB(Pos);
728 IRB.CreateGEP(ShadowAddr, ConstantInt::get(DFS.IntptrTy, 1));
729 return DFS.combineShadows(IRB.CreateAlignedLoad(ShadowAddr, ShadowAlign),
730 IRB.CreateAlignedLoad(ShadowAddr1, ShadowAlign),
734 if (Size % (64 / DFS.ShadowWidth) == 0) {
735 // Fast path for the common case where each byte has identical shadow: load
736 // shadow 64 bits at a time, fall out to a __dfsan_union_load call if any
737 // shadow is non-equal.
738 BasicBlock *FallbackBB = BasicBlock::Create(*DFS.Ctx, "", F);
739 IRBuilder<> FallbackIRB(FallbackBB);
740 CallInst *FallbackCall = FallbackIRB.CreateCall2(
741 DFS.DFSanUnionLoadFn, ShadowAddr, ConstantInt::get(DFS.IntptrTy, Size));
742 FallbackCall->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
744 // Compare each of the shadows stored in the loaded 64 bits to each other,
745 // by computing (WideShadow rotl ShadowWidth) == WideShadow.
746 IRBuilder<> IRB(Pos);
748 IRB.CreateBitCast(ShadowAddr, Type::getInt64PtrTy(*DFS.Ctx));
749 Value *WideShadow = IRB.CreateAlignedLoad(WideAddr, ShadowAlign);
750 Value *TruncShadow = IRB.CreateTrunc(WideShadow, DFS.ShadowTy);
751 Value *ShlShadow = IRB.CreateShl(WideShadow, DFS.ShadowWidth);
752 Value *ShrShadow = IRB.CreateLShr(WideShadow, 64 - DFS.ShadowWidth);
753 Value *RotShadow = IRB.CreateOr(ShlShadow, ShrShadow);
754 Value *ShadowsEq = IRB.CreateICmpEQ(WideShadow, RotShadow);
756 BasicBlock *Head = Pos->getParent();
757 BasicBlock *Tail = Head->splitBasicBlock(Pos);
758 // In the following code LastBr will refer to the previous basic block's
759 // conditional branch instruction, whose true successor is fixed up to point
760 // to the next block during the loop below or to the tail after the final
762 BranchInst *LastBr = BranchInst::Create(FallbackBB, FallbackBB, ShadowsEq);
763 ReplaceInstWithInst(Head->getTerminator(), LastBr);
765 for (uint64_t Ofs = 64 / DFS.ShadowWidth; Ofs != Size;
766 Ofs += 64 / DFS.ShadowWidth) {
767 BasicBlock *NextBB = BasicBlock::Create(*DFS.Ctx, "", F);
768 IRBuilder<> NextIRB(NextBB);
769 WideAddr = NextIRB.CreateGEP(WideAddr, ConstantInt::get(DFS.IntptrTy, 1));
770 Value *NextWideShadow = NextIRB.CreateAlignedLoad(WideAddr, ShadowAlign);
771 ShadowsEq = NextIRB.CreateICmpEQ(WideShadow, NextWideShadow);
772 LastBr->setSuccessor(0, NextBB);
773 LastBr = NextIRB.CreateCondBr(ShadowsEq, FallbackBB, FallbackBB);
776 LastBr->setSuccessor(0, Tail);
777 FallbackIRB.CreateBr(Tail);
778 PHINode *Shadow = PHINode::Create(DFS.ShadowTy, 2, "", &Tail->front());
779 Shadow->addIncoming(FallbackCall, FallbackBB);
780 Shadow->addIncoming(TruncShadow, LastBr->getParent());
784 IRBuilder<> IRB(Pos);
785 CallInst *FallbackCall = IRB.CreateCall2(
786 DFS.DFSanUnionLoadFn, ShadowAddr, ConstantInt::get(DFS.IntptrTy, Size));
787 FallbackCall->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
791 void DFSanVisitor::visitLoadInst(LoadInst &LI) {
792 uint64_t Size = DFSF.DFS.DL->getTypeStoreSize(LI.getType());
794 if (ClPreserveAlignment) {
795 Align = LI.getAlignment();
797 Align = DFSF.DFS.DL->getABITypeAlignment(LI.getType());
801 IRBuilder<> IRB(&LI);
802 Value *LoadedShadow =
803 DFSF.loadShadow(LI.getPointerOperand(), Size, Align, &LI);
804 Value *PtrShadow = DFSF.getShadow(LI.getPointerOperand());
805 DFSF.setShadow(&LI, DFSF.DFS.combineShadows(LoadedShadow, PtrShadow, &LI));
808 void DFSanFunction::storeShadow(Value *Addr, uint64_t Size, uint64_t Align,
809 Value *Shadow, Instruction *Pos) {
810 if (AllocaInst *AI = dyn_cast<AllocaInst>(Addr)) {
811 llvm::DenseMap<AllocaInst *, AllocaInst *>::iterator i =
812 AllocaShadowMap.find(AI);
813 if (i != AllocaShadowMap.end()) {
814 IRBuilder<> IRB(Pos);
815 IRB.CreateStore(Shadow, i->second);
820 uint64_t ShadowAlign = Align * DFS.ShadowWidth / 8;
821 IRBuilder<> IRB(Pos);
822 Value *ShadowAddr = DFS.getShadowAddress(Addr, Pos);
823 if (Shadow == DFS.ZeroShadow) {
824 IntegerType *ShadowTy = IntegerType::get(*DFS.Ctx, Size * DFS.ShadowWidth);
825 Value *ExtZeroShadow = ConstantInt::get(ShadowTy, 0);
826 Value *ExtShadowAddr =
827 IRB.CreateBitCast(ShadowAddr, PointerType::getUnqual(ShadowTy));
828 IRB.CreateAlignedStore(ExtZeroShadow, ExtShadowAddr, ShadowAlign);
832 const unsigned ShadowVecSize = 128 / DFS.ShadowWidth;
834 if (Size >= ShadowVecSize) {
835 VectorType *ShadowVecTy = VectorType::get(DFS.ShadowTy, ShadowVecSize);
836 Value *ShadowVec = UndefValue::get(ShadowVecTy);
837 for (unsigned i = 0; i != ShadowVecSize; ++i) {
838 ShadowVec = IRB.CreateInsertElement(
839 ShadowVec, Shadow, ConstantInt::get(Type::getInt32Ty(*DFS.Ctx), i));
841 Value *ShadowVecAddr =
842 IRB.CreateBitCast(ShadowAddr, PointerType::getUnqual(ShadowVecTy));
844 Value *CurShadowVecAddr = IRB.CreateConstGEP1_32(ShadowVecAddr, Offset);
845 IRB.CreateAlignedStore(ShadowVec, CurShadowVecAddr, ShadowAlign);
846 Size -= ShadowVecSize;
848 } while (Size >= ShadowVecSize);
849 Offset *= ShadowVecSize;
852 Value *CurShadowAddr = IRB.CreateConstGEP1_32(ShadowAddr, Offset);
853 IRB.CreateAlignedStore(Shadow, CurShadowAddr, ShadowAlign);
859 void DFSanVisitor::visitStoreInst(StoreInst &SI) {
861 DFSF.DFS.DL->getTypeStoreSize(SI.getValueOperand()->getType());
863 if (ClPreserveAlignment) {
864 Align = SI.getAlignment();
866 Align = DFSF.DFS.DL->getABITypeAlignment(SI.getValueOperand()->getType());
870 DFSF.storeShadow(SI.getPointerOperand(), Size, Align,
871 DFSF.getShadow(SI.getValueOperand()), &SI);
874 void DFSanVisitor::visitBinaryOperator(BinaryOperator &BO) {
875 visitOperandShadowInst(BO);
878 void DFSanVisitor::visitCastInst(CastInst &CI) { visitOperandShadowInst(CI); }
880 void DFSanVisitor::visitCmpInst(CmpInst &CI) { visitOperandShadowInst(CI); }
882 void DFSanVisitor::visitGetElementPtrInst(GetElementPtrInst &GEPI) {
883 visitOperandShadowInst(GEPI);
886 void DFSanVisitor::visitExtractElementInst(ExtractElementInst &I) {
887 visitOperandShadowInst(I);
890 void DFSanVisitor::visitInsertElementInst(InsertElementInst &I) {
891 visitOperandShadowInst(I);
894 void DFSanVisitor::visitShuffleVectorInst(ShuffleVectorInst &I) {
895 visitOperandShadowInst(I);
898 void DFSanVisitor::visitExtractValueInst(ExtractValueInst &I) {
899 visitOperandShadowInst(I);
902 void DFSanVisitor::visitInsertValueInst(InsertValueInst &I) {
903 visitOperandShadowInst(I);
906 void DFSanVisitor::visitAllocaInst(AllocaInst &I) {
907 bool AllLoadsStores = true;
908 for (Instruction::use_iterator i = I.use_begin(), e = I.use_end(); i != e;
910 if (isa<LoadInst>(*i))
913 if (StoreInst *SI = dyn_cast<StoreInst>(*i)) {
914 if (SI->getPointerOperand() == &I)
918 AllLoadsStores = false;
921 if (AllLoadsStores) {
923 DFSF.AllocaShadowMap[&I] = IRB.CreateAlloca(DFSF.DFS.ShadowTy);
925 DFSF.setShadow(&I, DFSF.DFS.ZeroShadow);
928 void DFSanVisitor::visitSelectInst(SelectInst &I) {
929 Value *CondShadow = DFSF.getShadow(I.getCondition());
930 Value *TrueShadow = DFSF.getShadow(I.getTrueValue());
931 Value *FalseShadow = DFSF.getShadow(I.getFalseValue());
933 if (isa<VectorType>(I.getCondition()->getType())) {
935 &I, DFSF.DFS.combineShadows(
937 DFSF.DFS.combineShadows(TrueShadow, FalseShadow, &I), &I));
940 if (TrueShadow == FalseShadow) {
941 ShadowSel = TrueShadow;
944 SelectInst::Create(I.getCondition(), TrueShadow, FalseShadow, "", &I);
946 DFSF.setShadow(&I, DFSF.DFS.combineShadows(CondShadow, ShadowSel, &I));
950 void DFSanVisitor::visitMemTransferInst(MemTransferInst &I) {
952 Value *DestShadow = DFSF.DFS.getShadowAddress(I.getDest(), &I);
953 Value *SrcShadow = DFSF.DFS.getShadowAddress(I.getSource(), &I);
954 Value *LenShadow = IRB.CreateMul(
956 ConstantInt::get(I.getLength()->getType(), DFSF.DFS.ShadowWidth / 8));
958 if (ClPreserveAlignment) {
959 AlignShadow = IRB.CreateMul(I.getAlignmentCst(),
960 ConstantInt::get(I.getAlignmentCst()->getType(),
961 DFSF.DFS.ShadowWidth / 8));
963 AlignShadow = ConstantInt::get(I.getAlignmentCst()->getType(),
964 DFSF.DFS.ShadowWidth / 8);
966 Type *Int8Ptr = Type::getInt8PtrTy(*DFSF.DFS.Ctx);
967 DestShadow = IRB.CreateBitCast(DestShadow, Int8Ptr);
968 SrcShadow = IRB.CreateBitCast(SrcShadow, Int8Ptr);
969 IRB.CreateCall5(I.getCalledValue(), DestShadow, SrcShadow, LenShadow,
970 AlignShadow, I.getVolatileCst());
973 void DFSanVisitor::visitReturnInst(ReturnInst &RI) {
974 if (!DFSF.IsNativeABI && RI.getReturnValue()) {
976 case DataFlowSanitizer::IA_TLS: {
977 Value *S = DFSF.getShadow(RI.getReturnValue());
978 IRBuilder<> IRB(&RI);
979 IRB.CreateStore(S, DFSF.getRetvalTLS());
982 case DataFlowSanitizer::IA_Args: {
983 IRBuilder<> IRB(&RI);
984 Type *RT = DFSF.F->getFunctionType()->getReturnType();
986 IRB.CreateInsertValue(UndefValue::get(RT), RI.getReturnValue(), 0);
988 IRB.CreateInsertValue(InsVal, DFSF.getShadow(RI.getReturnValue()), 1);
989 RI.setOperand(0, InsShadow);
996 void DFSanVisitor::visitCallSite(CallSite CS) {
997 Function *F = CS.getCalledFunction();
998 if ((F && F->isIntrinsic()) || isa<InlineAsm>(CS.getCalledValue())) {
999 visitOperandShadowInst(*CS.getInstruction());
1003 IRBuilder<> IRB(CS.getInstruction());
1005 DenseMap<Value *, Function *>::iterator i =
1006 DFSF.DFS.UnwrappedFnMap.find(CS.getCalledValue());
1007 if (i != DFSF.DFS.UnwrappedFnMap.end()) {
1008 Function *F = i->second;
1009 switch (DFSF.DFS.getWrapperKind(F)) {
1010 case DataFlowSanitizer::WK_Warning: {
1011 CS.setCalledFunction(F);
1012 IRB.CreateCall(DFSF.DFS.DFSanUnimplementedFn,
1013 IRB.CreateGlobalStringPtr(F->getName()));
1014 DFSF.setShadow(CS.getInstruction(), DFSF.DFS.ZeroShadow);
1017 case DataFlowSanitizer::WK_Discard: {
1018 CS.setCalledFunction(F);
1019 DFSF.setShadow(CS.getInstruction(), DFSF.DFS.ZeroShadow);
1022 case DataFlowSanitizer::WK_Functional: {
1023 CS.setCalledFunction(F);
1024 visitOperandShadowInst(*CS.getInstruction());
1027 case DataFlowSanitizer::WK_Custom: {
1028 // Don't try to handle invokes of custom functions, it's too complicated.
1029 // Instead, invoke the dfsw$ wrapper, which will in turn call the __dfsw_
1031 if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) {
1032 FunctionType *FT = F->getFunctionType();
1033 FunctionType *CustomFT = DFSF.DFS.getCustomFunctionType(FT);
1034 std::string CustomFName = "__dfsw_";
1035 CustomFName += F->getName();
1037 DFSF.DFS.Mod->getOrInsertFunction(CustomFName, CustomFT);
1038 if (Function *CustomFn = dyn_cast<Function>(CustomF)) {
1039 CustomFn->copyAttributesFrom(F);
1041 // Custom functions returning non-void will write to the return label.
1042 if (!FT->getReturnType()->isVoidTy()) {
1043 CustomFn->removeAttributes(AttributeSet::FunctionIndex,
1044 DFSF.DFS.ReadOnlyNoneAttrs);
1048 std::vector<Value *> Args;
1050 CallSite::arg_iterator i = CS.arg_begin();
1051 for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
1055 for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
1056 Args.push_back(DFSF.getShadow(*i));
1058 if (!FT->getReturnType()->isVoidTy()) {
1059 if (!DFSF.LabelReturnAlloca) {
1060 DFSF.LabelReturnAlloca =
1061 new AllocaInst(DFSF.DFS.ShadowTy, "labelreturn",
1062 DFSF.F->getEntryBlock().begin());
1064 Args.push_back(DFSF.LabelReturnAlloca);
1067 CallInst *CustomCI = IRB.CreateCall(CustomF, Args);
1068 CustomCI->setCallingConv(CI->getCallingConv());
1069 CustomCI->setAttributes(CI->getAttributes());
1071 if (!FT->getReturnType()->isVoidTy()) {
1072 LoadInst *LabelLoad = IRB.CreateLoad(DFSF.LabelReturnAlloca);
1073 DFSF.setShadow(CustomCI, LabelLoad);
1076 CI->replaceAllUsesWith(CustomCI);
1077 CI->eraseFromParent();
1085 FunctionType *FT = cast<FunctionType>(
1086 CS.getCalledValue()->getType()->getPointerElementType());
1087 if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_TLS) {
1088 for (unsigned i = 0, n = FT->getNumParams(); i != n; ++i) {
1089 IRB.CreateStore(DFSF.getShadow(CS.getArgument(i)),
1090 DFSF.getArgTLS(i, CS.getInstruction()));
1094 Instruction *Next = 0;
1095 if (!CS.getType()->isVoidTy()) {
1096 if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
1097 if (II->getNormalDest()->getSinglePredecessor()) {
1098 Next = II->getNormalDest()->begin();
1101 SplitEdge(II->getParent(), II->getNormalDest(), &DFSF.DFS);
1102 Next = NewBB->begin();
1105 Next = CS->getNextNode();
1108 if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_TLS) {
1109 IRBuilder<> NextIRB(Next);
1110 LoadInst *LI = NextIRB.CreateLoad(DFSF.getRetvalTLS());
1111 DFSF.SkipInsts.insert(LI);
1112 DFSF.setShadow(CS.getInstruction(), LI);
1116 // Do all instrumentation for IA_Args down here to defer tampering with the
1117 // CFG in a way that SplitEdge may be able to detect.
1118 if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_Args) {
1119 FunctionType *NewFT = DFSF.DFS.getArgsFunctionType(FT);
1121 IRB.CreateBitCast(CS.getCalledValue(), PointerType::getUnqual(NewFT));
1122 std::vector<Value *> Args;
1124 CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1125 for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
1129 for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
1130 Args.push_back(DFSF.getShadow(*i));
1132 if (FT->isVarArg()) {
1133 unsigned VarArgSize = CS.arg_size() - FT->getNumParams();
1134 ArrayType *VarArgArrayTy = ArrayType::get(DFSF.DFS.ShadowTy, VarArgSize);
1135 AllocaInst *VarArgShadow =
1136 new AllocaInst(VarArgArrayTy, "", DFSF.F->getEntryBlock().begin());
1137 Args.push_back(IRB.CreateConstGEP2_32(VarArgShadow, 0, 0));
1138 for (unsigned n = 0; i != e; ++i, ++n) {
1139 IRB.CreateStore(DFSF.getShadow(*i),
1140 IRB.CreateConstGEP2_32(VarArgShadow, 0, n));
1146 if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
1147 NewCS = IRB.CreateInvoke(Func, II->getNormalDest(), II->getUnwindDest(),
1150 NewCS = IRB.CreateCall(Func, Args);
1152 NewCS.setCallingConv(CS.getCallingConv());
1153 NewCS.setAttributes(CS.getAttributes().removeAttributes(
1154 *DFSF.DFS.Ctx, AttributeSet::ReturnIndex,
1155 AttributeFuncs::typeIncompatible(NewCS.getInstruction()->getType(),
1156 AttributeSet::ReturnIndex)));
1159 ExtractValueInst *ExVal =
1160 ExtractValueInst::Create(NewCS.getInstruction(), 0, "", Next);
1161 DFSF.SkipInsts.insert(ExVal);
1162 ExtractValueInst *ExShadow =
1163 ExtractValueInst::Create(NewCS.getInstruction(), 1, "", Next);
1164 DFSF.SkipInsts.insert(ExShadow);
1165 DFSF.setShadow(ExVal, ExShadow);
1167 CS.getInstruction()->replaceAllUsesWith(ExVal);
1170 CS.getInstruction()->eraseFromParent();
1174 void DFSanVisitor::visitPHINode(PHINode &PN) {
1176 PHINode::Create(DFSF.DFS.ShadowTy, PN.getNumIncomingValues(), "", &PN);
1178 // Give the shadow phi node valid predecessors to fool SplitEdge into working.
1179 Value *UndefShadow = UndefValue::get(DFSF.DFS.ShadowTy);
1180 for (PHINode::block_iterator i = PN.block_begin(), e = PN.block_end(); i != e;
1182 ShadowPN->addIncoming(UndefShadow, *i);
1185 DFSF.PHIFixups.push_back(std::make_pair(&PN, ShadowPN));
1186 DFSF.setShadow(&PN, ShadowPN);