| 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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 // Global holding sizes of all redzones | 92 // Global holding sizes of all redzones |
| 93 auto *RzSizes = VariableDeclaration::create(&NewGlobals); | 93 auto *RzSizes = VariableDeclaration::create(&NewGlobals); |
| 94 | 94 |
| 95 RzArray->setName(Ctx, RzArrayName); | 95 RzArray->setName(Ctx, RzArrayName); |
| 96 RzSizes->setName(Ctx, RzSizesName); | 96 RzSizes->setName(Ctx, RzSizesName); |
| 97 RzArray->setIsConstant(true); | 97 RzArray->setIsConstant(true); |
| 98 RzSizes->setIsConstant(true); | 98 RzSizes->setIsConstant(true); |
| 99 NewGlobals.push_back(RzArray); | 99 NewGlobals.push_back(RzArray); |
| 100 NewGlobals.push_back(RzSizes); | 100 NewGlobals.push_back(RzSizes); |
| 101 | 101 |
| 102 using PrototypeMap = std::unordered_map<std::string, FunctionDeclaration *>; |
| 103 PrototypeMap ProtoSubstitutions; |
| 102 for (VariableDeclaration *Global : Globals) { | 104 for (VariableDeclaration *Global : Globals) { |
| 103 assert(Global->getAlignment() <= RzSize); | 105 assert(Global->getAlignment() <= RzSize); |
| 104 VariableDeclaration *RzLeft = VariableDeclaration::create(&NewGlobals); | 106 VariableDeclaration *RzLeft = VariableDeclaration::create(&NewGlobals); |
| 107 VariableDeclaration *NewGlobal = Global; |
| 105 VariableDeclaration *RzRight = VariableDeclaration::create(&NewGlobals); | 108 VariableDeclaration *RzRight = VariableDeclaration::create(&NewGlobals); |
| 106 RzLeft->setName(Ctx, nextRzName()); | 109 RzLeft->setName(Ctx, nextRzName()); |
| 107 RzRight->setName(Ctx, nextRzName()); | 110 RzRight->setName(Ctx, nextRzName()); |
| 108 SizeT Alignment = std::max(RzSize, Global->getAlignment()); | 111 SizeT Alignment = std::max(RzSize, Global->getAlignment()); |
| 109 SizeT RzLeftSize = Alignment; | 112 SizeT RzLeftSize = Alignment; |
| 110 SizeT RzRightSize = | 113 SizeT RzRightSize = |
| 111 RzSize + Utils::OffsetToAlignment(Global->getNumBytes(), Alignment); | 114 RzSize + Utils::OffsetToAlignment(Global->getNumBytes(), Alignment); |
| 112 if (Global->hasNonzeroInitializer()) { | 115 if (!Global->hasNonzeroInitializer()) { |
| 116 RzLeft->addInitializer(VariableDeclaration::ZeroInitializer::create( |
| 117 &NewGlobals, RzLeftSize)); |
| 118 RzRight->addInitializer(VariableDeclaration::ZeroInitializer::create( |
| 119 &NewGlobals, RzRightSize)); |
| 120 } else { |
| 113 RzLeft->addInitializer(VariableDeclaration::DataInitializer::create( | 121 RzLeft->addInitializer(VariableDeclaration::DataInitializer::create( |
| 114 &NewGlobals, llvm::NaClBitcodeRecord::RecordVector(RzLeftSize, 'R'))); | 122 &NewGlobals, llvm::NaClBitcodeRecord::RecordVector(RzLeftSize, 'R'))); |
| 115 RzRight->addInitializer(VariableDeclaration::DataInitializer::create( | 123 RzRight->addInitializer(VariableDeclaration::DataInitializer::create( |
| 116 &NewGlobals, | 124 &NewGlobals, |
| 117 llvm::NaClBitcodeRecord::RecordVector(RzRightSize, 'R'))); | 125 llvm::NaClBitcodeRecord::RecordVector(RzRightSize, 'R'))); |
| 118 } else { | 126 |
| 119 RzLeft->addInitializer(VariableDeclaration::ZeroInitializer::create( | 127 // replace any pointers to allocator functions |
| 120 &NewGlobals, RzLeftSize)); | 128 NewGlobal = VariableDeclaration::create(&NewGlobals); |
| 121 RzRight->addInitializer(VariableDeclaration::ZeroInitializer::create( | 129 NewGlobal->setName(Global->getName()); |
| 122 &NewGlobals, RzRightSize)); | 130 std::vector<VariableDeclaration::Initializer *> GlobalInits = |
| 131 Global->getInitializers(); |
| 132 for (VariableDeclaration::Initializer *Init : GlobalInits) { |
| 133 auto *RelocInit = |
| 134 llvm::dyn_cast<VariableDeclaration::RelocInitializer>(Init); |
| 135 if (RelocInit == nullptr) { |
| 136 NewGlobal->addInitializer(Init); |
| 137 continue; |
| 138 } |
| 139 const GlobalDeclaration *TargetDecl = RelocInit->getDeclaration(); |
| 140 const auto *TargetFunc = |
| 141 llvm::dyn_cast<FunctionDeclaration>(TargetDecl); |
| 142 if (TargetFunc == nullptr) { |
| 143 NewGlobal->addInitializer(Init); |
| 144 continue; |
| 145 } |
| 146 std::string TargetName = TargetDecl->getName().toStringOrEmpty(); |
| 147 StringMap::const_iterator Subst = FuncSubstitutions.find(TargetName); |
| 148 if (Subst == FuncSubstitutions.end()) { |
| 149 NewGlobal->addInitializer(Init); |
| 150 continue; |
| 151 } |
| 152 std::string SubstName = Subst->second; |
| 153 PrototypeMap::iterator SubstProtoEntry = ProtoSubstitutions.find(SubstNa
me); |
| 154 FunctionDeclaration *SubstProto; |
| 155 if (SubstProtoEntry != ProtoSubstitutions.end()) |
| 156 SubstProto = SubstProtoEntry->second; |
| 157 else { |
| 158 constexpr bool IsProto = true; |
| 159 SubstProto = FunctionDeclaration::create( |
| 160 Ctx, TargetFunc->getSignature(), TargetFunc->getCallingConv(), |
| 161 llvm::GlobalValue::ExternalLinkage, IsProto); |
| 162 SubstProto->setName(Ctx, SubstName); |
| 163 ProtoSubstitutions.insert({SubstName, SubstProto}); |
| 164 } |
| 165 |
| 166 NewGlobal->addInitializer(VariableDeclaration::RelocInitializer::create( |
| 167 &NewGlobals, SubstProto, RelocOffsetArray(0))); |
| 168 } |
| 123 } | 169 } |
| 170 |
| 124 RzLeft->setIsConstant(Global->getIsConstant()); | 171 RzLeft->setIsConstant(Global->getIsConstant()); |
| 172 NewGlobal->setIsConstant(Global->getIsConstant()); |
| 125 RzRight->setIsConstant(Global->getIsConstant()); | 173 RzRight->setIsConstant(Global->getIsConstant()); |
| 126 RzLeft->setAlignment(Alignment); | 174 RzLeft->setAlignment(Alignment); |
| 127 Global->setAlignment(Alignment); | 175 NewGlobal->setAlignment(Alignment); |
| 128 RzRight->setAlignment(1); | 176 RzRight->setAlignment(1); |
| 129 RzArray->addInitializer(VariableDeclaration::RelocInitializer::create( | 177 RzArray->addInitializer(VariableDeclaration::RelocInitializer::create( |
| 130 &NewGlobals, RzLeft, RelocOffsetArray(0))); | 178 &NewGlobals, RzLeft, RelocOffsetArray(0))); |
| 131 RzArray->addInitializer(VariableDeclaration::RelocInitializer::create( | 179 RzArray->addInitializer(VariableDeclaration::RelocInitializer::create( |
| 132 &NewGlobals, RzRight, RelocOffsetArray(0))); | 180 &NewGlobals, RzRight, RelocOffsetArray(0))); |
| 133 RzSizes->addInitializer(VariableDeclaration::DataInitializer::create( | 181 RzSizes->addInitializer(VariableDeclaration::DataInitializer::create( |
| 134 &NewGlobals, sizeToByteVec(RzLeftSize))); | 182 &NewGlobals, sizeToByteVec(RzLeftSize))); |
| 135 RzSizes->addInitializer(VariableDeclaration::DataInitializer::create( | 183 RzSizes->addInitializer(VariableDeclaration::DataInitializer::create( |
| 136 &NewGlobals, sizeToByteVec(RzRightSize))); | 184 &NewGlobals, sizeToByteVec(RzRightSize))); |
| 137 | 185 |
| 138 NewGlobals.push_back(RzLeft); | 186 NewGlobals.push_back(RzLeft); |
| 139 NewGlobals.push_back(Global); | 187 NewGlobals.push_back(NewGlobal); |
| 140 NewGlobals.push_back(RzRight); | 188 NewGlobals.push_back(RzRight); |
| 141 RzGlobalsNum += 2; | 189 RzGlobalsNum += 2; |
| 142 | 190 |
| 143 GlobalSizes.insert({Global->getName(), Global->getNumBytes()}); | 191 GlobalSizes.insert({NewGlobal->getName(), NewGlobal->getNumBytes()}); |
| 144 } | 192 } |
| 145 | 193 |
| 146 // Replace old list of globals, without messing up arena allocators | 194 // Replace old list of globals, without messing up arena allocators |
| 147 Globals.clear(); | 195 Globals.clear(); |
| 148 Globals.merge(&NewGlobals); | 196 Globals.merge(&NewGlobals); |
| 149 DidProcessGlobals = true; | 197 DidProcessGlobals = true; |
| 150 | 198 |
| 151 // Log the new set of globals | 199 // Log the new set of globals |
| 152 if (BuildDefs::dump() && (getFlags().getVerbose() & IceV_GlobalInit)) { | 200 if (BuildDefs::dump() && (getFlags().getVerbose() & IceV_GlobalInit)) { |
| 153 OstreamLocker _(Ctx); | 201 OstreamLocker _(Ctx); |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 Call->addArg(Ctx->getConstantSym(0, Ctx->getGlobalString(RzSizesName))); | 453 Call->addArg(Ctx->getConstantSym(0, Ctx->getGlobalString(RzSizesName))); |
| 406 } | 454 } |
| 407 | 455 |
| 408 // TODO(tlively): make this more efficient with swap idiom | 456 // TODO(tlively): make this more efficient with swap idiom |
| 409 void ASanInstrumentation::finishFunc(Cfg *) { | 457 void ASanInstrumentation::finishFunc(Cfg *) { |
| 410 ICE_TLS_GET_FIELD(LocalVars)->clear(); | 458 ICE_TLS_GET_FIELD(LocalVars)->clear(); |
| 411 ICE_TLS_GET_FIELD(LocalDtors)->clear(); | 459 ICE_TLS_GET_FIELD(LocalDtors)->clear(); |
| 412 } | 460 } |
| 413 | 461 |
| 414 } // end of namespace Ice | 462 } // end of namespace Ice |
| OLD | NEW |