blob: c85fb4e072587ead194e14bdd69694348e86805d [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.
Brian Carlstromb9edb842011-08-28 16:31:06 -070064 Class* ResolveType(uint32_t type_idx, const Method* referrer) {
Brian Carlstromb63ec392011-08-27 17:38:27 -070065 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
Brian Carlstromb9edb842011-08-28 16:31:06 -070081 static StaticStorageBase* InitializeStaticStorageFromCode(uint32_t type_idx,
82 const Method* referrer);
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070083
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070084 // Resolve a method with a given ID from the DexFile, storing the
85 // result in DexCache. The ClassLinker and ClassLoader are used as
86 // in ResolveType. What is unique is the method type argument which
87 // is used to determine if this method is a direct, static, or
88 // virtual method.
89 Method* ResolveMethod(const DexFile& dex_file,
90 uint32_t method_idx,
91 DexCache* dex_cache,
92 const ClassLoader* class_loader,
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070093 bool is_direct);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070094
Brian Carlstromb9edb842011-08-28 16:31:06 -070095 Field* ResolveField(uint32_t field_idx, const Method* referrer) {
96 Class* declaring_class = referrer->GetDeclaringClass();
97 DexCache* dex_cache = declaring_class->GetDexCache();
98 const ClassLoader* class_loader = declaring_class->GetClassLoader();
99 const DexFile& dex_file = FindDexFile(dex_cache);
100 return ResolveField(dex_file, field_idx, dex_cache, class_loader, true);
101 }
102
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700103 // Resolve a method with a given ID from the DexFile, storing the
104 // result in DexCache. The ClassLinker and ClassLoader are used as
105 // in ResolveType. What is unique is the is_static argument which is
106 // used to determine if we are resolving a static or non-static
107 // field.
108 Field* ResolveField(const DexFile& dex_file,
109 uint32_t field_idx,
110 DexCache* dex_cache,
111 const ClassLoader* class_loader,
112 bool is_static);
113
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700114 // Returns true on success, false if there's an exception pending.
115 bool EnsureInitialized(Class* c);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700116
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700117 void RegisterDexFile(const DexFile& dex_file);
118 void RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700119
Brian Carlstroma663ea52011-08-19 23:33:41 -0700120 const InternTable& GetInternTable() {
121 return intern_table_;
122 }
123
124 void VisitRoots(Heap::RootVistor* root_visitor, void* arg) const;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700125
buzbeec143c552011-08-20 17:38:58 -0700126 const DexFile& FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700127 DexCache* FindDexCache(const DexFile& dex_file) const;
buzbeec143c552011-08-20 17:38:58 -0700128
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700129 ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length);
130
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700131 private:
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700132 ClassLinker();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700133
Brian Carlstroma663ea52011-08-19 23:33:41 -0700134 // Initialize class linker from DexFile instances.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700135 void Init(const std::vector<const DexFile*>& boot_class_path_);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700136
Brian Carlstroma663ea52011-08-19 23:33:41 -0700137 // Initialize class linker from pre-initialized space.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700138 void Init(const std::vector<const DexFile*>& boot_class_path_, Space* space);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700139 static void InitCallback(Object* obj, void *arg);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700140 struct InitCallbackState;
141
142 void FinishInit();
143
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700144 bool InitializeClass(Class* klass);
145
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700146 // For early bootstrapping by Init
Brian Carlstrom4873d462011-08-21 15:23:39 -0700147 Class* AllocClass(Class* java_lang_Class, size_t class_size);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700148
149 // Alloc* convenience functions to avoid needing to pass in Class*
150 // values that are known to the ClassLinker such as
151 // kObjectArrayClass and kJavaLangString etc.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700152 Class* AllocClass(size_t class_size);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700153 DexCache* AllocDexCache(const DexFile& dex_file);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400154 Field* AllocField();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700155 Method* AllocMethod();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700156 template <class T>
157 ObjectArray<T>* AllocObjectArray(size_t length) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700158 return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700159 }
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700160 CodeAndDirectMethods* AllocCodeAndDirectMethods(size_t length);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700161
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700162 Class* CreatePrimitiveClass(const char* descriptor);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700163
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700164 Class* CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700165 const ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700166
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700167 void AppendToBootClassPath(const DexFile& dex_file);
168 void AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700169
Brian Carlstrom4873d462011-08-21 15:23:39 -0700170 size_t SizeOfClass(const DexFile& dex_file,
171 const DexFile::ClassDef& dex_class_def);
172
Brian Carlstromf615a612011-07-23 12:50:34 -0700173 void LoadClass(const DexFile& dex_file,
174 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700175 Class* klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700176 const ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700177
Brian Carlstromf615a612011-07-23 12:50:34 -0700178 void LoadInterfaces(const DexFile& dex_file,
179 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700180 Class *klass);
181
Brian Carlstromf615a612011-07-23 12:50:34 -0700182 void LoadField(const DexFile& dex_file,
183 const DexFile::Field& dex_field,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700184 Class* klass,
185 Field* dst);
186
Brian Carlstromf615a612011-07-23 12:50:34 -0700187 void LoadMethod(const DexFile& dex_file,
188 const DexFile::Method& dex_method,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700189 Class* klass,
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700190 Method* dst,
191 bool is_direct);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700192
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700193 Class* LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700194
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700195 // Inserts a class into the class table. Returns true if the class
196 // was inserted.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700197 bool InsertClass(const StringPiece& descriptor, Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700198
199 bool InitializeSuperClass(Class* klass);
200
201 void InitializeStaticFields(Class* klass);
202
203 bool ValidateSuperClassDescriptors(const Class* klass);
204
205 bool HasSameDescriptorClasses(const char* descriptor,
206 const Class* klass1,
207 const Class* klass2);
208
209 bool HasSameMethodDescriptorClasses(const Method* descriptor,
210 const Class* klass1,
211 const Class* klass2);
212
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700213 bool LinkClass(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700214
215 bool LinkSuperClass(Class* klass);
216
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700217 bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700218
219 bool LinkMethods(Class* klass);
220
221 bool LinkVirtualMethods(Class* klass);
222
223 bool LinkInterfaceMethods(Class* klass);
224
225 void LinkAbstractMethods(Class* klass);
226
Jesse Wilson7833bd22011-08-09 18:31:44 -0400227 bool LinkStaticFields(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700228 bool LinkInstanceFields(Class* klass);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700229 bool LinkFields(size_t field_offset,
230 size_t& num_reference_fields,
231 size_t num_fields,
232 ObjectArray<Field>* fields,
233 size_t& size);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700234
Brian Carlstrom4873d462011-08-21 15:23:39 -0700235 void CreateReferenceInstanceOffsets(Class* klass);
236 void CreateReferenceStaticOffsets(Class* klass);
237 void CreateReferenceOffsets(uint32_t& reference_offsets,
238 size_t num_reference_fields,
239 const ObjectArray<Field>* fields);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700240
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700241 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700242
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700243 std::vector<const DexFile*> dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700244
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700245 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700246
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700247 // multimap from a StringPiece hash code of a class descriptor to
248 // Class* instances. Results should be compared for a matching
249 // Class::descriptor_ and Class::class_loader_.
250 typedef std::tr1::unordered_multimap<size_t, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700251 Table classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700252 Mutex* classes_lock_;
253
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700254 InternTable intern_table_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700255
Brian Carlstroma663ea52011-08-19 23:33:41 -0700256 // indexes into class_roots_.
257 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700258 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700259 kJavaLangClass,
260 kJavaLangObject,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700261 kObjectArrayClass,
262 kJavaLangString,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700263 kJavaLangReflectField,
264 kJavaLangReflectMethod,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700265 kJavaLangClassLoader,
266 kDalvikSystemBaseDexClassLoader,
267 kDalvikSystemPathClassLoader,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700268 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700269 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700270 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700271 kPrimitiveChar,
272 kPrimitiveDouble,
273 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700274 kPrimitiveInt,
275 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700276 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700277 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700278 kBooleanArrayClass,
279 kByteArrayClass,
280 kCharArrayClass,
281 kDoubleArrayClass,
282 kFloatArrayClass,
283 kIntArrayClass,
284 kLongArrayClass,
285 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700286 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700287 kClassRootsMax,
288 };
289 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700290
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700291 Class* GetClassRoot(ClassRoot class_root) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700292 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700293 Class* klass = class_roots_->Get(class_root);
294 DCHECK(klass != NULL);
295 return klass;
296 }
297
Brian Carlstroma663ea52011-08-19 23:33:41 -0700298 void SetClassRoot(ClassRoot class_root, Class* klass) {
299 DCHECK(!init_done_);
300
301 DCHECK(klass != NULL);
302 DCHECK(klass->class_loader_ == NULL);
303 DCHECK(klass->descriptor_ != NULL);
304 DCHECK(klass->descriptor_->Equals(GetClassRootDescriptor(class_root)));
305
306 DCHECK(class_roots_ != NULL);
307 DCHECK(class_roots_->Get(class_root) == NULL);
308 class_roots_->Set(class_root, klass);
309 }
310
311 static const char* class_roots_descriptors_[kClassRootsMax];
312
313 const char* GetClassRootDescriptor(ClassRoot class_root) {
314 const char* descriptor = class_roots_descriptors_[class_root];
315 CHECK(descriptor != NULL);
316 return descriptor;
317 }
318
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700319 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700320 InterfaceEntry* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700321
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700322 bool init_done_;
323
Brian Carlstromf734cf52011-08-17 16:28:14 -0700324 friend class CommonTest;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700325 FRIEND_TEST(DexCacheTest, Open);
326 friend class ObjectTest;
327 FRIEND_TEST(ObjectTest, AllocObjectArray);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700328 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700329 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
330};
331
332} // namespace art
333
334#endif // ART_SRC_CLASS_LINKER_H_