blob: f63ab3c90bb54a5090a66765bc854b3d7b8deb9d [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_CLASS_LINKER_H_
4#define ART_SRC_CLASS_LINKER_H_
5
6#include <map>
7#include <utility>
8#include <vector>
9
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070010#include "dex_file.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070011#include "heap.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070012#include "macros.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070013#include "object.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070014#include "thread.h"
15#include "unordered_map.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070016#include "unordered_set.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070017
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070018#include "gtest/gtest.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070019
20namespace art {
21
Elliott Hughescf4c6c42011-09-01 15:16:42 -070022class ClassLoader;
23class InternTable;
24
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070025class ClassLinker {
26 public:
Brian Carlstroma663ea52011-08-19 23:33:41 -070027 // Initializes the class linker using DexFile and an optional boot Space.
Elliott Hughescf4c6c42011-09-01 15:16:42 -070028 static ClassLinker* Create(const std::vector<const DexFile*>& boot_class_path,
29 InternTable* intern_table, Space* boot_space);
Carl Shapiro61e019d2011-07-14 16:53:09 -070030
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070031 ~ClassLinker();
Carl Shapiro565f5072011-07-10 13:39:43 -070032
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070033 // Finds a class by its descriptor name.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070034 // If class_loader is null, searches boot_class_path_.
Brian Carlstrom6cc18452011-07-18 15:10:33 -070035 Class* FindClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070036 const ClassLoader* class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070037
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070038 Class* FindPrimitiveClass(char type);
39
Brian Carlstrom6cc18452011-07-18 15:10:33 -070040 Class* FindSystemClass(const StringPiece& descriptor) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070041 return FindClass(descriptor, NULL);
Carl Shapiro565f5072011-07-10 13:39:43 -070042 }
43
Elliott Hughese27955c2011-08-26 15:21:24 -070044 size_t NumLoadedClasses() const;
45
Brian Carlstromb63ec392011-08-27 17:38:27 -070046 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070047 // result in the DexCache.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070048 String* ResolveString(const DexFile& dex_file, uint32_t string_idx, DexCache* dex_cache);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070049
Brian Carlstromb63ec392011-08-27 17:38:27 -070050 // Resolve a Type with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070051 // result in the DexCache. The referrer is used to identity the
52 // target DexCache and ClassLoader to use for resolution.
53 Class* ResolveType(const DexFile& dex_file,
54 uint32_t type_idx,
55 const Class* referrer) {
56 return ResolveType(dex_file,
57 type_idx,
58 referrer->GetDexCache(),
59 referrer->GetClassLoader());
60 }
61
Brian Carlstromb63ec392011-08-27 17:38:27 -070062 // Resolve a Type with the given index from the DexFile, storing the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070063 // result in the DexCache. The referrer is used to identify the
Brian Carlstromb63ec392011-08-27 17:38:27 -070064 // target DexCache and ClassLoader to use for resolution.
Brian Carlstromb9edb842011-08-28 16:31:06 -070065 Class* ResolveType(uint32_t type_idx, const Method* referrer) {
Brian Carlstromb63ec392011-08-27 17:38:27 -070066 Class* declaring_class = referrer->GetDeclaringClass();
67 DexCache* dex_cache = declaring_class->GetDexCache();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070068 // TODO: we could check for a dex cache hit here
69 const ClassLoader* class_loader = declaring_class->GetClassLoader();
70 const DexFile& dex_file = FindDexFile(dex_cache);
71 return ResolveType(dex_file, type_idx, dex_cache, class_loader);
72 }
73
74 Class* ResolveType(uint32_t type_idx, const Field* referrer) {
75 Class* declaring_class = referrer->GetDeclaringClass();
76 DexCache* dex_cache = declaring_class->GetDexCache();
77 // TODO: we could check for a dex cache hit here
Brian Carlstromb63ec392011-08-27 17:38:27 -070078 const ClassLoader* class_loader = declaring_class->GetClassLoader();
79 const DexFile& dex_file = FindDexFile(dex_cache);
80 return ResolveType(dex_file, type_idx, dex_cache, class_loader);
81 }
82
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070083 // Resolve a type with the given ID from the DexFile, storing the
84 // result in DexCache. The ClassLoader is used to search for the
85 // type, since it may be referenced from but not contained within
86 // the given DexFile.
87 Class* ResolveType(const DexFile& dex_file,
88 uint32_t type_idx,
89 DexCache* dex_cache,
90 const ClassLoader* class_loader);
91
Brian Carlstromb9edb842011-08-28 16:31:06 -070092 static StaticStorageBase* InitializeStaticStorageFromCode(uint32_t type_idx,
93 const Method* referrer);
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070094
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070095 // Resolve a method with a given ID from the DexFile, storing the
96 // result in DexCache. The ClassLinker and ClassLoader are used as
97 // in ResolveType. What is unique is the method type argument which
98 // is used to determine if this method is a direct, static, or
99 // virtual method.
100 Method* ResolveMethod(const DexFile& dex_file,
101 uint32_t method_idx,
102 DexCache* dex_cache,
103 const ClassLoader* class_loader,
Brian Carlstrom20cfffa2011-08-26 02:31:27 -0700104 bool is_direct);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700105
Brian Carlstromb9edb842011-08-28 16:31:06 -0700106 Field* ResolveField(uint32_t field_idx, const Method* referrer) {
107 Class* declaring_class = referrer->GetDeclaringClass();
108 DexCache* dex_cache = declaring_class->GetDexCache();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700109 // TODO: we could check for a dex cache hit here
Brian Carlstromb9edb842011-08-28 16:31:06 -0700110 const ClassLoader* class_loader = declaring_class->GetClassLoader();
111 const DexFile& dex_file = FindDexFile(dex_cache);
112 return ResolveField(dex_file, field_idx, dex_cache, class_loader, true);
113 }
114
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700115 // Resolve a method with a given ID from the DexFile, storing the
116 // result in DexCache. The ClassLinker and ClassLoader are used as
117 // in ResolveType. What is unique is the is_static argument which is
118 // used to determine if we are resolving a static or non-static
119 // field.
120 Field* ResolveField(const DexFile& dex_file,
121 uint32_t field_idx,
122 DexCache* dex_cache,
123 const ClassLoader* class_loader,
124 bool is_static);
125
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700126 // Returns true on success, false if there's an exception pending.
127 bool EnsureInitialized(Class* c);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700128
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700129 void RegisterDexFile(const DexFile& dex_file);
130 void RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700131
Brian Carlstrom8a487412011-08-29 20:08:52 -0700132 const std::vector<const DexFile*>& GetBootClassPath() {
133 return boot_class_path_;
134 }
135
Elliott Hughes410c0c82011-09-01 17:58:25 -0700136 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700137
buzbeec143c552011-08-20 17:38:58 -0700138 const DexFile& FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700139 DexCache* FindDexCache(const DexFile& dex_file) const;
buzbeec143c552011-08-20 17:38:58 -0700140
Shih-wei Liao44175362011-08-28 16:59:17 -0700141 template <class T>
142 ObjectArray<T>* AllocObjectArray(size_t length) {
143 return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
144 }
145
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700146 ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length);
147
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700148 private:
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700149 ClassLinker(InternTable*);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700150
Brian Carlstroma663ea52011-08-19 23:33:41 -0700151 // Initialize class linker from DexFile instances.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700152 void Init(const std::vector<const DexFile*>& boot_class_path_);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700153
Brian Carlstroma663ea52011-08-19 23:33:41 -0700154 // Initialize class linker from pre-initialized space.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700155 void Init(const std::vector<const DexFile*>& boot_class_path_, Space* space);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700156 static void InitCallback(Object* obj, void *arg);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700157 struct InitCallbackState;
158
159 void FinishInit();
160
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700161 bool InitializeClass(Class* klass);
162
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700163 // For early bootstrapping by Init
Brian Carlstrom4873d462011-08-21 15:23:39 -0700164 Class* AllocClass(Class* java_lang_Class, size_t class_size);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700165
166 // Alloc* convenience functions to avoid needing to pass in Class*
167 // values that are known to the ClassLinker such as
168 // kObjectArrayClass and kJavaLangString etc.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700169 Class* AllocClass(size_t class_size);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700170 DexCache* AllocDexCache(const DexFile& dex_file);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400171 Field* AllocField();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700172 Method* AllocMethod();
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700173 CodeAndDirectMethods* AllocCodeAndDirectMethods(size_t length);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700174
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700175 Class* CreatePrimitiveClass(const char* descriptor,
176 Class::PrimitiveType type);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700177
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700178 Class* CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700179 const ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700180
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700181 void AppendToBootClassPath(const DexFile& dex_file);
182 void AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700183
Brian Carlstrom4873d462011-08-21 15:23:39 -0700184 size_t SizeOfClass(const DexFile& dex_file,
185 const DexFile::ClassDef& dex_class_def);
186
Brian Carlstromf615a612011-07-23 12:50:34 -0700187 void LoadClass(const DexFile& dex_file,
188 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700189 Class* klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700190 const ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700191
Brian Carlstromf615a612011-07-23 12:50:34 -0700192 void LoadInterfaces(const DexFile& dex_file,
193 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700194 Class *klass);
195
Brian Carlstromf615a612011-07-23 12:50:34 -0700196 void LoadField(const DexFile& dex_file,
197 const DexFile::Field& dex_field,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700198 Class* klass,
199 Field* dst);
200
Brian Carlstromf615a612011-07-23 12:50:34 -0700201 void LoadMethod(const DexFile& dex_file,
202 const DexFile::Method& dex_method,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700203 Class* klass,
Brian Carlstrom1f870082011-08-23 16:02:11 -0700204 Method* dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700205
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700206 Class* LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700207
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700208 // Inserts a class into the class table. Returns true if the class
209 // was inserted.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700210 bool InsertClass(const StringPiece& descriptor, Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700211
212 bool InitializeSuperClass(Class* klass);
213
214 void InitializeStaticFields(Class* klass);
215
216 bool ValidateSuperClassDescriptors(const Class* klass);
217
218 bool HasSameDescriptorClasses(const char* descriptor,
219 const Class* klass1,
220 const Class* klass2);
221
222 bool HasSameMethodDescriptorClasses(const Method* descriptor,
223 const Class* klass1,
224 const Class* klass2);
225
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700226 bool LinkClass(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700227
228 bool LinkSuperClass(Class* klass);
229
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700230 bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700231
232 bool LinkMethods(Class* klass);
233
234 bool LinkVirtualMethods(Class* klass);
235
236 bool LinkInterfaceMethods(Class* klass);
237
238 void LinkAbstractMethods(Class* klass);
239
Jesse Wilson7833bd22011-08-09 18:31:44 -0400240 bool LinkStaticFields(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700241 bool LinkInstanceFields(Class* klass);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700242 bool LinkFields(Class *klass, bool instance);
243
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700244
Brian Carlstrom4873d462011-08-21 15:23:39 -0700245 void CreateReferenceInstanceOffsets(Class* klass);
246 void CreateReferenceStaticOffsets(Class* klass);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700247 void CreateReferenceOffsets(Class *klass, bool instance,
248 uint32_t reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700249
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700250 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700251
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700252 std::vector<const DexFile*> dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700253
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700254 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700255
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700256 // multimap from a StringPiece hash code of a class descriptor to
257 // Class* instances. Results should be compared for a matching
258 // Class::descriptor_ and Class::class_loader_.
259 typedef std::tr1::unordered_multimap<size_t, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700260 Table classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700261 Mutex* classes_lock_;
262
Brian Carlstroma663ea52011-08-19 23:33:41 -0700263 // indexes into class_roots_.
264 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700265 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700266 kJavaLangClass,
267 kJavaLangObject,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700268 kObjectArrayClass,
269 kJavaLangString,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700270 kJavaLangReflectField,
271 kJavaLangReflectMethod,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700272 kJavaLangClassLoader,
273 kDalvikSystemBaseDexClassLoader,
274 kDalvikSystemPathClassLoader,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700275 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700276 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700277 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700278 kPrimitiveChar,
279 kPrimitiveDouble,
280 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700281 kPrimitiveInt,
282 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700283 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700284 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700285 kBooleanArrayClass,
286 kByteArrayClass,
287 kCharArrayClass,
288 kDoubleArrayClass,
289 kFloatArrayClass,
290 kIntArrayClass,
291 kLongArrayClass,
292 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700293 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700294 kClassRootsMax,
295 };
296 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700297
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700298 Class* GetClassRoot(ClassRoot class_root) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700299 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700300 Class* klass = class_roots_->Get(class_root);
301 DCHECK(klass != NULL);
302 return klass;
303 }
304
Brian Carlstroma663ea52011-08-19 23:33:41 -0700305 void SetClassRoot(ClassRoot class_root, Class* klass) {
306 DCHECK(!init_done_);
307
308 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700309 DCHECK(klass->GetClassLoader() == NULL);
310 DCHECK(klass->GetDescriptor() != NULL);
311 DCHECK(klass->GetDescriptor()->Equals(GetClassRootDescriptor(class_root)));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700312
313 DCHECK(class_roots_ != NULL);
314 DCHECK(class_roots_->Get(class_root) == NULL);
315 class_roots_->Set(class_root, klass);
316 }
317
318 static const char* class_roots_descriptors_[kClassRootsMax];
319
320 const char* GetClassRootDescriptor(ClassRoot class_root) {
321 const char* descriptor = class_roots_descriptors_[class_root];
322 CHECK(descriptor != NULL);
323 return descriptor;
324 }
325
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700326 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700327 InterfaceEntry* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700328
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700329 bool init_done_;
330
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700331 InternTable* intern_table_;
332
Brian Carlstromf734cf52011-08-17 16:28:14 -0700333 friend class CommonTest;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700334 FRIEND_TEST(DexCacheTest, Open);
335 friend class ObjectTest;
336 FRIEND_TEST(ObjectTest, AllocObjectArray);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700337 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700338 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
339};
340
341} // namespace art
342
343#endif // ART_SRC_CLASS_LINKER_H_