blob: eb40d282dab34182cb91f57d096561a3e0a10dec [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 }
Brian Carlstrom83db7722011-08-26 17:32:56 -0700138 CodeAndMethods* AllocCodeAndMethods(size_t length);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700139
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700140 Class* CreatePrimitiveClass(const char* descriptor);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700141
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700142 Class* CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700143 const ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700144
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700145 void AppendToBootClassPath(const DexFile& dex_file);
146 void AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700147
Brian Carlstrom4873d462011-08-21 15:23:39 -0700148 size_t SizeOfClass(const DexFile& dex_file,
149 const DexFile::ClassDef& dex_class_def);
150
Brian Carlstromf615a612011-07-23 12:50:34 -0700151 void LoadClass(const DexFile& dex_file,
152 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700153 Class* klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700154 const ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700155
Brian Carlstromf615a612011-07-23 12:50:34 -0700156 void LoadInterfaces(const DexFile& dex_file,
157 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700158 Class *klass);
159
Brian Carlstromf615a612011-07-23 12:50:34 -0700160 void LoadField(const DexFile& dex_file,
161 const DexFile::Field& dex_field,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700162 Class* klass,
163 Field* dst);
164
Brian Carlstromf615a612011-07-23 12:50:34 -0700165 void LoadMethod(const DexFile& dex_file,
166 const DexFile::Method& dex_method,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700167 Class* klass,
168 Method* dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700169
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700170 Class* LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700171
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700172 // Inserts a class into the class table. Returns true if the class
173 // was inserted.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700174 bool InsertClass(const StringPiece& descriptor, Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700175
176 bool InitializeSuperClass(Class* klass);
177
178 void InitializeStaticFields(Class* klass);
179
180 bool ValidateSuperClassDescriptors(const Class* klass);
181
182 bool HasSameDescriptorClasses(const char* descriptor,
183 const Class* klass1,
184 const Class* klass2);
185
186 bool HasSameMethodDescriptorClasses(const Method* descriptor,
187 const Class* klass1,
188 const Class* klass2);
189
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700190 bool LinkClass(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700191
192 bool LinkSuperClass(Class* klass);
193
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700194 bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700195
196 bool LinkMethods(Class* klass);
197
198 bool LinkVirtualMethods(Class* klass);
199
200 bool LinkInterfaceMethods(Class* klass);
201
202 void LinkAbstractMethods(Class* klass);
203
Jesse Wilson7833bd22011-08-09 18:31:44 -0400204 bool LinkStaticFields(Class* klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700205 bool LinkInstanceFields(Class* klass);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700206 bool LinkFields(size_t field_offset,
207 size_t& num_reference_fields,
208 size_t num_fields,
209 ObjectArray<Field>* fields,
210 size_t& size);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700211
Brian Carlstrom4873d462011-08-21 15:23:39 -0700212 void CreateReferenceInstanceOffsets(Class* klass);
213 void CreateReferenceStaticOffsets(Class* klass);
214 void CreateReferenceOffsets(uint32_t& reference_offsets,
215 size_t num_reference_fields,
216 const ObjectArray<Field>* fields);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700217
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700218 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700219
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700220 std::vector<const DexFile*> dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700221
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700222 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700223
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700224 // multimap from a StringPiece hash code of a class descriptor to
225 // Class* instances. Results should be compared for a matching
226 // Class::descriptor_ and Class::class_loader_.
227 typedef std::tr1::unordered_multimap<size_t, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700228 Table classes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700229 Mutex* classes_lock_;
230
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700231 InternTable intern_table_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700232
Brian Carlstroma663ea52011-08-19 23:33:41 -0700233 // indexes into class_roots_.
234 // needs to be kept in sync with class_roots_descriptors_.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700235 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700236 kJavaLangClass,
237 kJavaLangObject,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700238 kObjectArrayClass,
239 kJavaLangString,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700240 kJavaLangReflectField,
241 kJavaLangReflectMethod,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700242 kJavaLangClassLoader,
243 kDalvikSystemBaseDexClassLoader,
244 kDalvikSystemPathClassLoader,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700245 kJavaLangStackTraceElement,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700246 kPrimitiveBoolean,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700247 kPrimitiveByte,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700248 kPrimitiveChar,
249 kPrimitiveDouble,
250 kPrimitiveFloat,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700251 kPrimitiveInt,
252 kPrimitiveLong,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700253 kPrimitiveShort,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700254 kPrimitiveVoid,
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700255 kBooleanArrayClass,
256 kByteArrayClass,
257 kCharArrayClass,
258 kDoubleArrayClass,
259 kFloatArrayClass,
260 kIntArrayClass,
261 kLongArrayClass,
262 kShortArrayClass,
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700263 kJavaLangStackTraceElementArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700264 kClassRootsMax,
265 };
266 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700267
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700268 Class* GetClassRoot(ClassRoot class_root) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700269 DCHECK(class_roots_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700270 Class* klass = class_roots_->Get(class_root);
271 DCHECK(klass != NULL);
272 return klass;
273 }
274
Brian Carlstroma663ea52011-08-19 23:33:41 -0700275 void SetClassRoot(ClassRoot class_root, Class* klass) {
276 DCHECK(!init_done_);
277
278 DCHECK(klass != NULL);
279 DCHECK(klass->class_loader_ == NULL);
280 DCHECK(klass->descriptor_ != NULL);
281 DCHECK(klass->descriptor_->Equals(GetClassRootDescriptor(class_root)));
282
283 DCHECK(class_roots_ != NULL);
284 DCHECK(class_roots_->Get(class_root) == NULL);
285 class_roots_->Set(class_root, klass);
286 }
287
288 static const char* class_roots_descriptors_[kClassRootsMax];
289
290 const char* GetClassRootDescriptor(ClassRoot class_root) {
291 const char* descriptor = class_roots_descriptors_[class_root];
292 CHECK(descriptor != NULL);
293 return descriptor;
294 }
295
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700296 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700297 InterfaceEntry* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700298
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700299 bool init_done_;
300
Brian Carlstromf734cf52011-08-17 16:28:14 -0700301 friend class CommonTest;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700302 FRIEND_TEST(DexCacheTest, Open);
303 friend class ObjectTest;
304 FRIEND_TEST(ObjectTest, AllocObjectArray);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700305 FRIEND_TEST(ExceptionTest, FindExceptionHandler);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700306 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
307};
308
309} // namespace art
310
311#endif // ART_SRC_CLASS_LINKER_H_