blob: 4cf82c6f965a5eb96675818ac8fdc21332c6c9bc [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
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070041 // Resolve a String with the given ID from the DexFile, storing the
42 // result in the DexCache.
43 String* ResolveString(const DexFile& dex_file,
44 uint32_t string_idx,
45 DexCache* dex_cache);
46
47 // Resolve a Type with the given ID from the DexFile, storing the
48 // result in the DexCache. The referrer is used to identity the
49 // target DexCache and ClassLoader to use for resolution.
50 Class* ResolveType(const DexFile& dex_file,
51 uint32_t type_idx,
52 const Class* referrer) {
53 return ResolveType(dex_file,
54 type_idx,
55 referrer->GetDexCache(),
56 referrer->GetClassLoader());
57 }
58
59 // Resolve a type with the given ID from the DexFile, storing the
60 // result in DexCache. The ClassLoader is used to search for the
61 // type, since it may be referenced from but not contained within
62 // the given DexFile.
63 Class* ResolveType(const DexFile& dex_file,
64 uint32_t type_idx,
65 DexCache* dex_cache,
66 const ClassLoader* class_loader);
67
68 // Resolve a method with a given ID from the DexFile, storing the
69 // result in DexCache. The ClassLinker and ClassLoader are used as
70 // in ResolveType. What is unique is the method type argument which
71 // is used to determine if this method is a direct, static, or
72 // virtual method.
73 Method* ResolveMethod(const DexFile& dex_file,
74 uint32_t method_idx,
75 DexCache* dex_cache,
76 const ClassLoader* class_loader,
77 /*MethodType*/ int method_type);
78
79 // Resolve a method with a given ID from the DexFile, storing the
80 // result in DexCache. The ClassLinker and ClassLoader are used as
81 // in ResolveType. What is unique is the is_static argument which is
82 // used to determine if we are resolving a static or non-static
83 // field.
84 Field* ResolveField(const DexFile& dex_file,
85 uint32_t field_idx,
86 DexCache* dex_cache,
87 const ClassLoader* class_loader,
88 bool is_static);
89
Elliott Hughesf4c21c92011-08-19 17:31:31 -070090 // Returns true on success, false if there's an exception pending.
91 bool EnsureInitialized(Class* c);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070092
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070093 void RegisterDexFile(const DexFile& dex_file);
94 void RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070095
Brian Carlstroma663ea52011-08-19 23:33:41 -070096 const InternTable& GetInternTable() {
97 return intern_table_;
98 }
99
100 void VisitRoots(Heap::RootVistor* root_visitor, void* arg) const;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700101
buzbeec143c552011-08-20 17:38:58 -0700102 const DexFile& FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700103 DexCache* FindDexCache(const DexFile& dex_file) const;
buzbeec143c552011-08-20 17:38:58 -0700104
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700105 private:
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700106 ClassLinker();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700107
Brian Carlstroma663ea52011-08-19 23:33:41 -0700108 // Initialize class linker from DexFile instances.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700109 void Init(const std::vector<const DexFile*>& boot_class_path_);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700110
Brian Carlstroma663ea52011-08-19 23:33:41 -0700111 // Initialize class linker from pre-initialized space.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700112 void Init(const std::vector<const DexFile*>& boot_class_path_, Space* space);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700113 static void InitCallback(Object* obj, void *arg);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700114 struct InitCallbackState;
115
116 void FinishInit();
117
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700118 bool InitializeClass(Class* klass);
119
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700120 // For early bootstrapping by Init
Brian Carlstrom4873d462011-08-21 15:23:39 -0700121 Class* AllocClass(Class* java_lang_Class, size_t class_size);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700122
123 // Alloc* convenience functions to avoid needing to pass in Class*
124 // values that are known to the ClassLinker such as
125 // kObjectArrayClass and kJavaLangString etc.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700126 Class* AllocClass(size_t class_size);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700127 DexCache* AllocDexCache(const DexFile& dex_file);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400128 Field* AllocField();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700129 Method* AllocMethod();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700130 template <class T>
131 ObjectArray<T>* AllocObjectArray(size_t length) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700132 return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700133 }
134
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700135 Class* CreatePrimitiveClass(const char* descriptor);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700136
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700137 Class* CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700138 const ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700139
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700140 void AppendToBootClassPath(const DexFile& dex_file);
141 void AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700142
Brian Carlstrom4873d462011-08-21 15:23:39 -0700143 size_t SizeOfClass(const DexFile& dex_file,
144 const DexFile::ClassDef& dex_class_def);
145
Brian Carlstromf615a612011-07-23 12:50:34 -0700146 void LoadClass(const DexFile& dex_file,
147 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700148 Class* klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700149 const ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700150
Brian Carlstromf615a612011-07-23 12:50:34 -0700151 void LoadInterfaces(const DexFile& dex_file,
152 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700153 Class *klass);
154
Brian Carlstromf615a612011-07-23 12:50:34 -0700155 void LoadField(const DexFile& dex_file,
156 const DexFile::Field& dex_field,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700157 Class* klass,
158 Field* dst);
159
Brian Carlstromf615a612011-07-23 12:50:34 -0700160 void LoadMethod(const DexFile& dex_file,
161 const DexFile::Method& dex_method,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700162 Class* klass,
163 Method* dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700164
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700165 Class* LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700166
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700167 // Inserts a class into the class table. Returns true if the class
168 // was inserted.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700169 bool InsertClass(const StringPiece& descriptor, Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700170
171 bool InitializeSuperClass(Class* klass);
172
173 void InitializeStaticFields(Class* klass);
174
175 bool ValidateSuperClassDescriptors(const Class* klass);
176
177 bool HasSameDescriptorClasses(const char* descriptor,
178 const Class* klass1,
179 const Class* klass2);
180
181 bool HasSameMethodDescriptorClasses(const Method* descriptor,
182 const Class* klass1,
183 const Class* klass2);
184
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700185 bool LinkClass(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700186
187 bool LinkSuperClass(Class* klass);
188
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700189 bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700190
191 bool LinkMethods(Class* klass);
192
193 bool LinkVirtualMethods(Class* klass);
194
195 bool LinkInterfaceMethods(Class* klass);
196
197 void LinkAbstractMethods(Class* klass);
198
Jesse Wilson7833bd22011-08-09 18:31:44 -0400199 bool LinkStaticFields(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700200 bool LinkInstanceFields(Class* klass);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700201 bool LinkFields(size_t field_offset,
202 size_t& num_reference_fields,
203 size_t num_fields,
204 ObjectArray<Field>* fields,
205 size_t& size);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700206
Brian Carlstrom4873d462011-08-21 15:23:39 -0700207 void CreateReferenceInstanceOffsets(Class* klass);
208 void CreateReferenceStaticOffsets(Class* klass);
209 void CreateReferenceOffsets(uint32_t& reference_offsets,
210 size_t num_reference_fields,
211 const ObjectArray<Field>* fields);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700212
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700213 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700214
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700215 std::vector<const DexFile*> dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700216
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700217 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700218
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700219 // multimap from a StringPiece hash code of a class descriptor to
220 // Class* instances. Results should be compared for a matching
221 // Class::descriptor_ and Class::class_loader_.
222 typedef std::tr1::unordered_multimap<size_t, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700223 Table classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700224 Mutex* classes_lock_;
225
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700226 InternTable intern_table_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700227
Brian Carlstroma663ea52011-08-19 23:33:41 -0700228 // indexes into class_roots_.
229 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700230 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700231 kJavaLangClass,
232 kJavaLangObject,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700233 kObjectArrayClass,
234 kJavaLangString,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700235 kJavaLangReflectField,
236 kJavaLangReflectMethod,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700237 kJavaLangClassLoader,
238 kDalvikSystemBaseDexClassLoader,
239 kDalvikSystemPathClassLoader,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700240 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700241 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700242 kPrimitiveChar,
243 kPrimitiveDouble,
244 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700245 kPrimitiveInt,
246 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700247 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700248 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700249 kBooleanArrayClass,
250 kByteArrayClass,
251 kCharArrayClass,
252 kDoubleArrayClass,
253 kFloatArrayClass,
254 kIntArrayClass,
255 kLongArrayClass,
256 kShortArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700257 kClassRootsMax,
258 };
259 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700260
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700261 Class* GetClassRoot(ClassRoot class_root) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700262 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700263 Class* klass = class_roots_->Get(class_root);
264 DCHECK(klass != NULL);
265 return klass;
266 }
267
Brian Carlstroma663ea52011-08-19 23:33:41 -0700268 void SetClassRoot(ClassRoot class_root, Class* klass) {
269 DCHECK(!init_done_);
270
271 DCHECK(klass != NULL);
272 DCHECK(klass->class_loader_ == NULL);
273 DCHECK(klass->descriptor_ != NULL);
274 DCHECK(klass->descriptor_->Equals(GetClassRootDescriptor(class_root)));
275
276 DCHECK(class_roots_ != NULL);
277 DCHECK(class_roots_->Get(class_root) == NULL);
278 class_roots_->Set(class_root, klass);
279 }
280
281 static const char* class_roots_descriptors_[kClassRootsMax];
282
283 const char* GetClassRootDescriptor(ClassRoot class_root) {
284 const char* descriptor = class_roots_descriptors_[class_root];
285 CHECK(descriptor != NULL);
286 return descriptor;
287 }
288
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700289 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700290 InterfaceEntry* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700291
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700292 bool init_done_;
293
Brian Carlstromf734cf52011-08-17 16:28:14 -0700294 friend class CommonTest;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700295 FRIEND_TEST(DexCacheTest, Open);
296 friend class ObjectTest;
297 FRIEND_TEST(ObjectTest, AllocObjectArray);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700298 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700299 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
300};
301
302} // namespace art
303
304#endif // ART_SRC_CLASS_LINKER_H_