| 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 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 #include <sstream> | 25 #include <sstream> |
| 26 #include <unordered_map> | 26 #include <unordered_map> |
| 27 #include <vector> | 27 #include <vector> |
| 28 | 28 |
| 29 namespace Ice { | 29 namespace Ice { |
| 30 | 30 |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 constexpr SizeT RzSize = 32; | 33 constexpr SizeT RzSize = 32; |
| 34 const std::string RzPrefix = "__$rz"; | 34 constexpr const char *RzPrefix = "__$rz"; |
| 35 constexpr const char *RzArrayName = "__$rz_array"; |
| 36 constexpr const char *RzSizesName = "__$rz_sizes"; |
| 35 const llvm::NaClBitcodeRecord::RecordVector RzContents = | 37 const llvm::NaClBitcodeRecord::RecordVector RzContents = |
| 36 llvm::NaClBitcodeRecord::RecordVector(RzSize, 'R'); | 38 llvm::NaClBitcodeRecord::RecordVector(RzSize, 'R'); |
| 37 | 39 |
| 38 // TODO(tlively): Handle all allocation functions | 40 // TODO(tlively): Handle all allocation functions |
| 39 // In order to instrument the code correctly, the .pexe must not have had its | 41 // In order to instrument the code correctly, the .pexe must not have had its |
| 40 // symbols stripped. | 42 // symbols stripped. |
| 41 using string_map = std::unordered_map<std::string, std::string>; | 43 using string_map = std::unordered_map<std::string, std::string>; |
| 42 const string_map FuncSubstitutions = {{"malloc", "__asan_malloc"}, | 44 const string_map FuncSubstitutions = {{"malloc", "__asan_malloc"}, |
| 43 {"free", "__asan_free"}}; | 45 {"free", "__asan_free"}}; |
| 44 | 46 |
| 47 llvm::NaClBitcodeRecord::RecordVector sizeToByteVec(SizeT Size) { |
| 48 llvm::NaClBitcodeRecord::RecordVector SizeContents; |
| 49 for (unsigned i = 0; i < sizeof(Size); ++i) { |
| 50 SizeContents.emplace_back(Size % (1 << CHAR_BIT)); |
| 51 Size >>= CHAR_BIT; |
| 52 } |
| 53 return SizeContents; |
| 54 } |
| 55 |
| 45 } // end of anonymous namespace | 56 } // end of anonymous namespace |
| 46 | 57 |
| 47 ICE_TLS_DEFINE_FIELD(std::vector<InstCall *> *, ASanInstrumentation, | 58 ICE_TLS_DEFINE_FIELD(std::vector<InstCall *> *, ASanInstrumentation, |
| 48 LocalDtors); | 59 LocalDtors); |
| 49 | 60 |
| 50 // Create redzones around all global variables, ensuring that the initializer | 61 // Create redzones around all global variables, ensuring that the initializer |
| 51 // types of the redzones and their associated globals match so that they are | 62 // types of the redzones and their associated globals match so that they are |
| 52 // laid out together in memory. | 63 // laid out together in memory. |
| 53 void ASanInstrumentation::instrumentGlobals(VariableDeclarationList &Globals) { | 64 void ASanInstrumentation::instrumentGlobals(VariableDeclarationList &Globals) { |
| 54 if (DidInsertRedZones) | 65 if (DidProcessGlobals) |
| 55 return; | 66 return; |
| 56 | 67 |
| 57 VariableDeclarationList NewGlobals; | 68 VariableDeclarationList NewGlobals; |
| 58 // Global holding pointers to all redzones | 69 // Global holding pointers to all redzones |
| 59 auto *RzArray = VariableDeclaration::create(&NewGlobals); | 70 auto *RzArray = VariableDeclaration::create(&NewGlobals); |
| 60 // Global holding the size of RzArray | 71 // Global holding sizes of all redzones |
| 61 auto *RzArraySizeVar = VariableDeclaration::create(&NewGlobals); | 72 auto *RzSizes = VariableDeclaration::create(&NewGlobals); |
| 62 SizeT RzArraySize = 0; | |
| 63 | 73 |
| 64 RzArray->setName(Ctx, nextRzName()); | 74 RzArray->setName(Ctx, RzArrayName); |
| 65 RzArraySizeVar->setName(Ctx, nextRzName()); | 75 RzSizes->setName(Ctx, RzSizesName); |
| 66 RzArray->setIsConstant(true); | 76 RzArray->setIsConstant(true); |
| 67 RzArraySizeVar->setIsConstant(true); | 77 RzSizes->setIsConstant(true); |
| 68 NewGlobals.push_back(RzArray); | 78 NewGlobals.push_back(RzArray); |
| 69 NewGlobals.push_back(RzArraySizeVar); | 79 NewGlobals.push_back(RzSizes); |
| 70 | 80 |
| 71 for (VariableDeclaration *Global : Globals) { | 81 for (VariableDeclaration *Global : Globals) { |
| 72 VariableDeclaration *RzLeft = | 82 assert(Global->getAlignment() <= RzSize); |
| 73 createRz(&NewGlobals, RzArray, RzArraySize, Global); | 83 VariableDeclaration *RzLeft = VariableDeclaration::create(&NewGlobals); |
| 74 VariableDeclaration *RzRight = | 84 VariableDeclaration *RzRight = VariableDeclaration::create(&NewGlobals); |
| 75 createRz(&NewGlobals, RzArray, RzArraySize, Global); | 85 RzLeft->setName(Ctx, nextRzName()); |
| 86 RzRight->setName(Ctx, nextRzName()); |
| 87 SizeT Alignment = std::max(RzSize, Global->getAlignment()); |
| 88 SizeT RzLeftSize = Alignment; |
| 89 SizeT RzRightSize = |
| 90 RzSize + Utils::OffsetToAlignment(Global->getNumBytes(), Alignment); |
| 91 if (Global->hasNonzeroInitializer()) { |
| 92 RzLeft->addInitializer(VariableDeclaration::DataInitializer::create( |
| 93 &NewGlobals, llvm::NaClBitcodeRecord::RecordVector(RzLeftSize, 'R'))); |
| 94 RzRight->addInitializer(VariableDeclaration::DataInitializer::create( |
| 95 &NewGlobals, |
| 96 llvm::NaClBitcodeRecord::RecordVector(RzRightSize, 'R'))); |
| 97 } else { |
| 98 RzLeft->addInitializer(VariableDeclaration::ZeroInitializer::create( |
| 99 &NewGlobals, RzLeftSize)); |
| 100 RzRight->addInitializer(VariableDeclaration::ZeroInitializer::create( |
| 101 &NewGlobals, RzRightSize)); |
| 102 } |
| 103 RzLeft->setIsConstant(Global->getIsConstant()); |
| 104 RzRight->setIsConstant(Global->getIsConstant()); |
| 105 RzLeft->setAlignment(Alignment); |
| 106 Global->setAlignment(Alignment); |
| 107 RzRight->setAlignment(1); |
| 108 RzArray->addInitializer(VariableDeclaration::RelocInitializer::create( |
| 109 &NewGlobals, RzLeft, RelocOffsetArray(0))); |
| 110 RzArray->addInitializer(VariableDeclaration::RelocInitializer::create( |
| 111 &NewGlobals, RzRight, RelocOffsetArray(0))); |
| 112 RzSizes->addInitializer(VariableDeclaration::DataInitializer::create( |
| 113 &NewGlobals, sizeToByteVec(RzLeftSize))); |
| 114 RzSizes->addInitializer(VariableDeclaration::DataInitializer::create( |
| 115 &NewGlobals, sizeToByteVec(RzRightSize))); |
| 116 |
| 76 NewGlobals.push_back(RzLeft); | 117 NewGlobals.push_back(RzLeft); |
| 77 NewGlobals.push_back(Global); | 118 NewGlobals.push_back(Global); |
| 78 NewGlobals.push_back(RzRight); | 119 NewGlobals.push_back(RzRight); |
| 120 RzGlobalsNum += 2; |
| 79 } | 121 } |
| 80 | 122 |
| 81 // update the contents of the RzArraySize global | |
| 82 llvm::NaClBitcodeRecord::RecordVector SizeContents; | |
| 83 for (unsigned i = 0; i < sizeof(RzArraySize); i++) { | |
| 84 SizeContents.emplace_back(RzArraySize % (1 << CHAR_BIT)); | |
| 85 RzArraySize >>= CHAR_BIT; | |
| 86 } | |
| 87 RzArraySizeVar->addInitializer( | |
| 88 VariableDeclaration::DataInitializer::create(&NewGlobals, SizeContents)); | |
| 89 | |
| 90 // Replace old list of globals, without messing up arena allocators | 123 // Replace old list of globals, without messing up arena allocators |
| 91 Globals.clear(); | 124 Globals.clear(); |
| 92 Globals.merge(&NewGlobals); | 125 Globals.merge(&NewGlobals); |
| 93 DidInsertRedZones = true; | 126 DidProcessGlobals = true; |
| 127 GlobalsDoneCV.notify_all(); |
| 94 | 128 |
| 95 // Log the new set of globals | 129 // Log the new set of globals |
| 96 if (BuildDefs::dump() && (getFlags().getVerbose() & IceV_GlobalInit)) { | 130 if (BuildDefs::dump() && (getFlags().getVerbose() & IceV_GlobalInit)) { |
| 97 OstreamLocker _(Ctx); | 131 OstreamLocker _(Ctx); |
| 98 Ctx->getStrDump() << "========= Instrumented Globals =========\n"; | 132 Ctx->getStrDump() << "========= Instrumented Globals =========\n"; |
| 99 for (VariableDeclaration *Global : Globals) { | 133 for (VariableDeclaration *Global : Globals) { |
| 100 Global->dump(Ctx->getStrDump()); | 134 Global->dump(Ctx->getStrDump()); |
| 101 } | 135 } |
| 102 } | 136 } |
| 103 } | 137 } |
| 104 | 138 |
| 105 std::string ASanInstrumentation::nextRzName() { | 139 std::string ASanInstrumentation::nextRzName() { |
| 106 std::stringstream Name; | 140 std::stringstream Name; |
| 107 Name << RzPrefix << RzNum++; | 141 Name << RzPrefix << RzNum++; |
| 108 return Name.str(); | 142 return Name.str(); |
| 109 } | 143 } |
| 110 | 144 |
| 111 VariableDeclaration * | |
| 112 ASanInstrumentation::createRz(VariableDeclarationList *List, | |
| 113 VariableDeclaration *RzArray, SizeT &RzArraySize, | |
| 114 VariableDeclaration *Global) { | |
| 115 auto *Rz = VariableDeclaration::create(List); | |
| 116 Rz->setName(Ctx, nextRzName()); | |
| 117 if (Global->hasNonzeroInitializer()) { | |
| 118 Rz->addInitializer( | |
| 119 VariableDeclaration::DataInitializer::create(List, RzContents)); | |
| 120 } else { | |
| 121 Rz->addInitializer( | |
| 122 VariableDeclaration::ZeroInitializer::create(List, RzSize)); | |
| 123 } | |
| 124 Rz->setIsConstant(Global->getIsConstant()); | |
| 125 RzArray->addInitializer(VariableDeclaration::RelocInitializer::create( | |
| 126 List, Rz, RelocOffsetArray(0))); | |
| 127 ++RzArraySize; | |
| 128 return Rz; | |
| 129 } | |
| 130 | |
| 131 // Check for an alloca signaling the presence of local variables and add a | 145 // Check for an alloca signaling the presence of local variables and add a |
| 132 // redzone if it is found | 146 // redzone if it is found |
| 133 void ASanInstrumentation::instrumentFuncStart(LoweringContext &Context) { | 147 void ASanInstrumentation::instrumentFuncStart(LoweringContext &Context) { |
| 134 if (ICE_TLS_GET_FIELD(LocalDtors) == nullptr) | 148 if (ICE_TLS_GET_FIELD(LocalDtors) == nullptr) |
| 135 ICE_TLS_SET_FIELD(LocalDtors, new std::vector<InstCall *>()); | 149 ICE_TLS_SET_FIELD(LocalDtors, new std::vector<InstCall *>()); |
| 136 | 150 |
| 137 Cfg *Func = Context.getNode()->getCfg(); | 151 Cfg *Func = Context.getNode()->getCfg(); |
| 138 bool HasLocals = false; | 152 bool HasLocals = false; |
| 139 LoweringContext C; | 153 LoweringContext C; |
| 140 C.init(Context.getNode()); | 154 C.init(Context.getNode()); |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 Context.setInsertPoint(Context.getCur()); | 295 Context.setInsertPoint(Context.getCur()); |
| 282 for (InstCall *RzUnpoison : *ICE_TLS_GET_FIELD(LocalDtors)) { | 296 for (InstCall *RzUnpoison : *ICE_TLS_GET_FIELD(LocalDtors)) { |
| 283 Context.insert(RzUnpoison); | 297 Context.insert(RzUnpoison); |
| 284 } | 298 } |
| 285 Context.setNext(Next); | 299 Context.setNext(Next); |
| 286 } | 300 } |
| 287 | 301 |
| 288 void ASanInstrumentation::instrumentStart(Cfg *Func) { | 302 void ASanInstrumentation::instrumentStart(Cfg *Func) { |
| 289 Constant *ShadowMemInit = | 303 Constant *ShadowMemInit = |
| 290 Ctx->getConstantExternSym(Ctx->getGlobalString("__asan_init")); | 304 Ctx->getConstantExternSym(Ctx->getGlobalString("__asan_init")); |
| 291 constexpr SizeT NumArgs = 0; | 305 constexpr SizeT NumArgs = 3; |
| 292 constexpr Variable *Void = nullptr; | 306 constexpr Variable *Void = nullptr; |
| 293 constexpr bool NoTailCall = false; | 307 constexpr bool NoTailCall = false; |
| 294 auto *Call = InstCall::create(Func, NumArgs, Void, ShadowMemInit, NoTailCall); | 308 auto *Call = InstCall::create(Func, NumArgs, Void, ShadowMemInit, NoTailCall); |
| 295 Func->getEntryNode()->getInsts().push_front(Call); | 309 Func->getEntryNode()->getInsts().push_front(Call); |
| 310 |
| 311 // wait to get the final count of global redzones |
| 312 if (!DidProcessGlobals) { |
| 313 GlobalsLock.lock(); |
| 314 while (!DidProcessGlobals) |
| 315 GlobalsDoneCV.wait(GlobalsLock); |
| 316 GlobalsLock.release(); |
| 317 } |
| 318 Call->addArg(ConstantInteger32::create(Ctx, IceType_i32, RzGlobalsNum)); |
| 319 Call->addArg(Ctx->getConstantSym(0, Ctx->getGlobalString(RzArrayName))); |
| 320 Call->addArg(Ctx->getConstantSym(0, Ctx->getGlobalString(RzSizesName))); |
| 296 } | 321 } |
| 297 | 322 |
| 298 // TODO(tlively): make this more efficient with swap idiom | 323 // TODO(tlively): make this more efficient with swap idiom |
| 299 void ASanInstrumentation::finishFunc(Cfg *Func) { | 324 void ASanInstrumentation::finishFunc(Cfg *Func) { |
| 300 (void)Func; | 325 (void)Func; |
| 301 ICE_TLS_GET_FIELD(LocalDtors)->clear(); | 326 ICE_TLS_GET_FIELD(LocalDtors)->clear(); |
| 302 } | 327 } |
| 303 | 328 |
| 304 } // end of namespace Ice | 329 } // end of namespace Ice |
| OLD | NEW |