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"),
98 static cl::opt<bool> ClDebugNonzeroLabels(
99 "dfsan-debug-nonzero-labels",
100 cl::desc("Insert calls to __dfsan_nonzero_label on observing a parameter, "
101 "load or return with a nonzero label"),
106 class DataFlowSanitizer : public ModulePass {
107 friend struct DFSanFunction;
108 friend class DFSanVisitor;
114 /// Which ABI should be used for instrumented functions?
115 enum InstrumentedABI {
116 /// Argument and return value labels are passed through additional
117 /// arguments and by modifying the return type.
120 /// Argument and return value labels are passed through TLS variables
121 /// __dfsan_arg_tls and __dfsan_retval_tls.
125 /// How should calls to uninstrumented functions be handled?
127 /// This function is present in an uninstrumented form but we don't know
128 /// how it should be handled. Print a warning and call the function anyway.
129 /// Don't label the return value.
132 /// This function does not write to (user-accessible) memory, and its return
133 /// value is unlabelled.
136 /// This function does not write to (user-accessible) memory, and the label
137 /// of its return value is the union of the label of its arguments.
140 /// Instead of calling the function, a custom wrapper __dfsw_F is called,
141 /// where F is the name of the function. This function may wrap the
142 /// original function or provide its own implementation. This is similar to
143 /// the IA_Args ABI, except that IA_Args uses a struct return type to
144 /// pass the return value shadow in a register, while WK_Custom uses an
145 /// extra pointer argument to return the shadow. This allows the wrapped
146 /// form of the function type to be expressed in C.
153 IntegerType *ShadowTy;
154 PointerType *ShadowPtrTy;
155 IntegerType *IntptrTy;
156 ConstantInt *ZeroShadow;
157 ConstantInt *ShadowPtrMask;
158 ConstantInt *ShadowPtrMul;
161 void *(*GetArgTLSPtr)();
162 void *(*GetRetvalTLSPtr)();
164 Constant *GetRetvalTLS;
165 FunctionType *DFSanUnionFnTy;
166 FunctionType *DFSanUnionLoadFnTy;
167 FunctionType *DFSanUnimplementedFnTy;
168 FunctionType *DFSanSetLabelFnTy;
169 FunctionType *DFSanNonzeroLabelFnTy;
170 Constant *DFSanUnionFn;
171 Constant *DFSanUnionLoadFn;
172 Constant *DFSanUnimplementedFn;
173 Constant *DFSanSetLabelFn;
174 Constant *DFSanNonzeroLabelFn;
175 MDNode *ColdCallWeights;
176 OwningPtr<SpecialCaseList> ABIList;
177 DenseMap<Value *, Function *> UnwrappedFnMap;
178 AttributeSet ReadOnlyNoneAttrs;
180 Value *getShadowAddress(Value *Addr, Instruction *Pos);
181 Value *combineShadows(Value *V1, Value *V2, Instruction *Pos);
182 bool isInstrumented(Function *F);
183 FunctionType *getArgsFunctionType(FunctionType *T);
184 FunctionType *getCustomFunctionType(FunctionType *T);
185 InstrumentedABI getInstrumentedABI();
186 WrapperKind getWrapperKind(Function *F);
189 DataFlowSanitizer(StringRef ABIListFile = StringRef(),
190 void *(*getArgTLS)() = 0, void *(*getRetValTLS)() = 0);
192 bool doInitialization(Module &M);
193 bool runOnModule(Module &M);
196 struct DFSanFunction {
197 DataFlowSanitizer &DFS;
199 DataFlowSanitizer::InstrumentedABI IA;
203 AllocaInst *LabelReturnAlloca;
204 DenseMap<Value *, Value *> ValShadowMap;
205 DenseMap<AllocaInst *, AllocaInst *> AllocaShadowMap;
206 std::vector<std::pair<PHINode *, PHINode *> > PHIFixups;
207 DenseSet<Instruction *> SkipInsts;
208 DenseSet<Value *> NonZeroChecks;
210 DFSanFunction(DataFlowSanitizer &DFS, Function *F, bool IsNativeABI)
211 : DFS(DFS), F(F), IA(DFS.getInstrumentedABI()),
212 IsNativeABI(IsNativeABI), ArgTLSPtr(0), RetvalTLSPtr(0),
213 LabelReturnAlloca(0) {}
214 Value *getArgTLSPtr();
215 Value *getArgTLS(unsigned Index, Instruction *Pos);
216 Value *getRetvalTLS();
217 Value *getShadow(Value *V);
218 void setShadow(Instruction *I, Value *Shadow);
219 Value *combineOperandShadows(Instruction *Inst);
220 Value *loadShadow(Value *ShadowAddr, uint64_t Size, uint64_t Align,
222 void storeShadow(Value *Addr, uint64_t Size, uint64_t Align, Value *Shadow,
226 class DFSanVisitor : public InstVisitor<DFSanVisitor> {
229 DFSanVisitor(DFSanFunction &DFSF) : DFSF(DFSF) {}
231 void visitOperandShadowInst(Instruction &I);
233 void visitBinaryOperator(BinaryOperator &BO);
234 void visitCastInst(CastInst &CI);
235 void visitCmpInst(CmpInst &CI);
236 void visitGetElementPtrInst(GetElementPtrInst &GEPI);
237 void visitLoadInst(LoadInst &LI);
238 void visitStoreInst(StoreInst &SI);
239 void visitReturnInst(ReturnInst &RI);
240 void visitCallSite(CallSite CS);
241 void visitPHINode(PHINode &PN);
242 void visitExtractElementInst(ExtractElementInst &I);
243 void visitInsertElementInst(InsertElementInst &I);
244 void visitShuffleVectorInst(ShuffleVectorInst &I);
245 void visitExtractValueInst(ExtractValueInst &I);
246 void visitInsertValueInst(InsertValueInst &I);
247 void visitAllocaInst(AllocaInst &I);
248 void visitSelectInst(SelectInst &I);
249 void visitMemSetInst(MemSetInst &I);
250 void visitMemTransferInst(MemTransferInst &I);
255 char DataFlowSanitizer::ID;
256 INITIALIZE_PASS(DataFlowSanitizer, "dfsan",
257 "DataFlowSanitizer: dynamic data flow analysis.", false, false)
259 ModulePass *llvm::createDataFlowSanitizerPass(StringRef ABIListFile,
260 void *(*getArgTLS)(),
261 void *(*getRetValTLS)()) {
262 return new DataFlowSanitizer(ABIListFile, getArgTLS, getRetValTLS);
265 DataFlowSanitizer::DataFlowSanitizer(StringRef ABIListFile,
266 void *(*getArgTLS)(),
267 void *(*getRetValTLS)())
268 : ModulePass(ID), GetArgTLSPtr(getArgTLS), GetRetvalTLSPtr(getRetValTLS),
269 ABIList(SpecialCaseList::createOrDie(ABIListFile.empty() ? ClABIListFile
273 FunctionType *DataFlowSanitizer::getArgsFunctionType(FunctionType *T) {
274 llvm::SmallVector<Type *, 4> ArgTypes;
275 std::copy(T->param_begin(), T->param_end(), std::back_inserter(ArgTypes));
276 for (unsigned i = 0, e = T->getNumParams(); i != e; ++i)
277 ArgTypes.push_back(ShadowTy);
279 ArgTypes.push_back(ShadowPtrTy);
280 Type *RetType = T->getReturnType();
281 if (!RetType->isVoidTy())
282 RetType = StructType::get(RetType, ShadowTy, (Type *)0);
283 return FunctionType::get(RetType, ArgTypes, T->isVarArg());
286 FunctionType *DataFlowSanitizer::getCustomFunctionType(FunctionType *T) {
287 assert(!T->isVarArg());
288 llvm::SmallVector<Type *, 4> ArgTypes;
289 std::copy(T->param_begin(), T->param_end(), std::back_inserter(ArgTypes));
290 for (unsigned i = 0, e = T->getNumParams(); i != e; ++i)
291 ArgTypes.push_back(ShadowTy);
292 Type *RetType = T->getReturnType();
293 if (!RetType->isVoidTy())
294 ArgTypes.push_back(ShadowPtrTy);
295 return FunctionType::get(T->getReturnType(), ArgTypes, false);
298 bool DataFlowSanitizer::doInitialization(Module &M) {
299 DL = getAnalysisIfAvailable<DataLayout>();
304 Ctx = &M.getContext();
305 ShadowTy = IntegerType::get(*Ctx, ShadowWidth);
306 ShadowPtrTy = PointerType::getUnqual(ShadowTy);
307 IntptrTy = DL->getIntPtrType(*Ctx);
308 ZeroShadow = ConstantInt::getSigned(ShadowTy, 0);
309 ShadowPtrMask = ConstantInt::getSigned(IntptrTy, ~0x700000000000LL);
310 ShadowPtrMul = ConstantInt::getSigned(IntptrTy, ShadowWidth / 8);
312 Type *DFSanUnionArgs[2] = { ShadowTy, ShadowTy };
314 FunctionType::get(ShadowTy, DFSanUnionArgs, /*isVarArg=*/ false);
315 Type *DFSanUnionLoadArgs[2] = { ShadowPtrTy, IntptrTy };
317 FunctionType::get(ShadowTy, DFSanUnionLoadArgs, /*isVarArg=*/ false);
318 DFSanUnimplementedFnTy = FunctionType::get(
319 Type::getVoidTy(*Ctx), Type::getInt8PtrTy(*Ctx), /*isVarArg=*/false);
320 Type *DFSanSetLabelArgs[3] = { ShadowTy, Type::getInt8PtrTy(*Ctx), IntptrTy };
321 DFSanSetLabelFnTy = FunctionType::get(Type::getVoidTy(*Ctx),
322 DFSanSetLabelArgs, /*isVarArg=*/false);
323 DFSanNonzeroLabelFnTy = FunctionType::get(
324 Type::getVoidTy(*Ctx), ArrayRef<Type *>(), /*isVarArg=*/false);
327 Type *ArgTLSTy = ArrayType::get(ShadowTy, 64);
329 GetArgTLS = ConstantExpr::getIntToPtr(
330 ConstantInt::get(IntptrTy, uintptr_t(GetArgTLSPtr)),
331 PointerType::getUnqual(
332 FunctionType::get(PointerType::getUnqual(ArgTLSTy), (Type *)0)));
334 if (GetRetvalTLSPtr) {
336 GetRetvalTLS = ConstantExpr::getIntToPtr(
337 ConstantInt::get(IntptrTy, uintptr_t(GetRetvalTLSPtr)),
338 PointerType::getUnqual(
339 FunctionType::get(PointerType::getUnqual(ShadowTy), (Type *)0)));
342 ColdCallWeights = MDBuilder(*Ctx).createBranchWeights(1, 1000);
346 bool DataFlowSanitizer::isInstrumented(Function *F) {
347 return !ABIList->isIn(*F, "uninstrumented");
350 DataFlowSanitizer::InstrumentedABI DataFlowSanitizer::getInstrumentedABI() {
351 return ClArgsABI ? IA_Args : IA_TLS;
354 DataFlowSanitizer::WrapperKind DataFlowSanitizer::getWrapperKind(Function *F) {
355 if (ABIList->isIn(*F, "functional"))
356 return WK_Functional;
357 if (ABIList->isIn(*F, "discard"))
359 if (ABIList->isIn(*F, "custom"))
365 bool DataFlowSanitizer::runOnModule(Module &M) {
369 if (ABIList->isIn(M, "skip"))
373 Type *ArgTLSTy = ArrayType::get(ShadowTy, 64);
374 ArgTLS = Mod->getOrInsertGlobal("__dfsan_arg_tls", ArgTLSTy);
375 if (GlobalVariable *G = dyn_cast<GlobalVariable>(ArgTLS))
376 G->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
378 if (!GetRetvalTLSPtr) {
379 RetvalTLS = Mod->getOrInsertGlobal("__dfsan_retval_tls", ShadowTy);
380 if (GlobalVariable *G = dyn_cast<GlobalVariable>(RetvalTLS))
381 G->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
384 DFSanUnionFn = Mod->getOrInsertFunction("__dfsan_union", DFSanUnionFnTy);
385 if (Function *F = dyn_cast<Function>(DFSanUnionFn)) {
386 F->addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
387 F->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
388 F->addAttribute(1, Attribute::ZExt);
389 F->addAttribute(2, Attribute::ZExt);
392 Mod->getOrInsertFunction("__dfsan_union_load", DFSanUnionLoadFnTy);
393 if (Function *F = dyn_cast<Function>(DFSanUnionLoadFn)) {
394 F->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
396 DFSanUnimplementedFn =
397 Mod->getOrInsertFunction("__dfsan_unimplemented", DFSanUnimplementedFnTy);
399 Mod->getOrInsertFunction("__dfsan_set_label", DFSanSetLabelFnTy);
400 if (Function *F = dyn_cast<Function>(DFSanSetLabelFn)) {
401 F->addAttribute(1, Attribute::ZExt);
403 DFSanNonzeroLabelFn =
404 Mod->getOrInsertFunction("__dfsan_nonzero_label", DFSanNonzeroLabelFnTy);
406 std::vector<Function *> FnsToInstrument;
407 llvm::SmallPtrSet<Function *, 2> FnsWithNativeABI;
408 for (Module::iterator i = M.begin(), e = M.end(); i != e; ++i) {
409 if (!i->isIntrinsic() &&
411 i != DFSanUnionLoadFn &&
412 i != DFSanUnimplementedFn &&
413 i != DFSanSetLabelFn &&
414 i != DFSanNonzeroLabelFn)
415 FnsToInstrument.push_back(&*i);
419 B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
420 ReadOnlyNoneAttrs = AttributeSet::get(*Ctx, AttributeSet::FunctionIndex, B);
422 // First, change the ABI of every function in the module. ABI-listed
423 // functions keep their original ABI and get a wrapper function.
424 for (std::vector<Function *>::iterator i = FnsToInstrument.begin(),
425 e = FnsToInstrument.end();
428 FunctionType *FT = F.getFunctionType();
430 if (FT->getNumParams() == 0 && !FT->isVarArg() &&
431 FT->getReturnType()->isVoidTy())
434 if (isInstrumented(&F)) {
435 if (getInstrumentedABI() == IA_Args) {
436 FunctionType *NewFT = getArgsFunctionType(FT);
437 Function *NewF = Function::Create(NewFT, F.getLinkage(), "", &M);
438 NewF->copyAttributesFrom(&F);
439 NewF->removeAttributes(
440 AttributeSet::ReturnIndex,
441 AttributeFuncs::typeIncompatible(NewFT->getReturnType(),
442 AttributeSet::ReturnIndex));
443 for (Function::arg_iterator FArg = F.arg_begin(),
444 NewFArg = NewF->arg_begin(),
445 FArgEnd = F.arg_end();
446 FArg != FArgEnd; ++FArg, ++NewFArg) {
447 FArg->replaceAllUsesWith(NewFArg);
449 NewF->getBasicBlockList().splice(NewF->begin(), F.getBasicBlockList());
451 for (Function::use_iterator ui = F.use_begin(), ue = F.use_end();
453 BlockAddress *BA = dyn_cast<BlockAddress>(ui.getUse().getUser());
456 BA->replaceAllUsesWith(
457 BlockAddress::get(NewF, BA->getBasicBlock()));
461 F.replaceAllUsesWith(
462 ConstantExpr::getBitCast(NewF, PointerType::getUnqual(FT)));
467 // Hopefully, nobody will try to indirectly call a vararg
469 } else if (FT->isVarArg()) {
470 UnwrappedFnMap[&F] = &F;
473 // Build a wrapper function for F. The wrapper simply calls F, and is
474 // added to FnsToInstrument so that any instrumentation according to its
475 // WrapperKind is done in the second pass below.
476 FunctionType *NewFT = getInstrumentedABI() == IA_Args
477 ? getArgsFunctionType(FT)
480 Function::Create(NewFT, GlobalValue::LinkOnceODRLinkage,
481 std::string("dfsw$") + F.getName(), &M);
482 NewF->copyAttributesFrom(&F);
483 NewF->removeAttributes(
484 AttributeSet::ReturnIndex,
485 AttributeFuncs::typeIncompatible(NewFT->getReturnType(),
486 AttributeSet::ReturnIndex));
487 if (getInstrumentedABI() == IA_TLS)
488 NewF->removeAttributes(AttributeSet::FunctionIndex,
491 BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", NewF);
492 std::vector<Value *> Args;
493 unsigned n = FT->getNumParams();
494 for (Function::arg_iterator ai = NewF->arg_begin(); n != 0; ++ai, --n)
495 Args.push_back(&*ai);
496 CallInst *CI = CallInst::Create(&F, Args, "", BB);
497 if (FT->getReturnType()->isVoidTy())
498 ReturnInst::Create(*Ctx, BB);
500 ReturnInst::Create(*Ctx, CI, BB);
502 Value *WrappedFnCst =
503 ConstantExpr::getBitCast(NewF, PointerType::getUnqual(FT));
504 F.replaceAllUsesWith(WrappedFnCst);
505 UnwrappedFnMap[WrappedFnCst] = &F;
508 if (!F.isDeclaration()) {
509 // This function is probably defining an interposition of an
510 // uninstrumented function and hence needs to keep the original ABI.
511 // But any functions it may call need to use the instrumented ABI, so
512 // we instrument it in a mode which preserves the original ABI.
513 FnsWithNativeABI.insert(&F);
515 // This code needs to rebuild the iterators, as they may be invalidated
516 // by the push_back, taking care that the new range does not include
517 // any functions added by this code.
518 size_t N = i - FnsToInstrument.begin(),
519 Count = e - FnsToInstrument.begin();
520 FnsToInstrument.push_back(&F);
521 i = FnsToInstrument.begin() + N;
522 e = FnsToInstrument.begin() + Count;
527 for (std::vector<Function *>::iterator i = FnsToInstrument.begin(),
528 e = FnsToInstrument.end();
530 if (!*i || (*i)->isDeclaration())
533 removeUnreachableBlocks(**i);
535 DFSanFunction DFSF(*this, *i, FnsWithNativeABI.count(*i));
537 // DFSanVisitor may create new basic blocks, which confuses df_iterator.
538 // Build a copy of the list before iterating over it.
539 llvm::SmallVector<BasicBlock *, 4> BBList;
540 std::copy(df_begin(&(*i)->getEntryBlock()), df_end(&(*i)->getEntryBlock()),
541 std::back_inserter(BBList));
543 for (llvm::SmallVector<BasicBlock *, 4>::iterator i = BBList.begin(),
546 Instruction *Inst = &(*i)->front();
548 // DFSanVisitor may split the current basic block, changing the current
549 // instruction's next pointer and moving the next instruction to the
550 // tail block from which we should continue.
551 Instruction *Next = Inst->getNextNode();
552 // DFSanVisitor may delete Inst, so keep track of whether it was a
554 bool IsTerminator = isa<TerminatorInst>(Inst);
555 if (!DFSF.SkipInsts.count(Inst))
556 DFSanVisitor(DFSF).visit(Inst);
563 // We will not necessarily be able to compute the shadow for every phi node
564 // until we have visited every block. Therefore, the code that handles phi
565 // nodes adds them to the PHIFixups list so that they can be properly
567 for (std::vector<std::pair<PHINode *, PHINode *> >::iterator
568 i = DFSF.PHIFixups.begin(),
569 e = DFSF.PHIFixups.end();
571 for (unsigned val = 0, n = i->first->getNumIncomingValues(); val != n;
573 i->second->setIncomingValue(
574 val, DFSF.getShadow(i->first->getIncomingValue(val)));
578 // -dfsan-debug-nonzero-labels will split the CFG in all kinds of crazy
579 // places (i.e. instructions in basic blocks we haven't even begun visiting
580 // yet). To make our life easier, do this work in a pass after the main
582 if (ClDebugNonzeroLabels) {
583 for (DenseSet<Value *>::iterator i = DFSF.NonZeroChecks.begin(),
584 e = DFSF.NonZeroChecks.end();
587 if (Instruction *I = dyn_cast<Instruction>(*i))
588 Pos = I->getNextNode();
590 Pos = DFSF.F->getEntryBlock().begin();
591 while (isa<PHINode>(Pos) || isa<AllocaInst>(Pos))
592 Pos = Pos->getNextNode();
593 IRBuilder<> IRB(Pos);
594 Instruction *NeInst = cast<Instruction>(
595 IRB.CreateICmpNE(*i, DFSF.DFS.ZeroShadow));
596 BranchInst *BI = cast<BranchInst>(SplitBlockAndInsertIfThen(
597 NeInst, /*Unreachable=*/ false, ColdCallWeights));
598 IRBuilder<> ThenIRB(BI);
599 ThenIRB.CreateCall(DFSF.DFS.DFSanNonzeroLabelFn);
607 Value *DFSanFunction::getArgTLSPtr() {
611 return ArgTLSPtr = DFS.ArgTLS;
613 IRBuilder<> IRB(F->getEntryBlock().begin());
614 return ArgTLSPtr = IRB.CreateCall(DFS.GetArgTLS);
617 Value *DFSanFunction::getRetvalTLS() {
621 return RetvalTLSPtr = DFS.RetvalTLS;
623 IRBuilder<> IRB(F->getEntryBlock().begin());
624 return RetvalTLSPtr = IRB.CreateCall(DFS.GetRetvalTLS);
627 Value *DFSanFunction::getArgTLS(unsigned Idx, Instruction *Pos) {
628 IRBuilder<> IRB(Pos);
629 return IRB.CreateConstGEP2_64(getArgTLSPtr(), 0, Idx);
632 Value *DFSanFunction::getShadow(Value *V) {
633 if (!isa<Argument>(V) && !isa<Instruction>(V))
634 return DFS.ZeroShadow;
635 Value *&Shadow = ValShadowMap[V];
637 if (Argument *A = dyn_cast<Argument>(V)) {
639 return DFS.ZeroShadow;
641 case DataFlowSanitizer::IA_TLS: {
642 Value *ArgTLSPtr = getArgTLSPtr();
643 Instruction *ArgTLSPos =
644 DFS.ArgTLS ? &*F->getEntryBlock().begin()
645 : cast<Instruction>(ArgTLSPtr)->getNextNode();
646 IRBuilder<> IRB(ArgTLSPos);
647 Shadow = IRB.CreateLoad(getArgTLS(A->getArgNo(), ArgTLSPos));
650 case DataFlowSanitizer::IA_Args: {
651 unsigned ArgIdx = A->getArgNo() + F->getArgumentList().size() / 2;
652 Function::arg_iterator i = F->arg_begin();
656 assert(Shadow->getType() == DFS.ShadowTy);
660 NonZeroChecks.insert(Shadow);
662 Shadow = DFS.ZeroShadow;
668 void DFSanFunction::setShadow(Instruction *I, Value *Shadow) {
669 assert(!ValShadowMap.count(I));
670 assert(Shadow->getType() == DFS.ShadowTy);
671 ValShadowMap[I] = Shadow;
674 Value *DataFlowSanitizer::getShadowAddress(Value *Addr, Instruction *Pos) {
675 assert(Addr != RetvalTLS && "Reinstrumenting?");
676 IRBuilder<> IRB(Pos);
677 return IRB.CreateIntToPtr(
679 IRB.CreateAnd(IRB.CreatePtrToInt(Addr, IntptrTy), ShadowPtrMask),
684 // Generates IR to compute the union of the two given shadows, inserting it
685 // before Pos. Returns the computed union Value.
686 Value *DataFlowSanitizer::combineShadows(Value *V1, Value *V2,
688 if (V1 == ZeroShadow)
690 if (V2 == ZeroShadow)
694 IRBuilder<> IRB(Pos);
695 BasicBlock *Head = Pos->getParent();
696 Value *Ne = IRB.CreateICmpNE(V1, V2);
697 Instruction *NeInst = dyn_cast<Instruction>(Ne);
699 BranchInst *BI = cast<BranchInst>(SplitBlockAndInsertIfThen(
700 NeInst, /*Unreachable=*/ false, ColdCallWeights));
701 IRBuilder<> ThenIRB(BI);
702 CallInst *Call = ThenIRB.CreateCall2(DFSanUnionFn, V1, V2);
703 Call->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
704 Call->addAttribute(1, Attribute::ZExt);
705 Call->addAttribute(2, Attribute::ZExt);
707 BasicBlock *Tail = BI->getSuccessor(0);
708 PHINode *Phi = PHINode::Create(ShadowTy, 2, "", Tail->begin());
709 Phi->addIncoming(Call, Call->getParent());
710 Phi->addIncoming(ZeroShadow, Head);
719 // A convenience function which folds the shadows of each of the operands
720 // of the provided instruction Inst, inserting the IR before Inst. Returns
721 // the computed union Value.
722 Value *DFSanFunction::combineOperandShadows(Instruction *Inst) {
723 if (Inst->getNumOperands() == 0)
724 return DFS.ZeroShadow;
726 Value *Shadow = getShadow(Inst->getOperand(0));
727 for (unsigned i = 1, n = Inst->getNumOperands(); i != n; ++i) {
728 Shadow = DFS.combineShadows(Shadow, getShadow(Inst->getOperand(i)), Inst);
733 void DFSanVisitor::visitOperandShadowInst(Instruction &I) {
734 Value *CombinedShadow = DFSF.combineOperandShadows(&I);
735 DFSF.setShadow(&I, CombinedShadow);
738 // Generates IR to load shadow corresponding to bytes [Addr, Addr+Size), where
739 // Addr has alignment Align, and take the union of each of those shadows.
740 Value *DFSanFunction::loadShadow(Value *Addr, uint64_t Size, uint64_t Align,
742 if (AllocaInst *AI = dyn_cast<AllocaInst>(Addr)) {
743 llvm::DenseMap<AllocaInst *, AllocaInst *>::iterator i =
744 AllocaShadowMap.find(AI);
745 if (i != AllocaShadowMap.end()) {
746 IRBuilder<> IRB(Pos);
747 return IRB.CreateLoad(i->second);
751 uint64_t ShadowAlign = Align * DFS.ShadowWidth / 8;
752 SmallVector<Value *, 2> Objs;
753 GetUnderlyingObjects(Addr, Objs, DFS.DL);
754 bool AllConstants = true;
755 for (SmallVector<Value *, 2>::iterator i = Objs.begin(), e = Objs.end();
757 if (isa<Function>(*i) || isa<BlockAddress>(*i))
759 if (isa<GlobalVariable>(*i) && cast<GlobalVariable>(*i)->isConstant())
762 AllConstants = false;
766 return DFS.ZeroShadow;
768 Value *ShadowAddr = DFS.getShadowAddress(Addr, Pos);
771 return DFS.ZeroShadow;
773 LoadInst *LI = new LoadInst(ShadowAddr, "", Pos);
774 LI->setAlignment(ShadowAlign);
778 IRBuilder<> IRB(Pos);
780 IRB.CreateGEP(ShadowAddr, ConstantInt::get(DFS.IntptrTy, 1));
781 return DFS.combineShadows(IRB.CreateAlignedLoad(ShadowAddr, ShadowAlign),
782 IRB.CreateAlignedLoad(ShadowAddr1, ShadowAlign),
786 if (Size % (64 / DFS.ShadowWidth) == 0) {
787 // Fast path for the common case where each byte has identical shadow: load
788 // shadow 64 bits at a time, fall out to a __dfsan_union_load call if any
789 // shadow is non-equal.
790 BasicBlock *FallbackBB = BasicBlock::Create(*DFS.Ctx, "", F);
791 IRBuilder<> FallbackIRB(FallbackBB);
792 CallInst *FallbackCall = FallbackIRB.CreateCall2(
793 DFS.DFSanUnionLoadFn, ShadowAddr, ConstantInt::get(DFS.IntptrTy, Size));
794 FallbackCall->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
796 // Compare each of the shadows stored in the loaded 64 bits to each other,
797 // by computing (WideShadow rotl ShadowWidth) == WideShadow.
798 IRBuilder<> IRB(Pos);
800 IRB.CreateBitCast(ShadowAddr, Type::getInt64PtrTy(*DFS.Ctx));
801 Value *WideShadow = IRB.CreateAlignedLoad(WideAddr, ShadowAlign);
802 Value *TruncShadow = IRB.CreateTrunc(WideShadow, DFS.ShadowTy);
803 Value *ShlShadow = IRB.CreateShl(WideShadow, DFS.ShadowWidth);
804 Value *ShrShadow = IRB.CreateLShr(WideShadow, 64 - DFS.ShadowWidth);
805 Value *RotShadow = IRB.CreateOr(ShlShadow, ShrShadow);
806 Value *ShadowsEq = IRB.CreateICmpEQ(WideShadow, RotShadow);
808 BasicBlock *Head = Pos->getParent();
809 BasicBlock *Tail = Head->splitBasicBlock(Pos);
810 // In the following code LastBr will refer to the previous basic block's
811 // conditional branch instruction, whose true successor is fixed up to point
812 // to the next block during the loop below or to the tail after the final
814 BranchInst *LastBr = BranchInst::Create(FallbackBB, FallbackBB, ShadowsEq);
815 ReplaceInstWithInst(Head->getTerminator(), LastBr);
817 for (uint64_t Ofs = 64 / DFS.ShadowWidth; Ofs != Size;
818 Ofs += 64 / DFS.ShadowWidth) {
819 BasicBlock *NextBB = BasicBlock::Create(*DFS.Ctx, "", F);
820 IRBuilder<> NextIRB(NextBB);
821 WideAddr = NextIRB.CreateGEP(WideAddr, ConstantInt::get(DFS.IntptrTy, 1));
822 Value *NextWideShadow = NextIRB.CreateAlignedLoad(WideAddr, ShadowAlign);
823 ShadowsEq = NextIRB.CreateICmpEQ(WideShadow, NextWideShadow);
824 LastBr->setSuccessor(0, NextBB);
825 LastBr = NextIRB.CreateCondBr(ShadowsEq, FallbackBB, FallbackBB);
828 LastBr->setSuccessor(0, Tail);
829 FallbackIRB.CreateBr(Tail);
830 PHINode *Shadow = PHINode::Create(DFS.ShadowTy, 2, "", &Tail->front());
831 Shadow->addIncoming(FallbackCall, FallbackBB);
832 Shadow->addIncoming(TruncShadow, LastBr->getParent());
836 IRBuilder<> IRB(Pos);
837 CallInst *FallbackCall = IRB.CreateCall2(
838 DFS.DFSanUnionLoadFn, ShadowAddr, ConstantInt::get(DFS.IntptrTy, Size));
839 FallbackCall->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
843 void DFSanVisitor::visitLoadInst(LoadInst &LI) {
844 uint64_t Size = DFSF.DFS.DL->getTypeStoreSize(LI.getType());
846 if (ClPreserveAlignment) {
847 Align = LI.getAlignment();
849 Align = DFSF.DFS.DL->getABITypeAlignment(LI.getType());
853 IRBuilder<> IRB(&LI);
854 Value *LoadedShadow =
855 DFSF.loadShadow(LI.getPointerOperand(), Size, Align, &LI);
856 Value *PtrShadow = DFSF.getShadow(LI.getPointerOperand());
857 Value *CombinedShadow = DFSF.DFS.combineShadows(LoadedShadow, PtrShadow, &LI);
858 if (CombinedShadow != DFSF.DFS.ZeroShadow)
859 DFSF.NonZeroChecks.insert(CombinedShadow);
861 DFSF.setShadow(&LI, CombinedShadow);
864 void DFSanFunction::storeShadow(Value *Addr, uint64_t Size, uint64_t Align,
865 Value *Shadow, Instruction *Pos) {
866 if (AllocaInst *AI = dyn_cast<AllocaInst>(Addr)) {
867 llvm::DenseMap<AllocaInst *, AllocaInst *>::iterator i =
868 AllocaShadowMap.find(AI);
869 if (i != AllocaShadowMap.end()) {
870 IRBuilder<> IRB(Pos);
871 IRB.CreateStore(Shadow, i->second);
876 uint64_t ShadowAlign = Align * DFS.ShadowWidth / 8;
877 IRBuilder<> IRB(Pos);
878 Value *ShadowAddr = DFS.getShadowAddress(Addr, Pos);
879 if (Shadow == DFS.ZeroShadow) {
880 IntegerType *ShadowTy = IntegerType::get(*DFS.Ctx, Size * DFS.ShadowWidth);
881 Value *ExtZeroShadow = ConstantInt::get(ShadowTy, 0);
882 Value *ExtShadowAddr =
883 IRB.CreateBitCast(ShadowAddr, PointerType::getUnqual(ShadowTy));
884 IRB.CreateAlignedStore(ExtZeroShadow, ExtShadowAddr, ShadowAlign);
888 const unsigned ShadowVecSize = 128 / DFS.ShadowWidth;
890 if (Size >= ShadowVecSize) {
891 VectorType *ShadowVecTy = VectorType::get(DFS.ShadowTy, ShadowVecSize);
892 Value *ShadowVec = UndefValue::get(ShadowVecTy);
893 for (unsigned i = 0; i != ShadowVecSize; ++i) {
894 ShadowVec = IRB.CreateInsertElement(
895 ShadowVec, Shadow, ConstantInt::get(Type::getInt32Ty(*DFS.Ctx), i));
897 Value *ShadowVecAddr =
898 IRB.CreateBitCast(ShadowAddr, PointerType::getUnqual(ShadowVecTy));
900 Value *CurShadowVecAddr = IRB.CreateConstGEP1_32(ShadowVecAddr, Offset);
901 IRB.CreateAlignedStore(ShadowVec, CurShadowVecAddr, ShadowAlign);
902 Size -= ShadowVecSize;
904 } while (Size >= ShadowVecSize);
905 Offset *= ShadowVecSize;
908 Value *CurShadowAddr = IRB.CreateConstGEP1_32(ShadowAddr, Offset);
909 IRB.CreateAlignedStore(Shadow, CurShadowAddr, ShadowAlign);
915 void DFSanVisitor::visitStoreInst(StoreInst &SI) {
917 DFSF.DFS.DL->getTypeStoreSize(SI.getValueOperand()->getType());
919 if (ClPreserveAlignment) {
920 Align = SI.getAlignment();
922 Align = DFSF.DFS.DL->getABITypeAlignment(SI.getValueOperand()->getType());
926 DFSF.storeShadow(SI.getPointerOperand(), Size, Align,
927 DFSF.getShadow(SI.getValueOperand()), &SI);
930 void DFSanVisitor::visitBinaryOperator(BinaryOperator &BO) {
931 visitOperandShadowInst(BO);
934 void DFSanVisitor::visitCastInst(CastInst &CI) { visitOperandShadowInst(CI); }
936 void DFSanVisitor::visitCmpInst(CmpInst &CI) { visitOperandShadowInst(CI); }
938 void DFSanVisitor::visitGetElementPtrInst(GetElementPtrInst &GEPI) {
939 visitOperandShadowInst(GEPI);
942 void DFSanVisitor::visitExtractElementInst(ExtractElementInst &I) {
943 visitOperandShadowInst(I);
946 void DFSanVisitor::visitInsertElementInst(InsertElementInst &I) {
947 visitOperandShadowInst(I);
950 void DFSanVisitor::visitShuffleVectorInst(ShuffleVectorInst &I) {
951 visitOperandShadowInst(I);
954 void DFSanVisitor::visitExtractValueInst(ExtractValueInst &I) {
955 visitOperandShadowInst(I);
958 void DFSanVisitor::visitInsertValueInst(InsertValueInst &I) {
959 visitOperandShadowInst(I);
962 void DFSanVisitor::visitAllocaInst(AllocaInst &I) {
963 bool AllLoadsStores = true;
964 for (Instruction::use_iterator i = I.use_begin(), e = I.use_end(); i != e;
966 if (isa<LoadInst>(*i))
969 if (StoreInst *SI = dyn_cast<StoreInst>(*i)) {
970 if (SI->getPointerOperand() == &I)
974 AllLoadsStores = false;
977 if (AllLoadsStores) {
979 DFSF.AllocaShadowMap[&I] = IRB.CreateAlloca(DFSF.DFS.ShadowTy);
981 DFSF.setShadow(&I, DFSF.DFS.ZeroShadow);
984 void DFSanVisitor::visitSelectInst(SelectInst &I) {
985 Value *CondShadow = DFSF.getShadow(I.getCondition());
986 Value *TrueShadow = DFSF.getShadow(I.getTrueValue());
987 Value *FalseShadow = DFSF.getShadow(I.getFalseValue());
989 if (isa<VectorType>(I.getCondition()->getType())) {
991 &I, DFSF.DFS.combineShadows(
993 DFSF.DFS.combineShadows(TrueShadow, FalseShadow, &I), &I));
996 if (TrueShadow == FalseShadow) {
997 ShadowSel = TrueShadow;
1000 SelectInst::Create(I.getCondition(), TrueShadow, FalseShadow, "", &I);
1002 DFSF.setShadow(&I, DFSF.DFS.combineShadows(CondShadow, ShadowSel, &I));
1006 void DFSanVisitor::visitMemSetInst(MemSetInst &I) {
1007 IRBuilder<> IRB(&I);
1008 Value *ValShadow = DFSF.getShadow(I.getValue());
1010 DFSF.DFS.DFSanSetLabelFn, ValShadow,
1011 IRB.CreateBitCast(I.getDest(), Type::getInt8PtrTy(*DFSF.DFS.Ctx)),
1012 IRB.CreateZExtOrTrunc(I.getLength(), DFSF.DFS.IntptrTy));
1015 void DFSanVisitor::visitMemTransferInst(MemTransferInst &I) {
1016 IRBuilder<> IRB(&I);
1017 Value *DestShadow = DFSF.DFS.getShadowAddress(I.getDest(), &I);
1018 Value *SrcShadow = DFSF.DFS.getShadowAddress(I.getSource(), &I);
1019 Value *LenShadow = IRB.CreateMul(
1021 ConstantInt::get(I.getLength()->getType(), DFSF.DFS.ShadowWidth / 8));
1023 if (ClPreserveAlignment) {
1024 AlignShadow = IRB.CreateMul(I.getAlignmentCst(),
1025 ConstantInt::get(I.getAlignmentCst()->getType(),
1026 DFSF.DFS.ShadowWidth / 8));
1028 AlignShadow = ConstantInt::get(I.getAlignmentCst()->getType(),
1029 DFSF.DFS.ShadowWidth / 8);
1031 Type *Int8Ptr = Type::getInt8PtrTy(*DFSF.DFS.Ctx);
1032 DestShadow = IRB.CreateBitCast(DestShadow, Int8Ptr);
1033 SrcShadow = IRB.CreateBitCast(SrcShadow, Int8Ptr);
1034 IRB.CreateCall5(I.getCalledValue(), DestShadow, SrcShadow, LenShadow,
1035 AlignShadow, I.getVolatileCst());
1038 void DFSanVisitor::visitReturnInst(ReturnInst &RI) {
1039 if (!DFSF.IsNativeABI && RI.getReturnValue()) {
1041 case DataFlowSanitizer::IA_TLS: {
1042 Value *S = DFSF.getShadow(RI.getReturnValue());
1043 IRBuilder<> IRB(&RI);
1044 IRB.CreateStore(S, DFSF.getRetvalTLS());
1047 case DataFlowSanitizer::IA_Args: {
1048 IRBuilder<> IRB(&RI);
1049 Type *RT = DFSF.F->getFunctionType()->getReturnType();
1051 IRB.CreateInsertValue(UndefValue::get(RT), RI.getReturnValue(), 0);
1053 IRB.CreateInsertValue(InsVal, DFSF.getShadow(RI.getReturnValue()), 1);
1054 RI.setOperand(0, InsShadow);
1061 void DFSanVisitor::visitCallSite(CallSite CS) {
1062 Function *F = CS.getCalledFunction();
1063 if ((F && F->isIntrinsic()) || isa<InlineAsm>(CS.getCalledValue())) {
1064 visitOperandShadowInst(*CS.getInstruction());
1068 IRBuilder<> IRB(CS.getInstruction());
1070 DenseMap<Value *, Function *>::iterator i =
1071 DFSF.DFS.UnwrappedFnMap.find(CS.getCalledValue());
1072 if (i != DFSF.DFS.UnwrappedFnMap.end()) {
1073 Function *F = i->second;
1074 switch (DFSF.DFS.getWrapperKind(F)) {
1075 case DataFlowSanitizer::WK_Warning: {
1076 CS.setCalledFunction(F);
1077 IRB.CreateCall(DFSF.DFS.DFSanUnimplementedFn,
1078 IRB.CreateGlobalStringPtr(F->getName()));
1079 DFSF.setShadow(CS.getInstruction(), DFSF.DFS.ZeroShadow);
1082 case DataFlowSanitizer::WK_Discard: {
1083 CS.setCalledFunction(F);
1084 DFSF.setShadow(CS.getInstruction(), DFSF.DFS.ZeroShadow);
1087 case DataFlowSanitizer::WK_Functional: {
1088 CS.setCalledFunction(F);
1089 visitOperandShadowInst(*CS.getInstruction());
1092 case DataFlowSanitizer::WK_Custom: {
1093 // Don't try to handle invokes of custom functions, it's too complicated.
1094 // Instead, invoke the dfsw$ wrapper, which will in turn call the __dfsw_
1096 if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) {
1097 FunctionType *FT = F->getFunctionType();
1098 FunctionType *CustomFT = DFSF.DFS.getCustomFunctionType(FT);
1099 std::string CustomFName = "__dfsw_";
1100 CustomFName += F->getName();
1102 DFSF.DFS.Mod->getOrInsertFunction(CustomFName, CustomFT);
1103 if (Function *CustomFn = dyn_cast<Function>(CustomF)) {
1104 CustomFn->copyAttributesFrom(F);
1106 // Custom functions returning non-void will write to the return label.
1107 if (!FT->getReturnType()->isVoidTy()) {
1108 CustomFn->removeAttributes(AttributeSet::FunctionIndex,
1109 DFSF.DFS.ReadOnlyNoneAttrs);
1113 std::vector<Value *> Args;
1115 CallSite::arg_iterator i = CS.arg_begin();
1116 for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
1120 for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
1121 Args.push_back(DFSF.getShadow(*i));
1123 if (!FT->getReturnType()->isVoidTy()) {
1124 if (!DFSF.LabelReturnAlloca) {
1125 DFSF.LabelReturnAlloca =
1126 new AllocaInst(DFSF.DFS.ShadowTy, "labelreturn",
1127 DFSF.F->getEntryBlock().begin());
1129 Args.push_back(DFSF.LabelReturnAlloca);
1132 CallInst *CustomCI = IRB.CreateCall(CustomF, Args);
1133 CustomCI->setCallingConv(CI->getCallingConv());
1134 CustomCI->setAttributes(CI->getAttributes());
1136 if (!FT->getReturnType()->isVoidTy()) {
1137 LoadInst *LabelLoad = IRB.CreateLoad(DFSF.LabelReturnAlloca);
1138 DFSF.setShadow(CustomCI, LabelLoad);
1141 CI->replaceAllUsesWith(CustomCI);
1142 CI->eraseFromParent();
1150 FunctionType *FT = cast<FunctionType>(
1151 CS.getCalledValue()->getType()->getPointerElementType());
1152 if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_TLS) {
1153 for (unsigned i = 0, n = FT->getNumParams(); i != n; ++i) {
1154 IRB.CreateStore(DFSF.getShadow(CS.getArgument(i)),
1155 DFSF.getArgTLS(i, CS.getInstruction()));
1159 Instruction *Next = 0;
1160 if (!CS.getType()->isVoidTy()) {
1161 if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
1162 if (II->getNormalDest()->getSinglePredecessor()) {
1163 Next = II->getNormalDest()->begin();
1166 SplitEdge(II->getParent(), II->getNormalDest(), &DFSF.DFS);
1167 Next = NewBB->begin();
1170 Next = CS->getNextNode();
1173 if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_TLS) {
1174 IRBuilder<> NextIRB(Next);
1175 LoadInst *LI = NextIRB.CreateLoad(DFSF.getRetvalTLS());
1176 DFSF.SkipInsts.insert(LI);
1177 DFSF.setShadow(CS.getInstruction(), LI);
1178 DFSF.NonZeroChecks.insert(LI);
1182 // Do all instrumentation for IA_Args down here to defer tampering with the
1183 // CFG in a way that SplitEdge may be able to detect.
1184 if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_Args) {
1185 FunctionType *NewFT = DFSF.DFS.getArgsFunctionType(FT);
1187 IRB.CreateBitCast(CS.getCalledValue(), PointerType::getUnqual(NewFT));
1188 std::vector<Value *> Args;
1190 CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1191 for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
1195 for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
1196 Args.push_back(DFSF.getShadow(*i));
1198 if (FT->isVarArg()) {
1199 unsigned VarArgSize = CS.arg_size() - FT->getNumParams();
1200 ArrayType *VarArgArrayTy = ArrayType::get(DFSF.DFS.ShadowTy, VarArgSize);
1201 AllocaInst *VarArgShadow =
1202 new AllocaInst(VarArgArrayTy, "", DFSF.F->getEntryBlock().begin());
1203 Args.push_back(IRB.CreateConstGEP2_32(VarArgShadow, 0, 0));
1204 for (unsigned n = 0; i != e; ++i, ++n) {
1205 IRB.CreateStore(DFSF.getShadow(*i),
1206 IRB.CreateConstGEP2_32(VarArgShadow, 0, n));
1212 if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
1213 NewCS = IRB.CreateInvoke(Func, II->getNormalDest(), II->getUnwindDest(),
1216 NewCS = IRB.CreateCall(Func, Args);
1218 NewCS.setCallingConv(CS.getCallingConv());
1219 NewCS.setAttributes(CS.getAttributes().removeAttributes(
1220 *DFSF.DFS.Ctx, AttributeSet::ReturnIndex,
1221 AttributeFuncs::typeIncompatible(NewCS.getInstruction()->getType(),
1222 AttributeSet::ReturnIndex)));
1225 ExtractValueInst *ExVal =
1226 ExtractValueInst::Create(NewCS.getInstruction(), 0, "", Next);
1227 DFSF.SkipInsts.insert(ExVal);
1228 ExtractValueInst *ExShadow =
1229 ExtractValueInst::Create(NewCS.getInstruction(), 1, "", Next);
1230 DFSF.SkipInsts.insert(ExShadow);
1231 DFSF.setShadow(ExVal, ExShadow);
1232 DFSF.NonZeroChecks.insert(ExShadow);
1234 CS.getInstruction()->replaceAllUsesWith(ExVal);
1237 CS.getInstruction()->eraseFromParent();
1241 void DFSanVisitor::visitPHINode(PHINode &PN) {
1243 PHINode::Create(DFSF.DFS.ShadowTy, PN.getNumIncomingValues(), "", &PN);
1245 // Give the shadow phi node valid predecessors to fool SplitEdge into working.
1246 Value *UndefShadow = UndefValue::get(DFSF.DFS.ShadowTy);
1247 for (PHINode::block_iterator i = PN.block_begin(), e = PN.block_end(); i != e;
1249 ShadowPN->addIncoming(UndefShadow, *i);
1252 DFSF.PHIFixups.push_back(std::make_pair(&PN, ShadowPN));
1253 DFSF.setShadow(&PN, ShadowPN);