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