blob: 88e1a67839876c6715079226da27cf49a18ea98e [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
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700107 ObjectArray<StackTraceElement>* AllocStackTraceElementArray(size_t length);
108
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700109 private:
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700110 ClassLinker();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700111
Brian Carlstroma663ea52011-08-19 23:33:41 -0700112 // Initialize class linker from DexFile instances.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700113 void Init(const std::vector<const DexFile*>& boot_class_path_);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700114
Brian Carlstroma663ea52011-08-19 23:33:41 -0700115 // Initialize class linker from pre-initialized space.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700116 void Init(const std::vector<const DexFile*>& boot_class_path_, Space* space);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700117 static void InitCallback(Object* obj, void *arg);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700118 struct InitCallbackState;
119
120 void FinishInit();
121
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700122 bool InitializeClass(Class* klass);
123
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700124 // For early bootstrapping by Init
Brian Carlstrom4873d462011-08-21 15:23:39 -0700125 Class* AllocClass(Class* java_lang_Class, size_t class_size);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700126
127 // Alloc* convenience functions to avoid needing to pass in Class*
128 // values that are known to the ClassLinker such as
129 // kObjectArrayClass and kJavaLangString etc.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700130 Class* AllocClass(size_t class_size);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700131 DexCache* AllocDexCache(const DexFile& dex_file);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400132 Field* AllocField();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700133 Method* AllocMethod();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700134 template <class T>
135 ObjectArray<T>* AllocObjectArray(size_t length) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700136 return ObjectArray<T>::Alloc(GetClassRoot(kObjectArrayClass), length);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700137 }
138
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700139 Class* CreatePrimitiveClass(const char* descriptor);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700140
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700141 Class* CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700142 const ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700143
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700144 void AppendToBootClassPath(const DexFile& dex_file);
145 void AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700146
Brian Carlstrom4873d462011-08-21 15:23:39 -0700147 size_t SizeOfClass(const DexFile& dex_file,
148 const DexFile::ClassDef& dex_class_def);
149
Brian Carlstromf615a612011-07-23 12:50:34 -0700150 void LoadClass(const DexFile& dex_file,
151 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700152 Class* klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700153 const ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700154
Brian Carlstromf615a612011-07-23 12:50:34 -0700155 void LoadInterfaces(const DexFile& dex_file,
156 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700157 Class *klass);
158
Brian Carlstromf615a612011-07-23 12:50:34 -0700159 void LoadField(const DexFile& dex_file,
160 const DexFile::Field& dex_field,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700161 Class* klass,
162 Field* dst);
163
Brian Carlstromf615a612011-07-23 12:50:34 -0700164 void LoadMethod(const DexFile& dex_file,
165 const DexFile::Method& dex_method,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700166 Class* klass,
167 Method* dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700168
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700169 Class* LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700170
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700171 // Inserts a class into the class table. Returns true if the class
172 // was inserted.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700173 bool InsertClass(const StringPiece& descriptor, Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700174
175 bool InitializeSuperClass(Class* klass);
176
177 void InitializeStaticFields(Class* klass);
178
179 bool ValidateSuperClassDescriptors(const Class* klass);
180
181 bool HasSameDescriptorClasses(const char* descriptor,
182 const Class* klass1,
183 const Class* klass2);
184
185 bool HasSameMethodDescriptorClasses(const Method* descriptor,
186 const Class* klass1,
187 const Class* klass2);
188
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700189 bool LinkClass(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700190
191 bool LinkSuperClass(Class* klass);
192
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700193 bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700194
195 bool LinkMethods(Class* klass);
196
197 bool LinkVirtualMethods(Class* klass);
198
199 bool LinkInterfaceMethods(Class* klass);
200
201 void LinkAbstractMethods(Class* klass);
202
Jesse Wilson7833bd22011-08-09 18:31:44 -0400203 bool LinkStaticFields(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700204 bool LinkInstanceFields(Class* klass);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700205 bool LinkFields(size_t field_offset,
206 size_t& num_reference_fields,
207 size_t num_fields,
208 ObjectArray<Field>* fields,
209 size_t& size);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700210
Brian Carlstrom4873d462011-08-21 15:23:39 -0700211 void CreateReferenceInstanceOffsets(Class* klass);
212 void CreateReferenceStaticOffsets(Class* klass);
213 void CreateReferenceOffsets(uint32_t& reference_offsets,
214 size_t num_reference_fields,
215 const ObjectArray<Field>* fields);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700216
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700217 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700218
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700219 std::vector<const DexFile*> dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700220
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700221 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700222
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700223 // multimap from a StringPiece hash code of a class descriptor to
224 // Class* instances. Results should be compared for a matching
225 // Class::descriptor_ and Class::class_loader_.
226 typedef std::tr1::unordered_multimap<size_t, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700227 Table classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700228 Mutex* classes_lock_;
229
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700230 InternTable intern_table_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700231
Brian Carlstroma663ea52011-08-19 23:33:41 -0700232 // indexes into class_roots_.
233 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700234 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700235 kJavaLangClass,
236 kJavaLangObject,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700237 kObjectArrayClass,
238 kJavaLangString,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700239 kJavaLangReflectField,
240 kJavaLangReflectMethod,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700241 kJavaLangClassLoader,
242 kDalvikSystemBaseDexClassLoader,
243 kDalvikSystemPathClassLoader,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700244 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700245 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700246 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700247 kPrimitiveChar,
248 kPrimitiveDouble,
249 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700250 kPrimitiveInt,
251 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700252 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700253 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700254 kBooleanArrayClass,
255 kByteArrayClass,
256 kCharArrayClass,
257 kDoubleArrayClass,
258 kFloatArrayClass,
259 kIntArrayClass,
260 kLongArrayClass,
261 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700262 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700263 kClassRootsMax,
264 };
265 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700266
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700267 Class* GetClassRoot(ClassRoot class_root) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700268 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700269 Class* klass = class_roots_->Get(class_root);
270 DCHECK(klass != NULL);
271 return klass;
272 }
273
Brian Carlstroma663ea52011-08-19 23:33:41 -0700274 void SetClassRoot(ClassRoot class_root, Class* klass) {
275 DCHECK(!init_done_);
276
277 DCHECK(klass != NULL);
278 DCHECK(klass->class_loader_ == NULL);
279 DCHECK(klass->descriptor_ != NULL);
280 DCHECK(klass->descriptor_->Equals(GetClassRootDescriptor(class_root)));
281
282 DCHECK(class_roots_ != NULL);
283 DCHECK(class_roots_->Get(class_root) == NULL);
284 class_roots_->Set(class_root, klass);
285 }
286
287 static const char* class_roots_descriptors_[kClassRootsMax];
288
289 const char* GetClassRootDescriptor(ClassRoot class_root) {
290 const char* descriptor = class_roots_descriptors_[class_root];
291 CHECK(descriptor != NULL);
292 return descriptor;
293 }
294
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700295 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700296 InterfaceEntry* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700297
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700298 bool init_done_;
299
Brian Carlstromf734cf52011-08-17 16:28:14 -0700300 friend class CommonTest;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700301 FRIEND_TEST(DexCacheTest, Open);
302 friend class ObjectTest;
303 FRIEND_TEST(ObjectTest, AllocObjectArray);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700304 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700305 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
306};
307
308} // namespace art
309
310#endif // ART_SRC_CLASS_LINKER_H_