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 "IceCfg.h" | |
18 #include "IceCfgNode.h" | 19 #include "IceCfgNode.h" |
19 #include "IceGlobalInits.h" | 20 #include "IceGlobalInits.h" |
20 #include "IceInst.h" | 21 #include "IceInst.h" |
21 #include "IceTargetLowering.h" | 22 #include "IceTargetLowering.h" |
22 #include "IceTypes.h" | 23 #include "IceTypes.h" |
23 | 24 |
24 #include <sstream> | 25 #include <sstream> |
25 #include <unordered_map> | 26 #include <unordered_map> |
26 | 27 |
27 namespace Ice { | 28 namespace Ice { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 | 89 |
89 // Log the new set of globals | 90 // Log the new set of globals |
90 if (BuildDefs::dump() && (getFlags().getVerbose() & IceV_GlobalInit)) { | 91 if (BuildDefs::dump() && (getFlags().getVerbose() & IceV_GlobalInit)) { |
91 OstreamLocker _(Ctx); | 92 OstreamLocker _(Ctx); |
92 Ctx->getStrDump() << "========= Instrumented Globals =========\n"; | 93 Ctx->getStrDump() << "========= Instrumented Globals =========\n"; |
93 for (VariableDeclaration *Global : Globals) { | 94 for (VariableDeclaration *Global : Globals) { |
94 Global->dump(Ctx->getStrDump()); | 95 Global->dump(Ctx->getStrDump()); |
95 } | 96 } |
96 } | 97 } |
97 } | 98 } |
98 | 99 // TODO(tlively): Make this thread safe |
99 std::string ASanInstrumentation::nextRzName() { | 100 std::string ASanInstrumentation::nextRzName() { |
100 std::stringstream Name; | 101 std::stringstream Name; |
101 Name << RzPrefix << RzNum++; | 102 Name << RzPrefix << RzNum++; |
102 return Name.str(); | 103 return Name.str(); |
103 } | 104 } |
104 | 105 |
105 VariableDeclaration * | 106 VariableDeclaration * |
106 ASanInstrumentation::createRz(VariableDeclarationList *List, | 107 ASanInstrumentation::createRz(VariableDeclarationList *List, |
107 VariableDeclaration *RzArray, SizeT &RzArraySize, | 108 VariableDeclaration *RzArray, SizeT &RzArraySize, |
108 VariableDeclaration *Global) { | 109 VariableDeclaration *Global) { |
109 auto *Rz = VariableDeclaration::create(List); | 110 auto *Rz = VariableDeclaration::create(List); |
110 Rz->setName(Ctx, nextRzName()); | 111 Rz->setName(Ctx, nextRzName()); |
111 if (Global->hasNonzeroInitializer()) { | 112 if (Global->hasNonzeroInitializer()) { |
112 Rz->addInitializer( | 113 Rz->addInitializer( |
113 VariableDeclaration::DataInitializer::create(List, RzContents)); | 114 VariableDeclaration::DataInitializer::create(List, RzContents)); |
114 } else { | 115 } else { |
115 Rz->addInitializer( | 116 Rz->addInitializer( |
116 VariableDeclaration::ZeroInitializer::create(List, RzSize)); | 117 VariableDeclaration::ZeroInitializer::create(List, RzSize)); |
117 } | 118 } |
118 Rz->setIsConstant(Global->getIsConstant()); | 119 Rz->setIsConstant(Global->getIsConstant()); |
119 RzArray->addInitializer(VariableDeclaration::RelocInitializer::create( | 120 RzArray->addInitializer(VariableDeclaration::RelocInitializer::create( |
120 List, Rz, RelocOffsetArray(0))); | 121 List, Rz, RelocOffsetArray(0))); |
121 ++RzArraySize; | 122 ++RzArraySize; |
122 return Rz; | 123 return Rz; |
123 } | 124 } |
124 | 125 |
126 // Check for an alloca signaling the presence of local variables and add a | |
127 // redzone if it is found | |
128 void ASanInstrumentation::instrumentFuncStart(LoweringContext &Context) { | |
129 auto *FirstAlloca = llvm::dyn_cast<InstAlloca>(Context.getCur()); | |
130 if (FirstAlloca == nullptr) | |
131 return; | |
132 | |
133 constexpr SizeT Alignment = 4; | |
134 InstAlloca *RzAlloca = createLocalRz(Context, RzSize, Alignment); | |
135 | |
136 // insert before the current instruction | |
137 InstList::iterator Next = Context.getNext(); | |
138 Context.setInsertPoint(Context.getCur()); | |
139 Context.insert(RzAlloca); | |
140 Context.setNext(Next); | |
141 } | |
142 | |
143 void ASanInstrumentation::instrumentAlloca(LoweringContext &Context, | |
144 InstAlloca *Instr) { | |
145 auto *VarSizeOp = llvm::dyn_cast<ConstantInteger32>(Instr->getSizeInBytes()); | |
146 SizeT VarSize = (VarSizeOp == nullptr) ? RzSize : VarSizeOp->getValue(); | |
147 SizeT Padding = (VarSize % RzSize) ? RzSize - VarSize % RzSize : 0; | |
Jim Stichnoth
2016/06/20 19:44:41
Can you use Utils::OffsetToAlignment() here?
tlively
2016/06/20 22:19:33
Done.
| |
148 constexpr SizeT Alignment = 1; | |
149 InstAlloca *Rz = createLocalRz(Context, RzSize + Padding, Alignment); | |
150 Context.insert(Rz); | |
151 } | |
152 | |
153 InstAlloca *ASanInstrumentation::createLocalRz(LoweringContext &Context, | |
154 SizeT Size, SizeT Alignment) { | |
155 Cfg *Func = Context.getNode()->getCfg(); | |
156 Variable *Rz = Func->makeVariable(IceType_i32); | |
157 Rz->setName(Func, nextRzName()); | |
158 // TODO(tlively): Make this unnecessary | |
Jim Stichnoth
2016/06/20 19:44:41
I'm not sure what the exact problem is here. Is i
tlively
2016/06/20 22:19:33
I'll just get rid of this now. It will be unnecess
| |
159 Rz->setIgnoreLiveness(); | |
160 | |
161 auto *ByteCount = ConstantInteger32::create(Ctx, IceType_i32, Size); | |
162 auto *RzAlloca = InstAlloca::create(Func, Rz, ByteCount, Alignment); | |
163 | |
164 return RzAlloca; | |
165 } | |
166 | |
125 void ASanInstrumentation::instrumentCall(LoweringContext &Context, | 167 void ASanInstrumentation::instrumentCall(LoweringContext &Context, |
126 InstCall *Instr) { | 168 InstCall *Instr) { |
127 auto *CallTarget = | 169 auto *CallTarget = |
128 llvm::dyn_cast<ConstantRelocatable>(Instr->getCallTarget()); | 170 llvm::dyn_cast<ConstantRelocatable>(Instr->getCallTarget()); |
129 if (CallTarget == nullptr) | 171 if (CallTarget == nullptr) |
130 return; | 172 return; |
131 | 173 |
132 std::string TargetName = CallTarget->getName().toStringOrEmpty(); | 174 std::string TargetName = CallTarget->getName().toStringOrEmpty(); |
133 auto Subst = FuncSubstitutions.find(TargetName); | 175 auto Subst = FuncSubstitutions.find(TargetName); |
134 if (Subst == FuncSubstitutions.end()) | 176 if (Subst == FuncSubstitutions.end()) |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
180 Constant *ShadowMemInit = | 222 Constant *ShadowMemInit = |
181 Ctx->getConstantExternSym(Ctx->getGlobalString("__asan_init")); | 223 Ctx->getConstantExternSym(Ctx->getGlobalString("__asan_init")); |
182 constexpr SizeT NumArgs = 0; | 224 constexpr SizeT NumArgs = 0; |
183 constexpr Variable *Void = nullptr; | 225 constexpr Variable *Void = nullptr; |
184 constexpr bool NoTailCall = false; | 226 constexpr bool NoTailCall = false; |
185 auto *Call = InstCall::create(Func, NumArgs, Void, ShadowMemInit, NoTailCall); | 227 auto *Call = InstCall::create(Func, NumArgs, Void, ShadowMemInit, NoTailCall); |
186 Func->getEntryNode()->getInsts().push_front(Call); | 228 Func->getEntryNode()->getInsts().push_front(Call); |
187 } | 229 } |
188 | 230 |
189 } // end of namespace Ice | 231 } // end of namespace Ice |
OLD | NEW |