blob: 4702a574ba5fff010c38c3fabd184afe2f48a9e6 [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.
Elliott Hughescf4c6c42011-09-01 15:16:42 -070048 const 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
63 // result in the DexCache. The referrer is used to identity the
64 // 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();
68 const ClassLoader* class_loader = declaring_class->GetClassLoader();
69 const DexFile& dex_file = FindDexFile(dex_cache);
70 return ResolveType(dex_file, type_idx, dex_cache, class_loader);
71 }
72
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070073 // Resolve a type with the given ID from the DexFile, storing the
74 // result in DexCache. The ClassLoader is used to search for the
75 // type, since it may be referenced from but not contained within
76 // the given DexFile.
77 Class* ResolveType(const DexFile& dex_file,
78 uint32_t type_idx,
79 DexCache* dex_cache,
80 const ClassLoader* class_loader);
81
Brian Carlstromb9edb842011-08-28 16:31:06 -070082 static StaticStorageBase* InitializeStaticStorageFromCode(uint32_t type_idx,
83 const Method* referrer);
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070084
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070085 // Resolve a method with a given ID from the DexFile, storing the
86 // result in DexCache. The ClassLinker and ClassLoader are used as
87 // in ResolveType. What is unique is the method type argument which
88 // is used to determine if this method is a direct, static, or
89 // virtual method.
90 Method* ResolveMethod(const DexFile& dex_file,
91 uint32_t method_idx,
92 DexCache* dex_cache,
93 const ClassLoader* class_loader,
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070094 bool is_direct);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070095
Brian Carlstromb9edb842011-08-28 16:31:06 -070096 Field* ResolveField(uint32_t field_idx, const Method* referrer) {
97 Class* declaring_class = referrer->GetDeclaringClass();
98 DexCache* dex_cache = declaring_class->GetDexCache();
99 const ClassLoader* class_loader = declaring_class->GetClassLoader();
100 const DexFile& dex_file = FindDexFile(dex_cache);
101 return ResolveField(dex_file, field_idx, dex_cache, class_loader, true);
102 }
103
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700104 // Resolve a method with a given ID from the DexFile, storing the
105 // result in DexCache. The ClassLinker and ClassLoader are used as
106 // in ResolveType. What is unique is the is_static argument which is
107 // used to determine if we are resolving a static or non-static
108 // field.
109 Field* ResolveField(const DexFile& dex_file,
110 uint32_t field_idx,
111 DexCache* dex_cache,
112 const ClassLoader* class_loader,
113 bool is_static);
114
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700115 // Returns true on success, false if there's an exception pending.
116 bool EnsureInitialized(Class* c);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700117
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700118 void RegisterDexFile(const DexFile& dex_file);
119 void RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700120
Brian Carlstrom8a487412011-08-29 20:08:52 -0700121 const std::vector<const DexFile*>& GetBootClassPath() {
122 return boot_class_path_;
123 }
124
Brian Carlstroma663ea52011-08-19 23:33:41 -0700125 void VisitRoots(Heap::RootVistor* root_visitor, void* arg) const;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700126
buzbeec143c552011-08-20 17:38:58 -0700127 const DexFile& FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700128 DexCache* FindDexCache(const DexFile& dex_file) const;
buzbeec143c552011-08-20 17:38:58 -0700129
Shih-wei Liao44175362011-08-28 16:59:17 -0700130 template <class T>
131 ObjectArray<T>* AllocObjectArray(size_t length) {
132 return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
133 }
134
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700135 ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length);
136
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700137 private:
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700138 ClassLinker(InternTable*);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700139
Brian Carlstroma663ea52011-08-19 23:33:41 -0700140 // Initialize class linker from DexFile instances.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700141 void Init(const std::vector<const DexFile*>& boot_class_path_);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700142
Brian Carlstroma663ea52011-08-19 23:33:41 -0700143 // Initialize class linker from pre-initialized space.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700144 void Init(const std::vector<const DexFile*>& boot_class_path_, Space* space);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700145 static void InitCallback(Object* obj, void *arg);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700146 struct InitCallbackState;
147
148 void FinishInit();
149
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700150 bool InitializeClass(Class* klass);
151
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700152 // For early bootstrapping by Init
Brian Carlstrom4873d462011-08-21 15:23:39 -0700153 Class* AllocClass(Class* java_lang_Class, size_t class_size);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700154
155 // Alloc* convenience functions to avoid needing to pass in Class*
156 // values that are known to the ClassLinker such as
157 // kObjectArrayClass and kJavaLangString etc.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700158 Class* AllocClass(size_t class_size);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700159 DexCache* AllocDexCache(const DexFile& dex_file);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400160 Field* AllocField();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700161 Method* AllocMethod();
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700162 CodeAndDirectMethods* AllocCodeAndDirectMethods(size_t length);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700163
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700164 Class* CreatePrimitiveClass(const char* descriptor);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700165
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700166 Class* CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700167 const ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700168
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700169 void AppendToBootClassPath(const DexFile& dex_file);
170 void AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700171
Brian Carlstrom4873d462011-08-21 15:23:39 -0700172 size_t SizeOfClass(const DexFile& dex_file,
173 const DexFile::ClassDef& dex_class_def);
174
Brian Carlstromf615a612011-07-23 12:50:34 -0700175 void LoadClass(const DexFile& dex_file,
176 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700177 Class* klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700178 const ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700179
Brian Carlstromf615a612011-07-23 12:50:34 -0700180 void LoadInterfaces(const DexFile& dex_file,
181 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700182 Class *klass);
183
Brian Carlstromf615a612011-07-23 12:50:34 -0700184 void LoadField(const DexFile& dex_file,
185 const DexFile::Field& dex_field,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700186 Class* klass,
187 Field* dst);
188
Brian Carlstromf615a612011-07-23 12:50:34 -0700189 void LoadMethod(const DexFile& dex_file,
190 const DexFile::Method& dex_method,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700191 Class* klass,
Brian Carlstrom1f870082011-08-23 16:02:11 -0700192 Method* dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700193
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700194 Class* LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700195
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700196 // Inserts a class into the class table. Returns true if the class
197 // was inserted.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700198 bool InsertClass(const StringPiece& descriptor, Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700199
200 bool InitializeSuperClass(Class* klass);
201
202 void InitializeStaticFields(Class* klass);
203
204 bool ValidateSuperClassDescriptors(const Class* klass);
205
206 bool HasSameDescriptorClasses(const char* descriptor,
207 const Class* klass1,
208 const Class* klass2);
209
210 bool HasSameMethodDescriptorClasses(const Method* descriptor,
211 const Class* klass1,
212 const Class* klass2);
213
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700214 bool LinkClass(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700215
216 bool LinkSuperClass(Class* klass);
217
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700218 bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700219
220 bool LinkMethods(Class* klass);
221
222 bool LinkVirtualMethods(Class* klass);
223
224 bool LinkInterfaceMethods(Class* klass);
225
226 void LinkAbstractMethods(Class* klass);
227
Jesse Wilson7833bd22011-08-09 18:31:44 -0400228 bool LinkStaticFields(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700229 bool LinkInstanceFields(Class* klass);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700230 bool LinkFields(size_t field_offset,
231 size_t& num_reference_fields,
232 size_t num_fields,
233 ObjectArray<Field>* fields,
234 size_t& size);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700235
Brian Carlstrom4873d462011-08-21 15:23:39 -0700236 void CreateReferenceInstanceOffsets(Class* klass);
237 void CreateReferenceStaticOffsets(Class* klass);
238 void CreateReferenceOffsets(uint32_t& reference_offsets,
239 size_t num_reference_fields,
240 const ObjectArray<Field>* fields);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700241
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700242 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700243
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700244 std::vector<const DexFile*> dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700245
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700246 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700247
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700248 // multimap from a StringPiece hash code of a class descriptor to
249 // Class* instances. Results should be compared for a matching
250 // Class::descriptor_ and Class::class_loader_.
251 typedef std::tr1::unordered_multimap<size_t, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700252 Table classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700253 Mutex* classes_lock_;
254
Brian Carlstroma663ea52011-08-19 23:33:41 -0700255 // indexes into class_roots_.
256 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700257 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700258 kJavaLangClass,
259 kJavaLangObject,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700260 kObjectArrayClass,
261 kJavaLangString,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700262 kJavaLangReflectField,
263 kJavaLangReflectMethod,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700264 kJavaLangClassLoader,
265 kDalvikSystemBaseDexClassLoader,
266 kDalvikSystemPathClassLoader,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700267 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700268 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700269 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700270 kPrimitiveChar,
271 kPrimitiveDouble,
272 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700273 kPrimitiveInt,
274 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700275 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700276 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700277 kBooleanArrayClass,
278 kByteArrayClass,
279 kCharArrayClass,
280 kDoubleArrayClass,
281 kFloatArrayClass,
282 kIntArrayClass,
283 kLongArrayClass,
284 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700285 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700286 kClassRootsMax,
287 };
288 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700289
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700290 Class* GetClassRoot(ClassRoot class_root) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700291 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700292 Class* klass = class_roots_->Get(class_root);
293 DCHECK(klass != NULL);
294 return klass;
295 }
296
Brian Carlstroma663ea52011-08-19 23:33:41 -0700297 void SetClassRoot(ClassRoot class_root, Class* klass) {
298 DCHECK(!init_done_);
299
300 DCHECK(klass != NULL);
301 DCHECK(klass->class_loader_ == NULL);
302 DCHECK(klass->descriptor_ != NULL);
303 DCHECK(klass->descriptor_->Equals(GetClassRootDescriptor(class_root)));
304
305 DCHECK(class_roots_ != NULL);
306 DCHECK(class_roots_->Get(class_root) == NULL);
307 class_roots_->Set(class_root, klass);
308 }
309
310 static const char* class_roots_descriptors_[kClassRootsMax];
311
312 const char* GetClassRootDescriptor(ClassRoot class_root) {
313 const char* descriptor = class_roots_descriptors_[class_root];
314 CHECK(descriptor != NULL);
315 return descriptor;
316 }
317
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700318 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700319 InterfaceEntry* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700320
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700321 bool init_done_;
322
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700323 InternTable* intern_table_;
324
Brian Carlstromf734cf52011-08-17 16:28:14 -0700325 friend class CommonTest;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700326 FRIEND_TEST(DexCacheTest, Open);
327 friend class ObjectTest;
328 FRIEND_TEST(ObjectTest, AllocObjectArray);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700329 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700330 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
331};
332
333} // namespace art
334
335#endif // ART_SRC_CLASS_LINKER_H_