Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Side by Side Diff: src/IceASanInstrumentation.cpp

Issue 2242243003: Subzero: Replace global pointers to allocation functions (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: formatting Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | tests_lit/asan_tests/globalreplacement.ll » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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()) {
Jim Stichnoth 2016/08/16 17:54:55 I prefer to structure these things like this: i
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 =
Jim Stichnoth 2016/08/16 17:54:55 Optional: I'm usually happy to use "auto" instead
154 ProtoSubstitutions.find(SubstName);
155 FunctionDeclaration *SubstProto;
156 if (SubstProtoEntry != ProtoSubstitutions.end())
157 SubstProto = SubstProtoEntry->second;
158 else {
159 constexpr bool IsProto = true;
160 SubstProto = FunctionDeclaration::create(
161 Ctx, TargetFunc->getSignature(), TargetFunc->getCallingConv(),
162 llvm::GlobalValue::ExternalLinkage, IsProto);
163 SubstProto->setName(Ctx, SubstName);
164 ProtoSubstitutions.insert({SubstName, SubstProto});
165 }
166
167 NewGlobal->addInitializer(VariableDeclaration::RelocInitializer::create(
168 &NewGlobals, SubstProto, RelocOffsetArray(0)));
169 }
123 } 170 }
171
124 RzLeft->setIsConstant(Global->getIsConstant()); 172 RzLeft->setIsConstant(Global->getIsConstant());
173 NewGlobal->setIsConstant(Global->getIsConstant());
125 RzRight->setIsConstant(Global->getIsConstant()); 174 RzRight->setIsConstant(Global->getIsConstant());
126 RzLeft->setAlignment(Alignment); 175 RzLeft->setAlignment(Alignment);
127 Global->setAlignment(Alignment); 176 NewGlobal->setAlignment(Alignment);
128 RzRight->setAlignment(1); 177 RzRight->setAlignment(1);
129 RzArray->addInitializer(VariableDeclaration::RelocInitializer::create( 178 RzArray->addInitializer(VariableDeclaration::RelocInitializer::create(
130 &NewGlobals, RzLeft, RelocOffsetArray(0))); 179 &NewGlobals, RzLeft, RelocOffsetArray(0)));
131 RzArray->addInitializer(VariableDeclaration::RelocInitializer::create( 180 RzArray->addInitializer(VariableDeclaration::RelocInitializer::create(
132 &NewGlobals, RzRight, RelocOffsetArray(0))); 181 &NewGlobals, RzRight, RelocOffsetArray(0)));
133 RzSizes->addInitializer(VariableDeclaration::DataInitializer::create( 182 RzSizes->addInitializer(VariableDeclaration::DataInitializer::create(
134 &NewGlobals, sizeToByteVec(RzLeftSize))); 183 &NewGlobals, sizeToByteVec(RzLeftSize)));
135 RzSizes->addInitializer(VariableDeclaration::DataInitializer::create( 184 RzSizes->addInitializer(VariableDeclaration::DataInitializer::create(
136 &NewGlobals, sizeToByteVec(RzRightSize))); 185 &NewGlobals, sizeToByteVec(RzRightSize)));
137 186
138 NewGlobals.push_back(RzLeft); 187 NewGlobals.push_back(RzLeft);
139 NewGlobals.push_back(Global); 188 NewGlobals.push_back(NewGlobal);
140 NewGlobals.push_back(RzRight); 189 NewGlobals.push_back(RzRight);
141 RzGlobalsNum += 2; 190 RzGlobalsNum += 2;
142 191
143 GlobalSizes.insert({Global->getName(), Global->getNumBytes()}); 192 GlobalSizes.insert({NewGlobal->getName(), NewGlobal->getNumBytes()});
144 } 193 }
145 194
146 // Replace old list of globals, without messing up arena allocators 195 // Replace old list of globals, without messing up arena allocators
147 Globals.clear(); 196 Globals.clear();
148 Globals.merge(&NewGlobals); 197 Globals.merge(&NewGlobals);
149 DidProcessGlobals = true; 198 DidProcessGlobals = true;
150 199
151 // Log the new set of globals 200 // Log the new set of globals
152 if (BuildDefs::dump() && (getFlags().getVerbose() & IceV_GlobalInit)) { 201 if (BuildDefs::dump() && (getFlags().getVerbose() & IceV_GlobalInit)) {
153 OstreamLocker _(Ctx); 202 OstreamLocker _(Ctx);
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 Call->addArg(Ctx->getConstantSym(0, Ctx->getGlobalString(RzSizesName))); 454 Call->addArg(Ctx->getConstantSym(0, Ctx->getGlobalString(RzSizesName)));
406 } 455 }
407 456
408 // TODO(tlively): make this more efficient with swap idiom 457 // TODO(tlively): make this more efficient with swap idiom
409 void ASanInstrumentation::finishFunc(Cfg *) { 458 void ASanInstrumentation::finishFunc(Cfg *) {
410 ICE_TLS_GET_FIELD(LocalVars)->clear(); 459 ICE_TLS_GET_FIELD(LocalVars)->clear();
411 ICE_TLS_GET_FIELD(LocalDtors)->clear(); 460 ICE_TLS_GET_FIELD(LocalDtors)->clear();
412 } 461 }
413 462
414 } // end of namespace Ice 463 } // end of namespace Ice
OLDNEW
« no previous file with comments | « no previous file | tests_lit/asan_tests/globalreplacement.ll » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698