1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
// Copyright 2011 Google Inc. All Rights Reserved.
#ifndef ART_SRC_CLASS_LINKER_H_
#define ART_SRC_CLASS_LINKER_H_
#include <map>
#include <utility>
#include <vector>
#include "dex_file.h"
#include "heap.h"
#include "macros.h"
#include "mutex.h"
#include "object.h"
#include "unordered_map.h"
#include "unordered_set.h"
#include "gtest/gtest.h"
namespace art {
class ClassLoader;
class InternTable;
class ClassLinker {
public:
// Initializes the class linker using DexFile and an optional boot Space.
static ClassLinker* Create(const std::vector<const DexFile*>& boot_class_path,
const std::vector<const DexFile*>& class_path,
InternTable* intern_table, Space* boot_space);
~ClassLinker();
// Finds a class by its descriptor name.
// If class_loader is null, searches boot_class_path_.
Class* FindClass(const StringPiece& descriptor,
const ClassLoader* class_loader);
Class* FindPrimitiveClass(char type);
Class* FindSystemClass(const StringPiece& descriptor) {
return FindClass(descriptor, NULL);
}
size_t NumLoadedClasses() const;
// Resolve a String with the given index from the DexFile, storing the
// result in the DexCache.
String* ResolveString(const DexFile& dex_file, uint32_t string_idx, DexCache* dex_cache);
// Resolve a Type with the given index from the DexFile, storing the
// result in the DexCache. The referrer is used to identity the
// target DexCache and ClassLoader to use for resolution.
Class* ResolveType(const DexFile& dex_file,
uint32_t type_idx,
const Class* referrer) {
return ResolveType(dex_file,
type_idx,
referrer->GetDexCache(),
referrer->GetClassLoader());
}
// Resolve a Type with the given index from the DexFile, storing the
// result in the DexCache. The referrer is used to identify the
// target DexCache and ClassLoader to use for resolution.
Class* ResolveType(uint32_t type_idx, const Method* referrer) {
Class* declaring_class = referrer->GetDeclaringClass();
DexCache* dex_cache = declaring_class->GetDexCache();
// TODO: we could check for a dex cache hit here
const ClassLoader* class_loader = declaring_class->GetClassLoader();
const DexFile& dex_file = FindDexFile(dex_cache);
return ResolveType(dex_file, type_idx, dex_cache, class_loader);
}
Class* ResolveType(uint32_t type_idx, const Field* referrer) {
Class* declaring_class = referrer->GetDeclaringClass();
DexCache* dex_cache = declaring_class->GetDexCache();
// TODO: we could check for a dex cache hit here
const ClassLoader* class_loader = declaring_class->GetClassLoader();
const DexFile& dex_file = FindDexFile(dex_cache);
return ResolveType(dex_file, type_idx, dex_cache, class_loader);
}
// Resolve a type with the given ID from the DexFile, storing the
// result in DexCache. The ClassLoader is used to search for the
// type, since it may be referenced from but not contained within
// the given DexFile.
Class* ResolveType(const DexFile& dex_file,
uint32_t type_idx,
DexCache* dex_cache,
const ClassLoader* class_loader);
static StaticStorageBase* InitializeStaticStorageFromCode(uint32_t type_idx,
const Method* referrer);
// Resolve a method with a given ID from the DexFile, storing the
// result in DexCache. The ClassLinker and ClassLoader are used as
// in ResolveType. What is unique is the method type argument which
// is used to determine if this method is a direct, static, or
// virtual method.
Method* ResolveMethod(const DexFile& dex_file,
uint32_t method_idx,
DexCache* dex_cache,
const ClassLoader* class_loader,
bool is_direct);
Field* ResolveField(uint32_t field_idx, const Method* referrer) {
Class* declaring_class = referrer->GetDeclaringClass();
DexCache* dex_cache = declaring_class->GetDexCache();
// TODO: we could check for a dex cache hit here
const ClassLoader* class_loader = declaring_class->GetClassLoader();
const DexFile& dex_file = FindDexFile(dex_cache);
return ResolveField(dex_file, field_idx, dex_cache, class_loader, true);
}
// Resolve a method with a given ID from the DexFile, storing the
// result in DexCache. The ClassLinker and ClassLoader are used as
// in ResolveType. What is unique is the is_static argument which is
// used to determine if we are resolving a static or non-static
// field.
Field* ResolveField(const DexFile& dex_file,
uint32_t field_idx,
DexCache* dex_cache,
const ClassLoader* class_loader,
bool is_static);
// Returns true on success, false if there's an exception pending.
bool EnsureInitialized(Class* c);
void RegisterDexFile(const DexFile& dex_file);
void RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache);
const std::vector<const DexFile*>& GetBootClassPath() {
return boot_class_path_;
}
void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
const DexFile& FindDexFile(const DexCache* dex_cache) const;
DexCache* FindDexCache(const DexFile& dex_file) const;
template <class T>
ObjectArray<T>* AllocObjectArray(size_t length) {
return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
}
ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length);
private:
ClassLinker(InternTable*);
// Initialize class linker from DexFile instances.
void Init(const std::vector<const DexFile*>& boot_class_path_,
const std::vector<const DexFile*>& class_path_);
// Initialize class linker from pre-initialized space.
void Init(const std::vector<const DexFile*>& boot_class_path_,
const std::vector<const DexFile*>& class_path_,
Space* space);
static void InitCallback(Object* obj, void *arg);
struct InitCallbackState;
void FinishInit();
bool InitializeClass(Class* klass);
// For early bootstrapping by Init
Class* AllocClass(Class* java_lang_Class, size_t class_size);
// Alloc* convenience functions to avoid needing to pass in Class*
// values that are known to the ClassLinker such as
// kObjectArrayClass and kJavaLangString etc.
Class* AllocClass(size_t class_size);
DexCache* AllocDexCache(const DexFile& dex_file);
Field* AllocField();
Method* AllocMethod();
CodeAndDirectMethods* AllocCodeAndDirectMethods(size_t length);
InterfaceEntry* AllocInterfaceEntry(Class* interface);
Class* CreatePrimitiveClass(const char* descriptor,
Class::PrimitiveType type);
Class* CreateArrayClass(const StringPiece& descriptor,
const ClassLoader* class_loader);
void AppendToBootClassPath(const DexFile& dex_file);
void AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache);
void ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
Class* c, std::map<int, Field*>& field_map);
size_t SizeOfClass(const DexFile& dex_file,
const DexFile::ClassDef& dex_class_def);
void LoadClass(const DexFile& dex_file,
const DexFile::ClassDef& dex_class_def,
Class* klass,
const ClassLoader* class_loader);
void LoadInterfaces(const DexFile& dex_file,
const DexFile::ClassDef& dex_class_def,
Class *klass);
void LoadField(const DexFile& dex_file,
const DexFile::Field& dex_field,
Class* klass,
Field* dst);
void LoadMethod(const DexFile& dex_file,
const DexFile::Method& dex_method,
Class* klass,
Method* dst);
Class* LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader);
// Inserts a class into the class table. Returns true if the class
// was inserted.
bool InsertClass(const StringPiece& descriptor, Class* klass);
bool InitializeSuperClass(Class* klass);
void InitializeStaticFields(Class* klass);
bool ValidateSuperClassDescriptors(const Class* klass);
bool HasSameDescriptorClasses(const char* descriptor,
const Class* klass1,
const Class* klass2);
bool HasSameMethodDescriptorClasses(const Method* descriptor,
const Class* klass1,
const Class* klass2);
bool LinkClass(Class* klass);
bool LinkSuperClass(Class* klass);
bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file);
bool LinkMethods(Class* klass);
bool LinkVirtualMethods(Class* klass);
bool LinkInterfaceMethods(Class* klass);
void LinkAbstractMethods(Class* klass);
bool LinkStaticFields(Class* klass);
bool LinkInstanceFields(Class* klass);
bool LinkFields(Class *klass, bool instance);
void CreateReferenceInstanceOffsets(Class* klass);
void CreateReferenceStaticOffsets(Class* klass);
void CreateReferenceOffsets(Class *klass, bool instance,
uint32_t reference_offsets);
std::vector<const DexFile*> boot_class_path_;
std::vector<const DexFile*> dex_files_;
std::vector<DexCache*> dex_caches_;
// multimap from a StringPiece hash code of a class descriptor to
// Class* instances. Results should be compared for a matching
// Class::descriptor_ and Class::class_loader_.
mutable Mutex classes_lock_;
typedef std::tr1::unordered_multimap<size_t, Class*> Table;
Table classes_;
// indexes into class_roots_.
// needs to be kept in sync with class_roots_descriptors_.
enum ClassRoot {
kJavaLangClass,
kJavaLangObject,
kObjectArrayClass,
kJavaLangString,
kJavaLangReflectField,
kJavaLangReflectMethod,
kJavaLangClassLoader,
kDalvikSystemBaseDexClassLoader,
kDalvikSystemPathClassLoader,
kJavaLangStackTraceElement,
kPrimitiveBoolean,
kPrimitiveByte,
kPrimitiveChar,
kPrimitiveDouble,
kPrimitiveFloat,
kPrimitiveInt,
kPrimitiveLong,
kPrimitiveShort,
kPrimitiveVoid,
kBooleanArrayClass,
kByteArrayClass,
kCharArrayClass,
kDoubleArrayClass,
kFloatArrayClass,
kIntArrayClass,
kLongArrayClass,
kShortArrayClass,
kJavaLangStackTraceElementArrayClass,
kClassRootsMax,
};
ObjectArray<Class>* class_roots_;
Class* GetClassRoot(ClassRoot class_root) {
DCHECK(class_roots_ != NULL);
Class* klass = class_roots_->Get(class_root);
DCHECK(klass != NULL);
return klass;
}
void SetClassRoot(ClassRoot class_root, Class* klass) {
DCHECK(!init_done_);
DCHECK(klass != NULL);
DCHECK(klass->GetClassLoader() == NULL);
DCHECK(klass->GetDescriptor() != NULL);
DCHECK(klass->GetDescriptor()->Equals(GetClassRootDescriptor(class_root)));
DCHECK(class_roots_ != NULL);
DCHECK(class_roots_->Get(class_root) == NULL);
class_roots_->Set(class_root, klass);
}
static const char* class_roots_descriptors_[kClassRootsMax];
const char* GetClassRootDescriptor(ClassRoot class_root) {
const char* descriptor = class_roots_descriptors_[class_root];
CHECK(descriptor != NULL);
return descriptor;
}
ObjectArray<Class>* array_interfaces_;
ObjectArray<InterfaceEntry>* array_iftable_;
bool init_done_;
InternTable* intern_table_;
friend class CommonTest;
FRIEND_TEST(DexCacheTest, Open);
friend class ObjectTest;
FRIEND_TEST(ObjectTest, AllocObjectArray);
FRIEND_TEST(ExceptionTest, FindExceptionHandler);
DISALLOW_COPY_AND_ASSIGN(ClassLinker);
};
} // namespace art
#endif // ART_SRC_CLASS_LINKER_H_
|