1 //===-- DataFlowSanitizer.cpp - dynamic data flow analysis ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This file is a part of DataFlowSanitizer, a generalised dynamic data flow
13 /// Unlike other Sanitizer tools, this tool is not designed to detect a specific
14 /// class of bugs on its own. Instead, it provides a generic dynamic data flow
15 /// analysis framework to be used by clients to help detect application-specific
16 /// issues within their own code.
18 /// The analysis is based on automatic propagation of data flow labels (also
19 /// known as taint labels) through a program as it performs computation. Each
20 /// byte of application memory is backed by two bytes of shadow memory which
21 /// hold the label. On Linux/x86_64, memory is laid out as follows:
23 /// +--------------------+ 0x800000000000 (top of memory)
24 /// | application memory |
25 /// +--------------------+ 0x700000008000 (kAppAddr)
29 /// +--------------------+ 0x200200000000 (kUnusedAddr)
31 /// +--------------------+ 0x200000000000 (kUnionTableAddr)
33 /// +--------------------+ 0x000000010000 (kShadowAddr)
34 /// | reserved by kernel |
35 /// +--------------------+ 0x000000000000
37 /// To derive a shadow memory address from an application memory address,
38 /// bits 44-46 are cleared to bring the address into the range
39 /// [0x000000008000,0x100000000000). Then the address is shifted left by 1 to
40 /// account for the double byte representation of shadow labels and move the
41 /// address into the shadow memory range. See the function
42 /// DataFlowSanitizer::getShadowAddress below.
44 /// For more information, please refer to the design document:
45 /// http://clang.llvm.org/docs/DataFlowSanitizerDesign.html
47 #include "llvm/Transforms/Instrumentation.h"
48 #include "llvm/ADT/DenseMap.h"
49 #include "llvm/ADT/DenseSet.h"
50 #include "llvm/ADT/DepthFirstIterator.h"
51 #include "llvm/ADT/StringExtras.h"
52 #include "llvm/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"
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,
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(
89 cl::desc("File listing native ABI functions and how the pass treats them"),
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(
96 cl::desc("Use the argument ABI rather than the TLS ABI"),
99 // Controls whether the pass includes or ignores the labels of pointers in load
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));
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));
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"),
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();
131 return "<unknown type>";
135 std::unique_ptr<SpecialCaseList> SCL;
138 DFSanABIList(SpecialCaseList *SCL) : SCL(SCL) {}
140 /// Returns whether either this function or its source file are listed in the
142 bool isIn(const Function &F, const StringRef Category) const {
143 return isIn(*F.getParent(), Category) ||
144 SCL->inSection("fun", F.getName(), Category);
147 /// Returns whether this global alias is listed in the given category.
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))
155 if (isa<FunctionType>(GA.getType()->getElementType()))
156 return SCL->inSection("fun", GA.getName(), Category);
158 return SCL->inSection("global", GA.getName(), Category) ||
159 SCL->inSection("type", GetGlobalTypeString(GA), Category);
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);
168 class DataFlowSanitizer : public ModulePass {
169 friend struct DFSanFunction;
170 friend class DFSanVisitor;
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.
182 /// Argument and return value labels are passed through TLS variables
183 /// __dfsan_arg_tls and __dfsan_retval_tls.
187 /// How should calls to uninstrumented functions be handled?
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.
194 /// This function does not write to (user-accessible) memory, and its return
195 /// value is unlabelled.
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.
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.
212 const DataLayout *DL;
215 IntegerType *ShadowTy;
216 PointerType *ShadowPtrTy;
217 IntegerType *IntptrTy;
218 ConstantInt *ZeroShadow;
219 ConstantInt *ShadowPtrMask;
220 ConstantInt *ShadowPtrMul;
223 void *(*GetArgTLSPtr)();
224 void *(*GetRetvalTLSPtr)();
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;
242 Value *getShadowAddress(Value *Addr, Instruction *Pos);
243 Value *combineShadows(Value *V1, Value *V2, Instruction *Pos);
244 bool isInstrumented(const Function *F);
245 bool isInstrumented(const GlobalAlias *GA);
246 FunctionType *getArgsFunctionType(FunctionType *T);
247 FunctionType *getTrampolineFunctionType(FunctionType *T);
248 FunctionType *getCustomFunctionType(FunctionType *T);
249 InstrumentedABI getInstrumentedABI();
250 WrapperKind getWrapperKind(Function *F);
251 void addGlobalNamePrefix(GlobalValue *GV);
252 Function *buildWrapperFunction(Function *F, StringRef NewFName,
253 GlobalValue::LinkageTypes NewFLink,
254 FunctionType *NewFT);
255 Constant *getOrBuildTrampolineFunction(FunctionType *FT, StringRef FName);
258 DataFlowSanitizer(StringRef ABIListFile = StringRef(),
259 void *(*getArgTLS)() = nullptr,
260 void *(*getRetValTLS)() = nullptr);
262 bool doInitialization(Module &M) override;
263 bool runOnModule(Module &M) override;
266 struct DFSanFunction {
267 DataFlowSanitizer &DFS;
269 DataFlowSanitizer::InstrumentedABI IA;
273 AllocaInst *LabelReturnAlloca;
274 DenseMap<Value *, Value *> ValShadowMap;
275 DenseMap<AllocaInst *, AllocaInst *> AllocaShadowMap;
276 std::vector<std::pair<PHINode *, PHINode *> > PHIFixups;
277 DenseSet<Instruction *> SkipInsts;
278 DenseSet<Value *> NonZeroChecks;
280 DFSanFunction(DataFlowSanitizer &DFS, Function *F, bool IsNativeABI)
281 : DFS(DFS), F(F), IA(DFS.getInstrumentedABI()),
282 IsNativeABI(IsNativeABI), ArgTLSPtr(nullptr), RetvalTLSPtr(nullptr),
283 LabelReturnAlloca(nullptr) {}
284 Value *getArgTLSPtr();
285 Value *getArgTLS(unsigned Index, Instruction *Pos);
286 Value *getRetvalTLS();
287 Value *getShadow(Value *V);
288 void setShadow(Instruction *I, Value *Shadow);
289 Value *combineOperandShadows(Instruction *Inst);
290 Value *loadShadow(Value *ShadowAddr, uint64_t Size, uint64_t Align,
292 void storeShadow(Value *Addr, uint64_t Size, uint64_t Align, Value *Shadow,
296 class DFSanVisitor : public InstVisitor<DFSanVisitor> {
299 DFSanVisitor(DFSanFunction &DFSF) : DFSF(DFSF) {}
301 void visitOperandShadowInst(Instruction &I);
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);
325 char DataFlowSanitizer::ID;
326 INITIALIZE_PASS(DataFlowSanitizer, "dfsan",
327 "DataFlowSanitizer: dynamic data flow analysis.", false, false)
329 ModulePass *llvm::createDataFlowSanitizerPass(StringRef ABIListFile,
330 void *(*getArgTLS)(),
331 void *(*getRetValTLS)()) {
332 return new DataFlowSanitizer(ABIListFile, getArgTLS, getRetValTLS);
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
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);
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());
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);
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();
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));
380 ArgTypes.push_back(*i);
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);
391 bool DataFlowSanitizer::doInitialization(Module &M) {
392 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
394 report_fatal_error("data layout missing");
395 DL = &DLP->getDataLayout();
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);
406 Type *DFSanUnionArgs[2] = { ShadowTy, ShadowTy };
408 FunctionType::get(ShadowTy, DFSanUnionArgs, /*isVarArg=*/ false);
409 Type *DFSanUnionLoadArgs[2] = { ShadowPtrTy, IntptrTy };
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);
421 Type *ArgTLSTy = ArrayType::get(ShadowTy, 64);
423 GetArgTLS = ConstantExpr::getIntToPtr(
424 ConstantInt::get(IntptrTy, uintptr_t(GetArgTLSPtr)),
425 PointerType::getUnqual(
426 FunctionType::get(PointerType::getUnqual(ArgTLSTy),
429 if (GetRetvalTLSPtr) {
431 GetRetvalTLS = ConstantExpr::getIntToPtr(
432 ConstantInt::get(IntptrTy, uintptr_t(GetRetvalTLSPtr)),
433 PointerType::getUnqual(
434 FunctionType::get(PointerType::getUnqual(ShadowTy),
438 ColdCallWeights = MDBuilder(*Ctx).createBranchWeights(1, 1000);
442 bool DataFlowSanitizer::isInstrumented(const Function *F) {
443 return !ABIList.isIn(*F, "uninstrumented");
446 bool DataFlowSanitizer::isInstrumented(const GlobalAlias *GA) {
447 return !ABIList.isIn(*GA, "uninstrumented");
450 DataFlowSanitizer::InstrumentedABI DataFlowSanitizer::getInstrumentedABI() {
451 return ClArgsABI ? IA_Args : IA_TLS;
454 DataFlowSanitizer::WrapperKind DataFlowSanitizer::getWrapperKind(Function *F) {
455 if (ABIList.isIn(*F, "functional"))
456 return WK_Functional;
457 if (ABIList.isIn(*F, "discard"))
459 if (ABIList.isIn(*F, "custom"))
465 void DataFlowSanitizer::addGlobalNamePrefix(GlobalValue *GV) {
466 std::string GVName = GV->getName(), Prefix = "dfs$";
467 GV->setName(Prefix + GVName);
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);
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,
491 NewF->copyAttributesFrom(F);
492 NewF->removeAttributes(
493 AttributeSet::ReturnIndex,
494 AttributeFuncs::typeIncompatible(NewFT->getReturnType(),
495 AttributeSet::ReturnIndex));
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);
506 ReturnInst::Create(*Ctx, CI, BB);
511 Constant *DataFlowSanitizer::getOrBuildTrampolineFunction(FunctionType *FT,
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);
524 CallInst::Create(&F->getArgumentList().front(), Args, "", BB);
526 if (FT->getReturnType()->isVoidTy())
527 RI = ReturnInst::Create(*Ctx, BB);
529 RI = ReturnInst::Create(*Ctx, CI, BB);
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);
544 bool DataFlowSanitizer::runOnModule(Module &M) {
548 if (ABIList.isIn(M, "skip"))
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);
557 if (!GetRetvalTLSPtr) {
558 RetvalTLS = Mod->getOrInsertGlobal("__dfsan_retval_tls", ShadowTy);
559 if (GlobalVariable *G = dyn_cast<GlobalVariable>(RetvalTLS))
560 G->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
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);
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);
576 DFSanUnimplementedFn =
577 Mod->getOrInsertFunction("__dfsan_unimplemented", DFSanUnimplementedFnTy);
579 Mod->getOrInsertFunction("__dfsan_set_label", DFSanSetLabelFnTy);
580 if (Function *F = dyn_cast<Function>(DFSanSetLabelFn)) {
581 F->addAttribute(1, Attribute::ZExt);
583 DFSanNonzeroLabelFn =
584 Mod->getOrInsertFunction("__dfsan_nonzero_label", DFSanNonzeroLabelFnTy);
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() &&
591 i != DFSanUnionLoadFn &&
592 i != DFSanUnimplementedFn &&
593 i != DFSanSetLabelFn &&
594 i != DFSanNonzeroLabelFn)
595 FnsToInstrument.push_back(&*i);
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;
603 // Don't stop on weak. We assume people aren't playing games with the
604 // instrumentedness of overridden weak aliases.
605 if (Function *F = dyn_cast<Function>(GA->getAliasee())) {
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.
614 buildWrapperFunction(F, "", GA->getLinkage(), F->getFunctionType());
615 GA->replaceAllUsesWith(NewF);
617 GA->eraseFromParent();
618 FnsToInstrument.push_back(NewF);
624 B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
625 ReadOnlyNoneAttrs = AttributeSet::get(*Ctx, AttributeSet::FunctionIndex, B);
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();
633 FunctionType *FT = F.getFunctionType();
635 bool IsZeroArgsVoidRet = (FT->getNumParams() == 0 && !FT->isVarArg() &&
636 FT->getReturnType()->isVoidTy());
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);
655 NewF->getBasicBlockList().splice(NewF->begin(), F.getBasicBlockList());
657 for (Function::user_iterator UI = F.user_begin(), UE = F.user_end();
659 BlockAddress *BA = dyn_cast<BlockAddress>(*UI);
662 BA->replaceAllUsesWith(
663 BlockAddress::get(NewF, BA->getBasicBlock()));
667 F.replaceAllUsesWith(
668 ConstantExpr::getBitCast(NewF, PointerType::getUnqual(FT)));
672 addGlobalNamePrefix(NewF);
674 addGlobalNamePrefix(&F);
676 // Hopefully, nobody will try to indirectly call a vararg
678 } else if (FT->isVarArg()) {
679 UnwrappedFnMap[&F] = &F;
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)
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);
694 Value *WrappedFnCst =
695 ConstantExpr::getBitCast(NewF, PointerType::getUnqual(FT));
696 F.replaceAllUsesWith(WrappedFnCst);
697 UnwrappedFnMap[WrappedFnCst] = &F;
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);
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;
719 for (std::vector<Function *>::iterator i = FnsToInstrument.begin(),
720 e = FnsToInstrument.end();
722 if (!*i || (*i)->isDeclaration())
725 removeUnreachableBlocks(**i);
727 DFSanFunction DFSF(*this, *i, FnsWithNativeABI.count(*i));
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()));
734 for (llvm::SmallVector<BasicBlock *, 4>::iterator i = BBList.begin(),
737 Instruction *Inst = &(*i)->front();
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
745 bool IsTerminator = isa<TerminatorInst>(Inst);
746 if (!DFSF.SkipInsts.count(Inst))
747 DFSanVisitor(DFSF).visit(Inst);
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
758 for (std::vector<std::pair<PHINode *, PHINode *> >::iterator
759 i = DFSF.PHIFixups.begin(),
760 e = DFSF.PHIFixups.end();
762 for (unsigned val = 0, n = i->first->getNumIncomingValues(); val != n;
764 i->second->setIncomingValue(
765 val, DFSF.getShadow(i->first->getIncomingValue(val)));
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
773 if (ClDebugNonzeroLabels) {
774 for (DenseSet<Value *>::iterator i = DFSF.NonZeroChecks.begin(),
775 e = DFSF.NonZeroChecks.end();
778 if (Instruction *I = dyn_cast<Instruction>(*i))
779 Pos = I->getNextNode();
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);
797 Value *DFSanFunction::getArgTLSPtr() {
801 return ArgTLSPtr = DFS.ArgTLS;
803 IRBuilder<> IRB(F->getEntryBlock().begin());
804 return ArgTLSPtr = IRB.CreateCall(DFS.GetArgTLS);
807 Value *DFSanFunction::getRetvalTLS() {
811 return RetvalTLSPtr = DFS.RetvalTLS;
813 IRBuilder<> IRB(F->getEntryBlock().begin());
814 return RetvalTLSPtr = IRB.CreateCall(DFS.GetRetvalTLS);
817 Value *DFSanFunction::getArgTLS(unsigned Idx, Instruction *Pos) {
818 IRBuilder<> IRB(Pos);
819 return IRB.CreateConstGEP2_64(getArgTLSPtr(), 0, Idx);
822 Value *DFSanFunction::getShadow(Value *V) {
823 if (!isa<Argument>(V) && !isa<Instruction>(V))
824 return DFS.ZeroShadow;
825 Value *&Shadow = ValShadowMap[V];
827 if (Argument *A = dyn_cast<Argument>(V)) {
829 return DFS.ZeroShadow;
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));
840 case DataFlowSanitizer::IA_Args: {
841 unsigned ArgIdx = A->getArgNo() + F->getArgumentList().size() / 2;
842 Function::arg_iterator i = F->arg_begin();
846 assert(Shadow->getType() == DFS.ShadowTy);
850 NonZeroChecks.insert(Shadow);
852 Shadow = DFS.ZeroShadow;
858 void DFSanFunction::setShadow(Instruction *I, Value *Shadow) {
859 assert(!ValShadowMap.count(I));
860 assert(Shadow->getType() == DFS.ShadowTy);
861 ValShadowMap[I] = Shadow;
864 Value *DataFlowSanitizer::getShadowAddress(Value *Addr, Instruction *Pos) {
865 assert(Addr != RetvalTLS && "Reinstrumenting?");
866 IRBuilder<> IRB(Pos);
867 return IRB.CreateIntToPtr(
869 IRB.CreateAnd(IRB.CreatePtrToInt(Addr, IntptrTy), ShadowPtrMask),
874 // Generates IR to compute the union of the two given shadows, inserting it
875 // before Pos. Returns the computed union Value.
876 Value *DataFlowSanitizer::combineShadows(Value *V1, Value *V2,
878 if (V1 == ZeroShadow)
880 if (V2 == ZeroShadow)
884 IRBuilder<> IRB(Pos);
885 BasicBlock *Head = Pos->getParent();
886 Value *Ne = IRB.CreateICmpNE(V1, V2);
887 BranchInst *BI = cast<BranchInst>(SplitBlockAndInsertIfThen(
888 Ne, Pos, /*Unreachable=*/false, ColdCallWeights));
889 IRBuilder<> ThenIRB(BI);
890 CallInst *Call = ThenIRB.CreateCall2(DFSanUnionFn, V1, V2);
891 Call->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
892 Call->addAttribute(1, Attribute::ZExt);
893 Call->addAttribute(2, Attribute::ZExt);
895 BasicBlock *Tail = BI->getSuccessor(0);
896 PHINode *Phi = PHINode::Create(ShadowTy, 2, "", Tail->begin());
897 Phi->addIncoming(Call, Call->getParent());
898 Phi->addIncoming(V1, Head);
902 // A convenience function which folds the shadows of each of the operands
903 // of the provided instruction Inst, inserting the IR before Inst. Returns
904 // the computed union Value.
905 Value *DFSanFunction::combineOperandShadows(Instruction *Inst) {
906 if (Inst->getNumOperands() == 0)
907 return DFS.ZeroShadow;
909 Value *Shadow = getShadow(Inst->getOperand(0));
910 for (unsigned i = 1, n = Inst->getNumOperands(); i != n; ++i) {
911 Shadow = DFS.combineShadows(Shadow, getShadow(Inst->getOperand(i)), Inst);
916 void DFSanVisitor::visitOperandShadowInst(Instruction &I) {
917 Value *CombinedShadow = DFSF.combineOperandShadows(&I);
918 DFSF.setShadow(&I, CombinedShadow);
921 // Generates IR to load shadow corresponding to bytes [Addr, Addr+Size), where
922 // Addr has alignment Align, and take the union of each of those shadows.
923 Value *DFSanFunction::loadShadow(Value *Addr, uint64_t Size, uint64_t Align,
925 if (AllocaInst *AI = dyn_cast<AllocaInst>(Addr)) {
926 llvm::DenseMap<AllocaInst *, AllocaInst *>::iterator i =
927 AllocaShadowMap.find(AI);
928 if (i != AllocaShadowMap.end()) {
929 IRBuilder<> IRB(Pos);
930 return IRB.CreateLoad(i->second);
934 uint64_t ShadowAlign = Align * DFS.ShadowWidth / 8;
935 SmallVector<Value *, 2> Objs;
936 GetUnderlyingObjects(Addr, Objs, DFS.DL);
937 bool AllConstants = true;
938 for (SmallVector<Value *, 2>::iterator i = Objs.begin(), e = Objs.end();
940 if (isa<Function>(*i) || isa<BlockAddress>(*i))
942 if (isa<GlobalVariable>(*i) && cast<GlobalVariable>(*i)->isConstant())
945 AllConstants = false;
949 return DFS.ZeroShadow;
951 Value *ShadowAddr = DFS.getShadowAddress(Addr, Pos);
954 return DFS.ZeroShadow;
956 LoadInst *LI = new LoadInst(ShadowAddr, "", Pos);
957 LI->setAlignment(ShadowAlign);
961 IRBuilder<> IRB(Pos);
963 IRB.CreateGEP(ShadowAddr, ConstantInt::get(DFS.IntptrTy, 1));
964 return DFS.combineShadows(IRB.CreateAlignedLoad(ShadowAddr, ShadowAlign),
965 IRB.CreateAlignedLoad(ShadowAddr1, ShadowAlign),
969 if (Size % (64 / DFS.ShadowWidth) == 0) {
970 // Fast path for the common case where each byte has identical shadow: load
971 // shadow 64 bits at a time, fall out to a __dfsan_union_load call if any
972 // shadow is non-equal.
973 BasicBlock *FallbackBB = BasicBlock::Create(*DFS.Ctx, "", F);
974 IRBuilder<> FallbackIRB(FallbackBB);
975 CallInst *FallbackCall = FallbackIRB.CreateCall2(
976 DFS.DFSanUnionLoadFn, ShadowAddr, ConstantInt::get(DFS.IntptrTy, Size));
977 FallbackCall->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
979 // Compare each of the shadows stored in the loaded 64 bits to each other,
980 // by computing (WideShadow rotl ShadowWidth) == WideShadow.
981 IRBuilder<> IRB(Pos);
983 IRB.CreateBitCast(ShadowAddr, Type::getInt64PtrTy(*DFS.Ctx));
984 Value *WideShadow = IRB.CreateAlignedLoad(WideAddr, ShadowAlign);
985 Value *TruncShadow = IRB.CreateTrunc(WideShadow, DFS.ShadowTy);
986 Value *ShlShadow = IRB.CreateShl(WideShadow, DFS.ShadowWidth);
987 Value *ShrShadow = IRB.CreateLShr(WideShadow, 64 - DFS.ShadowWidth);
988 Value *RotShadow = IRB.CreateOr(ShlShadow, ShrShadow);
989 Value *ShadowsEq = IRB.CreateICmpEQ(WideShadow, RotShadow);
991 BasicBlock *Head = Pos->getParent();
992 BasicBlock *Tail = Head->splitBasicBlock(Pos);
993 // In the following code LastBr will refer to the previous basic block's
994 // conditional branch instruction, whose true successor is fixed up to point
995 // to the next block during the loop below or to the tail after the final
997 BranchInst *LastBr = BranchInst::Create(FallbackBB, FallbackBB, ShadowsEq);
998 ReplaceInstWithInst(Head->getTerminator(), LastBr);
1000 for (uint64_t Ofs = 64 / DFS.ShadowWidth; Ofs != Size;
1001 Ofs += 64 / DFS.ShadowWidth) {
1002 BasicBlock *NextBB = BasicBlock::Create(*DFS.Ctx, "", F);
1003 IRBuilder<> NextIRB(NextBB);
1004 WideAddr = NextIRB.CreateGEP(WideAddr, ConstantInt::get(DFS.IntptrTy, 1));
1005 Value *NextWideShadow = NextIRB.CreateAlignedLoad(WideAddr, ShadowAlign);
1006 ShadowsEq = NextIRB.CreateICmpEQ(WideShadow, NextWideShadow);
1007 LastBr->setSuccessor(0, NextBB);
1008 LastBr = NextIRB.CreateCondBr(ShadowsEq, FallbackBB, FallbackBB);
1011 LastBr->setSuccessor(0, Tail);
1012 FallbackIRB.CreateBr(Tail);
1013 PHINode *Shadow = PHINode::Create(DFS.ShadowTy, 2, "", &Tail->front());
1014 Shadow->addIncoming(FallbackCall, FallbackBB);
1015 Shadow->addIncoming(TruncShadow, LastBr->getParent());
1019 IRBuilder<> IRB(Pos);
1020 CallInst *FallbackCall = IRB.CreateCall2(
1021 DFS.DFSanUnionLoadFn, ShadowAddr, ConstantInt::get(DFS.IntptrTy, Size));
1022 FallbackCall->addAttribute(AttributeSet::ReturnIndex, Attribute::ZExt);
1023 return FallbackCall;
1026 void DFSanVisitor::visitLoadInst(LoadInst &LI) {
1027 uint64_t Size = DFSF.DFS.DL->getTypeStoreSize(LI.getType());
1029 if (ClPreserveAlignment) {
1030 Align = LI.getAlignment();
1032 Align = DFSF.DFS.DL->getABITypeAlignment(LI.getType());
1036 IRBuilder<> IRB(&LI);
1037 Value *Shadow = DFSF.loadShadow(LI.getPointerOperand(), Size, Align, &LI);
1038 if (ClCombinePointerLabelsOnLoad) {
1039 Value *PtrShadow = DFSF.getShadow(LI.getPointerOperand());
1040 Shadow = DFSF.DFS.combineShadows(Shadow, PtrShadow, &LI);
1042 if (Shadow != DFSF.DFS.ZeroShadow)
1043 DFSF.NonZeroChecks.insert(Shadow);
1045 DFSF.setShadow(&LI, Shadow);
1048 void DFSanFunction::storeShadow(Value *Addr, uint64_t Size, uint64_t Align,
1049 Value *Shadow, Instruction *Pos) {
1050 if (AllocaInst *AI = dyn_cast<AllocaInst>(Addr)) {
1051 llvm::DenseMap<AllocaInst *, AllocaInst *>::iterator i =
1052 AllocaShadowMap.find(AI);
1053 if (i != AllocaShadowMap.end()) {
1054 IRBuilder<> IRB(Pos);
1055 IRB.CreateStore(Shadow, i->second);
1060 uint64_t ShadowAlign = Align * DFS.ShadowWidth / 8;
1061 IRBuilder<> IRB(Pos);
1062 Value *ShadowAddr = DFS.getShadowAddress(Addr, Pos);
1063 if (Shadow == DFS.ZeroShadow) {
1064 IntegerType *ShadowTy = IntegerType::get(*DFS.Ctx, Size * DFS.ShadowWidth);
1065 Value *ExtZeroShadow = ConstantInt::get(ShadowTy, 0);
1066 Value *ExtShadowAddr =
1067 IRB.CreateBitCast(ShadowAddr, PointerType::getUnqual(ShadowTy));
1068 IRB.CreateAlignedStore(ExtZeroShadow, ExtShadowAddr, ShadowAlign);
1072 const unsigned ShadowVecSize = 128 / DFS.ShadowWidth;
1073 uint64_t Offset = 0;
1074 if (Size >= ShadowVecSize) {
1075 VectorType *ShadowVecTy = VectorType::get(DFS.ShadowTy, ShadowVecSize);
1076 Value *ShadowVec = UndefValue::get(ShadowVecTy);
1077 for (unsigned i = 0; i != ShadowVecSize; ++i) {
1078 ShadowVec = IRB.CreateInsertElement(
1079 ShadowVec, Shadow, ConstantInt::get(Type::getInt32Ty(*DFS.Ctx), i));
1081 Value *ShadowVecAddr =
1082 IRB.CreateBitCast(ShadowAddr, PointerType::getUnqual(ShadowVecTy));
1084 Value *CurShadowVecAddr = IRB.CreateConstGEP1_32(ShadowVecAddr, Offset);
1085 IRB.CreateAlignedStore(ShadowVec, CurShadowVecAddr, ShadowAlign);
1086 Size -= ShadowVecSize;
1088 } while (Size >= ShadowVecSize);
1089 Offset *= ShadowVecSize;
1092 Value *CurShadowAddr = IRB.CreateConstGEP1_32(ShadowAddr, Offset);
1093 IRB.CreateAlignedStore(Shadow, CurShadowAddr, ShadowAlign);
1099 void DFSanVisitor::visitStoreInst(StoreInst &SI) {
1101 DFSF.DFS.DL->getTypeStoreSize(SI.getValueOperand()->getType());
1103 if (ClPreserveAlignment) {
1104 Align = SI.getAlignment();
1106 Align = DFSF.DFS.DL->getABITypeAlignment(SI.getValueOperand()->getType());
1111 Value* Shadow = DFSF.getShadow(SI.getValueOperand());
1112 if (ClCombinePointerLabelsOnStore) {
1113 Value *PtrShadow = DFSF.getShadow(SI.getPointerOperand());
1114 Shadow = DFSF.DFS.combineShadows(Shadow, PtrShadow, &SI);
1116 DFSF.storeShadow(SI.getPointerOperand(), Size, Align, Shadow, &SI);
1119 void DFSanVisitor::visitBinaryOperator(BinaryOperator &BO) {
1120 visitOperandShadowInst(BO);
1123 void DFSanVisitor::visitCastInst(CastInst &CI) { visitOperandShadowInst(CI); }
1125 void DFSanVisitor::visitCmpInst(CmpInst &CI) { visitOperandShadowInst(CI); }
1127 void DFSanVisitor::visitGetElementPtrInst(GetElementPtrInst &GEPI) {
1128 visitOperandShadowInst(GEPI);
1131 void DFSanVisitor::visitExtractElementInst(ExtractElementInst &I) {
1132 visitOperandShadowInst(I);
1135 void DFSanVisitor::visitInsertElementInst(InsertElementInst &I) {
1136 visitOperandShadowInst(I);
1139 void DFSanVisitor::visitShuffleVectorInst(ShuffleVectorInst &I) {
1140 visitOperandShadowInst(I);
1143 void DFSanVisitor::visitExtractValueInst(ExtractValueInst &I) {
1144 visitOperandShadowInst(I);
1147 void DFSanVisitor::visitInsertValueInst(InsertValueInst &I) {
1148 visitOperandShadowInst(I);
1151 void DFSanVisitor::visitAllocaInst(AllocaInst &I) {
1152 bool AllLoadsStores = true;
1153 for (User *U : I.users()) {
1154 if (isa<LoadInst>(U))
1157 if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
1158 if (SI->getPointerOperand() == &I)
1162 AllLoadsStores = false;
1165 if (AllLoadsStores) {
1166 IRBuilder<> IRB(&I);
1167 DFSF.AllocaShadowMap[&I] = IRB.CreateAlloca(DFSF.DFS.ShadowTy);
1169 DFSF.setShadow(&I, DFSF.DFS.ZeroShadow);
1172 void DFSanVisitor::visitSelectInst(SelectInst &I) {
1173 Value *CondShadow = DFSF.getShadow(I.getCondition());
1174 Value *TrueShadow = DFSF.getShadow(I.getTrueValue());
1175 Value *FalseShadow = DFSF.getShadow(I.getFalseValue());
1177 if (isa<VectorType>(I.getCondition()->getType())) {
1179 &I, DFSF.DFS.combineShadows(
1181 DFSF.DFS.combineShadows(TrueShadow, FalseShadow, &I), &I));
1184 if (TrueShadow == FalseShadow) {
1185 ShadowSel = TrueShadow;
1188 SelectInst::Create(I.getCondition(), TrueShadow, FalseShadow, "", &I);
1190 DFSF.setShadow(&I, DFSF.DFS.combineShadows(CondShadow, ShadowSel, &I));
1194 void DFSanVisitor::visitMemSetInst(MemSetInst &I) {
1195 IRBuilder<> IRB(&I);
1196 Value *ValShadow = DFSF.getShadow(I.getValue());
1198 DFSF.DFS.DFSanSetLabelFn, ValShadow,
1199 IRB.CreateBitCast(I.getDest(), Type::getInt8PtrTy(*DFSF.DFS.Ctx)),
1200 IRB.CreateZExtOrTrunc(I.getLength(), DFSF.DFS.IntptrTy));
1203 void DFSanVisitor::visitMemTransferInst(MemTransferInst &I) {
1204 IRBuilder<> IRB(&I);
1205 Value *DestShadow = DFSF.DFS.getShadowAddress(I.getDest(), &I);
1206 Value *SrcShadow = DFSF.DFS.getShadowAddress(I.getSource(), &I);
1207 Value *LenShadow = IRB.CreateMul(
1209 ConstantInt::get(I.getLength()->getType(), DFSF.DFS.ShadowWidth / 8));
1211 if (ClPreserveAlignment) {
1212 AlignShadow = IRB.CreateMul(I.getAlignmentCst(),
1213 ConstantInt::get(I.getAlignmentCst()->getType(),
1214 DFSF.DFS.ShadowWidth / 8));
1216 AlignShadow = ConstantInt::get(I.getAlignmentCst()->getType(),
1217 DFSF.DFS.ShadowWidth / 8);
1219 Type *Int8Ptr = Type::getInt8PtrTy(*DFSF.DFS.Ctx);
1220 DestShadow = IRB.CreateBitCast(DestShadow, Int8Ptr);
1221 SrcShadow = IRB.CreateBitCast(SrcShadow, Int8Ptr);
1222 IRB.CreateCall5(I.getCalledValue(), DestShadow, SrcShadow, LenShadow,
1223 AlignShadow, I.getVolatileCst());
1226 void DFSanVisitor::visitReturnInst(ReturnInst &RI) {
1227 if (!DFSF.IsNativeABI && RI.getReturnValue()) {
1229 case DataFlowSanitizer::IA_TLS: {
1230 Value *S = DFSF.getShadow(RI.getReturnValue());
1231 IRBuilder<> IRB(&RI);
1232 IRB.CreateStore(S, DFSF.getRetvalTLS());
1235 case DataFlowSanitizer::IA_Args: {
1236 IRBuilder<> IRB(&RI);
1237 Type *RT = DFSF.F->getFunctionType()->getReturnType();
1239 IRB.CreateInsertValue(UndefValue::get(RT), RI.getReturnValue(), 0);
1241 IRB.CreateInsertValue(InsVal, DFSF.getShadow(RI.getReturnValue()), 1);
1242 RI.setOperand(0, InsShadow);
1249 void DFSanVisitor::visitCallSite(CallSite CS) {
1250 Function *F = CS.getCalledFunction();
1251 if ((F && F->isIntrinsic()) || isa<InlineAsm>(CS.getCalledValue())) {
1252 visitOperandShadowInst(*CS.getInstruction());
1256 IRBuilder<> IRB(CS.getInstruction());
1258 DenseMap<Value *, Function *>::iterator i =
1259 DFSF.DFS.UnwrappedFnMap.find(CS.getCalledValue());
1260 if (i != DFSF.DFS.UnwrappedFnMap.end()) {
1261 Function *F = i->second;
1262 switch (DFSF.DFS.getWrapperKind(F)) {
1263 case DataFlowSanitizer::WK_Warning: {
1264 CS.setCalledFunction(F);
1265 IRB.CreateCall(DFSF.DFS.DFSanUnimplementedFn,
1266 IRB.CreateGlobalStringPtr(F->getName()));
1267 DFSF.setShadow(CS.getInstruction(), DFSF.DFS.ZeroShadow);
1270 case DataFlowSanitizer::WK_Discard: {
1271 CS.setCalledFunction(F);
1272 DFSF.setShadow(CS.getInstruction(), DFSF.DFS.ZeroShadow);
1275 case DataFlowSanitizer::WK_Functional: {
1276 CS.setCalledFunction(F);
1277 visitOperandShadowInst(*CS.getInstruction());
1280 case DataFlowSanitizer::WK_Custom: {
1281 // Don't try to handle invokes of custom functions, it's too complicated.
1282 // Instead, invoke the dfsw$ wrapper, which will in turn call the __dfsw_
1284 if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) {
1285 FunctionType *FT = F->getFunctionType();
1286 FunctionType *CustomFT = DFSF.DFS.getCustomFunctionType(FT);
1287 std::string CustomFName = "__dfsw_";
1288 CustomFName += F->getName();
1290 DFSF.DFS.Mod->getOrInsertFunction(CustomFName, CustomFT);
1291 if (Function *CustomFn = dyn_cast<Function>(CustomF)) {
1292 CustomFn->copyAttributesFrom(F);
1294 // Custom functions returning non-void will write to the return label.
1295 if (!FT->getReturnType()->isVoidTy()) {
1296 CustomFn->removeAttributes(AttributeSet::FunctionIndex,
1297 DFSF.DFS.ReadOnlyNoneAttrs);
1301 std::vector<Value *> Args;
1303 CallSite::arg_iterator i = CS.arg_begin();
1304 for (unsigned n = FT->getNumParams(); n != 0; ++i, --n) {
1305 Type *T = (*i)->getType();
1306 FunctionType *ParamFT;
1307 if (isa<PointerType>(T) &&
1308 (ParamFT = dyn_cast<FunctionType>(
1309 cast<PointerType>(T)->getElementType()))) {
1310 std::string TName = "dfst";
1311 TName += utostr(FT->getNumParams() - n);
1313 TName += F->getName();
1314 Constant *T = DFSF.DFS.getOrBuildTrampolineFunction(ParamFT, TName);
1317 IRB.CreateBitCast(*i, Type::getInt8PtrTy(*DFSF.DFS.Ctx)));
1324 for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
1325 Args.push_back(DFSF.getShadow(*i));
1327 if (!FT->getReturnType()->isVoidTy()) {
1328 if (!DFSF.LabelReturnAlloca) {
1329 DFSF.LabelReturnAlloca =
1330 new AllocaInst(DFSF.DFS.ShadowTy, "labelreturn",
1331 DFSF.F->getEntryBlock().begin());
1333 Args.push_back(DFSF.LabelReturnAlloca);
1336 CallInst *CustomCI = IRB.CreateCall(CustomF, Args);
1337 CustomCI->setCallingConv(CI->getCallingConv());
1338 CustomCI->setAttributes(CI->getAttributes());
1340 if (!FT->getReturnType()->isVoidTy()) {
1341 LoadInst *LabelLoad = IRB.CreateLoad(DFSF.LabelReturnAlloca);
1342 DFSF.setShadow(CustomCI, LabelLoad);
1345 CI->replaceAllUsesWith(CustomCI);
1346 CI->eraseFromParent();
1354 FunctionType *FT = cast<FunctionType>(
1355 CS.getCalledValue()->getType()->getPointerElementType());
1356 if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_TLS) {
1357 for (unsigned i = 0, n = FT->getNumParams(); i != n; ++i) {
1358 IRB.CreateStore(DFSF.getShadow(CS.getArgument(i)),
1359 DFSF.getArgTLS(i, CS.getInstruction()));
1363 Instruction *Next = nullptr;
1364 if (!CS.getType()->isVoidTy()) {
1365 if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
1366 if (II->getNormalDest()->getSinglePredecessor()) {
1367 Next = II->getNormalDest()->begin();
1370 SplitEdge(II->getParent(), II->getNormalDest(), &DFSF.DFS);
1371 Next = NewBB->begin();
1374 Next = CS->getNextNode();
1377 if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_TLS) {
1378 IRBuilder<> NextIRB(Next);
1379 LoadInst *LI = NextIRB.CreateLoad(DFSF.getRetvalTLS());
1380 DFSF.SkipInsts.insert(LI);
1381 DFSF.setShadow(CS.getInstruction(), LI);
1382 DFSF.NonZeroChecks.insert(LI);
1386 // Do all instrumentation for IA_Args down here to defer tampering with the
1387 // CFG in a way that SplitEdge may be able to detect.
1388 if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_Args) {
1389 FunctionType *NewFT = DFSF.DFS.getArgsFunctionType(FT);
1391 IRB.CreateBitCast(CS.getCalledValue(), PointerType::getUnqual(NewFT));
1392 std::vector<Value *> Args;
1394 CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1395 for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
1399 for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
1400 Args.push_back(DFSF.getShadow(*i));
1402 if (FT->isVarArg()) {
1403 unsigned VarArgSize = CS.arg_size() - FT->getNumParams();
1404 ArrayType *VarArgArrayTy = ArrayType::get(DFSF.DFS.ShadowTy, VarArgSize);
1405 AllocaInst *VarArgShadow =
1406 new AllocaInst(VarArgArrayTy, "", DFSF.F->getEntryBlock().begin());
1407 Args.push_back(IRB.CreateConstGEP2_32(VarArgShadow, 0, 0));
1408 for (unsigned n = 0; i != e; ++i, ++n) {
1409 IRB.CreateStore(DFSF.getShadow(*i),
1410 IRB.CreateConstGEP2_32(VarArgShadow, 0, n));
1416 if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
1417 NewCS = IRB.CreateInvoke(Func, II->getNormalDest(), II->getUnwindDest(),
1420 NewCS = IRB.CreateCall(Func, Args);
1422 NewCS.setCallingConv(CS.getCallingConv());
1423 NewCS.setAttributes(CS.getAttributes().removeAttributes(
1424 *DFSF.DFS.Ctx, AttributeSet::ReturnIndex,
1425 AttributeFuncs::typeIncompatible(NewCS.getInstruction()->getType(),
1426 AttributeSet::ReturnIndex)));
1429 ExtractValueInst *ExVal =
1430 ExtractValueInst::Create(NewCS.getInstruction(), 0, "", Next);
1431 DFSF.SkipInsts.insert(ExVal);
1432 ExtractValueInst *ExShadow =
1433 ExtractValueInst::Create(NewCS.getInstruction(), 1, "", Next);
1434 DFSF.SkipInsts.insert(ExShadow);
1435 DFSF.setShadow(ExVal, ExShadow);
1436 DFSF.NonZeroChecks.insert(ExShadow);
1438 CS.getInstruction()->replaceAllUsesWith(ExVal);
1441 CS.getInstruction()->eraseFromParent();
1445 void DFSanVisitor::visitPHINode(PHINode &PN) {
1447 PHINode::Create(DFSF.DFS.ShadowTy, PN.getNumIncomingValues(), "", &PN);
1449 // Give the shadow phi node valid predecessors to fool SplitEdge into working.
1450 Value *UndefShadow = UndefValue::get(DFSF.DFS.ShadowTy);
1451 for (PHINode::block_iterator i = PN.block_begin(), e = PN.block_end(); i != e;
1453 ShadowPN->addIncoming(UndefShadow, *i);
1456 DFSF.PHIFixups.push_back(std::make_pair(&PN, ShadowPN));
1457 DFSF.setShadow(&PN, ShadowPN);