blob: 5e3ac78739eb3be2e89e2b6eb14f05508afe8453 [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
81 // Resolve a method with a given ID from the DexFile, storing the
82 // result in DexCache. The ClassLinker and ClassLoader are used as
83 // in ResolveType. What is unique is the method type argument which
84 // is used to determine if this method is a direct, static, or
85 // virtual method.
86 Method* ResolveMethod(const DexFile& dex_file,
87 uint32_t method_idx,
88 DexCache* dex_cache,
89 const ClassLoader* class_loader,
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070090 bool is_direct);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070091
92 // Resolve a method with a given ID from the DexFile, storing the
93 // result in DexCache. The ClassLinker and ClassLoader are used as
94 // in ResolveType. What is unique is the is_static argument which is
95 // used to determine if we are resolving a static or non-static
96 // field.
97 Field* ResolveField(const DexFile& dex_file,
98 uint32_t field_idx,
99 DexCache* dex_cache,
100 const ClassLoader* class_loader,
101 bool is_static);
102
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700103 // Returns true on success, false if there's an exception pending.
104 bool EnsureInitialized(Class* c);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700105
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700106 void RegisterDexFile(const DexFile& dex_file);
107 void RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700108
Brian Carlstroma663ea52011-08-19 23:33:41 -0700109 const InternTable& GetInternTable() {
110 return intern_table_;
111 }
112
113 void VisitRoots(Heap::RootVistor* root_visitor, void* arg) const;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700114
buzbeec143c552011-08-20 17:38:58 -0700115 const DexFile& FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700116 DexCache* FindDexCache(const DexFile& dex_file) const;
buzbeec143c552011-08-20 17:38:58 -0700117
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700118 ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length);
119
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700120 private:
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700121 ClassLinker();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700122
Brian Carlstroma663ea52011-08-19 23:33:41 -0700123 // Initialize class linker from DexFile instances.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700124 void Init(const std::vector<const DexFile*>& boot_class_path_);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700125
Brian Carlstroma663ea52011-08-19 23:33:41 -0700126 // Initialize class linker from pre-initialized space.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700127 void Init(const std::vector<const DexFile*>& boot_class_path_, Space* space);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700128 static void InitCallback(Object* obj, void *arg);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700129 struct InitCallbackState;
130
131 void FinishInit();
132
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700133 bool InitializeClass(Class* klass);
134
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700135 // For early bootstrapping by Init
Brian Carlstrom4873d462011-08-21 15:23:39 -0700136 Class* AllocClass(Class* java_lang_Class, size_t class_size);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700137
138 // Alloc* convenience functions to avoid needing to pass in Class*
139 // values that are known to the ClassLinker such as
140 // kObjectArrayClass and kJavaLangString etc.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700141 Class* AllocClass(size_t class_size);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700142 DexCache* AllocDexCache(const DexFile& dex_file);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400143 Field* AllocField();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700144 Method* AllocMethod();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700145 template <class T>
146 ObjectArray<T>* AllocObjectArray(size_t length) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700147 return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700148 }
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700149 CodeAndDirectMethods* AllocCodeAndDirectMethods(size_t length);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700150
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700151 Class* CreatePrimitiveClass(const char* descriptor);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700152
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700153 Class* CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700154 const ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700155
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700156 void AppendToBootClassPath(const DexFile& dex_file);
157 void AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700158
Brian Carlstrom4873d462011-08-21 15:23:39 -0700159 size_t SizeOfClass(const DexFile& dex_file,
160 const DexFile::ClassDef& dex_class_def);
161
Brian Carlstromf615a612011-07-23 12:50:34 -0700162 void LoadClass(const DexFile& dex_file,
163 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700164 Class* klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700165 const ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700166
Brian Carlstromf615a612011-07-23 12:50:34 -0700167 void LoadInterfaces(const DexFile& dex_file,
168 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700169 Class *klass);
170
Brian Carlstromf615a612011-07-23 12:50:34 -0700171 void LoadField(const DexFile& dex_file,
172 const DexFile::Field& dex_field,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700173 Class* klass,
174 Field* dst);
175
Brian Carlstromf615a612011-07-23 12:50:34 -0700176 void LoadMethod(const DexFile& dex_file,
177 const DexFile::Method& dex_method,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700178 Class* klass,
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700179 Method* dst,
180 bool is_direct);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700181
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700182 Class* LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700183
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700184 // Inserts a class into the class table. Returns true if the class
185 // was inserted.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700186 bool InsertClass(const StringPiece& descriptor, Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700187
188 bool InitializeSuperClass(Class* klass);
189
190 void InitializeStaticFields(Class* klass);
191
192 bool ValidateSuperClassDescriptors(const Class* klass);
193
194 bool HasSameDescriptorClasses(const char* descriptor,
195 const Class* klass1,
196 const Class* klass2);
197
198 bool HasSameMethodDescriptorClasses(const Method* descriptor,
199 const Class* klass1,
200 const Class* klass2);
201
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700202 bool LinkClass(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700203
204 bool LinkSuperClass(Class* klass);
205
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700206 bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700207
208 bool LinkMethods(Class* klass);
209
210 bool LinkVirtualMethods(Class* klass);
211
212 bool LinkInterfaceMethods(Class* klass);
213
214 void LinkAbstractMethods(Class* klass);
215
Jesse Wilson7833bd22011-08-09 18:31:44 -0400216 bool LinkStaticFields(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700217 bool LinkInstanceFields(Class* klass);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700218 bool LinkFields(size_t field_offset,
219 size_t& num_reference_fields,
220 size_t num_fields,
221 ObjectArray<Field>* fields,
222 size_t& size);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700223
Brian Carlstrom4873d462011-08-21 15:23:39 -0700224 void CreateReferenceInstanceOffsets(Class* klass);
225 void CreateReferenceStaticOffsets(Class* klass);
226 void CreateReferenceOffsets(uint32_t& reference_offsets,
227 size_t num_reference_fields,
228 const ObjectArray<Field>* fields);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700229
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700230 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700231
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700232 std::vector<const DexFile*> dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700233
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700234 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700235
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700236 // multimap from a StringPiece hash code of a class descriptor to
237 // Class* instances. Results should be compared for a matching
238 // Class::descriptor_ and Class::class_loader_.
239 typedef std::tr1::unordered_multimap<size_t, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700240 Table classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700241 Mutex* classes_lock_;
242
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700243 InternTable intern_table_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700244
Brian Carlstroma663ea52011-08-19 23:33:41 -0700245 // indexes into class_roots_.
246 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700247 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700248 kJavaLangClass,
249 kJavaLangObject,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700250 kObjectArrayClass,
251 kJavaLangString,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700252 kJavaLangReflectField,
253 kJavaLangReflectMethod,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700254 kJavaLangClassLoader,
255 kDalvikSystemBaseDexClassLoader,
256 kDalvikSystemPathClassLoader,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700257 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700258 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700259 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700260 kPrimitiveChar,
261 kPrimitiveDouble,
262 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700263 kPrimitiveInt,
264 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700265 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700266 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700267 kBooleanArrayClass,
268 kByteArrayClass,
269 kCharArrayClass,
270 kDoubleArrayClass,
271 kFloatArrayClass,
272 kIntArrayClass,
273 kLongArrayClass,
274 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700275 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700276 kClassRootsMax,
277 };
278 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700279
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700280 Class* GetClassRoot(ClassRoot class_root) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700281 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700282 Class* klass = class_roots_->Get(class_root);
283 DCHECK(klass != NULL);
284 return klass;
285 }
286
Brian Carlstroma663ea52011-08-19 23:33:41 -0700287 void SetClassRoot(ClassRoot class_root, Class* klass) {
288 DCHECK(!init_done_);
289
290 DCHECK(klass != NULL);
291 DCHECK(klass->class_loader_ == NULL);
292 DCHECK(klass->descriptor_ != NULL);
293 DCHECK(klass->descriptor_->Equals(GetClassRootDescriptor(class_root)));
294
295 DCHECK(class_roots_ != NULL);
296 DCHECK(class_roots_->Get(class_root) == NULL);
297 class_roots_->Set(class_root, klass);
298 }
299
300 static const char* class_roots_descriptors_[kClassRootsMax];
301
302 const char* GetClassRootDescriptor(ClassRoot class_root) {
303 const char* descriptor = class_roots_descriptors_[class_root];
304 CHECK(descriptor != NULL);
305 return descriptor;
306 }
307
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700308 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700309 InterfaceEntry* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700310
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700311 bool init_done_;
312
Brian Carlstromf734cf52011-08-17 16:28:14 -0700313 friend class CommonTest;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700314 FRIEND_TEST(DexCacheTest, Open);
315 friend class ObjectTest;
316 FRIEND_TEST(ObjectTest, AllocObjectArray);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700317 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700318 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
319};
320
321} // namespace art
322
323#endif // ART_SRC_CLASS_LINKER_H_