OLD | NEW |
1 //===- subzero/src/IceASanInstrumentation.cpp - ASan ------------*- C++ -*-===// | 1 //===- subzero/src/IceASanInstrumentation.cpp - ASan ------------*- C++ -*-===// |
2 // | 2 // |
3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 /// | 9 /// |
10 /// \file | 10 /// \file |
11 /// \brief Implements the AddressSanitizer instrumentation class. | 11 /// \brief Implements the AddressSanitizer instrumentation class. |
12 /// | 12 /// |
13 //===----------------------------------------------------------------------===// | 13 //===----------------------------------------------------------------------===// |
14 | 14 |
15 #include "IceASanInstrumentation.h" | 15 #include "IceASanInstrumentation.h" |
16 | 16 |
17 #include "IceBuildDefs.h" | 17 #include "IceBuildDefs.h" |
18 #include "IceCfgNode.h" | 18 #include "IceCfgNode.h" |
19 #include "IceGlobalInits.h" | 19 #include "IceGlobalInits.h" |
20 #include "IceInst.h" | 20 #include "IceInst.h" |
| 21 #include "IceTargetLowering.h" |
| 22 #include "IceTypes.h" |
21 | 23 |
22 #include <sstream> | 24 #include <sstream> |
23 | 25 |
24 namespace Ice { | 26 namespace Ice { |
25 | 27 |
26 namespace { | 28 namespace { |
27 constexpr SizeT RzSize = 32; | 29 constexpr SizeT RzSize = 32; |
28 const std::string RzPrefix = "__$rz"; | 30 const std::string RzPrefix = "__$rz"; |
29 const llvm::NaClBitcodeRecord::RecordVector RzContents = | 31 const llvm::NaClBitcodeRecord::RecordVector RzContents = |
30 llvm::NaClBitcodeRecord::RecordVector(RzSize, 'R'); | 32 llvm::NaClBitcodeRecord::RecordVector(RzSize, 'R'); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 Rz->addInitializer( | 106 Rz->addInitializer( |
105 VariableDeclaration::ZeroInitializer::create(List, RzSize)); | 107 VariableDeclaration::ZeroInitializer::create(List, RzSize)); |
106 } | 108 } |
107 Rz->setIsConstant(Global->getIsConstant()); | 109 Rz->setIsConstant(Global->getIsConstant()); |
108 RzArray->addInitializer(VariableDeclaration::RelocInitializer::create( | 110 RzArray->addInitializer(VariableDeclaration::RelocInitializer::create( |
109 List, Rz, RelocOffsetArray(0))); | 111 List, Rz, RelocOffsetArray(0))); |
110 ++RzArraySize; | 112 ++RzArraySize; |
111 return Rz; | 113 return Rz; |
112 } | 114 } |
113 | 115 |
| 116 void ASanInstrumentation::instrumentLoad(LoweringContext &Context, |
| 117 const InstLoad *Inst) { |
| 118 instrumentAccess(Context, Inst->getSourceAddress(), |
| 119 typeWidthInBytes(Inst->getDest()->getType())); |
| 120 } |
| 121 |
| 122 void ASanInstrumentation::instrumentStore(LoweringContext &Context, |
| 123 const InstStore *Inst) { |
| 124 instrumentAccess(Context, Inst->getAddr(), |
| 125 typeWidthInBytes(Inst->getData()->getType())); |
| 126 } |
| 127 |
| 128 // TODO(tlively): Take size of access into account as well |
| 129 void ASanInstrumentation::instrumentAccess(LoweringContext &Context, |
| 130 Operand *Op, SizeT Size) { |
| 131 Constant *AccessCheck = |
| 132 Ctx->getConstantExternSym(Ctx->getGlobalString("__asan_check")); |
| 133 constexpr SizeT NumArgs = 2; |
| 134 constexpr Variable *Void = nullptr; |
| 135 constexpr bool NoTailCall = false; |
| 136 auto *Call = InstCall::create(Context.getNode()->getCfg(), NumArgs, Void, |
| 137 AccessCheck, NoTailCall); |
| 138 Call->addArg(Op); |
| 139 Call->addArg(ConstantInteger32::create(Ctx, IceType_i32, Size)); |
| 140 // play games to insert the call before the access instruction |
| 141 InstList::iterator Next = Context.getNext(); |
| 142 Context.setInsertPoint(Context.getCur()); |
| 143 Context.insert(Call); |
| 144 Context.setNext(Next); |
| 145 } |
| 146 |
114 void ASanInstrumentation::instrumentStart(Cfg *Func) { | 147 void ASanInstrumentation::instrumentStart(Cfg *Func) { |
115 Constant *ShadowMemInit = | 148 Constant *ShadowMemInit = |
116 Ctx->getConstantExternSym(Ctx->getGlobalString("__asan_init")); | 149 Ctx->getConstantExternSym(Ctx->getGlobalString("__asan_init")); |
117 constexpr SizeT NumArgs = 0; | 150 constexpr SizeT NumArgs = 0; |
118 constexpr Variable *Void = nullptr; | 151 constexpr Variable *Void = nullptr; |
119 constexpr bool NoTailCall = false; | 152 constexpr bool NoTailCall = false; |
120 | |
121 auto *Call = InstCall::create(Func, NumArgs, Void, ShadowMemInit, NoTailCall); | 153 auto *Call = InstCall::create(Func, NumArgs, Void, ShadowMemInit, NoTailCall); |
122 Func->getEntryNode()->getInsts().push_front(Call); | 154 Func->getEntryNode()->getInsts().push_front(Call); |
123 } | 155 } |
124 | 156 |
125 } // end of namespace Ice | 157 } // end of namespace Ice |
OLD | NEW |