blob: 28dfce7439c47253b3326d35448fbcedca844d7b [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.
26 static ClassLinker* Create(const std::vector<DexFile*>& boot_class_path, Space* boot_space);
Carl Shapiro61e019d2011-07-14 16:53:09 -070027
Elliott Hughesde69d7f2011-08-18 16:49:37 -070028 ~ClassLinker() {
29 delete classes_lock_;
Brian Carlstroma663ea52011-08-19 23:33:41 -070030 String::ResetClass();
31 BooleanArray::ResetArrayClass();
32 ByteArray::ResetArrayClass();
33 CharArray::ResetArrayClass();
34 DoubleArray::ResetArrayClass();
35 FloatArray::ResetArrayClass();
36 IntArray::ResetArrayClass();
37 LongArray::ResetArrayClass();
38 ShortArray::ResetArrayClass();
Elliott Hughesde69d7f2011-08-18 16:49:37 -070039 }
Carl Shapiro565f5072011-07-10 13:39:43 -070040
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070041 // Finds a class by its descriptor name.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070042 // If class_loader is null, searches boot_class_path_.
Brian Carlstrom6cc18452011-07-18 15:10:33 -070043 Class* FindClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070044 ClassLoader* class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070045
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070046 Class* FindPrimitiveClass(char type);
47
Brian Carlstrom6cc18452011-07-18 15:10:33 -070048 Class* FindSystemClass(const StringPiece& descriptor) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070049 return FindClass(descriptor, NULL);
Carl Shapiro565f5072011-07-10 13:39:43 -070050 }
51
Elliott Hughesf4c21c92011-08-19 17:31:31 -070052 // Returns true on success, false if there's an exception pending.
53 bool EnsureInitialized(Class* c);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070054
Brian Carlstrom4a96b602011-07-26 16:40:23 -070055 void RegisterDexFile(const DexFile* dex_file);
Brian Carlstroma663ea52011-08-19 23:33:41 -070056 void RegisterDexFile(const DexFile* dex_file, DexCache* dex_cache);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070057
Brian Carlstroma663ea52011-08-19 23:33:41 -070058 const InternTable& GetInternTable() {
59 return intern_table_;
60 }
61
62 void VisitRoots(Heap::RootVistor* root_visitor, void* arg) const;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070063
buzbeec143c552011-08-20 17:38:58 -070064 const DexFile& FindDexFile(const DexCache* dex_cache) const;
65
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070066 private:
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070067 ClassLinker()
68 : classes_lock_(Mutex::Create("ClassLinker::Lock")),
Brian Carlstroma663ea52011-08-19 23:33:41 -070069 class_roots_(NULL),
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070070 init_done_(false) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -070071 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070072
Brian Carlstroma663ea52011-08-19 23:33:41 -070073 // Initialize class linker from DexFile instances.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070074 void Init(const std::vector<DexFile*>& boot_class_path_);
Carl Shapiro61e019d2011-07-14 16:53:09 -070075
Brian Carlstroma663ea52011-08-19 23:33:41 -070076 // Initialize class linker from pre-initialized space.
77 void Init(const std::vector<DexFile*>& boot_class_path_, Space* space);
Brian Carlstrom4873d462011-08-21 15:23:39 -070078 static void InitCallback(Object* obj, void *arg);
Brian Carlstroma663ea52011-08-19 23:33:41 -070079 struct InitCallbackState;
80
81 void FinishInit();
82
Elliott Hughesf4c21c92011-08-19 17:31:31 -070083 bool InitializeClass(Class* klass);
84
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070085 // For early bootstrapping by Init
Brian Carlstrom4873d462011-08-21 15:23:39 -070086 Class* AllocClass(Class* java_lang_Class, size_t class_size);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070087
88 // Alloc* convenience functions to avoid needing to pass in Class*
89 // values that are known to the ClassLinker such as
90 // kObjectArrayClass and kJavaLangString etc.
Brian Carlstrom4873d462011-08-21 15:23:39 -070091 Class* AllocClass(size_t class_size);
Brian Carlstroma663ea52011-08-19 23:33:41 -070092 DexCache* AllocDexCache(const DexFile* dex_file);
Jesse Wilson35baaab2011-08-10 16:18:03 -040093 Field* AllocField();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070094 Method* AllocMethod();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070095 template <class T>
96 ObjectArray<T>* AllocObjectArray(size_t length) {
Brian Carlstroma663ea52011-08-19 23:33:41 -070097 return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070098 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070099 PathClassLoader* AllocPathClassLoader(std::vector<const DexFile*> dex_files);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700100
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700101 Class* CreatePrimitiveClass(const char* descriptor);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700102
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700103 Class* CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700104 ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700105
Brian Carlstromf615a612011-07-23 12:50:34 -0700106 DexCache* FindDexCache(const DexFile* dex_file) const;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700107
Brian Carlstroma663ea52011-08-19 23:33:41 -0700108 void AppendToBootClassPath(const DexFile* dex_file);
109 void AppendToBootClassPath(const DexFile* dex_file, DexCache* dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700110
Brian Carlstrom4873d462011-08-21 15:23:39 -0700111 size_t SizeOfClass(const DexFile& dex_file,
112 const DexFile::ClassDef& dex_class_def);
113
Brian Carlstromf615a612011-07-23 12:50:34 -0700114 void LoadClass(const DexFile& dex_file,
115 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700116 Class* klass,
117 ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700118
Brian Carlstromf615a612011-07-23 12:50:34 -0700119 void LoadInterfaces(const DexFile& dex_file,
120 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700121 Class *klass);
122
Brian Carlstromf615a612011-07-23 12:50:34 -0700123 void LoadField(const DexFile& dex_file,
124 const DexFile::Field& dex_field,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700125 Class* klass,
126 Field* dst);
127
Brian Carlstromf615a612011-07-23 12:50:34 -0700128 void LoadMethod(const DexFile& dex_file,
129 const DexFile::Method& dex_method,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700130 Class* klass,
131 Method* dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700132
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700133 Class* ResolveClass(const Class* referring,
134 uint32_t class_idx,
135 const DexFile& dex_file);
136
137 String* ResolveString(const Class* referring,
138 uint32_t string_idx,
139 const DexFile& dex_file);
140
141 Class* LookupClass(const StringPiece& descriptor, ClassLoader* class_loader);
142
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700143 // Inserts a class into the class table. Returns true if the class
144 // was inserted.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700145 bool InsertClass(const StringPiece& descriptor, Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700146
147 bool InitializeSuperClass(Class* klass);
148
149 void InitializeStaticFields(Class* klass);
150
151 bool ValidateSuperClassDescriptors(const Class* klass);
152
153 bool HasSameDescriptorClasses(const char* descriptor,
154 const Class* klass1,
155 const Class* klass2);
156
157 bool HasSameMethodDescriptorClasses(const Method* descriptor,
158 const Class* klass1,
159 const Class* klass2);
160
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700161 bool LinkClass(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700162
163 bool LinkSuperClass(Class* klass);
164
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700165 bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700166
167 bool LinkMethods(Class* klass);
168
169 bool LinkVirtualMethods(Class* klass);
170
171 bool LinkInterfaceMethods(Class* klass);
172
173 void LinkAbstractMethods(Class* klass);
174
Jesse Wilson7833bd22011-08-09 18:31:44 -0400175 bool LinkStaticFields(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700176 bool LinkInstanceFields(Class* klass);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700177 bool LinkFields(size_t field_offset,
178 size_t& num_reference_fields,
179 size_t num_fields,
180 ObjectArray<Field>* fields,
181 size_t& size);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700182
Brian Carlstrom4873d462011-08-21 15:23:39 -0700183 void CreateReferenceInstanceOffsets(Class* klass);
184 void CreateReferenceStaticOffsets(Class* klass);
185 void CreateReferenceOffsets(uint32_t& reference_offsets,
186 size_t num_reference_fields,
187 const ObjectArray<Field>* fields);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700188
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700189 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700190
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700191 std::vector<const DexFile*> dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700192
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700193 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700194
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700195 // multimap from a StringPiece hash code of a class descriptor to
196 // Class* instances. Results should be compared for a matching
197 // Class::descriptor_ and Class::class_loader_.
198 typedef std::tr1::unordered_multimap<size_t, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700199 Table classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700200 Mutex* classes_lock_;
201
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700202 InternTable intern_table_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700203
Brian Carlstroma663ea52011-08-19 23:33:41 -0700204 // indexes into class_roots_.
205 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700206 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700207 kJavaLangClass,
208 kJavaLangObject,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700209 kObjectArrayClass,
210 kJavaLangString,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700211 kJavaLangReflectField,
212 kJavaLangReflectMethod,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700213 kJavaLangClassLoader,
214 kDalvikSystemBaseDexClassLoader,
215 kDalvikSystemPathClassLoader,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700216 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700217 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700218 kPrimitiveChar,
219 kPrimitiveDouble,
220 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700221 kPrimitiveInt,
222 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700223 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700224 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700225 kBooleanArrayClass,
226 kByteArrayClass,
227 kCharArrayClass,
228 kDoubleArrayClass,
229 kFloatArrayClass,
230 kIntArrayClass,
231 kLongArrayClass,
232 kShortArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700233 kClassRootsMax,
234 };
235 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700236
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700237 Class* GetClassRoot(ClassRoot class_root) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700238 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700239 Class* klass = class_roots_->Get(class_root);
240 DCHECK(klass != NULL);
241 return klass;
242 }
243
Brian Carlstroma663ea52011-08-19 23:33:41 -0700244 void SetClassRoot(ClassRoot class_root, Class* klass) {
245 DCHECK(!init_done_);
246
247 DCHECK(klass != NULL);
248 DCHECK(klass->class_loader_ == NULL);
249 DCHECK(klass->descriptor_ != NULL);
250 DCHECK(klass->descriptor_->Equals(GetClassRootDescriptor(class_root)));
251
252 DCHECK(class_roots_ != NULL);
253 DCHECK(class_roots_->Get(class_root) == NULL);
254 class_roots_->Set(class_root, klass);
255 }
256
257 static const char* class_roots_descriptors_[kClassRootsMax];
258
259 const char* GetClassRootDescriptor(ClassRoot class_root) {
260 const char* descriptor = class_roots_descriptors_[class_root];
261 CHECK(descriptor != NULL);
262 return descriptor;
263 }
264
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700265 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700266 InterfaceEntry* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700267
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700268 bool init_done_;
269
Brian Carlstromf734cf52011-08-17 16:28:14 -0700270 friend class CommonTest;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700271 FRIEND_TEST(DexCacheTest, Open);
272 friend class ObjectTest;
273 FRIEND_TEST(ObjectTest, AllocObjectArray);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700274 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700275 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
276};
277
278} // namespace art
279
280#endif // ART_SRC_CLASS_LINKER_H_