[dfsan] Move combineShadows to DFSanFunction in preparation for it to use a domtree.
[oota-llvm.git] / lib / Transforms / Instrumentation / DataFlowSanitizer.cpp
1 //===-- DataFlowSanitizer.cpp - dynamic data flow analysis ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 /// \file
10 /// This file is a part of DataFlowSanitizer, a generalised dynamic data flow
11 /// analysis.
12 ///
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.
17 ///
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:
22 ///
23 /// +--------------------+ 0x800000000000 (top of memory)
24 /// | application memory |
25 /// +--------------------+ 0x700000008000 (kAppAddr)
26 /// |                    |
27 /// |       unused       |
28 /// |                    |
29 /// +--------------------+ 0x200200000000 (kUnusedAddr)
30 /// |    union table     |
31 /// +--------------------+ 0x200000000000 (kUnionTableAddr)
32 /// |   shadow memory    |
33 /// +--------------------+ 0x000000010000 (kShadowAddr)
34 /// | reserved by kernel |
35 /// +--------------------+ 0x000000000000
36 ///
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.
43 ///
44 /// For more information, please refer to the design document:
45 /// http://clang.llvm.org/docs/DataFlowSanitizerDesign.html
46
47 #include "llvm/Transforms/Instrumentation.h"
48 #include "llvm/ADT/DenseMap.h"
49 #include "llvm/ADT/DenseSet.h"
50 #include "llvm/ADT/DepthFirstIterator.h"
51 #include "llvm/ADT/StringExtras.h"
52 #include "llvm/Analysis/ValueTracking.h"
53 #include "llvm/IR/IRBuilder.h"
54 #include "llvm/IR/InlineAsm.h"
55 #include "llvm/IR/InstVisitor.h"
56 #include "llvm/IR/LLVMContext.h"
57 #include "llvm/IR/MDBuilder.h"
58 #include "llvm/IR/Type.h"
59 #include "llvm/IR/Value.h"
60 #include "llvm/Pass.h"
61 #include "llvm/Support/CommandLine.h"
62 #include "llvm/Support/SpecialCaseList.h"
63 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
64 #include "llvm/Transforms/Utils/Local.h"
65 #include <iterator>
66
67 using namespace llvm;
68
69 // The -dfsan-preserve-alignment flag controls whether this pass assumes that
70 // alignment requirements provided by the input IR are correct.  For example,
71 // if the input IR contains a load with alignment 8, this flag will cause
72 // the shadow load to have alignment 16.  This flag is disabled by default as
73 // we have unfortunately encountered too much code (including Clang itself;
74 // see PR14291) which performs misaligned access.
75 static cl::opt<bool> ClPreserveAlignment(
76     "dfsan-preserve-alignment",
77     cl::desc("respect alignment requirements provided by input IR"), cl::Hidden,
78     cl::init(false));
79
80 // The ABI list file controls how shadow parameters are passed.  The pass treats
81 // every function labelled "uninstrumented" in the ABI list file as conforming
82 // to the "native" (i.e. unsanitized) ABI.  Unless the ABI list contains
83 // additional annotations for those functions, a call to one of those functions
84 // will produce a warning message, as the labelling behaviour of the function is
85 // unknown.  The other supported annotations are "functional" and "discard",
86 // which are described below under DataFlowSanitizer::WrapperKind.
87 static cl::opt<std::string> ClABIListFile(
88     "dfsan-abilist",
89     cl::desc("File listing native ABI functions and how the pass treats them"),
90     cl::Hidden);
91
92 // Controls whether the pass uses IA_Args or IA_TLS as the ABI for instrumented
93 // functions (see DataFlowSanitizer::InstrumentedABI below).
94 static cl::opt<bool> ClArgsABI(
95     "dfsan-args-abi",
96     cl::desc("Use the argument ABI rather than the TLS ABI"),
97     cl::Hidden);
98
99 // Controls whether the pass includes or ignores the labels of pointers in load
100 // instructions.
101 static cl::opt<bool> ClCombinePointerLabelsOnLoad(
102     "dfsan-combine-pointer-labels-on-load",
103     cl::desc("Combine the label of the pointer with the label of the data when "
104              "loading from memory."),
105     cl::Hidden, cl::init(true));
106
107 // Controls whether the pass includes or ignores the labels of pointers in
108 // stores instructions.
109 static cl::opt<bool> ClCombinePointerLabelsOnStore(
110     "dfsan-combine-pointer-labels-on-store",
111     cl::desc("Combine the label of the pointer with the label of the data when "
112              "storing in memory."),
113     cl::Hidden, cl::init(false));
114
115 static cl::opt<bool> ClDebugNonzeroLabels(
116     "dfsan-debug-nonzero-labels",
117     cl::desc("Insert calls to __dfsan_nonzero_label on observing a parameter, "
118              "load or return with a nonzero label"),
119     cl::Hidden);
120
121 namespace {
122
123 StringRef GetGlobalTypeString(const GlobalValue &G) {
124   // Types of GlobalVariables are always pointer types.
125   Type *GType = G.getType()->getElementType();
126   // For now we support blacklisting struct types only.
127   if (StructType *SGType = dyn_cast<StructType>(GType)) {
128     if (!SGType->isLiteral())
129       return SGType->getName();
130   }
131   return "<unknown type>";
132 }
133
134 class DFSanABIList {
135   std::unique_ptr<SpecialCaseList> SCL;
136
137  public:
138   DFSanABIList(SpecialCaseList *SCL) : SCL(SCL) {}
139
140   /// Returns whether either this function or its source file are listed in the
141   /// given category.
142   bool isIn(const Function &F, const StringRef Category) const {
143     return isIn(*F.getParent(), Category) ||
144            SCL->inSection("fun", F.getName(), Category);
145   }
146
147   /// Returns whether this global alias is listed in the given category.
148   ///
149   /// If GA aliases a function, the alias's name is matched as a function name
150   /// would be.  Similarly, aliases of globals are matched like globals.
151   bool isIn(const GlobalAlias &GA, const StringRef Category) const {
152     if (isIn(*GA.getParent(), Category))
153       return true;
154
155     if (isa<FunctionType>(GA.getType()->getElementType()))
156       return SCL->inSection("fun", GA.getName(), Category);
157
158     return SCL->inSection("global", GA.getName(), Category) ||
159            SCL->inSection("type", GetGlobalTypeString(GA), Category);
160   }
161
162   /// Returns whether this module is listed in the given category.
163   bool isIn(const Module &M, const StringRef Category) const {
164     return SCL->inSection("src", M.getModuleIdentifier(), Category);
165   }
166 };
167
168 class DataFlowSanitizer : public ModulePass {
169   friend struct DFSanFunction;
170   friend class DFSanVisitor;
171
172   enum {
173     ShadowWidth = 16
174   };
175
176   /// Which ABI should be used for instrumented functions?
177   enum InstrumentedABI {
178     /// Argument and return value labels are passed through additional
179     /// arguments and by modifying the return type.
180     IA_Args,
181
182     /// Argument and return value labels are passed through TLS variables
183     /// __dfsan_arg_tls and __dfsan_retval_tls.
184     IA_TLS
185   };
186
187   /// How should calls to uninstrumented functions be handled?
188   enum WrapperKind {
189     /// This function is present in an uninstrumented form but we don't know
190     /// how it should be handled.  Print a warning and call the function anyway.
191     /// Don't label the return value.
192     WK_Warning,
193
194     /// This function does not write to (user-accessible) memory, and its return
195     /// value is unlabelled.
196     WK_Discard,
197
198     /// This function does not write to (user-accessible) memory, and the label
199     /// of its return value is the union of the label of its arguments.
200     WK_Functional,
201
202     /// Instead of calling the function, a custom wrapper __dfsw_F is called,
203     /// where F is the name of the function.  This function may wrap the
204     /// original function or provide its own implementation.  This is similar to
205     /// the IA_Args ABI, except that IA_Args uses a struct return type to
206     /// pass the return value shadow in a register, while WK_Custom uses an
207     /// extra pointer argument to return the shadow.  This allows the wrapped
208     /// form of the function type to be expressed in C.
209     WK_Custom
210   };
211
212   const DataLayout *DL;
213   Module *Mod;
214   LLVMContext *Ctx;
215   IntegerType *ShadowTy;
216   PointerType *ShadowPtrTy;
217   IntegerType *IntptrTy;
218   ConstantInt *ZeroShadow;
219   ConstantInt *ShadowPtrMask;
220   ConstantInt *ShadowPtrMul;
221   Constant *ArgTLS;
222   Constant *RetvalTLS;
223   void *(*GetArgTLSPtr)();
224   void *(*GetRetvalTLSPtr)();
225   Constant *GetArgTLS;
226   Constant *GetRetvalTLS;
227   FunctionType *DFSanUnionFnTy;
228   FunctionType *DFSanUnionLoadFnTy;
229   FunctionType *DFSanUnimplementedFnTy;
230   FunctionType *DFSanSetLabelFnTy;
231   FunctionType *DFSanNonzeroLabelFnTy;
232   Constant *DFSanUnionFn;
233   Constant *DFSanUnionLoadFn;
234   Constant *DFSanUnimplementedFn;
235   Constant *DFSanSetLabelFn;
236   Constant *DFSanNonzeroLabelFn;
237   MDNode *ColdCallWeights;
238   DFSanABIList ABIList;
239   DenseMap<Value *, Function *> UnwrappedFnMap;
240   AttributeSet ReadOnlyNoneAttrs;
241
242   Value *getShadowAddress(Value *Addr, Instruction *Pos);
243   bool isInstrumented(const Function *F);
244   bool isInstrumented(const GlobalAlias *GA);
245   FunctionType *getArgsFunctionType(FunctionType *T);
246   FunctionType *getTrampolineFunctionType(FunctionType *T);
247   FunctionType *getCustomFunctionType(FunctionType *T);
248   InstrumentedABI getInstrumentedABI();
249   WrapperKind getWrapperKind(Function *F);
250   void addGlobalNamePrefix(GlobalValue *GV);
251   Function *buildWrapperFunction(Function *F, StringRef NewFName,
252                                  GlobalValue::LinkageTypes NewFLink,
253                                  FunctionType *NewFT);
254   Constant *getOrBuildTrampolineFunction(FunctionType *FT, StringRef FName);
255
256  public:
257   DataFlowSanitizer(StringRef ABIListFile = StringRef(),
258                     void *(*getArgTLS)() = nullptr,
259                     void *(*getRetValTLS)() = nullptr);
260   static char ID;
261   bool doInitialization(Module &M) override;
262   bool runOnModule(Module &M) override;
263 };
264
265 struct DFSanFunction {
266   DataFlowSanitizer &DFS;
267   Function *F;
268   DataFlowSanitizer::InstrumentedABI IA;
269   bool IsNativeABI;
270   Value *ArgTLSPtr;
271   Value *RetvalTLSPtr;
272   AllocaInst *LabelReturnAlloca;
273   DenseMap<Value *, Value *> ValShadowMap;
274   DenseMap<AllocaInst *, AllocaInst *> AllocaShadowMap;
275   std::vector<std::pair<PHINode *, PHINode *> > PHIFixups;
276   DenseSet<Instruction *> SkipInsts;
277   DenseSet<Value *> NonZeroChecks;
278
279   DFSanFunction(DataFlowSanitizer &DFS, Function *F, bool IsNativeABI)
280       : DFS(DFS), F(F), IA(DFS.getInstrumentedABI()),
281         IsNativeABI(IsNativeABI), ArgTLSPtr(nullptr), RetvalTLSPtr(nullptr),
282         LabelReturnAlloca(nullptr) {}
283   Value *getArgTLSPtr();
284   Value *getArgTLS(unsigned Index, Instruction *Pos);
285   Value *getRetvalTLS();
286   Value *getShadow(Value *V);
287   void setShadow(Instruction *I, Value *Shadow);
288   Value *combineShadows(Value *V1, Value *V2, Instruction *Pos);
289   Value *combineOperandShadows(Instruction *Inst);
290   Value *loadShadow(Value *ShadowAddr, uint64_t Size, uint64_t Align,
291                     Instruction *Pos);
292   void storeShadow(Value *Addr, uint64_t Size, uint64_t Align, Value *Shadow,
293                    Instruction *Pos);
294 };
295
296 class DFSanVisitor : public InstVisitor<DFSanVisitor> {
297  public:
298   DFSanFunction &DFSF;
299   DFSanVisitor(DFSanFunction &DFSF) : DFSF(DFSF) {}
300
301   void visitOperandShadowInst(Instruction &I);
302
303   void visitBinaryOperator(BinaryOperator &BO);
304   void visitCastInst(CastInst &CI);
305   void visitCmpInst(CmpInst &CI);
306   void visitGetElementPtrInst(GetElementPtrInst &GEPI);
307   void visitLoadInst(LoadInst &LI);
308   void visitStoreInst(StoreInst &SI);
309   void visitReturnInst(ReturnInst &RI);
310   void visitCallSite(CallSite CS);
311   void visitPHINode(PHINode &PN);
312   void visitExtractElementInst(ExtractElementInst &I);
313   void visitInsertElementInst(InsertElementInst &I);
314   void visitShuffleVectorInst(ShuffleVectorInst &I);
315   void visitExtractValueInst(ExtractValueInst &I);
316   void visitInsertValueInst(InsertValueInst &I);
317   void visitAllocaInst(AllocaInst &I);
318   void visitSelectInst(SelectInst &I);
319   void visitMemSetInst(MemSetInst &I);
320   void visitMemTransferInst(MemTransferInst &I);
321 };
322
323 }
324
325 char DataFlowSanitizer::ID;
326 INITIALIZE_PASS(DataFlowSanitizer, "dfsan",
327                 "DataFlowSanitizer: dynamic data flow analysis.", false, false)
328
329 ModulePass *llvm::createDataFlowSanitizerPass(StringRef ABIListFile,
330                                               void *(*getArgTLS)(),
331                                               void *(*getRetValTLS)()) {
332   return new DataFlowSanitizer(ABIListFile, getArgTLS, getRetValTLS);
333 }
334
335 DataFlowSanitizer::DataFlowSanitizer(StringRef ABIListFile,
336                                      void *(*getArgTLS)(),
337                                      void *(*getRetValTLS)())
338     : ModulePass(ID), GetArgTLSPtr(getArgTLS), GetRetvalTLSPtr(getRetValTLS),
339       ABIList(SpecialCaseList::createOrDie(ABIListFile.empty() ? ClABIListFile
340                                                                : ABIListFile)) {
341 }
342
343 FunctionType *DataFlowSanitizer::getArgsFunctionType(FunctionType *T) {
344   llvm::SmallVector<Type *, 4> ArgTypes;
345   std::copy(T->param_begin(), T->param_end(), std::back_inserter(ArgTypes));
346   for (unsigned i = 0, e = T->getNumParams(); i != e; ++i)
347     ArgTypes.push_back(ShadowTy);
348   if (T->isVarArg())
349     ArgTypes.push_back(ShadowPtrTy);
350   Type *RetType = T->getReturnType();
351   if (!RetType->isVoidTy())
352     RetType = StructType::get(RetType, ShadowTy, (Type *)nullptr);
353   return FunctionType::get(RetType, ArgTypes, T->isVarArg());
354 }
355
356 FunctionType *DataFlowSanitizer::getTrampolineFunctionType(FunctionType *T) {
357   assert(!T->isVarArg());
358   llvm::SmallVector<Type *, 4> ArgTypes;
359   ArgTypes.push_back(T->getPointerTo());
360   std::copy(T->param_begin(), T->param_end(), std::back_inserter(ArgTypes));
361   for (unsigned i = 0, e = T->getNumParams(); i != e; ++i)
362     ArgTypes.push_back(ShadowTy);
363   Type *RetType = T->getReturnType();
364   if (!RetType->isVoidTy())
365     ArgTypes.push_back(ShadowPtrTy);
366   return FunctionType::get(T->getReturnType(), ArgTypes, false);
367 }
368
369 FunctionType *DataFlowSanitizer::getCustomFunctionType(FunctionType *T) {
370   assert(!T->isVarArg());
371   llvm::SmallVector<Type *, 4> ArgTypes;
372   for (FunctionType::param_iterator i = T->param_begin(), e = T->param_end();
373        i != e; ++i) {
374     FunctionType *FT;
375     if (isa<PointerType>(*i) && (FT = dyn_cast<FunctionType>(cast<PointerType>(
376                                      *i)->getElementType()))) {
377       ArgTypes.push_back(getTrampolineFunctionType(FT)->getPointerTo());
378       ArgTypes.push_back(Type::getInt8PtrTy(*Ctx));
379     } else {
380       ArgTypes.push_back(*i);
381     }
382   }
383   for (unsigned i = 0, e = T->getNumParams(); i != e; ++i)
384     ArgTypes.push_back(ShadowTy);
385   Type *RetType = T->getReturnType();
386   if (!RetType->isVoidTy())
387     ArgTypes.push_back(ShadowPtrTy);
388   return FunctionType::get(T->getReturnType(), ArgTypes, false);
389 }
390
391 bool DataFlowSanitizer::doInitialization(Module &M) {
392   DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
393   if (!DLP)
394     report_fatal_error("data layout missing");
395   DL = &DLP->getDataLayout();
396
397   Mod = &M;
398   Ctx = &M.getContext();
399   ShadowTy = IntegerType::get(*Ctx, ShadowWidth);
400   ShadowPtrTy = PointerType::getUnqual(ShadowTy);
401   IntptrTy = DL->getIntPtrType(*Ctx);
402   ZeroShadow = ConstantInt::getSigned(ShadowTy, 0);
403   ShadowPtrMask = ConstantInt::getSigned(IntptrTy, ~0x700000000000LL);
404   ShadowPtrMul = ConstantInt::getSigned(IntptrTy, ShadowWidth / 8);
405
406   Type *DFSanUnionArgs[2] = { ShadowTy, ShadowTy };
407   DFSanUnionFnTy =
408       FunctionType::get(ShadowTy, DFSanUnionArgs, /*isVarArg=*/ false);
409   Type *DFSanUnionLoadArgs[2] = { ShadowPtrTy, IntptrTy };
410   DFSanUnionLoadFnTy =
411       FunctionType::get(ShadowTy, DFSanUnionLoadArgs, /*isVarArg=*/ false);
412   DFSanUnimplementedFnTy = FunctionType::get(
413       Type::getVoidTy(*Ctx), Type::getInt8PtrTy(*Ctx), /*isVarArg=*/false);
414   Type *DFSanSetLabelArgs[3] = { ShadowTy, Type::getInt8PtrTy(*Ctx), IntptrTy };
415   DFSanSetLabelFnTy = FunctionType::get(Type::getVoidTy(*Ctx),
416                                         DFSanSetLabelArgs, /*isVarArg=*/false);
417   DFSanNonzeroLabelFnTy = FunctionType::get(
418       Type::getVoidTy(*Ctx), ArrayRef<Type *>(), /*isVarArg=*/false);
419
420   if (GetArgTLSPtr) {
421     Type *ArgTLSTy = ArrayType::get(ShadowTy, 64);
422     ArgTLS = nullptr;
423     GetArgTLS = ConstantExpr::getIntToPtr(
424         ConstantInt::get(IntptrTy, uintptr_t(GetArgTLSPtr)),
425         PointerType::getUnqual(
426             FunctionType::get(PointerType::getUnqual(ArgTLSTy),
427                               (Type *)nullptr)));
428   }
429   if (GetRetvalTLSPtr) {
430     RetvalTLS = nullptr;
431     GetRetvalTLS = ConstantExpr::getIntToPtr(
432         ConstantInt::get(IntptrTy, uintptr_t(GetRetvalTLSPtr)),
433         PointerType::getUnqual(
434             FunctionType::get(PointerType::getUnqual(ShadowTy),
435                               (Type *)nullptr)));
436   }
437
438   ColdCallWeights = MDBuilder(*Ctx).createBranchWeights(1, 1000);
439   return true;
440 }
441
442 bool DataFlowSanitizer::isInstrumented(const Function *F) {
443   return !ABIList.isIn(*F, "uninstrumented");
444 }
445
446 bool DataFlowSanitizer::isInstrumented(const GlobalAlias *GA) {
447   return !ABIList.isIn(*GA, "uninstrumented");
448 }
449
450 DataFlowSanitizer::InstrumentedABI DataFlowSanitizer::getInstrumentedABI() {
451   return ClArgsABI ? IA_Args : IA_TLS;
452 }
453
454 DataFlowSanitizer::WrapperKind DataFlowSanitizer::getWrapperKind(Function *F) {
455   if (ABIList.isIn(*F, "functional"))
456     return WK_Functional;
457   if (ABIList.isIn(*F, "discard"))
458     return WK_Discard;
459   if (ABIList.isIn(*F, "custom"))
460     return WK_Custom;
461
462   return WK_Warning;
463 }
464
465 void DataFlowSanitizer::addGlobalNamePrefix(GlobalValue *GV) {
466   std::string GVName = GV->getName(), Prefix = "dfs$";
467   GV->setName(Prefix + GVName);
468
469   // Try to change the name of the function in module inline asm.  We only do
470   // this for specific asm directives, currently only ".symver", to try to avoid
471   // corrupting asm which happens to contain the symbol name as a substring.
472   // Note that the substitution for .symver assumes that the versioned symbol
473   // also has an instrumented name.
474   std::string Asm = GV->getParent()->getModuleInlineAsm();
475   std::string SearchStr = ".symver " + GVName + ",";
476   size_t Pos = Asm.find(SearchStr);
477   if (Pos != std::string::npos) {
478     Asm.replace(Pos, SearchStr.size(),
479                 ".symver " + Prefix + GVName + "," + Prefix);
480     GV->getParent()->setModuleInlineAsm(Asm);
481   }
482 }
483
484 Function *
485 DataFlowSanitizer::buildWrapperFunction(Function *F, StringRef NewFName,
486                                         GlobalValue::LinkageTypes NewFLink,
487                                         FunctionType *NewFT) {
488   FunctionType *FT = F->getFunctionType();
489   Function *NewF = Function::Create(NewFT, NewFLink, NewFName,
490                                     F->getParent());
491   NewF->copyAttributesFrom(F);
492   NewF->removeAttributes(
493       AttributeSet::ReturnIndex,
494       AttributeFuncs::typeIncompatible(NewFT->getReturnType(),
495                                        AttributeSet::ReturnIndex));
496
497   BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", NewF);
498   std::vector<Value *> Args;
499   unsigned n = FT->getNumParams();
500   for (Function::arg_iterator ai = NewF->arg_begin(); n != 0; ++ai, --n)
501     Args.push_back(&*ai);
502   CallInst *CI = CallInst::Create(F, Args, "", BB);
503   if (FT->getReturnType()->isVoidTy())
504     ReturnInst::Create(*Ctx, BB);
505   else
506     ReturnInst::Create(*Ctx, CI, BB);
507
508   return NewF;
509 }
510
511 Constant *DataFlowSanitizer::getOrBuildTrampolineFunction(FunctionType *FT,
512                                                           StringRef FName) {
513   FunctionType *FTT = getTrampolineFunctionType(FT);
514   Constant *C = Mod->getOrInsertFunction(FName, FTT);
515   Function *F = dyn_cast<Function>(C);
516   if (F && F->isDeclaration()) {
517     F->setLinkage(GlobalValue::LinkOnceODRLinkage);
518     BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", F);
519     std::vector<Value *> Args;
520     Function::arg_iterator AI = F->arg_begin(); ++AI;
521     for (unsigned N = FT->getNumParams(); N != 0; ++AI, --N)
522       Args.push_back(&*AI);
523     CallInst *CI =
524         CallInst::Create(&F->getArgumentList().front(), Args, "", BB);
525     ReturnInst *RI;
526     if (FT->getReturnType()->isVoidTy())
527       RI = ReturnInst::Create(*Ctx, BB);
528     else
529       RI = ReturnInst::Create(*Ctx, CI, BB);
530
531     DFSanFunction DFSF(*this, F, /*IsNativeABI=*/true);
532     Function::arg_iterator ValAI = F->arg_begin(), ShadowAI = AI; ++ValAI;
533     for (unsigned N = FT->getNumParams(); N != 0; ++ValAI, ++ShadowAI, --N)
534       DFSF.ValShadowMap[ValAI] = ShadowAI;
535     DFSanVisitor(DFSF).visitCallInst(*CI);
536     if (!FT->getReturnType()->isVoidTy())
537       new StoreInst(DFSF.getShadow(RI->getReturnValue()),
538                     &F->getArgumentList().back(), RI);
539   }
540
541   return C;
542 }
543
544 bool DataFlowSanitizer::runOnModule(Module &M) {
545   if (!DL)
546     return false;
547
548   if (ABIList.isIn(M, "skip"))
549     return false;
550
551   if (!GetArgTLSPtr) {
552     Type *ArgTLSTy = ArrayType::get(ShadowTy, 64);
553     ArgTLS = Mod->getOrInsertGlobal("__dfsan_arg_tls", ArgTLSTy);
554     if (GlobalVariable *G = dyn_cast<GlobalVariable>(ArgTLS))
555       G->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
556   }
557   if (!GetRetvalTLSPtr) {
558     RetvalTLS = Mod->getOrInsertGlobal("__dfsan_retval_tls", ShadowTy);
559     if (GlobalVariable *G = dyn_cast<GlobalVariable>(RetvalTLS))
560       G->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
561   }
562
563   DFSanUnionFn = Mod->getOrInsertFunction("__dfsan_union", DFSanUnionFnTy);
564   if (Function *F = dyn_cast<Function>(DFSanUnionFn)) {
565     F->addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
566     F->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
567     F->addAttribute(1, Attribute::ZExt);
568     F->addAttribute(2, Attribute::ZExt);
569   }
570   DFSanUnionLoadFn =
571       Mod->getOrInsertFunction("__dfsan_union_load", DFSanUnionLoadFnTy);
572   if (Function *F = dyn_cast<Function>(DFSanUnionLoadFn)) {
573     F->addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
574     F->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
575   }
576   DFSanUnimplementedFn =
577       Mod->getOrInsertFunction("__dfsan_unimplemented", DFSanUnimplementedFnTy);
578   DFSanSetLabelFn =
579       Mod->getOrInsertFunction("__dfsan_set_label", DFSanSetLabelFnTy);
580   if (Function *F = dyn_cast<Function>(DFSanSetLabelFn)) {
581     F->addAttribute(1, Attribute::ZExt);
582   }
583   DFSanNonzeroLabelFn =
584       Mod->getOrInsertFunction("__dfsan_nonzero_label", DFSanNonzeroLabelFnTy);
585
586   std::vector<Function *> FnsToInstrument;
587   llvm::SmallPtrSet<Function *, 2> FnsWithNativeABI;
588   for (Module::iterator i = M.begin(), e = M.end(); i != e; ++i) {
589     if (!i->isIntrinsic() &&
590         i != DFSanUnionFn &&
591         i != DFSanUnionLoadFn &&
592         i != DFSanUnimplementedFn &&
593         i != DFSanSetLabelFn &&
594         i != DFSanNonzeroLabelFn)
595       FnsToInstrument.push_back(&*i);
596   }
597
598   // Give function aliases prefixes when necessary, and build wrappers where the
599   // instrumentedness is inconsistent.
600   for (Module::alias_iterator i = M.alias_begin(), e = M.alias_end(); i != e;) {
601     GlobalAlias *GA = &*i;
602     ++i;
603     // Don't stop on weak.  We assume people aren't playing games with the
604     // instrumentedness of overridden weak aliases.
605     if (auto F = dyn_cast<Function>(GA->getBaseObject())) {
606       bool GAInst = isInstrumented(GA), FInst = isInstrumented(F);
607       if (GAInst && FInst) {
608         addGlobalNamePrefix(GA);
609       } else if (GAInst != FInst) {
610         // Non-instrumented alias of an instrumented function, or vice versa.
611         // Replace the alias with a native-ABI wrapper of the aliasee.  The pass
612         // below will take care of instrumenting it.
613         Function *NewF =
614             buildWrapperFunction(F, "", GA->getLinkage(), F->getFunctionType());
615         GA->replaceAllUsesWith(ConstantExpr::getBitCast(NewF, GA->getType()));
616         NewF->takeName(GA);
617         GA->eraseFromParent();
618         FnsToInstrument.push_back(NewF);
619       }
620     }
621   }
622
623   AttrBuilder B;
624   B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
625   ReadOnlyNoneAttrs = AttributeSet::get(*Ctx, AttributeSet::FunctionIndex, B);
626
627   // First, change the ABI of every function in the module.  ABI-listed
628   // functions keep their original ABI and get a wrapper function.
629   for (std::vector<Function *>::iterator i = FnsToInstrument.begin(),
630                                          e = FnsToInstrument.end();
631        i != e; ++i) {
632     Function &F = **i;
633     FunctionType *FT = F.getFunctionType();
634
635     bool IsZeroArgsVoidRet = (FT->getNumParams() == 0 && !FT->isVarArg() &&
636                               FT->getReturnType()->isVoidTy());
637
638     if (isInstrumented(&F)) {
639       // Instrumented functions get a 'dfs$' prefix.  This allows us to more
640       // easily identify cases of mismatching ABIs.
641       if (getInstrumentedABI() == IA_Args && !IsZeroArgsVoidRet) {
642         FunctionType *NewFT = getArgsFunctionType(FT);
643         Function *NewF = Function::Create(NewFT, F.getLinkage(), "", &M);
644         NewF->copyAttributesFrom(&F);
645         NewF->removeAttributes(
646             AttributeSet::ReturnIndex,
647             AttributeFuncs::typeIncompatible(NewFT->getReturnType(),
648                                              AttributeSet::ReturnIndex));
649         for (Function::arg_iterator FArg = F.arg_begin(),
650                                     NewFArg = NewF->arg_begin(),
651                                     FArgEnd = F.arg_end();
652              FArg != FArgEnd; ++FArg, ++NewFArg) {
653           FArg->replaceAllUsesWith(NewFArg);
654         }
655         NewF->getBasicBlockList().splice(NewF->begin(), F.getBasicBlockList());
656
657         for (Function::user_iterator UI = F.user_begin(), UE = F.user_end();
658              UI != UE;) {
659           BlockAddress *BA = dyn_cast<BlockAddress>(*UI);
660           ++UI;
661           if (BA) {
662             BA->replaceAllUsesWith(
663                 BlockAddress::get(NewF, BA->getBasicBlock()));
664             delete BA;
665           }
666         }
667         F.replaceAllUsesWith(
668             ConstantExpr::getBitCast(NewF, PointerType::getUnqual(FT)));
669         NewF->takeName(&F);
670         F.eraseFromParent();
671         *i = NewF;
672         addGlobalNamePrefix(NewF);
673       } else {
674         addGlobalNamePrefix(&F);
675       }
676                // Hopefully, nobody will try to indirectly call a vararg
677                // function... yet.
678     } else if (FT->isVarArg()) {
679       UnwrappedFnMap[&F] = &F;
680       *i = nullptr;
681     } else if (!IsZeroArgsVoidRet || getWrapperKind(&F) == WK_Custom) {
682       // Build a wrapper function for F.  The wrapper simply calls F, and is
683       // added to FnsToInstrument so that any instrumentation according to its
684       // WrapperKind is done in the second pass below.
685       FunctionType *NewFT = getInstrumentedABI() == IA_Args
686                                 ? getArgsFunctionType(FT)
687                                 : FT;
688       Function *NewF = buildWrapperFunction(
689           &F, std::string("dfsw$") + std::string(F.getName()),
690           GlobalValue::LinkOnceODRLinkage, NewFT);
691       if (getInstrumentedABI() == IA_TLS)
692         NewF->removeAttributes(AttributeSet::FunctionIndex, ReadOnlyNoneAttrs);
693
694       Value *WrappedFnCst =
695           ConstantExpr::getBitCast(NewF, PointerType::getUnqual(FT));
696       F.replaceAllUsesWith(WrappedFnCst);
697       UnwrappedFnMap[WrappedFnCst] = &F;
698       *i = NewF;
699
700       if (!F.isDeclaration()) {
701         // This function is probably defining an interposition of an
702         // uninstrumented function and hence needs to keep the original ABI.
703         // But any functions it may call need to use the instrumented ABI, so
704         // we instrument it in a mode which preserves the original ABI.
705         FnsWithNativeABI.insert(&F);
706
707         // This code needs to rebuild the iterators, as they may be invalidated
708         // by the push_back, taking care that the new range does not include
709         // any functions added by this code.
710         size_t N = i - FnsToInstrument.begin(),
711                Count = e - FnsToInstrument.begin();
712         FnsToInstrument.push_back(&F);
713         i = FnsToInstrument.begin() + N;
714         e = FnsToInstrument.begin() + Count;
715       }
716     }
717   }
718
719   for (std::vector<Function *>::iterator i = FnsToInstrument.begin(),
720                                          e = FnsToInstrument.end();
721        i != e; ++i) {
722     if (!*i || (*i)->isDeclaration())
723       continue;
724
725     removeUnreachableBlocks(**i);
726
727     DFSanFunction DFSF(*this, *i, FnsWithNativeABI.count(*i));
728
729     // DFSanVisitor may create new basic blocks, which confuses df_iterator.
730     // Build a copy of the list before iterating over it.
731     llvm::SmallVector<BasicBlock *, 4> BBList(
732         depth_first(&(*i)->getEntryBlock()));
733
734     for (llvm::SmallVector<BasicBlock *, 4>::iterator i = BBList.begin(),
735                                                       e = BBList.end();
736          i != e; ++i) {
737       Instruction *Inst = &(*i)->front();
738       while (1) {
739         // DFSanVisitor may split the current basic block, changing the current
740         // instruction's next pointer and moving the next instruction to the
741         // tail block from which we should continue.
742         Instruction *Next = Inst->getNextNode();
743         // DFSanVisitor may delete Inst, so keep track of whether it was a
744         // terminator.
745         bool IsTerminator = isa<TerminatorInst>(Inst);
746         if (!DFSF.SkipInsts.count(Inst))
747           DFSanVisitor(DFSF).visit(Inst);
748         if (IsTerminator)
749           break;
750         Inst = Next;
751       }
752     }
753
754     // We will not necessarily be able to compute the shadow for every phi node
755     // until we have visited every block.  Therefore, the code that handles phi
756     // nodes adds them to the PHIFixups list so that they can be properly
757     // handled here.
758     for (std::vector<std::pair<PHINode *, PHINode *> >::iterator
759              i = DFSF.PHIFixups.begin(),
760              e = DFSF.PHIFixups.end();
761          i != e; ++i) {
762       for (unsigned val = 0, n = i->first->getNumIncomingValues(); val != n;
763            ++val) {
764         i->second->setIncomingValue(
765             val, DFSF.getShadow(i->first->getIncomingValue(val)));
766       }
767     }
768
769     // -dfsan-debug-nonzero-labels will split the CFG in all kinds of crazy
770     // places (i.e. instructions in basic blocks we haven't even begun visiting
771     // yet).  To make our life easier, do this work in a pass after the main
772     // instrumentation.
773     if (ClDebugNonzeroLabels) {
774       for (DenseSet<Value *>::iterator i = DFSF.NonZeroChecks.begin(),
775                                        e = DFSF.NonZeroChecks.end();
776            i != e; ++i) {
777         Instruction *Pos;
778         if (Instruction *I = dyn_cast<Instruction>(*i))
779           Pos = I->getNextNode();
780         else
781           Pos = DFSF.F->getEntryBlock().begin();
782         while (isa<PHINode>(Pos) || isa<AllocaInst>(Pos))
783           Pos = Pos->getNextNode();
784         IRBuilder<> IRB(Pos);
785         Value *Ne = IRB.CreateICmpNE(*i, DFSF.DFS.ZeroShadow);
786         BranchInst *BI = cast<BranchInst>(SplitBlockAndInsertIfThen(
787             Ne, Pos, /*Unreachable=*/false, ColdCallWeights));
788         IRBuilder<> ThenIRB(BI);
789         ThenIRB.CreateCall(DFSF.DFS.DFSanNonzeroLabelFn);
790       }
791     }
792   }
793
794   return false;
795 }
796
797 Value *DFSanFunction::getArgTLSPtr() {
798   if (ArgTLSPtr)
799     return ArgTLSPtr;
800   if (DFS.ArgTLS)
801     return ArgTLSPtr = DFS.ArgTLS;
802
803   IRBuilder<> IRB(F->getEntryBlock().begin());
804   return ArgTLSPtr = IRB.CreateCall(DFS.GetArgTLS);
805 }
806
807 Value *DFSanFunction::getRetvalTLS() {
808   if (RetvalTLSPtr)
809     return RetvalTLSPtr;
810   if (DFS.RetvalTLS)
811     return RetvalTLSPtr = DFS.RetvalTLS;
812
813   IRBuilder<> IRB(F->getEntryBlock().begin());
814   return RetvalTLSPtr = IRB.CreateCall(DFS.GetRetvalTLS);
815 }
816
817 Value *DFSanFunction::getArgTLS(unsigned Idx, Instruction *Pos) {
818   IRBuilder<> IRB(Pos);
819   return IRB.CreateConstGEP2_64(getArgTLSPtr(), 0, Idx);
820 }
821
822 Value *DFSanFunction::getShadow(Value *V) {
823   if (!isa<Argument>(V) && !isa<Instruction>(V))
824     return DFS.ZeroShadow;
825   Value *&Shadow = ValShadowMap[V];
826   if (!Shadow) {
827     if (Argument *A = dyn_cast<Argument>(V)) {
828       if (IsNativeABI)
829         return DFS.ZeroShadow;
830       switch (IA) {
831       case DataFlowSanitizer::IA_TLS: {
832         Value *ArgTLSPtr = getArgTLSPtr();
833         Instruction *ArgTLSPos =
834             DFS.ArgTLS ? &*F->getEntryBlock().begin()
835                        : cast<Instruction>(ArgTLSPtr)->getNextNode();
836         IRBuilder<> IRB(ArgTLSPos);
837         Shadow = IRB.CreateLoad(getArgTLS(A->getArgNo(), ArgTLSPos));
838         break;
839       }
840       case DataFlowSanitizer::IA_Args: {
841         unsigned ArgIdx = A->getArgNo() + F->getArgumentList().size() / 2;
842         Function::arg_iterator i = F->arg_begin();
843         while (ArgIdx--)
844           ++i;
845         Shadow = i;
846         assert(Shadow->getType() == DFS.ShadowTy);
847         break;
848       }
849       }
850       NonZeroChecks.insert(Shadow);
851     } else {
852       Shadow = DFS.ZeroShadow;
853     }
854   }
855   return Shadow;
856 }
857
858 void DFSanFunction::setShadow(Instruction *I, Value *Shadow) {
859   assert(!ValShadowMap.count(I));
860   assert(Shadow->getType() == DFS.ShadowTy);
861   ValShadowMap[I] = Shadow;
862 }
863
864 Value *DataFlowSanitizer::getShadowAddress(Value *Addr, Instruction *Pos) {
865   assert(Addr != RetvalTLS && "Reinstrumenting?");
866   IRBuilder<> IRB(Pos);
867   return IRB.CreateIntToPtr(
868       IRB.CreateMul(
869           IRB.CreateAnd(IRB.CreatePtrToInt(Addr, IntptrTy), ShadowPtrMask),
870           ShadowPtrMul),
871       ShadowPtrTy);
872 }
873
874 // Generates IR to compute the union of the two given shadows, inserting it
875 // before Pos.  Returns the computed union Value.
876 Value *DFSanFunction::combineShadows(Value *V1, Value *V2, Instruction *Pos) {
877   if (V1 == DFS.ZeroShadow)
878     return V2;
879   if (V2 == DFS.ZeroShadow)
880     return V1;
881   if (V1 == V2)
882     return V1;
883   IRBuilder<> IRB(Pos);
884   BasicBlock *Head = Pos->getParent();
885   Value *Ne = IRB.CreateICmpNE(V1, V2);
886   BranchInst *BI = cast<BranchInst>(SplitBlockAndInsertIfThen(
887       Ne, Pos, /*Unreachable=*/false, DFS.ColdCallWeights, &DT));
888   IRBuilder<> ThenIRB(BI);
889   CallInst *Call = ThenIRB.CreateCall2(DFS.DFSanUnionFn, V1, V2);
890   Call->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
891   Call->addAttribute(1, Attribute::ZExt);
892   Call->addAttribute(2, Attribute::ZExt);
893
894   BasicBlock *Tail = BI->getSuccessor(0);
895   PHINode *Phi = PHINode::Create(DFS.ShadowTy, 2, "", Tail->begin());
896   Phi->addIncoming(Call, Call->getParent());
897   Phi->addIncoming(V1, Head);
898   return Phi;
899 }
900
901 // A convenience function which folds the shadows of each of the operands
902 // of the provided instruction Inst, inserting the IR before Inst.  Returns
903 // the computed union Value.
904 Value *DFSanFunction::combineOperandShadows(Instruction *Inst) {
905   if (Inst->getNumOperands() == 0)
906     return DFS.ZeroShadow;
907
908   Value *Shadow = getShadow(Inst->getOperand(0));
909   for (unsigned i = 1, n = Inst->getNumOperands(); i != n; ++i) {
910     Shadow = combineShadows(Shadow, getShadow(Inst->getOperand(i)), Inst);
911   }
912   return Shadow;
913 }
914
915 void DFSanVisitor::visitOperandShadowInst(Instruction &I) {
916   Value *CombinedShadow = DFSF.combineOperandShadows(&I);
917   DFSF.setShadow(&I, CombinedShadow);
918 }
919
920 // Generates IR to load shadow corresponding to bytes [Addr, Addr+Size), where
921 // Addr has alignment Align, and take the union of each of those shadows.
922 Value *DFSanFunction::loadShadow(Value *Addr, uint64_t Size, uint64_t Align,
923                                  Instruction *Pos) {
924   if (AllocaInst *AI = dyn_cast<AllocaInst>(Addr)) {
925     llvm::DenseMap<AllocaInst *, AllocaInst *>::iterator i =
926         AllocaShadowMap.find(AI);
927     if (i != AllocaShadowMap.end()) {
928       IRBuilder<> IRB(Pos);
929       return IRB.CreateLoad(i->second);
930     }
931   }
932
933   uint64_t ShadowAlign = Align * DFS.ShadowWidth / 8;
934   SmallVector<Value *, 2> Objs;
935   GetUnderlyingObjects(Addr, Objs, DFS.DL);
936   bool AllConstants = true;
937   for (SmallVector<Value *, 2>::iterator i = Objs.begin(), e = Objs.end();
938        i != e; ++i) {
939     if (isa<Function>(*i) || isa<BlockAddress>(*i))
940       continue;
941     if (isa<GlobalVariable>(*i) && cast<GlobalVariable>(*i)->isConstant())
942       continue;
943
944     AllConstants = false;
945     break;
946   }
947   if (AllConstants)
948     return DFS.ZeroShadow;
949
950   Value *ShadowAddr = DFS.getShadowAddress(Addr, Pos);
951   switch (Size) {
952   case 0:
953     return DFS.ZeroShadow;
954   case 1: {
955     LoadInst *LI = new LoadInst(ShadowAddr, "", Pos);
956     LI->setAlignment(ShadowAlign);
957     return LI;
958   }
959   case 2: {
960     IRBuilder<> IRB(Pos);
961     Value *ShadowAddr1 =
962         IRB.CreateGEP(ShadowAddr, ConstantInt::get(DFS.IntptrTy, 1));
963     return combineShadows(IRB.CreateAlignedLoad(ShadowAddr, ShadowAlign),
964                           IRB.CreateAlignedLoad(ShadowAddr1, ShadowAlign), Pos);
965   }
966   }
967   if (Size % (64 / DFS.ShadowWidth) == 0) {
968     // Fast path for the common case where each byte has identical shadow: load
969     // shadow 64 bits at a time, fall out to a __dfsan_union_load call if any
970     // shadow is non-equal.
971     BasicBlock *FallbackBB = BasicBlock::Create(*DFS.Ctx, "", F);
972     IRBuilder<> FallbackIRB(FallbackBB);
973     CallInst *FallbackCall = FallbackIRB.CreateCall2(
974         DFS.DFSanUnionLoadFn, ShadowAddr, ConstantInt::get(DFS.IntptrTy, Size));
975     FallbackCall->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
976
977     // Compare each of the shadows stored in the loaded 64 bits to each other,
978     // by computing (WideShadow rotl ShadowWidth) == WideShadow.
979     IRBuilder<> IRB(Pos);
980     Value *WideAddr =
981         IRB.CreateBitCast(ShadowAddr, Type::getInt64PtrTy(*DFS.Ctx));
982     Value *WideShadow = IRB.CreateAlignedLoad(WideAddr, ShadowAlign);
983     Value *TruncShadow = IRB.CreateTrunc(WideShadow, DFS.ShadowTy);
984     Value *ShlShadow = IRB.CreateShl(WideShadow, DFS.ShadowWidth);
985     Value *ShrShadow = IRB.CreateLShr(WideShadow, 64 - DFS.ShadowWidth);
986     Value *RotShadow = IRB.CreateOr(ShlShadow, ShrShadow);
987     Value *ShadowsEq = IRB.CreateICmpEQ(WideShadow, RotShadow);
988
989     BasicBlock *Head = Pos->getParent();
990     BasicBlock *Tail = Head->splitBasicBlock(Pos);
991     // In the following code LastBr will refer to the previous basic block's
992     // conditional branch instruction, whose true successor is fixed up to point
993     // to the next block during the loop below or to the tail after the final
994     // iteration.
995     BranchInst *LastBr = BranchInst::Create(FallbackBB, FallbackBB, ShadowsEq);
996     ReplaceInstWithInst(Head->getTerminator(), LastBr);
997
998     for (uint64_t Ofs = 64 / DFS.ShadowWidth; Ofs != Size;
999          Ofs += 64 / DFS.ShadowWidth) {
1000       BasicBlock *NextBB = BasicBlock::Create(*DFS.Ctx, "", F);
1001       IRBuilder<> NextIRB(NextBB);
1002       WideAddr = NextIRB.CreateGEP(WideAddr, ConstantInt::get(DFS.IntptrTy, 1));
1003       Value *NextWideShadow = NextIRB.CreateAlignedLoad(WideAddr, ShadowAlign);
1004       ShadowsEq = NextIRB.CreateICmpEQ(WideShadow, NextWideShadow);
1005       LastBr->setSuccessor(0, NextBB);
1006       LastBr = NextIRB.CreateCondBr(ShadowsEq, FallbackBB, FallbackBB);
1007     }
1008
1009     LastBr->setSuccessor(0, Tail);
1010     FallbackIRB.CreateBr(Tail);
1011     PHINode *Shadow = PHINode::Create(DFS.ShadowTy, 2, "", &Tail->front());
1012     Shadow->addIncoming(FallbackCall, FallbackBB);
1013     Shadow->addIncoming(TruncShadow, LastBr->getParent());
1014     return Shadow;
1015   }
1016
1017   IRBuilder<> IRB(Pos);
1018   CallInst *FallbackCall = IRB.CreateCall2(
1019       DFS.DFSanUnionLoadFn, ShadowAddr, ConstantInt::get(DFS.IntptrTy, Size));
1020   FallbackCall->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
1021   return FallbackCall;
1022 }
1023
1024 void DFSanVisitor::visitLoadInst(LoadInst &LI) {
1025   uint64_t Size = DFSF.DFS.DL->getTypeStoreSize(LI.getType());
1026   uint64_t Align;
1027   if (ClPreserveAlignment) {
1028     Align = LI.getAlignment();
1029     if (Align == 0)
1030       Align = DFSF.DFS.DL->getABITypeAlignment(LI.getType());
1031   } else {
1032     Align = 1;
1033   }
1034   IRBuilder<> IRB(&LI);
1035   Value *Shadow = DFSF.loadShadow(LI.getPointerOperand(), Size, Align, &LI);
1036   if (ClCombinePointerLabelsOnLoad) {
1037     Value *PtrShadow = DFSF.getShadow(LI.getPointerOperand());
1038     Shadow = DFSF.combineShadows(Shadow, PtrShadow, &LI);
1039   }
1040   if (Shadow != DFSF.DFS.ZeroShadow)
1041     DFSF.NonZeroChecks.insert(Shadow);
1042
1043   DFSF.setShadow(&LI, Shadow);
1044 }
1045
1046 void DFSanFunction::storeShadow(Value *Addr, uint64_t Size, uint64_t Align,
1047                                 Value *Shadow, Instruction *Pos) {
1048   if (AllocaInst *AI = dyn_cast<AllocaInst>(Addr)) {
1049     llvm::DenseMap<AllocaInst *, AllocaInst *>::iterator i =
1050         AllocaShadowMap.find(AI);
1051     if (i != AllocaShadowMap.end()) {
1052       IRBuilder<> IRB(Pos);
1053       IRB.CreateStore(Shadow, i->second);
1054       return;
1055     }
1056   }
1057
1058   uint64_t ShadowAlign = Align * DFS.ShadowWidth / 8;
1059   IRBuilder<> IRB(Pos);
1060   Value *ShadowAddr = DFS.getShadowAddress(Addr, Pos);
1061   if (Shadow == DFS.ZeroShadow) {
1062     IntegerType *ShadowTy = IntegerType::get(*DFS.Ctx, Size * DFS.ShadowWidth);
1063     Value *ExtZeroShadow = ConstantInt::get(ShadowTy, 0);
1064     Value *ExtShadowAddr =
1065         IRB.CreateBitCast(ShadowAddr, PointerType::getUnqual(ShadowTy));
1066     IRB.CreateAlignedStore(ExtZeroShadow, ExtShadowAddr, ShadowAlign);
1067     return;
1068   }
1069
1070   const unsigned ShadowVecSize = 128 / DFS.ShadowWidth;
1071   uint64_t Offset = 0;
1072   if (Size >= ShadowVecSize) {
1073     VectorType *ShadowVecTy = VectorType::get(DFS.ShadowTy, ShadowVecSize);
1074     Value *ShadowVec = UndefValue::get(ShadowVecTy);
1075     for (unsigned i = 0; i != ShadowVecSize; ++i) {
1076       ShadowVec = IRB.CreateInsertElement(
1077           ShadowVec, Shadow, ConstantInt::get(Type::getInt32Ty(*DFS.Ctx), i));
1078     }
1079     Value *ShadowVecAddr =
1080         IRB.CreateBitCast(ShadowAddr, PointerType::getUnqual(ShadowVecTy));
1081     do {
1082       Value *CurShadowVecAddr = IRB.CreateConstGEP1_32(ShadowVecAddr, Offset);
1083       IRB.CreateAlignedStore(ShadowVec, CurShadowVecAddr, ShadowAlign);
1084       Size -= ShadowVecSize;
1085       ++Offset;
1086     } while (Size >= ShadowVecSize);
1087     Offset *= ShadowVecSize;
1088   }
1089   while (Size > 0) {
1090     Value *CurShadowAddr = IRB.CreateConstGEP1_32(ShadowAddr, Offset);
1091     IRB.CreateAlignedStore(Shadow, CurShadowAddr, ShadowAlign);
1092     --Size;
1093     ++Offset;
1094   }
1095 }
1096
1097 void DFSanVisitor::visitStoreInst(StoreInst &SI) {
1098   uint64_t Size =
1099       DFSF.DFS.DL->getTypeStoreSize(SI.getValueOperand()->getType());
1100   uint64_t Align;
1101   if (ClPreserveAlignment) {
1102     Align = SI.getAlignment();
1103     if (Align == 0)
1104       Align = DFSF.DFS.DL->getABITypeAlignment(SI.getValueOperand()->getType());
1105   } else {
1106     Align = 1;
1107   }
1108
1109   Value* Shadow = DFSF.getShadow(SI.getValueOperand());
1110   if (ClCombinePointerLabelsOnStore) {
1111     Value *PtrShadow = DFSF.getShadow(SI.getPointerOperand());
1112     Shadow = DFSF.combineShadows(Shadow, PtrShadow, &SI);
1113   }
1114   DFSF.storeShadow(SI.getPointerOperand(), Size, Align, Shadow, &SI);
1115 }
1116
1117 void DFSanVisitor::visitBinaryOperator(BinaryOperator &BO) {
1118   visitOperandShadowInst(BO);
1119 }
1120
1121 void DFSanVisitor::visitCastInst(CastInst &CI) { visitOperandShadowInst(CI); }
1122
1123 void DFSanVisitor::visitCmpInst(CmpInst &CI) { visitOperandShadowInst(CI); }
1124
1125 void DFSanVisitor::visitGetElementPtrInst(GetElementPtrInst &GEPI) {
1126   visitOperandShadowInst(GEPI);
1127 }
1128
1129 void DFSanVisitor::visitExtractElementInst(ExtractElementInst &I) {
1130   visitOperandShadowInst(I);
1131 }
1132
1133 void DFSanVisitor::visitInsertElementInst(InsertElementInst &I) {
1134   visitOperandShadowInst(I);
1135 }
1136
1137 void DFSanVisitor::visitShuffleVectorInst(ShuffleVectorInst &I) {
1138   visitOperandShadowInst(I);
1139 }
1140
1141 void DFSanVisitor::visitExtractValueInst(ExtractValueInst &I) {
1142   visitOperandShadowInst(I);
1143 }
1144
1145 void DFSanVisitor::visitInsertValueInst(InsertValueInst &I) {
1146   visitOperandShadowInst(I);
1147 }
1148
1149 void DFSanVisitor::visitAllocaInst(AllocaInst &I) {
1150   bool AllLoadsStores = true;
1151   for (User *U : I.users()) {
1152     if (isa<LoadInst>(U))
1153       continue;
1154
1155     if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
1156       if (SI->getPointerOperand() == &I)
1157         continue;
1158     }
1159
1160     AllLoadsStores = false;
1161     break;
1162   }
1163   if (AllLoadsStores) {
1164     IRBuilder<> IRB(&I);
1165     DFSF.AllocaShadowMap[&I] = IRB.CreateAlloca(DFSF.DFS.ShadowTy);
1166   }
1167   DFSF.setShadow(&I, DFSF.DFS.ZeroShadow);
1168 }
1169
1170 void DFSanVisitor::visitSelectInst(SelectInst &I) {
1171   Value *CondShadow = DFSF.getShadow(I.getCondition());
1172   Value *TrueShadow = DFSF.getShadow(I.getTrueValue());
1173   Value *FalseShadow = DFSF.getShadow(I.getFalseValue());
1174
1175   if (isa<VectorType>(I.getCondition()->getType())) {
1176     DFSF.setShadow(
1177         &I,
1178         DFSF.combineShadows(
1179             CondShadow, DFSF.combineShadows(TrueShadow, FalseShadow, &I), &I));
1180   } else {
1181     Value *ShadowSel;
1182     if (TrueShadow == FalseShadow) {
1183       ShadowSel = TrueShadow;
1184     } else {
1185       ShadowSel =
1186           SelectInst::Create(I.getCondition(), TrueShadow, FalseShadow, "", &I);
1187     }
1188     DFSF.setShadow(&I, DFSF.combineShadows(CondShadow, ShadowSel, &I));
1189   }
1190 }
1191
1192 void DFSanVisitor::visitMemSetInst(MemSetInst &I) {
1193   IRBuilder<> IRB(&I);
1194   Value *ValShadow = DFSF.getShadow(I.getValue());
1195   IRB.CreateCall3(
1196       DFSF.DFS.DFSanSetLabelFn, ValShadow,
1197       IRB.CreateBitCast(I.getDest(), Type::getInt8PtrTy(*DFSF.DFS.Ctx)),
1198       IRB.CreateZExtOrTrunc(I.getLength(), DFSF.DFS.IntptrTy));
1199 }
1200
1201 void DFSanVisitor::visitMemTransferInst(MemTransferInst &I) {
1202   IRBuilder<> IRB(&I);
1203   Value *DestShadow = DFSF.DFS.getShadowAddress(I.getDest(), &I);
1204   Value *SrcShadow = DFSF.DFS.getShadowAddress(I.getSource(), &I);
1205   Value *LenShadow = IRB.CreateMul(
1206       I.getLength(),
1207       ConstantInt::get(I.getLength()->getType(), DFSF.DFS.ShadowWidth / 8));
1208   Value *AlignShadow;
1209   if (ClPreserveAlignment) {
1210     AlignShadow = IRB.CreateMul(I.getAlignmentCst(),
1211                                 ConstantInt::get(I.getAlignmentCst()->getType(),
1212                                                  DFSF.DFS.ShadowWidth / 8));
1213   } else {
1214     AlignShadow = ConstantInt::get(I.getAlignmentCst()->getType(),
1215                                    DFSF.DFS.ShadowWidth / 8);
1216   }
1217   Type *Int8Ptr = Type::getInt8PtrTy(*DFSF.DFS.Ctx);
1218   DestShadow = IRB.CreateBitCast(DestShadow, Int8Ptr);
1219   SrcShadow = IRB.CreateBitCast(SrcShadow, Int8Ptr);
1220   IRB.CreateCall5(I.getCalledValue(), DestShadow, SrcShadow, LenShadow,
1221                   AlignShadow, I.getVolatileCst());
1222 }
1223
1224 void DFSanVisitor::visitReturnInst(ReturnInst &RI) {
1225   if (!DFSF.IsNativeABI && RI.getReturnValue()) {
1226     switch (DFSF.IA) {
1227     case DataFlowSanitizer::IA_TLS: {
1228       Value *S = DFSF.getShadow(RI.getReturnValue());
1229       IRBuilder<> IRB(&RI);
1230       IRB.CreateStore(S, DFSF.getRetvalTLS());
1231       break;
1232     }
1233     case DataFlowSanitizer::IA_Args: {
1234       IRBuilder<> IRB(&RI);
1235       Type *RT = DFSF.F->getFunctionType()->getReturnType();
1236       Value *InsVal =
1237           IRB.CreateInsertValue(UndefValue::get(RT), RI.getReturnValue(), 0);
1238       Value *InsShadow =
1239           IRB.CreateInsertValue(InsVal, DFSF.getShadow(RI.getReturnValue()), 1);
1240       RI.setOperand(0, InsShadow);
1241       break;
1242     }
1243     }
1244   }
1245 }
1246
1247 void DFSanVisitor::visitCallSite(CallSite CS) {
1248   Function *F = CS.getCalledFunction();
1249   if ((F && F->isIntrinsic()) || isa<InlineAsm>(CS.getCalledValue())) {
1250     visitOperandShadowInst(*CS.getInstruction());
1251     return;
1252   }
1253
1254   IRBuilder<> IRB(CS.getInstruction());
1255
1256   DenseMap<Value *, Function *>::iterator i =
1257       DFSF.DFS.UnwrappedFnMap.find(CS.getCalledValue());
1258   if (i != DFSF.DFS.UnwrappedFnMap.end()) {
1259     Function *F = i->second;
1260     switch (DFSF.DFS.getWrapperKind(F)) {
1261     case DataFlowSanitizer::WK_Warning: {
1262       CS.setCalledFunction(F);
1263       IRB.CreateCall(DFSF.DFS.DFSanUnimplementedFn,
1264                      IRB.CreateGlobalStringPtr(F->getName()));
1265       DFSF.setShadow(CS.getInstruction(), DFSF.DFS.ZeroShadow);
1266       return;
1267     }
1268     case DataFlowSanitizer::WK_Discard: {
1269       CS.setCalledFunction(F);
1270       DFSF.setShadow(CS.getInstruction(), DFSF.DFS.ZeroShadow);
1271       return;
1272     }
1273     case DataFlowSanitizer::WK_Functional: {
1274       CS.setCalledFunction(F);
1275       visitOperandShadowInst(*CS.getInstruction());
1276       return;
1277     }
1278     case DataFlowSanitizer::WK_Custom: {
1279       // Don't try to handle invokes of custom functions, it's too complicated.
1280       // Instead, invoke the dfsw$ wrapper, which will in turn call the __dfsw_
1281       // wrapper.
1282       if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) {
1283         FunctionType *FT = F->getFunctionType();
1284         FunctionType *CustomFT = DFSF.DFS.getCustomFunctionType(FT);
1285         std::string CustomFName = "__dfsw_";
1286         CustomFName += F->getName();
1287         Constant *CustomF =
1288             DFSF.DFS.Mod->getOrInsertFunction(CustomFName, CustomFT);
1289         if (Function *CustomFn = dyn_cast<Function>(CustomF)) {
1290           CustomFn->copyAttributesFrom(F);
1291
1292           // Custom functions returning non-void will write to the return label.
1293           if (!FT->getReturnType()->isVoidTy()) {
1294             CustomFn->removeAttributes(AttributeSet::FunctionIndex,
1295                                        DFSF.DFS.ReadOnlyNoneAttrs);
1296           }
1297         }
1298
1299         std::vector<Value *> Args;
1300
1301         CallSite::arg_iterator i = CS.arg_begin();
1302         for (unsigned n = FT->getNumParams(); n != 0; ++i, --n) {
1303           Type *T = (*i)->getType();
1304           FunctionType *ParamFT;
1305           if (isa<PointerType>(T) &&
1306               (ParamFT = dyn_cast<FunctionType>(
1307                    cast<PointerType>(T)->getElementType()))) {
1308             std::string TName = "dfst";
1309             TName += utostr(FT->getNumParams() - n);
1310             TName += "$";
1311             TName += F->getName();
1312             Constant *T = DFSF.DFS.getOrBuildTrampolineFunction(ParamFT, TName);
1313             Args.push_back(T);
1314             Args.push_back(
1315                 IRB.CreateBitCast(*i, Type::getInt8PtrTy(*DFSF.DFS.Ctx)));
1316           } else {
1317             Args.push_back(*i);
1318           }
1319         }
1320
1321         i = CS.arg_begin();
1322         for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
1323           Args.push_back(DFSF.getShadow(*i));
1324
1325         if (!FT->getReturnType()->isVoidTy()) {
1326           if (!DFSF.LabelReturnAlloca) {
1327             DFSF.LabelReturnAlloca =
1328                 new AllocaInst(DFSF.DFS.ShadowTy, "labelreturn",
1329                                DFSF.F->getEntryBlock().begin());
1330           }
1331           Args.push_back(DFSF.LabelReturnAlloca);
1332         }
1333
1334         CallInst *CustomCI = IRB.CreateCall(CustomF, Args);
1335         CustomCI->setCallingConv(CI->getCallingConv());
1336         CustomCI->setAttributes(CI->getAttributes());
1337
1338         if (!FT->getReturnType()->isVoidTy()) {
1339           LoadInst *LabelLoad = IRB.CreateLoad(DFSF.LabelReturnAlloca);
1340           DFSF.setShadow(CustomCI, LabelLoad);
1341         }
1342
1343         CI->replaceAllUsesWith(CustomCI);
1344         CI->eraseFromParent();
1345         return;
1346       }
1347       break;
1348     }
1349     }
1350   }
1351
1352   FunctionType *FT = cast<FunctionType>(
1353       CS.getCalledValue()->getType()->getPointerElementType());
1354   if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_TLS) {
1355     for (unsigned i = 0, n = FT->getNumParams(); i != n; ++i) {
1356       IRB.CreateStore(DFSF.getShadow(CS.getArgument(i)),
1357                       DFSF.getArgTLS(i, CS.getInstruction()));
1358     }
1359   }
1360
1361   Instruction *Next = nullptr;
1362   if (!CS.getType()->isVoidTy()) {
1363     if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
1364       if (II->getNormalDest()->getSinglePredecessor()) {
1365         Next = II->getNormalDest()->begin();
1366       } else {
1367         BasicBlock *NewBB =
1368             SplitEdge(II->getParent(), II->getNormalDest(), &DFSF.DFS);
1369         Next = NewBB->begin();
1370       }
1371     } else {
1372       Next = CS->getNextNode();
1373     }
1374
1375     if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_TLS) {
1376       IRBuilder<> NextIRB(Next);
1377       LoadInst *LI = NextIRB.CreateLoad(DFSF.getRetvalTLS());
1378       DFSF.SkipInsts.insert(LI);
1379       DFSF.setShadow(CS.getInstruction(), LI);
1380       DFSF.NonZeroChecks.insert(LI);
1381     }
1382   }
1383
1384   // Do all instrumentation for IA_Args down here to defer tampering with the
1385   // CFG in a way that SplitEdge may be able to detect.
1386   if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_Args) {
1387     FunctionType *NewFT = DFSF.DFS.getArgsFunctionType(FT);
1388     Value *Func =
1389         IRB.CreateBitCast(CS.getCalledValue(), PointerType::getUnqual(NewFT));
1390     std::vector<Value *> Args;
1391
1392     CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1393     for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
1394       Args.push_back(*i);
1395
1396     i = CS.arg_begin();
1397     for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
1398       Args.push_back(DFSF.getShadow(*i));
1399
1400     if (FT->isVarArg()) {
1401       unsigned VarArgSize = CS.arg_size() - FT->getNumParams();
1402       ArrayType *VarArgArrayTy = ArrayType::get(DFSF.DFS.ShadowTy, VarArgSize);
1403       AllocaInst *VarArgShadow =
1404           new AllocaInst(VarArgArrayTy, "", DFSF.F->getEntryBlock().begin());
1405       Args.push_back(IRB.CreateConstGEP2_32(VarArgShadow, 0, 0));
1406       for (unsigned n = 0; i != e; ++i, ++n) {
1407         IRB.CreateStore(DFSF.getShadow(*i),
1408                         IRB.CreateConstGEP2_32(VarArgShadow, 0, n));
1409         Args.push_back(*i);
1410       }
1411     }
1412
1413     CallSite NewCS;
1414     if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
1415       NewCS = IRB.CreateInvoke(Func, II->getNormalDest(), II->getUnwindDest(),
1416                                Args);
1417     } else {
1418       NewCS = IRB.CreateCall(Func, Args);
1419     }
1420     NewCS.setCallingConv(CS.getCallingConv());
1421     NewCS.setAttributes(CS.getAttributes().removeAttributes(
1422         *DFSF.DFS.Ctx, AttributeSet::ReturnIndex,
1423         AttributeFuncs::typeIncompatible(NewCS.getInstruction()->getType(),
1424                                          AttributeSet::ReturnIndex)));
1425
1426     if (Next) {
1427       ExtractValueInst *ExVal =
1428           ExtractValueInst::Create(NewCS.getInstruction(), 0, "", Next);
1429       DFSF.SkipInsts.insert(ExVal);
1430       ExtractValueInst *ExShadow =
1431           ExtractValueInst::Create(NewCS.getInstruction(), 1, "", Next);
1432       DFSF.SkipInsts.insert(ExShadow);
1433       DFSF.setShadow(ExVal, ExShadow);
1434       DFSF.NonZeroChecks.insert(ExShadow);
1435
1436       CS.getInstruction()->replaceAllUsesWith(ExVal);
1437     }
1438
1439     CS.getInstruction()->eraseFromParent();
1440   }
1441 }
1442
1443 void DFSanVisitor::visitPHINode(PHINode &PN) {
1444   PHINode *ShadowPN =
1445       PHINode::Create(DFSF.DFS.ShadowTy, PN.getNumIncomingValues(), "", &PN);
1446
1447   // Give the shadow phi node valid predecessors to fool SplitEdge into working.
1448   Value *UndefShadow = UndefValue::get(DFSF.DFS.ShadowTy);
1449   for (PHINode::block_iterator i = PN.block_begin(), e = PN.block_end(); i != e;
1450        ++i) {
1451     ShadowPN->addIncoming(UndefShadow, *i);
1452   }
1453
1454   DFSF.PHIFixups.push_back(std::make_pair(&PN, ShadowPN));
1455   DFSF.setShadow(&PN, ShadowPN);
1456 }