blob: 6e13aa7d2c3ff4800b13da3cbb22f8bca35b5e14 [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"
12#include "intern_table.h"
13#include "macros.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070014#include "object.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070015#include "thread.h"
16#include "unordered_map.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070017#include "unordered_set.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070018
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070019#include "gtest/gtest.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070020
21namespace art {
22
23class ClassLinker {
24 public:
Brian Carlstroma663ea52011-08-19 23:33:41 -070025 // Initializes the class linker using DexFile and an optional boot Space.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070026 static ClassLinker* Create(const std::vector<const DexFile*>& boot_class_path, Space* boot_space);
Carl Shapiro61e019d2011-07-14 16:53:09 -070027
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070028 ~ClassLinker();
Carl Shapiro565f5072011-07-10 13:39:43 -070029
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070030 // Finds a class by its descriptor name.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070031 // If class_loader is null, searches boot_class_path_.
Brian Carlstrom6cc18452011-07-18 15:10:33 -070032 Class* FindClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070033 const ClassLoader* class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070034
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070035 Class* FindPrimitiveClass(char type);
36
Brian Carlstrom6cc18452011-07-18 15:10:33 -070037 Class* FindSystemClass(const StringPiece& descriptor) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070038 return FindClass(descriptor, NULL);
Carl Shapiro565f5072011-07-10 13:39:43 -070039 }
40
Elliott Hughese27955c2011-08-26 15:21:24 -070041 size_t NumLoadedClasses() const;
42
Brian Carlstromb63ec392011-08-27 17:38:27 -070043 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070044 // result in the DexCache.
45 String* ResolveString(const DexFile& dex_file,
46 uint32_t string_idx,
47 DexCache* dex_cache);
48
Brian Carlstromb63ec392011-08-27 17:38:27 -070049 // Resolve a Type with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070050 // result in the DexCache. The referrer is used to identity the
51 // target DexCache and ClassLoader to use for resolution.
52 Class* ResolveType(const DexFile& dex_file,
53 uint32_t type_idx,
54 const Class* referrer) {
55 return ResolveType(dex_file,
56 type_idx,
57 referrer->GetDexCache(),
58 referrer->GetClassLoader());
59 }
60
Brian Carlstromb63ec392011-08-27 17:38:27 -070061 // Resolve a Type with the given index from the DexFile, storing the
62 // result in the DexCache. The referrer is used to identity the
63 // target DexCache and ClassLoader to use for resolution.
Brian Carlstromb9edb842011-08-28 16:31:06 -070064 Class* ResolveType(uint32_t type_idx, const Method* referrer) {
Brian Carlstromb63ec392011-08-27 17:38:27 -070065 Class* declaring_class = referrer->GetDeclaringClass();
66 DexCache* dex_cache = declaring_class->GetDexCache();
67 const ClassLoader* class_loader = declaring_class->GetClassLoader();
68 const DexFile& dex_file = FindDexFile(dex_cache);
69 return ResolveType(dex_file, type_idx, dex_cache, class_loader);
70 }
71
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070072 // Resolve a type with the given ID from the DexFile, storing the
73 // result in DexCache. The ClassLoader is used to search for the
74 // type, since it may be referenced from but not contained within
75 // the given DexFile.
76 Class* ResolveType(const DexFile& dex_file,
77 uint32_t type_idx,
78 DexCache* dex_cache,
79 const ClassLoader* class_loader);
80
Brian Carlstromb9edb842011-08-28 16:31:06 -070081 static StaticStorageBase* InitializeStaticStorageFromCode(uint32_t type_idx,
82 const Method* referrer);
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070083
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070084 // Resolve a method with a given ID from the DexFile, storing the
85 // result in DexCache. The ClassLinker and ClassLoader are used as
86 // in ResolveType. What is unique is the method type argument which
87 // is used to determine if this method is a direct, static, or
88 // virtual method.
89 Method* ResolveMethod(const DexFile& dex_file,
90 uint32_t method_idx,
91 DexCache* dex_cache,
92 const ClassLoader* class_loader,
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070093 bool is_direct);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070094
Brian Carlstromb9edb842011-08-28 16:31:06 -070095 Field* ResolveField(uint32_t field_idx, const Method* referrer) {
96 Class* declaring_class = referrer->GetDeclaringClass();
97 DexCache* dex_cache = declaring_class->GetDexCache();
98 const ClassLoader* class_loader = declaring_class->GetClassLoader();
99 const DexFile& dex_file = FindDexFile(dex_cache);
100 return ResolveField(dex_file, field_idx, dex_cache, class_loader, true);
101 }
102
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700103 // Resolve a method with a given ID from the DexFile, storing the
104 // result in DexCache. The ClassLinker and ClassLoader are used as
105 // in ResolveType. What is unique is the is_static argument which is
106 // used to determine if we are resolving a static or non-static
107 // field.
108 Field* ResolveField(const DexFile& dex_file,
109 uint32_t field_idx,
110 DexCache* dex_cache,
111 const ClassLoader* class_loader,
112 bool is_static);
113
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700114 // Returns true on success, false if there's an exception pending.
115 bool EnsureInitialized(Class* c);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700116
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700117 void RegisterDexFile(const DexFile& dex_file);
118 void RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700119
Brian Carlstrom8a487412011-08-29 20:08:52 -0700120 const std::vector<const DexFile*>& GetBootClassPath() {
121 return boot_class_path_;
122 }
123
Brian Carlstroma663ea52011-08-19 23:33:41 -0700124 const InternTable& GetInternTable() {
125 return intern_table_;
126 }
127
128 void VisitRoots(Heap::RootVistor* root_visitor, void* arg) const;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700129
buzbeec143c552011-08-20 17:38:58 -0700130 const DexFile& FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700131 DexCache* FindDexCache(const DexFile& dex_file) const;
buzbeec143c552011-08-20 17:38:58 -0700132
Shih-wei Liao44175362011-08-28 16:59:17 -0700133 template <class T>
134 ObjectArray<T>* AllocObjectArray(size_t length) {
135 return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
136 }
137
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700138 ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length);
139
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700140 private:
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700141 ClassLinker();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700142
Brian Carlstroma663ea52011-08-19 23:33:41 -0700143 // Initialize class linker from DexFile instances.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700144 void Init(const std::vector<const DexFile*>& boot_class_path_);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700145
Brian Carlstroma663ea52011-08-19 23:33:41 -0700146 // Initialize class linker from pre-initialized space.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700147 void Init(const std::vector<const DexFile*>& boot_class_path_, Space* space);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700148 static void InitCallback(Object* obj, void *arg);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700149 struct InitCallbackState;
150
151 void FinishInit();
152
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700153 bool InitializeClass(Class* klass);
154
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700155 // For early bootstrapping by Init
Brian Carlstrom4873d462011-08-21 15:23:39 -0700156 Class* AllocClass(Class* java_lang_Class, size_t class_size);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700157
158 // Alloc* convenience functions to avoid needing to pass in Class*
159 // values that are known to the ClassLinker such as
160 // kObjectArrayClass and kJavaLangString etc.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700161 Class* AllocClass(size_t class_size);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700162 DexCache* AllocDexCache(const DexFile& dex_file);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400163 Field* AllocField();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700164 Method* AllocMethod();
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700165 CodeAndDirectMethods* AllocCodeAndDirectMethods(size_t length);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700166
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700167 Class* CreatePrimitiveClass(const char* descriptor);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700168
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700169 Class* CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700170 const ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700171
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700172 void AppendToBootClassPath(const DexFile& dex_file);
173 void AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700174
Brian Carlstrom4873d462011-08-21 15:23:39 -0700175 size_t SizeOfClass(const DexFile& dex_file,
176 const DexFile::ClassDef& dex_class_def);
177
Brian Carlstromf615a612011-07-23 12:50:34 -0700178 void LoadClass(const DexFile& dex_file,
179 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700180 Class* klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700181 const ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700182
Brian Carlstromf615a612011-07-23 12:50:34 -0700183 void LoadInterfaces(const DexFile& dex_file,
184 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700185 Class *klass);
186
Brian Carlstromf615a612011-07-23 12:50:34 -0700187 void LoadField(const DexFile& dex_file,
188 const DexFile::Field& dex_field,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700189 Class* klass,
190 Field* dst);
191
Brian Carlstromf615a612011-07-23 12:50:34 -0700192 void LoadMethod(const DexFile& dex_file,
193 const DexFile::Method& dex_method,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700194 Class* klass,
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700195 Method* dst,
196 bool is_direct);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700197
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700198 Class* LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700199
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700200 // Inserts a class into the class table. Returns true if the class
201 // was inserted.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700202 bool InsertClass(const StringPiece& descriptor, Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700203
204 bool InitializeSuperClass(Class* klass);
205
206 void InitializeStaticFields(Class* klass);
207
208 bool ValidateSuperClassDescriptors(const Class* klass);
209
210 bool HasSameDescriptorClasses(const char* descriptor,
211 const Class* klass1,
212 const Class* klass2);
213
214 bool HasSameMethodDescriptorClasses(const Method* descriptor,
215 const Class* klass1,
216 const Class* klass2);
217
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700218 bool LinkClass(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700219
220 bool LinkSuperClass(Class* klass);
221
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700222 bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700223
224 bool LinkMethods(Class* klass);
225
226 bool LinkVirtualMethods(Class* klass);
227
228 bool LinkInterfaceMethods(Class* klass);
229
230 void LinkAbstractMethods(Class* klass);
231
Jesse Wilson7833bd22011-08-09 18:31:44 -0400232 bool LinkStaticFields(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700233 bool LinkInstanceFields(Class* klass);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700234 bool LinkFields(size_t field_offset,
235 size_t& num_reference_fields,
236 size_t num_fields,
237 ObjectArray<Field>* fields,
238 size_t& size);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700239
Brian Carlstrom4873d462011-08-21 15:23:39 -0700240 void CreateReferenceInstanceOffsets(Class* klass);
241 void CreateReferenceStaticOffsets(Class* klass);
242 void CreateReferenceOffsets(uint32_t& reference_offsets,
243 size_t num_reference_fields,
244 const ObjectArray<Field>* fields);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700245
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700246 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700247
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700248 std::vector<const DexFile*> dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700249
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700250 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700251
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700252 // multimap from a StringPiece hash code of a class descriptor to
253 // Class* instances. Results should be compared for a matching
254 // Class::descriptor_ and Class::class_loader_.
255 typedef std::tr1::unordered_multimap<size_t, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700256 Table classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700257 Mutex* classes_lock_;
258
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700259 InternTable intern_table_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700260
Brian Carlstroma663ea52011-08-19 23:33:41 -0700261 // indexes into class_roots_.
262 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700263 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700264 kJavaLangClass,
265 kJavaLangObject,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700266 kObjectArrayClass,
267 kJavaLangString,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700268 kJavaLangReflectField,
269 kJavaLangReflectMethod,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700270 kJavaLangClassLoader,
271 kDalvikSystemBaseDexClassLoader,
272 kDalvikSystemPathClassLoader,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700273 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700274 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700275 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700276 kPrimitiveChar,
277 kPrimitiveDouble,
278 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700279 kPrimitiveInt,
280 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700281 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700282 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700283 kBooleanArrayClass,
284 kByteArrayClass,
285 kCharArrayClass,
286 kDoubleArrayClass,
287 kFloatArrayClass,
288 kIntArrayClass,
289 kLongArrayClass,
290 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700291 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700292 kClassRootsMax,
293 };
294 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700295
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700296 Class* GetClassRoot(ClassRoot class_root) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700297 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700298 Class* klass = class_roots_->Get(class_root);
299 DCHECK(klass != NULL);
300 return klass;
301 }
302
Brian Carlstroma663ea52011-08-19 23:33:41 -0700303 void SetClassRoot(ClassRoot class_root, Class* klass) {
304 DCHECK(!init_done_);
305
306 DCHECK(klass != NULL);
307 DCHECK(klass->class_loader_ == NULL);
308 DCHECK(klass->descriptor_ != NULL);
309 DCHECK(klass->descriptor_->Equals(GetClassRootDescriptor(class_root)));
310
311 DCHECK(class_roots_ != NULL);
312 DCHECK(class_roots_->Get(class_root) == NULL);
313 class_roots_->Set(class_root, klass);
314 }
315
316 static const char* class_roots_descriptors_[kClassRootsMax];
317
318 const char* GetClassRootDescriptor(ClassRoot class_root) {
319 const char* descriptor = class_roots_descriptors_[class_root];
320 CHECK(descriptor != NULL);
321 return descriptor;
322 }
323
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700324 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700325 InterfaceEntry* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700326
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700327 bool init_done_;
328
Brian Carlstromf734cf52011-08-17 16:28:14 -0700329 friend class CommonTest;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700330 FRIEND_TEST(DexCacheTest, Open);
331 friend class ObjectTest;
332 FRIEND_TEST(ObjectTest, AllocObjectArray);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700333 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700334 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
335};
336
337} // namespace art
338
339#endif // ART_SRC_CLASS_LINKER_H_