blob: 1f233b0c8e33b8e2e8a21f914e83bebe837b64f1 [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
Brian Carlstrom1f870082011-08-23 16:02:11 -070021class ClassLoader;
22
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070023namespace art {
24
25class ClassLinker {
26 public:
Brian Carlstroma663ea52011-08-19 23:33:41 -070027 // Initializes the class linker using DexFile and an optional boot Space.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070028 static ClassLinker* Create(const std::vector<const DexFile*>& boot_class_path, Space* boot_space);
Carl Shapiro61e019d2011-07-14 16:53:09 -070029
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070030 ~ClassLinker();
Carl Shapiro565f5072011-07-10 13:39:43 -070031
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070032 // Finds a class by its descriptor name.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070033 // If class_loader is null, searches boot_class_path_.
Brian Carlstrom6cc18452011-07-18 15:10:33 -070034 Class* FindClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070035 const ClassLoader* class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070036
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070037 Class* FindPrimitiveClass(char type);
38
Brian Carlstrom6cc18452011-07-18 15:10:33 -070039 Class* FindSystemClass(const StringPiece& descriptor) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070040 return FindClass(descriptor, NULL);
Carl Shapiro565f5072011-07-10 13:39:43 -070041 }
42
Elliott Hughese27955c2011-08-26 15:21:24 -070043 size_t NumLoadedClasses() const;
44
Brian Carlstromb63ec392011-08-27 17:38:27 -070045 // Resolve a String with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070046 // result in the DexCache.
47 String* ResolveString(const DexFile& dex_file,
48 uint32_t string_idx,
49 DexCache* dex_cache);
50
Brian Carlstromb63ec392011-08-27 17:38:27 -070051 // Resolve a Type with the given index from the DexFile, storing the
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070052 // result in the DexCache. The referrer is used to identity the
53 // target DexCache and ClassLoader to use for resolution.
54 Class* ResolveType(const DexFile& dex_file,
55 uint32_t type_idx,
56 const Class* referrer) {
57 return ResolveType(dex_file,
58 type_idx,
59 referrer->GetDexCache(),
60 referrer->GetClassLoader());
61 }
62
Brian Carlstromb63ec392011-08-27 17:38:27 -070063 // Resolve a Type with the given index from the DexFile, storing the
64 // result in the DexCache. The referrer is used to identity the
65 // target DexCache and ClassLoader to use for resolution.
Brian Carlstromb9edb842011-08-28 16:31:06 -070066 Class* ResolveType(uint32_t type_idx, const Method* referrer) {
Brian Carlstromb63ec392011-08-27 17:38:27 -070067 Class* declaring_class = referrer->GetDeclaringClass();
68 DexCache* dex_cache = declaring_class->GetDexCache();
69 const ClassLoader* class_loader = declaring_class->GetClassLoader();
70 const DexFile& dex_file = FindDexFile(dex_cache);
71 return ResolveType(dex_file, type_idx, dex_cache, class_loader);
72 }
73
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070074 // Resolve a type with the given ID from the DexFile, storing the
75 // result in DexCache. The ClassLoader is used to search for the
76 // type, since it may be referenced from but not contained within
77 // the given DexFile.
78 Class* ResolveType(const DexFile& dex_file,
79 uint32_t type_idx,
80 DexCache* dex_cache,
81 const ClassLoader* class_loader);
82
Brian Carlstromb9edb842011-08-28 16:31:06 -070083 static StaticStorageBase* InitializeStaticStorageFromCode(uint32_t type_idx,
84 const Method* referrer);
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070085
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070086 // Resolve a method with a given ID from the DexFile, storing the
87 // result in DexCache. The ClassLinker and ClassLoader are used as
88 // in ResolveType. What is unique is the method type argument which
89 // is used to determine if this method is a direct, static, or
90 // virtual method.
91 Method* ResolveMethod(const DexFile& dex_file,
92 uint32_t method_idx,
93 DexCache* dex_cache,
94 const ClassLoader* class_loader,
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070095 bool is_direct);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070096
Brian Carlstromb9edb842011-08-28 16:31:06 -070097 Field* ResolveField(uint32_t field_idx, const Method* referrer) {
98 Class* declaring_class = referrer->GetDeclaringClass();
99 DexCache* dex_cache = declaring_class->GetDexCache();
100 const ClassLoader* class_loader = declaring_class->GetClassLoader();
101 const DexFile& dex_file = FindDexFile(dex_cache);
102 return ResolveField(dex_file, field_idx, dex_cache, class_loader, true);
103 }
104
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700105 // Resolve a method with a given ID from the DexFile, storing the
106 // result in DexCache. The ClassLinker and ClassLoader are used as
107 // in ResolveType. What is unique is the is_static argument which is
108 // used to determine if we are resolving a static or non-static
109 // field.
110 Field* ResolveField(const DexFile& dex_file,
111 uint32_t field_idx,
112 DexCache* dex_cache,
113 const ClassLoader* class_loader,
114 bool is_static);
115
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700116 // Returns true on success, false if there's an exception pending.
117 bool EnsureInitialized(Class* c);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700118
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700119 void RegisterDexFile(const DexFile& dex_file);
120 void RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700121
Brian Carlstrom8a487412011-08-29 20:08:52 -0700122 const std::vector<const DexFile*>& GetBootClassPath() {
123 return boot_class_path_;
124 }
125
Brian Carlstroma663ea52011-08-19 23:33:41 -0700126 const InternTable& GetInternTable() {
127 return intern_table_;
128 }
129
130 void VisitRoots(Heap::RootVistor* root_visitor, void* arg) const;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700131
buzbeec143c552011-08-20 17:38:58 -0700132 const DexFile& FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700133 DexCache* FindDexCache(const DexFile& dex_file) const;
buzbeec143c552011-08-20 17:38:58 -0700134
Shih-wei Liao44175362011-08-28 16:59:17 -0700135 template <class T>
136 ObjectArray<T>* AllocObjectArray(size_t length) {
137 return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
138 }
139
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700140 ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length);
141
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700142 private:
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700143 ClassLinker();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700144
Brian Carlstroma663ea52011-08-19 23:33:41 -0700145 // Initialize class linker from DexFile instances.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700146 void Init(const std::vector<const DexFile*>& boot_class_path_);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700147
Brian Carlstroma663ea52011-08-19 23:33:41 -0700148 // Initialize class linker from pre-initialized space.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700149 void Init(const std::vector<const DexFile*>& boot_class_path_, Space* space);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700150 static void InitCallback(Object* obj, void *arg);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700151 struct InitCallbackState;
152
153 void FinishInit();
154
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700155 bool InitializeClass(Class* klass);
156
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700157 // For early bootstrapping by Init
Brian Carlstrom4873d462011-08-21 15:23:39 -0700158 Class* AllocClass(Class* java_lang_Class, size_t class_size);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700159
160 // Alloc* convenience functions to avoid needing to pass in Class*
161 // values that are known to the ClassLinker such as
162 // kObjectArrayClass and kJavaLangString etc.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700163 Class* AllocClass(size_t class_size);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700164 DexCache* AllocDexCache(const DexFile& dex_file);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400165 Field* AllocField();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700166 Method* AllocMethod();
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700167 CodeAndDirectMethods* AllocCodeAndDirectMethods(size_t length);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700168
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700169 Class* CreatePrimitiveClass(const char* descriptor);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700170
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700171 Class* CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700172 const ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700173
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700174 void AppendToBootClassPath(const DexFile& dex_file);
175 void AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700176
Brian Carlstrom4873d462011-08-21 15:23:39 -0700177 size_t SizeOfClass(const DexFile& dex_file,
178 const DexFile::ClassDef& dex_class_def);
179
Brian Carlstromf615a612011-07-23 12:50:34 -0700180 void LoadClass(const DexFile& dex_file,
181 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700182 Class* klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700183 const ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700184
Brian Carlstromf615a612011-07-23 12:50:34 -0700185 void LoadInterfaces(const DexFile& dex_file,
186 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700187 Class *klass);
188
Brian Carlstromf615a612011-07-23 12:50:34 -0700189 void LoadField(const DexFile& dex_file,
190 const DexFile::Field& dex_field,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700191 Class* klass,
192 Field* dst);
193
Brian Carlstromf615a612011-07-23 12:50:34 -0700194 void LoadMethod(const DexFile& dex_file,
195 const DexFile::Method& dex_method,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700196 Class* klass,
Brian Carlstrom1f870082011-08-23 16:02:11 -0700197 Method* dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700198
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700199 Class* LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700200
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700201 // Inserts a class into the class table. Returns true if the class
202 // was inserted.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700203 bool InsertClass(const StringPiece& descriptor, Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700204
205 bool InitializeSuperClass(Class* klass);
206
207 void InitializeStaticFields(Class* klass);
208
209 bool ValidateSuperClassDescriptors(const Class* klass);
210
211 bool HasSameDescriptorClasses(const char* descriptor,
212 const Class* klass1,
213 const Class* klass2);
214
215 bool HasSameMethodDescriptorClasses(const Method* descriptor,
216 const Class* klass1,
217 const Class* klass2);
218
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700219 bool LinkClass(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700220
221 bool LinkSuperClass(Class* klass);
222
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700223 bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700224
225 bool LinkMethods(Class* klass);
226
227 bool LinkVirtualMethods(Class* klass);
228
229 bool LinkInterfaceMethods(Class* klass);
230
231 void LinkAbstractMethods(Class* klass);
232
Jesse Wilson7833bd22011-08-09 18:31:44 -0400233 bool LinkStaticFields(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700234 bool LinkInstanceFields(Class* klass);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700235 bool LinkFields(size_t field_offset,
236 size_t& num_reference_fields,
237 size_t num_fields,
238 ObjectArray<Field>* fields,
239 size_t& size);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700240
Brian Carlstrom4873d462011-08-21 15:23:39 -0700241 void CreateReferenceInstanceOffsets(Class* klass);
242 void CreateReferenceStaticOffsets(Class* klass);
243 void CreateReferenceOffsets(uint32_t& reference_offsets,
244 size_t num_reference_fields,
245 const ObjectArray<Field>* fields);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700246
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700247 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700248
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700249 std::vector<const DexFile*> dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700250
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700251 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700252
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700253 // multimap from a StringPiece hash code of a class descriptor to
254 // Class* instances. Results should be compared for a matching
255 // Class::descriptor_ and Class::class_loader_.
256 typedef std::tr1::unordered_multimap<size_t, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700257 Table classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700258 Mutex* classes_lock_;
259
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700260 InternTable intern_table_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700261
Brian Carlstroma663ea52011-08-19 23:33:41 -0700262 // indexes into class_roots_.
263 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700264 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700265 kJavaLangClass,
266 kJavaLangObject,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700267 kObjectArrayClass,
268 kJavaLangString,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700269 kJavaLangReflectField,
270 kJavaLangReflectMethod,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700271 kJavaLangClassLoader,
272 kDalvikSystemBaseDexClassLoader,
273 kDalvikSystemPathClassLoader,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700274 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700275 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700276 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700277 kPrimitiveChar,
278 kPrimitiveDouble,
279 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700280 kPrimitiveInt,
281 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700282 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700283 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700284 kBooleanArrayClass,
285 kByteArrayClass,
286 kCharArrayClass,
287 kDoubleArrayClass,
288 kFloatArrayClass,
289 kIntArrayClass,
290 kLongArrayClass,
291 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700292 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700293 kClassRootsMax,
294 };
295 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700296
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700297 Class* GetClassRoot(ClassRoot class_root) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700298 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700299 Class* klass = class_roots_->Get(class_root);
300 DCHECK(klass != NULL);
301 return klass;
302 }
303
Brian Carlstroma663ea52011-08-19 23:33:41 -0700304 void SetClassRoot(ClassRoot class_root, Class* klass) {
305 DCHECK(!init_done_);
306
307 DCHECK(klass != NULL);
308 DCHECK(klass->class_loader_ == NULL);
309 DCHECK(klass->descriptor_ != NULL);
310 DCHECK(klass->descriptor_->Equals(GetClassRootDescriptor(class_root)));
311
312 DCHECK(class_roots_ != NULL);
313 DCHECK(class_roots_->Get(class_root) == NULL);
314 class_roots_->Set(class_root, klass);
315 }
316
317 static const char* class_roots_descriptors_[kClassRootsMax];
318
319 const char* GetClassRootDescriptor(ClassRoot class_root) {
320 const char* descriptor = class_roots_descriptors_[class_root];
321 CHECK(descriptor != NULL);
322 return descriptor;
323 }
324
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700325 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700326 InterfaceEntry* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700327
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700328 bool init_done_;
329
Brian Carlstromf734cf52011-08-17 16:28:14 -0700330 friend class CommonTest;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700331 FRIEND_TEST(DexCacheTest, Open);
332 friend class ObjectTest;
333 FRIEND_TEST(ObjectTest, AllocObjectArray);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700334 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700335 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
336};
337
338} // namespace art
339
340#endif // ART_SRC_CLASS_LINKER_H_