blob: d9716ee111da9ed40bdad77c9d6712190adabb4e [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.
64 Class* ResolveType(uint32_t type_idx, Method* referrer) {
65 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 Carlstrom1caa2c22011-08-28 13:02:33 -070081 static StaticStorageBase* InitializeStaticStorageFromCode(uint32_t type_idx, Method* referrer);
82
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070083 // Resolve a method with a given ID from the DexFile, storing the
84 // result in DexCache. The ClassLinker and ClassLoader are used as
85 // in ResolveType. What is unique is the method type argument which
86 // is used to determine if this method is a direct, static, or
87 // virtual method.
88 Method* ResolveMethod(const DexFile& dex_file,
89 uint32_t method_idx,
90 DexCache* dex_cache,
91 const ClassLoader* class_loader,
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070092 bool is_direct);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070093
94 // Resolve a method with a given ID from the DexFile, storing the
95 // result in DexCache. The ClassLinker and ClassLoader are used as
96 // in ResolveType. What is unique is the is_static argument which is
97 // used to determine if we are resolving a static or non-static
98 // field.
99 Field* ResolveField(const DexFile& dex_file,
100 uint32_t field_idx,
101 DexCache* dex_cache,
102 const ClassLoader* class_loader,
103 bool is_static);
104
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700105 // Returns true on success, false if there's an exception pending.
106 bool EnsureInitialized(Class* c);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700107
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700108 void RegisterDexFile(const DexFile& dex_file);
109 void RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700110
Brian Carlstroma663ea52011-08-19 23:33:41 -0700111 const InternTable& GetInternTable() {
112 return intern_table_;
113 }
114
115 void VisitRoots(Heap::RootVistor* root_visitor, void* arg) const;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700116
buzbeec143c552011-08-20 17:38:58 -0700117 const DexFile& FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700118 DexCache* FindDexCache(const DexFile& dex_file) const;
buzbeec143c552011-08-20 17:38:58 -0700119
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700120 ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length);
121
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700122 private:
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700123 ClassLinker();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700124
Brian Carlstroma663ea52011-08-19 23:33:41 -0700125 // Initialize class linker from DexFile instances.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700126 void Init(const std::vector<const DexFile*>& boot_class_path_);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700127
Brian Carlstroma663ea52011-08-19 23:33:41 -0700128 // Initialize class linker from pre-initialized space.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700129 void Init(const std::vector<const DexFile*>& boot_class_path_, Space* space);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700130 static void InitCallback(Object* obj, void *arg);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700131 struct InitCallbackState;
132
133 void FinishInit();
134
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700135 bool InitializeClass(Class* klass);
136
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700137 // For early bootstrapping by Init
Brian Carlstrom4873d462011-08-21 15:23:39 -0700138 Class* AllocClass(Class* java_lang_Class, size_t class_size);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700139
140 // Alloc* convenience functions to avoid needing to pass in Class*
141 // values that are known to the ClassLinker such as
142 // kObjectArrayClass and kJavaLangString etc.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700143 Class* AllocClass(size_t class_size);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700144 DexCache* AllocDexCache(const DexFile& dex_file);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400145 Field* AllocField();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700146 Method* AllocMethod();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700147 template <class T>
148 ObjectArray<T>* AllocObjectArray(size_t length) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700149 return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700150 }
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700151 CodeAndDirectMethods* AllocCodeAndDirectMethods(size_t length);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700152
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700153 Class* CreatePrimitiveClass(const char* descriptor);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700154
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700155 Class* CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700156 const ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700157
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700158 void AppendToBootClassPath(const DexFile& dex_file);
159 void AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700160
Brian Carlstrom4873d462011-08-21 15:23:39 -0700161 size_t SizeOfClass(const DexFile& dex_file,
162 const DexFile::ClassDef& dex_class_def);
163
Brian Carlstromf615a612011-07-23 12:50:34 -0700164 void LoadClass(const DexFile& dex_file,
165 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700166 Class* klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700167 const ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700168
Brian Carlstromf615a612011-07-23 12:50:34 -0700169 void LoadInterfaces(const DexFile& dex_file,
170 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700171 Class *klass);
172
Brian Carlstromf615a612011-07-23 12:50:34 -0700173 void LoadField(const DexFile& dex_file,
174 const DexFile::Field& dex_field,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700175 Class* klass,
176 Field* dst);
177
Brian Carlstromf615a612011-07-23 12:50:34 -0700178 void LoadMethod(const DexFile& dex_file,
179 const DexFile::Method& dex_method,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700180 Class* klass,
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700181 Method* dst,
182 bool is_direct);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700183
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700184 Class* LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700185
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700186 // Inserts a class into the class table. Returns true if the class
187 // was inserted.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700188 bool InsertClass(const StringPiece& descriptor, Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700189
190 bool InitializeSuperClass(Class* klass);
191
192 void InitializeStaticFields(Class* klass);
193
194 bool ValidateSuperClassDescriptors(const Class* klass);
195
196 bool HasSameDescriptorClasses(const char* descriptor,
197 const Class* klass1,
198 const Class* klass2);
199
200 bool HasSameMethodDescriptorClasses(const Method* descriptor,
201 const Class* klass1,
202 const Class* klass2);
203
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700204 bool LinkClass(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700205
206 bool LinkSuperClass(Class* klass);
207
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700208 bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700209
210 bool LinkMethods(Class* klass);
211
212 bool LinkVirtualMethods(Class* klass);
213
214 bool LinkInterfaceMethods(Class* klass);
215
216 void LinkAbstractMethods(Class* klass);
217
Jesse Wilson7833bd22011-08-09 18:31:44 -0400218 bool LinkStaticFields(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700219 bool LinkInstanceFields(Class* klass);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700220 bool LinkFields(size_t field_offset,
221 size_t& num_reference_fields,
222 size_t num_fields,
223 ObjectArray<Field>* fields,
224 size_t& size);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700225
Brian Carlstrom4873d462011-08-21 15:23:39 -0700226 void CreateReferenceInstanceOffsets(Class* klass);
227 void CreateReferenceStaticOffsets(Class* klass);
228 void CreateReferenceOffsets(uint32_t& reference_offsets,
229 size_t num_reference_fields,
230 const ObjectArray<Field>* fields);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700231
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700232 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700233
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700234 std::vector<const DexFile*> dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700235
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700236 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700237
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700238 // multimap from a StringPiece hash code of a class descriptor to
239 // Class* instances. Results should be compared for a matching
240 // Class::descriptor_ and Class::class_loader_.
241 typedef std::tr1::unordered_multimap<size_t, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700242 Table classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700243 Mutex* classes_lock_;
244
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700245 InternTable intern_table_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700246
Brian Carlstroma663ea52011-08-19 23:33:41 -0700247 // indexes into class_roots_.
248 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700249 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700250 kJavaLangClass,
251 kJavaLangObject,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700252 kObjectArrayClass,
253 kJavaLangString,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700254 kJavaLangReflectField,
255 kJavaLangReflectMethod,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700256 kJavaLangClassLoader,
257 kDalvikSystemBaseDexClassLoader,
258 kDalvikSystemPathClassLoader,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700259 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700260 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700261 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700262 kPrimitiveChar,
263 kPrimitiveDouble,
264 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700265 kPrimitiveInt,
266 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700267 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700268 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700269 kBooleanArrayClass,
270 kByteArrayClass,
271 kCharArrayClass,
272 kDoubleArrayClass,
273 kFloatArrayClass,
274 kIntArrayClass,
275 kLongArrayClass,
276 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700277 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700278 kClassRootsMax,
279 };
280 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700281
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700282 Class* GetClassRoot(ClassRoot class_root) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700283 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700284 Class* klass = class_roots_->Get(class_root);
285 DCHECK(klass != NULL);
286 return klass;
287 }
288
Brian Carlstroma663ea52011-08-19 23:33:41 -0700289 void SetClassRoot(ClassRoot class_root, Class* klass) {
290 DCHECK(!init_done_);
291
292 DCHECK(klass != NULL);
293 DCHECK(klass->class_loader_ == NULL);
294 DCHECK(klass->descriptor_ != NULL);
295 DCHECK(klass->descriptor_->Equals(GetClassRootDescriptor(class_root)));
296
297 DCHECK(class_roots_ != NULL);
298 DCHECK(class_roots_->Get(class_root) == NULL);
299 class_roots_->Set(class_root, klass);
300 }
301
302 static const char* class_roots_descriptors_[kClassRootsMax];
303
304 const char* GetClassRootDescriptor(ClassRoot class_root) {
305 const char* descriptor = class_roots_descriptors_[class_root];
306 CHECK(descriptor != NULL);
307 return descriptor;
308 }
309
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700310 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700311 InterfaceEntry* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700312
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700313 bool init_done_;
314
Brian Carlstromf734cf52011-08-17 16:28:14 -0700315 friend class CommonTest;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700316 FRIEND_TEST(DexCacheTest, Open);
317 friend class ObjectTest;
318 FRIEND_TEST(ObjectTest, AllocObjectArray);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700319 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700320 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
321};
322
323} // namespace art
324
325#endif // ART_SRC_CLASS_LINKER_H_