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