blob: c8488254a9e9308cfe9cd6d150a22ebad2e278ba [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 Carlstrom9ea1cb12011-08-24 23:18:18 -070043 // Resolve a String with the given ID from the DexFile, storing the
44 // result in the DexCache.
45 String* ResolveString(const DexFile& dex_file,
46 uint32_t string_idx,
47 DexCache* dex_cache);
48
49 // Resolve a Type with the given ID from the DexFile, storing the
50 // 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
61 // Resolve a type with the given ID from the DexFile, storing the
62 // result in DexCache. The ClassLoader is used to search for the
63 // type, since it may be referenced from but not contained within
64 // the given DexFile.
65 Class* ResolveType(const DexFile& dex_file,
66 uint32_t type_idx,
67 DexCache* dex_cache,
68 const ClassLoader* class_loader);
69
70 // Resolve a method with a given ID from the DexFile, storing the
71 // result in DexCache. The ClassLinker and ClassLoader are used as
72 // in ResolveType. What is unique is the method type argument which
73 // is used to determine if this method is a direct, static, or
74 // virtual method.
75 Method* ResolveMethod(const DexFile& dex_file,
76 uint32_t method_idx,
77 DexCache* dex_cache,
78 const ClassLoader* class_loader,
Brian Carlstrom20cfffa2011-08-26 02:31:27 -070079 bool is_direct);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070080
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 is_static argument which is
84 // used to determine if we are resolving a static or non-static
85 // field.
86 Field* ResolveField(const DexFile& dex_file,
87 uint32_t field_idx,
88 DexCache* dex_cache,
89 const ClassLoader* class_loader,
90 bool is_static);
91
Elliott Hughesf4c21c92011-08-19 17:31:31 -070092 // Returns true on success, false if there's an exception pending.
93 bool EnsureInitialized(Class* c);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070094
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070095 void RegisterDexFile(const DexFile& dex_file);
96 void RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070097
Brian Carlstroma663ea52011-08-19 23:33:41 -070098 const InternTable& GetInternTable() {
99 return intern_table_;
100 }
101
102 void VisitRoots(Heap::RootVistor* root_visitor, void* arg) const;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700103
buzbeec143c552011-08-20 17:38:58 -0700104 const DexFile& FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700105 DexCache* FindDexCache(const DexFile& dex_file) const;
buzbeec143c552011-08-20 17:38:58 -0700106
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700107 private:
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700108 ClassLinker();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700109
Brian Carlstroma663ea52011-08-19 23:33:41 -0700110 // Initialize class linker from DexFile instances.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700111 void Init(const std::vector<const DexFile*>& boot_class_path_);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700112
Brian Carlstroma663ea52011-08-19 23:33:41 -0700113 // Initialize class linker from pre-initialized space.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700114 void Init(const std::vector<const DexFile*>& boot_class_path_, Space* space);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700115 static void InitCallback(Object* obj, void *arg);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700116 struct InitCallbackState;
117
118 void FinishInit();
119
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700120 bool InitializeClass(Class* klass);
121
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700122 // For early bootstrapping by Init
Brian Carlstrom4873d462011-08-21 15:23:39 -0700123 Class* AllocClass(Class* java_lang_Class, size_t class_size);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700124
125 // Alloc* convenience functions to avoid needing to pass in Class*
126 // values that are known to the ClassLinker such as
127 // kObjectArrayClass and kJavaLangString etc.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700128 Class* AllocClass(size_t class_size);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700129 DexCache* AllocDexCache(const DexFile& dex_file);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400130 Field* AllocField();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700131 Method* AllocMethod();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700132 template <class T>
133 ObjectArray<T>* AllocObjectArray(size_t length) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700134 return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700135 }
136
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700137 Class* CreatePrimitiveClass(const char* descriptor);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700138
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700139 Class* CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700140 const ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700141
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700142 void AppendToBootClassPath(const DexFile& dex_file);
143 void AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700144
Brian Carlstrom4873d462011-08-21 15:23:39 -0700145 size_t SizeOfClass(const DexFile& dex_file,
146 const DexFile::ClassDef& dex_class_def);
147
Brian Carlstromf615a612011-07-23 12:50:34 -0700148 void LoadClass(const DexFile& dex_file,
149 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700150 Class* klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700151 const ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700152
Brian Carlstromf615a612011-07-23 12:50:34 -0700153 void LoadInterfaces(const DexFile& dex_file,
154 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700155 Class *klass);
156
Brian Carlstromf615a612011-07-23 12:50:34 -0700157 void LoadField(const DexFile& dex_file,
158 const DexFile::Field& dex_field,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700159 Class* klass,
160 Field* dst);
161
Brian Carlstromf615a612011-07-23 12:50:34 -0700162 void LoadMethod(const DexFile& dex_file,
163 const DexFile::Method& dex_method,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700164 Class* klass,
165 Method* dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700166
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700167 Class* LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700168
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700169 // Inserts a class into the class table. Returns true if the class
170 // was inserted.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700171 bool InsertClass(const StringPiece& descriptor, Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700172
173 bool InitializeSuperClass(Class* klass);
174
175 void InitializeStaticFields(Class* klass);
176
177 bool ValidateSuperClassDescriptors(const Class* klass);
178
179 bool HasSameDescriptorClasses(const char* descriptor,
180 const Class* klass1,
181 const Class* klass2);
182
183 bool HasSameMethodDescriptorClasses(const Method* descriptor,
184 const Class* klass1,
185 const Class* klass2);
186
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700187 bool LinkClass(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700188
189 bool LinkSuperClass(Class* klass);
190
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700191 bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700192
193 bool LinkMethods(Class* klass);
194
195 bool LinkVirtualMethods(Class* klass);
196
197 bool LinkInterfaceMethods(Class* klass);
198
199 void LinkAbstractMethods(Class* klass);
200
Jesse Wilson7833bd22011-08-09 18:31:44 -0400201 bool LinkStaticFields(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700202 bool LinkInstanceFields(Class* klass);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700203 bool LinkFields(size_t field_offset,
204 size_t& num_reference_fields,
205 size_t num_fields,
206 ObjectArray<Field>* fields,
207 size_t& size);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700208
Brian Carlstrom4873d462011-08-21 15:23:39 -0700209 void CreateReferenceInstanceOffsets(Class* klass);
210 void CreateReferenceStaticOffsets(Class* klass);
211 void CreateReferenceOffsets(uint32_t& reference_offsets,
212 size_t num_reference_fields,
213 const ObjectArray<Field>* fields);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700214
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700215 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700216
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700217 std::vector<const DexFile*> dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700218
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700219 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700220
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700221 // multimap from a StringPiece hash code of a class descriptor to
222 // Class* instances. Results should be compared for a matching
223 // Class::descriptor_ and Class::class_loader_.
224 typedef std::tr1::unordered_multimap<size_t, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700225 Table classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700226 Mutex* classes_lock_;
227
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700228 InternTable intern_table_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700229
Brian Carlstroma663ea52011-08-19 23:33:41 -0700230 // indexes into class_roots_.
231 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700232 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700233 kJavaLangClass,
234 kJavaLangObject,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700235 kObjectArrayClass,
236 kJavaLangString,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700237 kJavaLangReflectField,
238 kJavaLangReflectMethod,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700239 kJavaLangClassLoader,
240 kDalvikSystemBaseDexClassLoader,
241 kDalvikSystemPathClassLoader,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700242 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700243 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700244 kPrimitiveChar,
245 kPrimitiveDouble,
246 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700247 kPrimitiveInt,
248 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700249 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700250 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700251 kBooleanArrayClass,
252 kByteArrayClass,
253 kCharArrayClass,
254 kDoubleArrayClass,
255 kFloatArrayClass,
256 kIntArrayClass,
257 kLongArrayClass,
258 kShortArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700259 kClassRootsMax,
260 };
261 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700262
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700263 Class* GetClassRoot(ClassRoot class_root) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700264 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700265 Class* klass = class_roots_->Get(class_root);
266 DCHECK(klass != NULL);
267 return klass;
268 }
269
Brian Carlstroma663ea52011-08-19 23:33:41 -0700270 void SetClassRoot(ClassRoot class_root, Class* klass) {
271 DCHECK(!init_done_);
272
273 DCHECK(klass != NULL);
274 DCHECK(klass->class_loader_ == NULL);
275 DCHECK(klass->descriptor_ != NULL);
276 DCHECK(klass->descriptor_->Equals(GetClassRootDescriptor(class_root)));
277
278 DCHECK(class_roots_ != NULL);
279 DCHECK(class_roots_->Get(class_root) == NULL);
280 class_roots_->Set(class_root, klass);
281 }
282
283 static const char* class_roots_descriptors_[kClassRootsMax];
284
285 const char* GetClassRootDescriptor(ClassRoot class_root) {
286 const char* descriptor = class_roots_descriptors_[class_root];
287 CHECK(descriptor != NULL);
288 return descriptor;
289 }
290
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700291 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700292 InterfaceEntry* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700293
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700294 bool init_done_;
295
Brian Carlstromf734cf52011-08-17 16:28:14 -0700296 friend class CommonTest;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700297 FRIEND_TEST(DexCacheTest, Open);
298 friend class ObjectTest;
299 FRIEND_TEST(ObjectTest, AllocObjectArray);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700300 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700301 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
302};
303
304} // namespace art
305
306#endif // ART_SRC_CLASS_LINKER_H_