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