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 FunctionType *DFSanSetLabelFnTy;
163 Constant *DFSanUnionFn;
164 Constant *DFSanUnionLoadFn;
165 Constant *DFSanUnimplementedFn;
166 Constant *DFSanSetLabelFn;
167 MDNode *ColdCallWeights;
168 OwningPtr<SpecialCaseList> ABIList;
169 DenseMap<Value *, Function *> UnwrappedFnMap;
170 AttributeSet ReadOnlyNoneAttrs;
172 Value *getShadowAddress(Value *Addr, Instruction *Pos);
173 Value *combineShadows(Value *V1, Value *V2, Instruction *Pos);
174 bool isInstrumented(Function *F);
175 FunctionType *getArgsFunctionType(FunctionType *T);
176 FunctionType *getCustomFunctionType(FunctionType *T);
177 InstrumentedABI getInstrumentedABI();
178 WrapperKind getWrapperKind(Function *F);
181 DataFlowSanitizer(StringRef ABIListFile = StringRef(),
182 void *(*getArgTLS)() = 0, void *(*getRetValTLS)() = 0);
184 bool doInitialization(Module &M);
185 bool runOnModule(Module &M);
188 struct DFSanFunction {
189 DataFlowSanitizer &DFS;
191 DataFlowSanitizer::InstrumentedABI IA;
195 AllocaInst *LabelReturnAlloca;
196 DenseMap<Value *, Value *> ValShadowMap;
197 DenseMap<AllocaInst *, AllocaInst *> AllocaShadowMap;
198 std::vector<std::pair<PHINode *, PHINode *> > PHIFixups;
199 DenseSet<Instruction *> SkipInsts;
201 DFSanFunction(DataFlowSanitizer &DFS, Function *F, bool IsNativeABI)
202 : DFS(DFS), F(F), IA(DFS.getInstrumentedABI()),
203 IsNativeABI(IsNativeABI), ArgTLSPtr(0), RetvalTLSPtr(0),
204 LabelReturnAlloca(0) {}
205 Value *getArgTLSPtr();
206 Value *getArgTLS(unsigned Index, Instruction *Pos);
207 Value *getRetvalTLS();
208 Value *getShadow(Value *V);
209 void setShadow(Instruction *I, Value *Shadow);
210 Value *combineOperandShadows(Instruction *Inst);
211 Value *loadShadow(Value *ShadowAddr, uint64_t Size, uint64_t Align,
213 void storeShadow(Value *Addr, uint64_t Size, uint64_t Align, Value *Shadow,
217 class DFSanVisitor : public InstVisitor<DFSanVisitor> {
220 DFSanVisitor(DFSanFunction &DFSF) : DFSF(DFSF) {}
222 void visitOperandShadowInst(Instruction &I);
224 void visitBinaryOperator(BinaryOperator &BO);
225 void visitCastInst(CastInst &CI);
226 void visitCmpInst(CmpInst &CI);
227 void visitGetElementPtrInst(GetElementPtrInst &GEPI);
228 void visitLoadInst(LoadInst &LI);
229 void visitStoreInst(StoreInst &SI);
230 void visitReturnInst(ReturnInst &RI);
231 void visitCallSite(CallSite CS);
232 void visitPHINode(PHINode &PN);
233 void visitExtractElementInst(ExtractElementInst &I);
234 void visitInsertElementInst(InsertElementInst &I);
235 void visitShuffleVectorInst(ShuffleVectorInst &I);
236 void visitExtractValueInst(ExtractValueInst &I);
237 void visitInsertValueInst(InsertValueInst &I);
238 void visitAllocaInst(AllocaInst &I);
239 void visitSelectInst(SelectInst &I);
240 void visitMemSetInst(MemSetInst &I);
241 void visitMemTransferInst(MemTransferInst &I);
246 char DataFlowSanitizer::ID;
247 INITIALIZE_PASS(DataFlowSanitizer, "dfsan",
248 "DataFlowSanitizer: dynamic data flow analysis.", false, false)
250 ModulePass *llvm::createDataFlowSanitizerPass(StringRef ABIListFile,
251 void *(*getArgTLS)(),
252 void *(*getRetValTLS)()) {
253 return new DataFlowSanitizer(ABIListFile, getArgTLS, getRetValTLS);
256 DataFlowSanitizer::DataFlowSanitizer(StringRef ABIListFile,
257 void *(*getArgTLS)(),
258 void *(*getRetValTLS)())
259 : ModulePass(ID), GetArgTLSPtr(getArgTLS), GetRetvalTLSPtr(getRetValTLS),
260 ABIList(SpecialCaseList::createOrDie(ABIListFile.empty() ? ClABIListFile
264 FunctionType *DataFlowSanitizer::getArgsFunctionType(FunctionType *T) {
265 llvm::SmallVector<Type *, 4> ArgTypes;
266 std::copy(T->param_begin(), T->param_end(), std::back_inserter(ArgTypes));
267 for (unsigned i = 0, e = T->getNumParams(); i != e; ++i)
268 ArgTypes.push_back(ShadowTy);
270 ArgTypes.push_back(ShadowPtrTy);
271 Type *RetType = T->getReturnType();
272 if (!RetType->isVoidTy())
273 RetType = StructType::get(RetType, ShadowTy, (Type *)0);
274 return FunctionType::get(RetType, ArgTypes, T->isVarArg());
277 FunctionType *DataFlowSanitizer::getCustomFunctionType(FunctionType *T) {
278 assert(!T->isVarArg());
279 llvm::SmallVector<Type *, 4> ArgTypes;
280 std::copy(T->param_begin(), T->param_end(), std::back_inserter(ArgTypes));
281 for (unsigned i = 0, e = T->getNumParams(); i != e; ++i)
282 ArgTypes.push_back(ShadowTy);
283 Type *RetType = T->getReturnType();
284 if (!RetType->isVoidTy())
285 ArgTypes.push_back(ShadowPtrTy);
286 return FunctionType::get(T->getReturnType(), ArgTypes, false);
289 bool DataFlowSanitizer::doInitialization(Module &M) {
290 DL = getAnalysisIfAvailable<DataLayout>();
295 Ctx = &M.getContext();
296 ShadowTy = IntegerType::get(*Ctx, ShadowWidth);
297 ShadowPtrTy = PointerType::getUnqual(ShadowTy);
298 IntptrTy = DL->getIntPtrType(*Ctx);
299 ZeroShadow = ConstantInt::getSigned(ShadowTy, 0);
300 ShadowPtrMask = ConstantInt::getSigned(IntptrTy, ~0x700000000000LL);
301 ShadowPtrMul = ConstantInt::getSigned(IntptrTy, ShadowWidth / 8);
303 Type *DFSanUnionArgs[2] = { ShadowTy, ShadowTy };
305 FunctionType::get(ShadowTy, DFSanUnionArgs, /*isVarArg=*/ false);
306 Type *DFSanUnionLoadArgs[2] = { ShadowPtrTy, IntptrTy };
308 FunctionType::get(ShadowTy, DFSanUnionLoadArgs, /*isVarArg=*/ false);
309 DFSanUnimplementedFnTy = FunctionType::get(
310 Type::getVoidTy(*Ctx), Type::getInt8PtrTy(*Ctx), /*isVarArg=*/false);
311 Type *DFSanSetLabelArgs[3] = { ShadowTy, Type::getInt8PtrTy(*Ctx), IntptrTy };
312 DFSanSetLabelFnTy = FunctionType::get(Type::getVoidTy(*Ctx),
313 DFSanSetLabelArgs, /*isVarArg=*/false);
316 Type *ArgTLSTy = ArrayType::get(ShadowTy, 64);
318 GetArgTLS = ConstantExpr::getIntToPtr(
319 ConstantInt::get(IntptrTy, uintptr_t(GetArgTLSPtr)),
320 PointerType::getUnqual(
321 FunctionType::get(PointerType::getUnqual(ArgTLSTy), (Type *)0)));
323 if (GetRetvalTLSPtr) {
325 GetRetvalTLS = ConstantExpr::getIntToPtr(
326 ConstantInt::get(IntptrTy, uintptr_t(GetRetvalTLSPtr)),
327 PointerType::getUnqual(
328 FunctionType::get(PointerType::getUnqual(ShadowTy), (Type *)0)));
331 ColdCallWeights = MDBuilder(*Ctx).createBranchWeights(1, 1000);
335 bool DataFlowSanitizer::isInstrumented(Function *F) {
336 return !ABIList->isIn(*F, "uninstrumented");
339 DataFlowSanitizer::InstrumentedABI DataFlowSanitizer::getInstrumentedABI() {
340 return ClArgsABI ? IA_Args : IA_TLS;
343 DataFlowSanitizer::WrapperKind DataFlowSanitizer::getWrapperKind(Function *F) {
344 if (ABIList->isIn(*F, "functional"))
345 return WK_Functional;
346 if (ABIList->isIn(*F, "discard"))
348 if (ABIList->isIn(*F, "custom"))
354 bool DataFlowSanitizer::runOnModule(Module &M) {
358 if (ABIList->isIn(M, "skip"))
362 Type *ArgTLSTy = ArrayType::get(ShadowTy, 64);
363 ArgTLS = Mod->getOrInsertGlobal("__dfsan_arg_tls", ArgTLSTy);
364 if (GlobalVariable *G = dyn_cast<GlobalVariable>(ArgTLS))
365 G->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
367 if (!GetRetvalTLSPtr) {
368 RetvalTLS = Mod->getOrInsertGlobal("__dfsan_retval_tls", ShadowTy);
369 if (GlobalVariable *G = dyn_cast<GlobalVariable>(RetvalTLS))
370 G->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
373 DFSanUnionFn = Mod->getOrInsertFunction("__dfsan_union", DFSanUnionFnTy);
374 if (Function *F = dyn_cast<Function>(DFSanUnionFn)) {
375 F->addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
376 F->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
377 F->addAttribute(1, Attribute::ZExt);
378 F->addAttribute(2, Attribute::ZExt);
381 Mod->getOrInsertFunction("__dfsan_union_load", DFSanUnionLoadFnTy);
382 if (Function *F = dyn_cast<Function>(DFSanUnionLoadFn)) {
383 F->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
385 DFSanUnimplementedFn =
386 Mod->getOrInsertFunction("__dfsan_unimplemented", DFSanUnimplementedFnTy);
388 Mod->getOrInsertFunction("__dfsan_set_label", DFSanSetLabelFnTy);
389 if (Function *F = dyn_cast<Function>(DFSanSetLabelFn)) {
390 F->addAttribute(1, Attribute::ZExt);
393 std::vector<Function *> FnsToInstrument;
394 llvm::SmallPtrSet<Function *, 2> FnsWithNativeABI;
395 for (Module::iterator i = M.begin(), e = M.end(); i != e; ++i) {
396 if (!i->isIntrinsic() &&
398 i != DFSanUnionLoadFn &&
399 i != DFSanUnimplementedFn &&
400 i != DFSanSetLabelFn)
401 FnsToInstrument.push_back(&*i);
405 B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
406 ReadOnlyNoneAttrs = AttributeSet::get(*Ctx, AttributeSet::FunctionIndex, B);
408 // First, change the ABI of every function in the module. ABI-listed
409 // functions keep their original ABI and get a wrapper function.
410 for (std::vector<Function *>::iterator i = FnsToInstrument.begin(),
411 e = FnsToInstrument.end();
414 FunctionType *FT = F.getFunctionType();
416 if (FT->getNumParams() == 0 && !FT->isVarArg() &&
417 FT->getReturnType()->isVoidTy())
420 if (isInstrumented(&F)) {
421 if (getInstrumentedABI() == IA_Args) {
422 FunctionType *NewFT = getArgsFunctionType(FT);
423 Function *NewF = Function::Create(NewFT, F.getLinkage(), "", &M);
424 NewF->copyAttributesFrom(&F);
425 NewF->removeAttributes(
426 AttributeSet::ReturnIndex,
427 AttributeFuncs::typeIncompatible(NewFT->getReturnType(),
428 AttributeSet::ReturnIndex));
429 for (Function::arg_iterator FArg = F.arg_begin(),
430 NewFArg = NewF->arg_begin(),
431 FArgEnd = F.arg_end();
432 FArg != FArgEnd; ++FArg, ++NewFArg) {
433 FArg->replaceAllUsesWith(NewFArg);
435 NewF->getBasicBlockList().splice(NewF->begin(), F.getBasicBlockList());
437 for (Function::use_iterator ui = F.use_begin(), ue = F.use_end();
439 BlockAddress *BA = dyn_cast<BlockAddress>(ui.getUse().getUser());
442 BA->replaceAllUsesWith(
443 BlockAddress::get(NewF, BA->getBasicBlock()));
447 F.replaceAllUsesWith(
448 ConstantExpr::getBitCast(NewF, PointerType::getUnqual(FT)));
453 // Hopefully, nobody will try to indirectly call a vararg
455 } else if (FT->isVarArg()) {
456 UnwrappedFnMap[&F] = &F;
459 // Build a wrapper function for F. The wrapper simply calls F, and is
460 // added to FnsToInstrument so that any instrumentation according to its
461 // WrapperKind is done in the second pass below.
462 FunctionType *NewFT = getInstrumentedABI() == IA_Args
463 ? getArgsFunctionType(FT)
466 Function::Create(NewFT, GlobalValue::LinkOnceODRLinkage,
467 std::string("dfsw$") + F.getName(), &M);
468 NewF->copyAttributesFrom(&F);
469 NewF->removeAttributes(
470 AttributeSet::ReturnIndex,
471 AttributeFuncs::typeIncompatible(NewFT->getReturnType(),
472 AttributeSet::ReturnIndex));
473 if (getInstrumentedABI() == IA_TLS)
474 NewF->removeAttributes(AttributeSet::FunctionIndex,
477 BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", NewF);
478 std::vector<Value *> Args;
479 unsigned n = FT->getNumParams();
480 for (Function::arg_iterator ai = NewF->arg_begin(); n != 0; ++ai, --n)
481 Args.push_back(&*ai);
482 CallInst *CI = CallInst::Create(&F, Args, "", BB);
483 if (FT->getReturnType()->isVoidTy())
484 ReturnInst::Create(*Ctx, BB);
486 ReturnInst::Create(*Ctx, CI, BB);
488 Value *WrappedFnCst =
489 ConstantExpr::getBitCast(NewF, PointerType::getUnqual(FT));
490 F.replaceAllUsesWith(WrappedFnCst);
491 UnwrappedFnMap[WrappedFnCst] = &F;
494 if (!F.isDeclaration()) {
495 // This function is probably defining an interposition of an
496 // uninstrumented function and hence needs to keep the original ABI.
497 // But any functions it may call need to use the instrumented ABI, so
498 // we instrument it in a mode which preserves the original ABI.
499 FnsWithNativeABI.insert(&F);
501 // This code needs to rebuild the iterators, as they may be invalidated
502 // by the push_back, taking care that the new range does not include
503 // any functions added by this code.
504 size_t N = i - FnsToInstrument.begin(),
505 Count = e - FnsToInstrument.begin();
506 FnsToInstrument.push_back(&F);
507 i = FnsToInstrument.begin() + N;
508 e = FnsToInstrument.begin() + Count;
513 for (std::vector<Function *>::iterator i = FnsToInstrument.begin(),
514 e = FnsToInstrument.end();
516 if (!*i || (*i)->isDeclaration())
519 removeUnreachableBlocks(**i);
521 DFSanFunction DFSF(*this, *i, FnsWithNativeABI.count(*i));
523 // DFSanVisitor may create new basic blocks, which confuses df_iterator.
524 // Build a copy of the list before iterating over it.
525 llvm::SmallVector<BasicBlock *, 4> BBList;
526 std::copy(df_begin(&(*i)->getEntryBlock()), df_end(&(*i)->getEntryBlock()),
527 std::back_inserter(BBList));
529 for (llvm::SmallVector<BasicBlock *, 4>::iterator i = BBList.begin(),
532 Instruction *Inst = &(*i)->front();
534 // DFSanVisitor may split the current basic block, changing the current
535 // instruction's next pointer and moving the next instruction to the
536 // tail block from which we should continue.
537 Instruction *Next = Inst->getNextNode();
538 // DFSanVisitor may delete Inst, so keep track of whether it was a
540 bool IsTerminator = isa<TerminatorInst>(Inst);
541 if (!DFSF.SkipInsts.count(Inst))
542 DFSanVisitor(DFSF).visit(Inst);
549 // We will not necessarily be able to compute the shadow for every phi node
550 // until we have visited every block. Therefore, the code that handles phi
551 // nodes adds them to the PHIFixups list so that they can be properly
553 for (std::vector<std::pair<PHINode *, PHINode *> >::iterator
554 i = DFSF.PHIFixups.begin(),
555 e = DFSF.PHIFixups.end();
557 for (unsigned val = 0, n = i->first->getNumIncomingValues(); val != n;
559 i->second->setIncomingValue(
560 val, DFSF.getShadow(i->first->getIncomingValue(val)));
568 Value *DFSanFunction::getArgTLSPtr() {
572 return ArgTLSPtr = DFS.ArgTLS;
574 IRBuilder<> IRB(F->getEntryBlock().begin());
575 return ArgTLSPtr = IRB.CreateCall(DFS.GetArgTLS);
578 Value *DFSanFunction::getRetvalTLS() {
582 return RetvalTLSPtr = DFS.RetvalTLS;
584 IRBuilder<> IRB(F->getEntryBlock().begin());
585 return RetvalTLSPtr = IRB.CreateCall(DFS.GetRetvalTLS);
588 Value *DFSanFunction::getArgTLS(unsigned Idx, Instruction *Pos) {
589 IRBuilder<> IRB(Pos);
590 return IRB.CreateConstGEP2_64(getArgTLSPtr(), 0, Idx);
593 Value *DFSanFunction::getShadow(Value *V) {
594 if (!isa<Argument>(V) && !isa<Instruction>(V))
595 return DFS.ZeroShadow;
596 Value *&Shadow = ValShadowMap[V];
598 if (Argument *A = dyn_cast<Argument>(V)) {
600 return DFS.ZeroShadow;
602 case DataFlowSanitizer::IA_TLS: {
603 Value *ArgTLSPtr = getArgTLSPtr();
604 Instruction *ArgTLSPos =
605 DFS.ArgTLS ? &*F->getEntryBlock().begin()
606 : cast<Instruction>(ArgTLSPtr)->getNextNode();
607 IRBuilder<> IRB(ArgTLSPos);
608 Shadow = IRB.CreateLoad(getArgTLS(A->getArgNo(), ArgTLSPos));
611 case DataFlowSanitizer::IA_Args: {
612 unsigned ArgIdx = A->getArgNo() + F->getArgumentList().size() / 2;
613 Function::arg_iterator i = F->arg_begin();
617 assert(Shadow->getType() == DFS.ShadowTy);
622 Shadow = DFS.ZeroShadow;
628 void DFSanFunction::setShadow(Instruction *I, Value *Shadow) {
629 assert(!ValShadowMap.count(I));
630 assert(Shadow->getType() == DFS.ShadowTy);
631 ValShadowMap[I] = Shadow;
634 Value *DataFlowSanitizer::getShadowAddress(Value *Addr, Instruction *Pos) {
635 assert(Addr != RetvalTLS && "Reinstrumenting?");
636 IRBuilder<> IRB(Pos);
637 return IRB.CreateIntToPtr(
639 IRB.CreateAnd(IRB.CreatePtrToInt(Addr, IntptrTy), ShadowPtrMask),
644 // Generates IR to compute the union of the two given shadows, inserting it
645 // before Pos. Returns the computed union Value.
646 Value *DataFlowSanitizer::combineShadows(Value *V1, Value *V2,
648 if (V1 == ZeroShadow)
650 if (V2 == ZeroShadow)
654 IRBuilder<> IRB(Pos);
655 BasicBlock *Head = Pos->getParent();
656 Value *Ne = IRB.CreateICmpNE(V1, V2);
657 Instruction *NeInst = dyn_cast<Instruction>(Ne);
659 BranchInst *BI = cast<BranchInst>(SplitBlockAndInsertIfThen(
660 NeInst, /*Unreachable=*/ false, ColdCallWeights));
661 IRBuilder<> ThenIRB(BI);
662 CallInst *Call = ThenIRB.CreateCall2(DFSanUnionFn, V1, V2);
663 Call->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
664 Call->addAttribute(1, Attribute::ZExt);
665 Call->addAttribute(2, Attribute::ZExt);
667 BasicBlock *Tail = BI->getSuccessor(0);
668 PHINode *Phi = PHINode::Create(ShadowTy, 2, "", Tail->begin());
669 Phi->addIncoming(Call, Call->getParent());
670 Phi->addIncoming(ZeroShadow, Head);
679 // A convenience function which folds the shadows of each of the operands
680 // of the provided instruction Inst, inserting the IR before Inst. Returns
681 // the computed union Value.
682 Value *DFSanFunction::combineOperandShadows(Instruction *Inst) {
683 if (Inst->getNumOperands() == 0)
684 return DFS.ZeroShadow;
686 Value *Shadow = getShadow(Inst->getOperand(0));
687 for (unsigned i = 1, n = Inst->getNumOperands(); i != n; ++i) {
688 Shadow = DFS.combineShadows(Shadow, getShadow(Inst->getOperand(i)), Inst);
693 void DFSanVisitor::visitOperandShadowInst(Instruction &I) {
694 Value *CombinedShadow = DFSF.combineOperandShadows(&I);
695 DFSF.setShadow(&I, CombinedShadow);
698 // Generates IR to load shadow corresponding to bytes [Addr, Addr+Size), where
699 // Addr has alignment Align, and take the union of each of those shadows.
700 Value *DFSanFunction::loadShadow(Value *Addr, uint64_t Size, uint64_t Align,
702 if (AllocaInst *AI = dyn_cast<AllocaInst>(Addr)) {
703 llvm::DenseMap<AllocaInst *, AllocaInst *>::iterator i =
704 AllocaShadowMap.find(AI);
705 if (i != AllocaShadowMap.end()) {
706 IRBuilder<> IRB(Pos);
707 return IRB.CreateLoad(i->second);
711 uint64_t ShadowAlign = Align * DFS.ShadowWidth / 8;
712 SmallVector<Value *, 2> Objs;
713 GetUnderlyingObjects(Addr, Objs, DFS.DL);
714 bool AllConstants = true;
715 for (SmallVector<Value *, 2>::iterator i = Objs.begin(), e = Objs.end();
717 if (isa<Function>(*i) || isa<BlockAddress>(*i))
719 if (isa<GlobalVariable>(*i) && cast<GlobalVariable>(*i)->isConstant())
722 AllConstants = false;
726 return DFS.ZeroShadow;
728 Value *ShadowAddr = DFS.getShadowAddress(Addr, Pos);
731 return DFS.ZeroShadow;
733 LoadInst *LI = new LoadInst(ShadowAddr, "", Pos);
734 LI->setAlignment(ShadowAlign);
738 IRBuilder<> IRB(Pos);
740 IRB.CreateGEP(ShadowAddr, ConstantInt::get(DFS.IntptrTy, 1));
741 return DFS.combineShadows(IRB.CreateAlignedLoad(ShadowAddr, ShadowAlign),
742 IRB.CreateAlignedLoad(ShadowAddr1, ShadowAlign),
746 if (Size % (64 / DFS.ShadowWidth) == 0) {
747 // Fast path for the common case where each byte has identical shadow: load
748 // shadow 64 bits at a time, fall out to a __dfsan_union_load call if any
749 // shadow is non-equal.
750 BasicBlock *FallbackBB = BasicBlock::Create(*DFS.Ctx, "", F);
751 IRBuilder<> FallbackIRB(FallbackBB);
752 CallInst *FallbackCall = FallbackIRB.CreateCall2(
753 DFS.DFSanUnionLoadFn, ShadowAddr, ConstantInt::get(DFS.IntptrTy, Size));
754 FallbackCall->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
756 // Compare each of the shadows stored in the loaded 64 bits to each other,
757 // by computing (WideShadow rotl ShadowWidth) == WideShadow.
758 IRBuilder<> IRB(Pos);
760 IRB.CreateBitCast(ShadowAddr, Type::getInt64PtrTy(*DFS.Ctx));
761 Value *WideShadow = IRB.CreateAlignedLoad(WideAddr, ShadowAlign);
762 Value *TruncShadow = IRB.CreateTrunc(WideShadow, DFS.ShadowTy);
763 Value *ShlShadow = IRB.CreateShl(WideShadow, DFS.ShadowWidth);
764 Value *ShrShadow = IRB.CreateLShr(WideShadow, 64 - DFS.ShadowWidth);
765 Value *RotShadow = IRB.CreateOr(ShlShadow, ShrShadow);
766 Value *ShadowsEq = IRB.CreateICmpEQ(WideShadow, RotShadow);
768 BasicBlock *Head = Pos->getParent();
769 BasicBlock *Tail = Head->splitBasicBlock(Pos);
770 // In the following code LastBr will refer to the previous basic block's
771 // conditional branch instruction, whose true successor is fixed up to point
772 // to the next block during the loop below or to the tail after the final
774 BranchInst *LastBr = BranchInst::Create(FallbackBB, FallbackBB, ShadowsEq);
775 ReplaceInstWithInst(Head->getTerminator(), LastBr);
777 for (uint64_t Ofs = 64 / DFS.ShadowWidth; Ofs != Size;
778 Ofs += 64 / DFS.ShadowWidth) {
779 BasicBlock *NextBB = BasicBlock::Create(*DFS.Ctx, "", F);
780 IRBuilder<> NextIRB(NextBB);
781 WideAddr = NextIRB.CreateGEP(WideAddr, ConstantInt::get(DFS.IntptrTy, 1));
782 Value *NextWideShadow = NextIRB.CreateAlignedLoad(WideAddr, ShadowAlign);
783 ShadowsEq = NextIRB.CreateICmpEQ(WideShadow, NextWideShadow);
784 LastBr->setSuccessor(0, NextBB);
785 LastBr = NextIRB.CreateCondBr(ShadowsEq, FallbackBB, FallbackBB);
788 LastBr->setSuccessor(0, Tail);
789 FallbackIRB.CreateBr(Tail);
790 PHINode *Shadow = PHINode::Create(DFS.ShadowTy, 2, "", &Tail->front());
791 Shadow->addIncoming(FallbackCall, FallbackBB);
792 Shadow->addIncoming(TruncShadow, LastBr->getParent());
796 IRBuilder<> IRB(Pos);
797 CallInst *FallbackCall = IRB.CreateCall2(
798 DFS.DFSanUnionLoadFn, ShadowAddr, ConstantInt::get(DFS.IntptrTy, Size));
799 FallbackCall->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
803 void DFSanVisitor::visitLoadInst(LoadInst &LI) {
804 uint64_t Size = DFSF.DFS.DL->getTypeStoreSize(LI.getType());
806 if (ClPreserveAlignment) {
807 Align = LI.getAlignment();
809 Align = DFSF.DFS.DL->getABITypeAlignment(LI.getType());
813 IRBuilder<> IRB(&LI);
814 Value *LoadedShadow =
815 DFSF.loadShadow(LI.getPointerOperand(), Size, Align, &LI);
816 Value *PtrShadow = DFSF.getShadow(LI.getPointerOperand());
817 DFSF.setShadow(&LI, DFSF.DFS.combineShadows(LoadedShadow, PtrShadow, &LI));
820 void DFSanFunction::storeShadow(Value *Addr, uint64_t Size, uint64_t Align,
821 Value *Shadow, Instruction *Pos) {
822 if (AllocaInst *AI = dyn_cast<AllocaInst>(Addr)) {
823 llvm::DenseMap<AllocaInst *, AllocaInst *>::iterator i =
824 AllocaShadowMap.find(AI);
825 if (i != AllocaShadowMap.end()) {
826 IRBuilder<> IRB(Pos);
827 IRB.CreateStore(Shadow, i->second);
832 uint64_t ShadowAlign = Align * DFS.ShadowWidth / 8;
833 IRBuilder<> IRB(Pos);
834 Value *ShadowAddr = DFS.getShadowAddress(Addr, Pos);
835 if (Shadow == DFS.ZeroShadow) {
836 IntegerType *ShadowTy = IntegerType::get(*DFS.Ctx, Size * DFS.ShadowWidth);
837 Value *ExtZeroShadow = ConstantInt::get(ShadowTy, 0);
838 Value *ExtShadowAddr =
839 IRB.CreateBitCast(ShadowAddr, PointerType::getUnqual(ShadowTy));
840 IRB.CreateAlignedStore(ExtZeroShadow, ExtShadowAddr, ShadowAlign);
844 const unsigned ShadowVecSize = 128 / DFS.ShadowWidth;
846 if (Size >= ShadowVecSize) {
847 VectorType *ShadowVecTy = VectorType::get(DFS.ShadowTy, ShadowVecSize);
848 Value *ShadowVec = UndefValue::get(ShadowVecTy);
849 for (unsigned i = 0; i != ShadowVecSize; ++i) {
850 ShadowVec = IRB.CreateInsertElement(
851 ShadowVec, Shadow, ConstantInt::get(Type::getInt32Ty(*DFS.Ctx), i));
853 Value *ShadowVecAddr =
854 IRB.CreateBitCast(ShadowAddr, PointerType::getUnqual(ShadowVecTy));
856 Value *CurShadowVecAddr = IRB.CreateConstGEP1_32(ShadowVecAddr, Offset);
857 IRB.CreateAlignedStore(ShadowVec, CurShadowVecAddr, ShadowAlign);
858 Size -= ShadowVecSize;
860 } while (Size >= ShadowVecSize);
861 Offset *= ShadowVecSize;
864 Value *CurShadowAddr = IRB.CreateConstGEP1_32(ShadowAddr, Offset);
865 IRB.CreateAlignedStore(Shadow, CurShadowAddr, ShadowAlign);
871 void DFSanVisitor::visitStoreInst(StoreInst &SI) {
873 DFSF.DFS.DL->getTypeStoreSize(SI.getValueOperand()->getType());
875 if (ClPreserveAlignment) {
876 Align = SI.getAlignment();
878 Align = DFSF.DFS.DL->getABITypeAlignment(SI.getValueOperand()->getType());
882 DFSF.storeShadow(SI.getPointerOperand(), Size, Align,
883 DFSF.getShadow(SI.getValueOperand()), &SI);
886 void DFSanVisitor::visitBinaryOperator(BinaryOperator &BO) {
887 visitOperandShadowInst(BO);
890 void DFSanVisitor::visitCastInst(CastInst &CI) { visitOperandShadowInst(CI); }
892 void DFSanVisitor::visitCmpInst(CmpInst &CI) { visitOperandShadowInst(CI); }
894 void DFSanVisitor::visitGetElementPtrInst(GetElementPtrInst &GEPI) {
895 visitOperandShadowInst(GEPI);
898 void DFSanVisitor::visitExtractElementInst(ExtractElementInst &I) {
899 visitOperandShadowInst(I);
902 void DFSanVisitor::visitInsertElementInst(InsertElementInst &I) {
903 visitOperandShadowInst(I);
906 void DFSanVisitor::visitShuffleVectorInst(ShuffleVectorInst &I) {
907 visitOperandShadowInst(I);
910 void DFSanVisitor::visitExtractValueInst(ExtractValueInst &I) {
911 visitOperandShadowInst(I);
914 void DFSanVisitor::visitInsertValueInst(InsertValueInst &I) {
915 visitOperandShadowInst(I);
918 void DFSanVisitor::visitAllocaInst(AllocaInst &I) {
919 bool AllLoadsStores = true;
920 for (Instruction::use_iterator i = I.use_begin(), e = I.use_end(); i != e;
922 if (isa<LoadInst>(*i))
925 if (StoreInst *SI = dyn_cast<StoreInst>(*i)) {
926 if (SI->getPointerOperand() == &I)
930 AllLoadsStores = false;
933 if (AllLoadsStores) {
935 DFSF.AllocaShadowMap[&I] = IRB.CreateAlloca(DFSF.DFS.ShadowTy);
937 DFSF.setShadow(&I, DFSF.DFS.ZeroShadow);
940 void DFSanVisitor::visitSelectInst(SelectInst &I) {
941 Value *CondShadow = DFSF.getShadow(I.getCondition());
942 Value *TrueShadow = DFSF.getShadow(I.getTrueValue());
943 Value *FalseShadow = DFSF.getShadow(I.getFalseValue());
945 if (isa<VectorType>(I.getCondition()->getType())) {
947 &I, DFSF.DFS.combineShadows(
949 DFSF.DFS.combineShadows(TrueShadow, FalseShadow, &I), &I));
952 if (TrueShadow == FalseShadow) {
953 ShadowSel = TrueShadow;
956 SelectInst::Create(I.getCondition(), TrueShadow, FalseShadow, "", &I);
958 DFSF.setShadow(&I, DFSF.DFS.combineShadows(CondShadow, ShadowSel, &I));
962 void DFSanVisitor::visitMemSetInst(MemSetInst &I) {
964 Value *ValShadow = DFSF.getShadow(I.getValue());
966 DFSF.DFS.DFSanSetLabelFn, ValShadow,
967 IRB.CreateBitCast(I.getDest(), Type::getInt8PtrTy(*DFSF.DFS.Ctx)),
968 IRB.CreateZExtOrTrunc(I.getLength(), DFSF.DFS.IntptrTy));
971 void DFSanVisitor::visitMemTransferInst(MemTransferInst &I) {
973 Value *DestShadow = DFSF.DFS.getShadowAddress(I.getDest(), &I);
974 Value *SrcShadow = DFSF.DFS.getShadowAddress(I.getSource(), &I);
975 Value *LenShadow = IRB.CreateMul(
977 ConstantInt::get(I.getLength()->getType(), DFSF.DFS.ShadowWidth / 8));
979 if (ClPreserveAlignment) {
980 AlignShadow = IRB.CreateMul(I.getAlignmentCst(),
981 ConstantInt::get(I.getAlignmentCst()->getType(),
982 DFSF.DFS.ShadowWidth / 8));
984 AlignShadow = ConstantInt::get(I.getAlignmentCst()->getType(),
985 DFSF.DFS.ShadowWidth / 8);
987 Type *Int8Ptr = Type::getInt8PtrTy(*DFSF.DFS.Ctx);
988 DestShadow = IRB.CreateBitCast(DestShadow, Int8Ptr);
989 SrcShadow = IRB.CreateBitCast(SrcShadow, Int8Ptr);
990 IRB.CreateCall5(I.getCalledValue(), DestShadow, SrcShadow, LenShadow,
991 AlignShadow, I.getVolatileCst());
994 void DFSanVisitor::visitReturnInst(ReturnInst &RI) {
995 if (!DFSF.IsNativeABI && RI.getReturnValue()) {
997 case DataFlowSanitizer::IA_TLS: {
998 Value *S = DFSF.getShadow(RI.getReturnValue());
999 IRBuilder<> IRB(&RI);
1000 IRB.CreateStore(S, DFSF.getRetvalTLS());
1003 case DataFlowSanitizer::IA_Args: {
1004 IRBuilder<> IRB(&RI);
1005 Type *RT = DFSF.F->getFunctionType()->getReturnType();
1007 IRB.CreateInsertValue(UndefValue::get(RT), RI.getReturnValue(), 0);
1009 IRB.CreateInsertValue(InsVal, DFSF.getShadow(RI.getReturnValue()), 1);
1010 RI.setOperand(0, InsShadow);
1017 void DFSanVisitor::visitCallSite(CallSite CS) {
1018 Function *F = CS.getCalledFunction();
1019 if ((F && F->isIntrinsic()) || isa<InlineAsm>(CS.getCalledValue())) {
1020 visitOperandShadowInst(*CS.getInstruction());
1024 IRBuilder<> IRB(CS.getInstruction());
1026 DenseMap<Value *, Function *>::iterator i =
1027 DFSF.DFS.UnwrappedFnMap.find(CS.getCalledValue());
1028 if (i != DFSF.DFS.UnwrappedFnMap.end()) {
1029 Function *F = i->second;
1030 switch (DFSF.DFS.getWrapperKind(F)) {
1031 case DataFlowSanitizer::WK_Warning: {
1032 CS.setCalledFunction(F);
1033 IRB.CreateCall(DFSF.DFS.DFSanUnimplementedFn,
1034 IRB.CreateGlobalStringPtr(F->getName()));
1035 DFSF.setShadow(CS.getInstruction(), DFSF.DFS.ZeroShadow);
1038 case DataFlowSanitizer::WK_Discard: {
1039 CS.setCalledFunction(F);
1040 DFSF.setShadow(CS.getInstruction(), DFSF.DFS.ZeroShadow);
1043 case DataFlowSanitizer::WK_Functional: {
1044 CS.setCalledFunction(F);
1045 visitOperandShadowInst(*CS.getInstruction());
1048 case DataFlowSanitizer::WK_Custom: {
1049 // Don't try to handle invokes of custom functions, it's too complicated.
1050 // Instead, invoke the dfsw$ wrapper, which will in turn call the __dfsw_
1052 if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) {
1053 FunctionType *FT = F->getFunctionType();
1054 FunctionType *CustomFT = DFSF.DFS.getCustomFunctionType(FT);
1055 std::string CustomFName = "__dfsw_";
1056 CustomFName += F->getName();
1058 DFSF.DFS.Mod->getOrInsertFunction(CustomFName, CustomFT);
1059 if (Function *CustomFn = dyn_cast<Function>(CustomF)) {
1060 CustomFn->copyAttributesFrom(F);
1062 // Custom functions returning non-void will write to the return label.
1063 if (!FT->getReturnType()->isVoidTy()) {
1064 CustomFn->removeAttributes(AttributeSet::FunctionIndex,
1065 DFSF.DFS.ReadOnlyNoneAttrs);
1069 std::vector<Value *> Args;
1071 CallSite::arg_iterator i = CS.arg_begin();
1072 for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
1076 for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
1077 Args.push_back(DFSF.getShadow(*i));
1079 if (!FT->getReturnType()->isVoidTy()) {
1080 if (!DFSF.LabelReturnAlloca) {
1081 DFSF.LabelReturnAlloca =
1082 new AllocaInst(DFSF.DFS.ShadowTy, "labelreturn",
1083 DFSF.F->getEntryBlock().begin());
1085 Args.push_back(DFSF.LabelReturnAlloca);
1088 CallInst *CustomCI = IRB.CreateCall(CustomF, Args);
1089 CustomCI->setCallingConv(CI->getCallingConv());
1090 CustomCI->setAttributes(CI->getAttributes());
1092 if (!FT->getReturnType()->isVoidTy()) {
1093 LoadInst *LabelLoad = IRB.CreateLoad(DFSF.LabelReturnAlloca);
1094 DFSF.setShadow(CustomCI, LabelLoad);
1097 CI->replaceAllUsesWith(CustomCI);
1098 CI->eraseFromParent();
1106 FunctionType *FT = cast<FunctionType>(
1107 CS.getCalledValue()->getType()->getPointerElementType());
1108 if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_TLS) {
1109 for (unsigned i = 0, n = FT->getNumParams(); i != n; ++i) {
1110 IRB.CreateStore(DFSF.getShadow(CS.getArgument(i)),
1111 DFSF.getArgTLS(i, CS.getInstruction()));
1115 Instruction *Next = 0;
1116 if (!CS.getType()->isVoidTy()) {
1117 if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
1118 if (II->getNormalDest()->getSinglePredecessor()) {
1119 Next = II->getNormalDest()->begin();
1122 SplitEdge(II->getParent(), II->getNormalDest(), &DFSF.DFS);
1123 Next = NewBB->begin();
1126 Next = CS->getNextNode();
1129 if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_TLS) {
1130 IRBuilder<> NextIRB(Next);
1131 LoadInst *LI = NextIRB.CreateLoad(DFSF.getRetvalTLS());
1132 DFSF.SkipInsts.insert(LI);
1133 DFSF.setShadow(CS.getInstruction(), LI);
1137 // Do all instrumentation for IA_Args down here to defer tampering with the
1138 // CFG in a way that SplitEdge may be able to detect.
1139 if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_Args) {
1140 FunctionType *NewFT = DFSF.DFS.getArgsFunctionType(FT);
1142 IRB.CreateBitCast(CS.getCalledValue(), PointerType::getUnqual(NewFT));
1143 std::vector<Value *> Args;
1145 CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1146 for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
1150 for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
1151 Args.push_back(DFSF.getShadow(*i));
1153 if (FT->isVarArg()) {
1154 unsigned VarArgSize = CS.arg_size() - FT->getNumParams();
1155 ArrayType *VarArgArrayTy = ArrayType::get(DFSF.DFS.ShadowTy, VarArgSize);
1156 AllocaInst *VarArgShadow =
1157 new AllocaInst(VarArgArrayTy, "", DFSF.F->getEntryBlock().begin());
1158 Args.push_back(IRB.CreateConstGEP2_32(VarArgShadow, 0, 0));
1159 for (unsigned n = 0; i != e; ++i, ++n) {
1160 IRB.CreateStore(DFSF.getShadow(*i),
1161 IRB.CreateConstGEP2_32(VarArgShadow, 0, n));
1167 if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
1168 NewCS = IRB.CreateInvoke(Func, II->getNormalDest(), II->getUnwindDest(),
1171 NewCS = IRB.CreateCall(Func, Args);
1173 NewCS.setCallingConv(CS.getCallingConv());
1174 NewCS.setAttributes(CS.getAttributes().removeAttributes(
1175 *DFSF.DFS.Ctx, AttributeSet::ReturnIndex,
1176 AttributeFuncs::typeIncompatible(NewCS.getInstruction()->getType(),
1177 AttributeSet::ReturnIndex)));
1180 ExtractValueInst *ExVal =
1181 ExtractValueInst::Create(NewCS.getInstruction(), 0, "", Next);
1182 DFSF.SkipInsts.insert(ExVal);
1183 ExtractValueInst *ExShadow =
1184 ExtractValueInst::Create(NewCS.getInstruction(), 1, "", Next);
1185 DFSF.SkipInsts.insert(ExShadow);
1186 DFSF.setShadow(ExVal, ExShadow);
1188 CS.getInstruction()->replaceAllUsesWith(ExVal);
1191 CS.getInstruction()->eraseFromParent();
1195 void DFSanVisitor::visitPHINode(PHINode &PN) {
1197 PHINode::Create(DFSF.DFS.ShadowTy, PN.getNumIncomingValues(), "", &PN);
1199 // Give the shadow phi node valid predecessors to fool SplitEdge into working.
1200 Value *UndefShadow = UndefValue::get(DFSF.DFS.ShadowTy);
1201 for (PHINode::block_iterator i = PN.block_begin(), e = PN.block_end(); i != e;
1203 ShadowPN->addIncoming(UndefShadow, *i);
1206 DFSF.PHIFixups.push_back(std::make_pair(&PN, ShadowPN));
1207 DFSF.setShadow(&PN, ShadowPN);