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 |
(...skipping 18 matching lines...) Expand all Loading... |
29 | 29 |
30 namespace Ice { | 30 namespace Ice { |
31 | 31 |
32 namespace { | 32 namespace { |
33 | 33 |
34 constexpr const char *ASanPrefix = "__asan"; | 34 constexpr const char *ASanPrefix = "__asan"; |
35 constexpr SizeT RzSize = 32; | 35 constexpr SizeT RzSize = 32; |
36 constexpr const char *RzPrefix = "__$rz"; | 36 constexpr const char *RzPrefix = "__$rz"; |
37 constexpr const char *RzArrayName = "__$rz_array"; | 37 constexpr const char *RzArrayName = "__$rz_array"; |
38 constexpr const char *RzSizesName = "__$rz_sizes"; | 38 constexpr const char *RzSizesName = "__$rz_sizes"; |
| 39 constexpr char RzStackPoison = -1; |
39 const llvm::NaClBitcodeRecord::RecordVector RzContents = | 40 const llvm::NaClBitcodeRecord::RecordVector RzContents = |
40 llvm::NaClBitcodeRecord::RecordVector(RzSize, 'R'); | 41 llvm::NaClBitcodeRecord::RecordVector(RzSize, 'R'); |
41 | 42 |
42 // In order to instrument the code correctly, the .pexe must not have had its | 43 // In order to instrument the code correctly, the .pexe must not have had its |
43 // symbols stripped. | 44 // symbols stripped. |
44 using StringMap = std::unordered_map<std::string, std::string>; | 45 using StringMap = std::unordered_map<std::string, std::string>; |
45 using StringSet = std::unordered_set<std::string>; | 46 using StringSet = std::unordered_set<std::string>; |
46 // TODO(tlively): Handle all allocation functions | 47 // TODO(tlively): Handle all allocation functions |
47 const StringMap FuncSubstitutions = {{"malloc", "__asan_malloc"}, | 48 const StringMap FuncSubstitutions = {{"malloc", "__asan_malloc"}, |
48 {"free", "__asan_free"}, | 49 {"free", "__asan_free"}, |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 Variable *RzLocVar = Func->makeVariable(IceType_i32); | 196 Variable *RzLocVar = Func->makeVariable(IceType_i32); |
196 RzLocVar->setName(Func, nextRzName()); | 197 RzLocVar->setName(Func, nextRzName()); |
197 auto *Offset = ConstantInteger32::create(Ctx, IceType_i32, VarSize); | 198 auto *Offset = ConstantInteger32::create(Ctx, IceType_i32, VarSize); |
198 auto *RzLoc = InstArithmetic::create(Func, InstArithmetic::Add, RzLocVar, | 199 auto *RzLoc = InstArithmetic::create(Func, InstArithmetic::Add, RzLocVar, |
199 Dest, Offset); | 200 Dest, Offset); |
200 | 201 |
201 // instructions to poison and unpoison the redzone | 202 // instructions to poison and unpoison the redzone |
202 constexpr SizeT NumArgs = 2; | 203 constexpr SizeT NumArgs = 2; |
203 constexpr Variable *Void = nullptr; | 204 constexpr Variable *Void = nullptr; |
204 constexpr bool NoTailcall = false; | 205 constexpr bool NoTailcall = false; |
| 206 auto *RzSizeConst = ConstantInteger32::create(Ctx, IceType_i32, RzPadding); |
| 207 auto *RzPoisonConst = |
| 208 ConstantInteger32::create(Ctx, IceType_i32, RzStackPoison); |
205 auto *Init = InstCall::create(Func, NumArgs, Void, InitFunc, NoTailcall); | 209 auto *Init = InstCall::create(Func, NumArgs, Void, InitFunc, NoTailcall); |
| 210 Init->addArg(RzLocVar); |
| 211 Init->addArg(RzSizeConst); |
| 212 Init->addArg(RzPoisonConst); |
206 auto *Destroy = | 213 auto *Destroy = |
207 InstCall::create(Func, NumArgs, Void, DestroyFunc, NoTailcall); | 214 InstCall::create(Func, NumArgs, Void, DestroyFunc, NoTailcall); |
208 Init->addArg(RzLocVar); | |
209 Destroy->addArg(RzLocVar); | 215 Destroy->addArg(RzLocVar); |
210 auto *RzSizeConst = ConstantInteger32::create(Ctx, IceType_i32, RzPadding); | |
211 Init->addArg(RzSizeConst); | |
212 Destroy->addArg(RzSizeConst); | 216 Destroy->addArg(RzSizeConst); |
213 | |
214 Cur->setDeleted(); | 217 Cur->setDeleted(); |
215 C.insert(NewVar); | 218 C.insert(NewVar); |
216 ICE_TLS_GET_FIELD(LocalDtors)->emplace_back(Destroy); | 219 ICE_TLS_GET_FIELD(LocalDtors)->emplace_back(Destroy); |
217 Initializations.emplace_back(RzLoc); | 220 Initializations.emplace_back(RzLoc); |
218 Initializations.emplace_back(Init); | 221 Initializations.emplace_back(Init); |
219 | 222 |
220 C.advanceCur(); | 223 C.advanceCur(); |
221 C.advanceNext(); | 224 C.advanceNext(); |
222 } | 225 } |
223 | 226 |
224 C.setInsertPoint(C.getCur()); | 227 C.setInsertPoint(C.getCur()); |
225 | 228 |
226 // add the leftmost redzone | 229 // add the leftmost redzone |
227 if (HasLocals) { | 230 if (HasLocals) { |
228 Variable *LastRz = Func->makeVariable(IceType_i32); | 231 Variable *LastRz = Func->makeVariable(IceType_i32); |
229 LastRz->setName(Func, nextRzName()); | 232 LastRz->setName(Func, nextRzName()); |
230 auto *ByteCount = ConstantInteger32::create(Ctx, IceType_i32, RzSize); | 233 auto *ByteCount = ConstantInteger32::create(Ctx, IceType_i32, RzSize); |
231 constexpr SizeT Alignment = 8; | 234 constexpr SizeT Alignment = 8; |
232 auto *RzAlloca = InstAlloca::create(Func, LastRz, ByteCount, Alignment); | 235 auto *RzAlloca = InstAlloca::create(Func, LastRz, ByteCount, Alignment); |
233 | 236 |
234 constexpr SizeT NumArgs = 2; | 237 constexpr SizeT NumArgs = 2; |
235 constexpr Variable *Void = nullptr; | 238 constexpr Variable *Void = nullptr; |
236 constexpr bool NoTailcall = false; | 239 constexpr bool NoTailcall = false; |
| 240 auto *RzPoisonConst = |
| 241 ConstantInteger32::create(Ctx, IceType_i32, RzStackPoison); |
237 auto *Init = InstCall::create(Func, NumArgs, Void, InitFunc, NoTailcall); | 242 auto *Init = InstCall::create(Func, NumArgs, Void, InitFunc, NoTailcall); |
| 243 Init->addArg(LastRz); |
| 244 Init->addArg(RzAlloca->getSizeInBytes()); |
| 245 Init->addArg(RzPoisonConst); |
238 auto *Destroy = | 246 auto *Destroy = |
239 InstCall::create(Func, NumArgs, Void, DestroyFunc, NoTailcall); | 247 InstCall::create(Func, NumArgs, Void, DestroyFunc, NoTailcall); |
240 Init->addArg(LastRz); | |
241 Destroy->addArg(LastRz); | 248 Destroy->addArg(LastRz); |
242 Init->addArg(RzAlloca->getSizeInBytes()); | |
243 Destroy->addArg(RzAlloca->getSizeInBytes()); | 249 Destroy->addArg(RzAlloca->getSizeInBytes()); |
244 | |
245 ICE_TLS_GET_FIELD(LocalDtors)->emplace_back(Destroy); | 250 ICE_TLS_GET_FIELD(LocalDtors)->emplace_back(Destroy); |
246 C.insert(RzAlloca); | 251 C.insert(RzAlloca); |
247 C.insert(Init); | 252 C.insert(Init); |
248 } | 253 } |
249 | 254 |
250 // insert initializers for the redzones | 255 // insert initializers for the redzones |
251 for (Inst *Init : Initializations) { | 256 for (Inst *Init : Initializations) { |
252 C.insert(Init); | 257 C.insert(Init); |
253 } | 258 } |
254 } | 259 } |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 Call->addArg(Ctx->getConstantSym(0, Ctx->getGlobalString(RzSizesName))); | 366 Call->addArg(Ctx->getConstantSym(0, Ctx->getGlobalString(RzSizesName))); |
362 } | 367 } |
363 | 368 |
364 // TODO(tlively): make this more efficient with swap idiom | 369 // TODO(tlively): make this more efficient with swap idiom |
365 void ASanInstrumentation::finishFunc(Cfg *) { | 370 void ASanInstrumentation::finishFunc(Cfg *) { |
366 ICE_TLS_GET_FIELD(LocalVars)->clear(); | 371 ICE_TLS_GET_FIELD(LocalVars)->clear(); |
367 ICE_TLS_GET_FIELD(LocalDtors)->clear(); | 372 ICE_TLS_GET_FIELD(LocalDtors)->clear(); |
368 } | 373 } |
369 | 374 |
370 } // end of namespace Ice | 375 } // end of namespace Ice |
OLD | NEW |