blob: 306613f87e06ec02fc97a40bcc3cdd4ec956381d [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 Carlstrom83db7722011-08-26 17:32:56 -0700149 CodeAndMethods* AllocCodeAndMethods(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,
179 Method* dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700180
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700181 Class* LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700182
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700183 // Inserts a class into the class table. Returns true if the class
184 // was inserted.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700185 bool InsertClass(const StringPiece& descriptor, Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700186
187 bool InitializeSuperClass(Class* klass);
188
189 void InitializeStaticFields(Class* klass);
190
191 bool ValidateSuperClassDescriptors(const Class* klass);
192
193 bool HasSameDescriptorClasses(const char* descriptor,
194 const Class* klass1,
195 const Class* klass2);
196
197 bool HasSameMethodDescriptorClasses(const Method* descriptor,
198 const Class* klass1,
199 const Class* klass2);
200
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700201 bool LinkClass(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700202
203 bool LinkSuperClass(Class* klass);
204
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700205 bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700206
207 bool LinkMethods(Class* klass);
208
209 bool LinkVirtualMethods(Class* klass);
210
211 bool LinkInterfaceMethods(Class* klass);
212
213 void LinkAbstractMethods(Class* klass);
214
Jesse Wilson7833bd22011-08-09 18:31:44 -0400215 bool LinkStaticFields(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700216 bool LinkInstanceFields(Class* klass);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700217 bool LinkFields(size_t field_offset,
218 size_t& num_reference_fields,
219 size_t num_fields,
220 ObjectArray<Field>* fields,
221 size_t& size);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700222
Brian Carlstrom4873d462011-08-21 15:23:39 -0700223 void CreateReferenceInstanceOffsets(Class* klass);
224 void CreateReferenceStaticOffsets(Class* klass);
225 void CreateReferenceOffsets(uint32_t& reference_offsets,
226 size_t num_reference_fields,
227 const ObjectArray<Field>* fields);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700228
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700229 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700230
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700231 std::vector<const DexFile*> dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700232
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700233 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700234
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700235 // multimap from a StringPiece hash code of a class descriptor to
236 // Class* instances. Results should be compared for a matching
237 // Class::descriptor_ and Class::class_loader_.
238 typedef std::tr1::unordered_multimap<size_t, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700239 Table classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700240 Mutex* classes_lock_;
241
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700242 InternTable intern_table_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700243
Brian Carlstroma663ea52011-08-19 23:33:41 -0700244 // indexes into class_roots_.
245 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700246 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700247 kJavaLangClass,
248 kJavaLangObject,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700249 kObjectArrayClass,
250 kJavaLangString,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700251 kJavaLangReflectField,
252 kJavaLangReflectMethod,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700253 kJavaLangClassLoader,
254 kDalvikSystemBaseDexClassLoader,
255 kDalvikSystemPathClassLoader,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700256 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700257 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700258 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700259 kPrimitiveChar,
260 kPrimitiveDouble,
261 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700262 kPrimitiveInt,
263 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700264 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700265 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700266 kBooleanArrayClass,
267 kByteArrayClass,
268 kCharArrayClass,
269 kDoubleArrayClass,
270 kFloatArrayClass,
271 kIntArrayClass,
272 kLongArrayClass,
273 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700274 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700275 kClassRootsMax,
276 };
277 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700278
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700279 Class* GetClassRoot(ClassRoot class_root) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700280 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700281 Class* klass = class_roots_->Get(class_root);
282 DCHECK(klass != NULL);
283 return klass;
284 }
285
Brian Carlstroma663ea52011-08-19 23:33:41 -0700286 void SetClassRoot(ClassRoot class_root, Class* klass) {
287 DCHECK(!init_done_);
288
289 DCHECK(klass != NULL);
290 DCHECK(klass->class_loader_ == NULL);
291 DCHECK(klass->descriptor_ != NULL);
292 DCHECK(klass->descriptor_->Equals(GetClassRootDescriptor(class_root)));
293
294 DCHECK(class_roots_ != NULL);
295 DCHECK(class_roots_->Get(class_root) == NULL);
296 class_roots_->Set(class_root, klass);
297 }
298
299 static const char* class_roots_descriptors_[kClassRootsMax];
300
301 const char* GetClassRootDescriptor(ClassRoot class_root) {
302 const char* descriptor = class_roots_descriptors_[class_root];
303 CHECK(descriptor != NULL);
304 return descriptor;
305 }
306
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700307 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700308 InterfaceEntry* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700309
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700310 bool init_done_;
311
Brian Carlstromf734cf52011-08-17 16:28:14 -0700312 friend class CommonTest;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700313 FRIEND_TEST(DexCacheTest, Open);
314 friend class ObjectTest;
315 FRIEND_TEST(ObjectTest, AllocObjectArray);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700316 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700317 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
318};
319
320} // namespace art
321
322#endif // ART_SRC_CLASS_LINKER_H_