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