llvm.org GIT mirror llvm / 0d5bd59
To catch bugs like the one fixed in http://llvm.org/viewvc/llvm-project?view=rev&revision=78127, I'm changing the ExecutionEngine's global mappings to hold AssertingVH<const GlobalValue>. That way, if unregistering a mapping fails to actually unregister it, we'll get an assert. Running the jit nightly tests didn't uncover any actual instances of the problem. This also uncovered the fact that AssertingVH<const X> didn't work, so I fixed that too. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78400 91177308-0d34-0410-b5e6-96231b3b80d8 Jeffrey Yasskin 10 years ago
4 changed file(s) with 37 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
3636 class MutexGuard;
3737 class TargetData;
3838 class Type;
39 template class AssertingVH;
3940
4041 class ExecutionEngineState {
4142 private:
4243 /// GlobalAddressMap - A mapping between LLVM global values and their
4344 /// actualized version...
44 std::map<const GlobalValue*, void *> GlobalAddressMap;
45 std::map<AssertingVH, void *> GlobalAddressMap;
4546
4647 /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap,
4748 /// used to convert raw addresses into the LLVM global value that is emitted
4849 /// at the address. This map is not computed unless getGlobalValueAtAddress
4950 /// is called at some point.
50 std::mapconst GlobalValue*> GlobalAddressReverseMap;
51 std::mapAssertingVH > GlobalAddressReverseMap;
5152
5253 public:
53 std::map<const GlobalValue*, void *> &
54 std::map<AssertingVH, void *> &
5455 getGlobalAddressMap(const MutexGuard &) {
5556 return GlobalAddressMap;
5657 }
5758
58 std::mapconst GlobalValue*> &
59 std::mapAssertingVH > &
5960 getGlobalAddressReverseMap(const MutexGuard &) {
6061 return GlobalAddressReverseMap;
6162 }
170170 return static_cast(ValueHandleBase::getValPtr());
171171 }
172172 void setValPtr(ValueTy *P) {
173 ValueHandleBase::operator=(P);
173 ValueHandleBase::operator=(GetAsValue(P));
174174 }
175175 #else
176176 ValueTy *ThePtr;
178178 void setValPtr(ValueTy *P) { ThePtr = P; }
179179 #endif
180180
181 // Convert a ValueTy*, which may be const, to the type the base
182 // class expects.
183 static Value *GetAsValue(Value *V) { return V; }
184 static Value *GetAsValue(const Value *V) { return const_cast(V); }
185
181186 public:
182187 #ifndef NDEBUG
183188 AssertingVH() : ValueHandleBase(Assert) {}
184 AssertingVH(ValueTy *P) : ValueHandleBase(Assert, P) {}
189 AssertingVH(ValueTy *P) : ValueHandleBase(Assert, GetAsValue(P)) {}
185190 AssertingVH(const AssertingVH &RHS) : ValueHandleBase(Assert, RHS) {}
186191 #else
187192 AssertingVH() : ThePtr(0) {}
1212 //===----------------------------------------------------------------------===//
1313
1414 #define DEBUG_TYPE "jit"
15 #include "llvm/ExecutionEngine/ExecutionEngine.h"
16
1517 #include "llvm/Constants.h"
1618 #include "llvm/DerivedTypes.h"
1719 #include "llvm/Module.h"
1820 #include "llvm/ModuleProvider.h"
1921 #include "llvm/ADT/Statistic.h"
2022 #include "llvm/Config/alloca.h"
21 #include "llvm/ExecutionEngine/ExecutionEngine.h"
2223 #include "llvm/ExecutionEngine/GenericValue.h"
2324 #include "llvm/Support/Debug.h"
2425 #include "llvm/Support/ErrorHandling.h"
2526 #include "llvm/Support/MutexGuard.h"
27 #include "llvm/Support/ValueHandle.h"
2628 #include "llvm/Support/raw_ostream.h"
2729 #include "llvm/System/DynamicLibrary.h"
2830 #include "llvm/System/Host.h"
127129
128130 // If we are using the reverse mapping, add it too
129131 if (!state.getGlobalAddressReverseMap(locked).empty()) {
130 const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
132 AssertingVH &V =
133 state.getGlobalAddressReverseMap(locked)[Addr];
131134 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
132135 V = GV;
133136 }
148151 MutexGuard locked(lock);
149152
150153 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) {
151 state.getGlobalAddressMap(locked).erase(FI);
152 state.getGlobalAddressReverseMap(locked).erase(FI);
154 state.getGlobalAddressMap(locked).erase(&*FI);
155 state.getGlobalAddressReverseMap(locked).erase(&*FI);
153156 }
154157 for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
155158 GI != GE; ++GI) {
156 state.getGlobalAddressMap(locked).erase(GI);
157 state.getGlobalAddressReverseMap(locked).erase(GI);
159 state.getGlobalAddressMap(locked).erase(&*GI);
160 state.getGlobalAddressReverseMap(locked).erase(&*GI);
158161 }
159162 }
160163
164167 void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
165168 MutexGuard locked(lock);
166169
167 std::map<const GlobalValue*, void *> &Map = state.getGlobalAddressMap(locked);
170 std::map<AssertingVH, void *> &Map =
171 state.getGlobalAddressMap(locked);
168172
169173 // Deleting from the mapping?
170174 if (Addr == 0) {
171 std::map<const GlobalValue*, void *>::iterator I = Map.find(GV);
175 std::map<AssertingVH, void *>::iterator I = Map.find(GV);
172176 void *OldVal;
173177 if (I == Map.end())
174178 OldVal = 0;
191195
192196 // If we are using the reverse mapping, add it too
193197 if (!state.getGlobalAddressReverseMap(locked).empty()) {
194 const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
198 AssertingVH &V =
199 state.getGlobalAddressReverseMap(locked)[Addr];
195200 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
196201 V = GV;
197202 }
204209 void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
205210 MutexGuard locked(lock);
206211
207 std::map::iterator I =
208 state.getGlobalAddressMap(locked).find(GV);
212 std::map, void*>::iterator I =
213 state.getGlobalAddressMap(locked).find(GV);
209214 return I != state.getGlobalAddressMap(locked).end() ? I->second : 0;
210215 }
211216
217222
218223 // If we haven't computed the reverse mapping yet, do so first.
219224 if (state.getGlobalAddressReverseMap(locked).empty()) {
220 for (std::map<const GlobalValue*, void *>::iterator
225 for (std::map<AssertingVH, void *>::iterator
221226 I = state.getGlobalAddressMap(locked).begin(),
222227 E = state.getGlobalAddressMap(locked).end(); I != E; ++I)
223228 state.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second,
224229 I->first));
225230 }
226231
227 std::mapconst GlobalValue*>::iterator I =
232 std::mapAssertingVH >::iterator I =
228233 state.getGlobalAddressReverseMap(locked).find(Addr);
229234 return I != state.getGlobalAddressReverseMap(locked).end() ? I->second : 0;
230235 }
117117 // doesn't matter which method.
118118 EXPECT_FALSE(AVH->mayWriteToMemory());
119119 EXPECT_FALSE((*AVH).mayWriteToMemory());
120 }
121
122 TEST_F(ValueHandle, AssertingVH_Const) {
123 const CastInst *ConstBitcast = BitcastV.get();
124 AssertingVH AVH(ConstBitcast);
125 const CastInst *implicit_to_exact_type = AVH;
126 implicit_to_exact_type = implicit_to_exact_type; // Avoid warning.
120127 }
121128
122129 TEST_F(ValueHandle, AssertingVH_Comparisons) {