blob: 63791135c6caa275430d425688925217b7921186 [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "class_linker.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004
5#include <vector>
6#include <utility>
7
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07008#include "casts.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07009#include "dex_cache.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "dex_verifier.h"
11#include "heap.h"
12#include "logging.h"
13#include "monitor.h"
14#include "object.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070015#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070016#include "scoped_ptr.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070017#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070018#include "thread.h"
19#include "utils.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070020
21namespace art {
22
Brian Carlstroma663ea52011-08-19 23:33:41 -070023const char* ClassLinker::class_roots_descriptors_[kClassRootsMax] = {
24 "Ljava/lang/Class;",
25 "Ljava/lang/Object;",
26 "[Ljava/lang/Object;",
27 "Ljava/lang/String;",
28 "Ljava/lang/reflect/Field;",
29 "Ljava/lang/reflect/Method;",
30 "Ljava/lang/ClassLoader;",
31 "Ldalvik/system/BaseDexClassLoader;",
32 "Ldalvik/system/PathClassLoader;",
33 "Z",
34 "B",
35 "C",
36 "D",
37 "F",
38 "I",
39 "J",
40 "S",
41 "V",
42 "[Z",
43 "[B",
44 "[C",
45 "[D",
46 "[F",
47 "[I",
48 "[J",
49 "[S",
50};
51
52ClassLinker* ClassLinker::Create(const std::vector<DexFile*>& boot_class_path, Space* space) {
Carl Shapiro61e019d2011-07-14 16:53:09 -070053 scoped_ptr<ClassLinker> class_linker(new ClassLinker);
Brian Carlstroma663ea52011-08-19 23:33:41 -070054 if (space == NULL) {
55 class_linker->Init(boot_class_path);
56 } else {
57 class_linker->Init(boot_class_path, space);
58 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070059 // TODO: check for failure during initialization
60 return class_linker.release();
61}
62
Brian Carlstroma663ea52011-08-19 23:33:41 -070063
Carl Shapiro2ed144c2011-07-26 16:52:08 -070064void ClassLinker::Init(const std::vector<DexFile*>& boot_class_path) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070065 CHECK(!init_done_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070066
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070067 // java_lang_Class comes first, its needed for AllocClass
68 Class* java_lang_Class = down_cast<Class*>(Heap::AllocObject(NULL, sizeof(Class)));
69 CHECK(java_lang_Class != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070070 java_lang_Class->object_size_ = sizeof(Class);
71 java_lang_Class->klass_ = java_lang_Class;
72 // AllocClass(Class*) can now be used
Brian Carlstroma0808032011-07-18 00:39:23 -070073
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070074 // java_lang_Object comes next so that object_array_class can be created
75 Class* java_lang_Object = AllocClass(java_lang_Class);
76 CHECK(java_lang_Object != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070077 // backfill Object as the super class of Class
78 java_lang_Class->super_class_ = java_lang_Object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070079 // mark as non-primitive for object_array_class
80 java_lang_Object->primitive_type_ = Class::kPrimNot;
Brian Carlstroma0808032011-07-18 00:39:23 -070081
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070082 // object_array_class is for root_classes to provide the storage for these classes
83 Class* object_array_class = AllocClass(java_lang_Class);
84 CHECK(object_array_class != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070085 object_array_class->component_type_ = java_lang_Object;
Brian Carlstroma0808032011-07-18 00:39:23 -070086
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070087 // String and char[] are necessary so that FindClass can assign names to members
Jesse Wilson14150742011-07-29 19:04:44 -040088 Class* java_lang_String = AllocClass(java_lang_Class);
89 CHECK(java_lang_String != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070090 CHECK_LT(java_lang_String->object_size_, sizeof(String));
Jesse Wilson14150742011-07-29 19:04:44 -040091 java_lang_String->object_size_ = sizeof(String);
Brian Carlstroma663ea52011-08-19 23:33:41 -070092 String::SetClass(java_lang_String);
Jesse Wilson8989d992011-08-02 13:39:42 -070093 Class* char_array_class = AllocClass(java_lang_Class);
94 CHECK(char_array_class != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070095 CharArray::SetArrayClass(char_array_class);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070096 // Now String::Alloc* can be used
97
98 // backfill Class descriptors missing until this point
99 java_lang_Class->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/Class;");
100 java_lang_Object->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/Object;");
101 object_array_class->descriptor_ = String::AllocFromModifiedUtf8("[Ljava/lang/Object;");
102 java_lang_String->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/String;");
103 char_array_class->descriptor_ = String::AllocFromModifiedUtf8("[C");
Jesse Wilson14150742011-07-29 19:04:44 -0400104
Jesse Wilson7833bd22011-08-09 18:31:44 -0400105 // int[] and long[] are used for static field storage
106 Class* int_array_class = AllocClass(java_lang_Class);
107 CHECK(int_array_class != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700108 int_array_class->descriptor_ = String::AllocFromModifiedUtf8("[I");
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700109 IntArray::SetArrayClass(int_array_class);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400110 Class* long_array_class = AllocClass(java_lang_Class);
111 CHECK(long_array_class != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700112 long_array_class->descriptor_ = String::AllocFromModifiedUtf8("[J");
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700113 LongArray::SetArrayClass(long_array_class);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400114
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700115 // Field and Method are necessary so that FindClass can link members
116 Class* java_lang_reflect_Field = AllocClass(java_lang_Class);
117 CHECK(java_lang_reflect_Field != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700118 java_lang_reflect_Field->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/reflect/Field;");
Jesse Wilson35baaab2011-08-10 16:18:03 -0400119 CHECK_LT(java_lang_reflect_Field->object_size_, sizeof(Field));
120 java_lang_reflect_Field->object_size_ = sizeof(Field);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700121 Class* java_lang_reflect_Method = AllocClass(java_lang_Class);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700122 java_lang_reflect_Method->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/reflect/Method;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700123 CHECK(java_lang_reflect_Method != NULL);
124 CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method));
125 java_lang_reflect_Method->object_size_ = sizeof(Method);
126
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700127 // create storage for root classes, save away our work so far
128 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700129 SetClassRoot(kJavaLangClass, java_lang_Class);
130 SetClassRoot(kJavaLangObject, java_lang_Object);
131 SetClassRoot(kObjectArrayClass, object_array_class);
132 SetClassRoot(kJavaLangString, java_lang_String);
133 SetClassRoot(kCharArrayClass, char_array_class);
134 SetClassRoot(kIntArrayClass, int_array_class);
135 SetClassRoot(kLongArrayClass, long_array_class);
136 SetClassRoot(kJavaLangReflectField, java_lang_reflect_Field);
137 SetClassRoot(kJavaLangReflectMethod, java_lang_reflect_Method);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700138 // now that these are registered, we can use AllocClass() and AllocObjectArray
Brian Carlstroma0808032011-07-18 00:39:23 -0700139
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700140 // setup boot_class_path_ now that we can use AllocObjectArray to
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700141 // create DexCache instances
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700142 for (size_t i = 0; i != boot_class_path.size(); ++i) {
143 AppendToBootClassPath(boot_class_path[i]);
144 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700145 // now we can use FindSystemClass, at least for non-arrays classes.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700146
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700147 // run Class, Field, and Method through FindSystemClass.
148 // this initializes their dex_cache_ fields and register them in classes_.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700149 // we also override their object_size_ values to accommodate the extra C++ fields.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700150 Class* Class_class = FindSystemClass("Ljava/lang/Class;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700151 CHECK_EQ(java_lang_Class, Class_class);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700152 CHECK_LT(java_lang_Class->object_size_, sizeof(Class));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700153 java_lang_Class->object_size_ = sizeof(Class);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700154 Class* Field_class = FindSystemClass("Ljava/lang/reflect/Field;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700155 CHECK_EQ(java_lang_reflect_Field, Field_class);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400156 CHECK_LT(java_lang_reflect_Field->object_size_, sizeof(Field));
157 java_lang_reflect_Field->object_size_ = sizeof(Field);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700158 Class* Method_class = FindSystemClass("Ljava/lang/reflect/Method;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700159 CHECK_EQ(java_lang_reflect_Method, Method_class);
160 CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700161 java_lang_reflect_Method->object_size_ = sizeof(Method);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700162
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700163 // Object and String just need more minimal setup, since they do not have extra C++ fields.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700164 Class* Object_class = FindSystemClass("Ljava/lang/Object;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700165 CHECK_EQ(java_lang_Object, Object_class);
166 CHECK_EQ(java_lang_Object->object_size_, sizeof(Object));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700167 Class* String_class = FindSystemClass("Ljava/lang/String;");
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700168 CHECK_EQ(java_lang_String, String_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700169 CHECK_EQ(java_lang_String->object_size_, sizeof(String));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700170
171 // Setup the ClassLoaders, adjusting the object_size_ as necessary
172 Class* java_lang_ClassLoader = FindSystemClass("Ljava/lang/ClassLoader;");
173 CHECK(java_lang_ClassLoader != NULL);
174 CHECK_LT(java_lang_ClassLoader->object_size_, sizeof(ClassLoader));
175 java_lang_ClassLoader->object_size_ = sizeof(ClassLoader);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700176 SetClassRoot(kJavaLangClassLoader, java_lang_ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700177 Class* dalvik_system_BaseDexClassLoader = FindSystemClass("Ldalvik/system/BaseDexClassLoader;");
178 CHECK(dalvik_system_BaseDexClassLoader != NULL);
179 CHECK_EQ(dalvik_system_BaseDexClassLoader->object_size_, sizeof(BaseDexClassLoader));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700180 SetClassRoot(kDalvikSystemBaseDexClassLoader, dalvik_system_BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700181 Class* dalvik_system_PathClassLoader = FindSystemClass("Ldalvik/system/PathClassLoader;");
182 CHECK(dalvik_system_PathClassLoader != NULL);
183 CHECK_EQ(dalvik_system_PathClassLoader->object_size_, sizeof(PathClassLoader));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700184 SetClassRoot(kDalvikSystemPathClassLoader, dalvik_system_PathClassLoader);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700185
186 // Setup a single, global copy of "interfaces" and "iftable" for
187 // reuse across array classes
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700188 Class* java_lang_Cloneable = FindSystemClass("Ljava/lang/Cloneable;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700189 CHECK(java_lang_Cloneable != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700190 Class* java_io_Serializable = FindSystemClass("Ljava/io/Serializable;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700191 CHECK(java_io_Serializable != NULL);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700192
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700193 array_interfaces_ = AllocObjectArray<Class>(2);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700194 CHECK(array_interfaces_ != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700195 array_interfaces_->Set(0, java_lang_Cloneable);
196 array_interfaces_->Set(1, java_io_Serializable);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700197
198 // We assume that Cloneable/Serializable don't have superinterfaces --
199 // normally we'd have to crawl up and explicitly list all of the
200 // supers as well. These interfaces don't have any methods, so we
201 // don't have to worry about the ifviPool either.
202 array_iftable_ = new InterfaceEntry[2];
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700203 array_iftable_[0].SetClass(array_interfaces_->Get(0));
204 array_iftable_[1].SetClass(array_interfaces_->Get(1));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700205 // now FindClass can be used for non-primitive array classes
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700206
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700207 // run Object[] through FindClass to complete initialization
Jesse Wilson8989d992011-08-02 13:39:42 -0700208 Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;");
209 CHECK_EQ(object_array_class, found_object_array_class);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700210 CHECK_EQ(java_lang_Cloneable, object_array_class->GetInterface(0));
211 CHECK_EQ(java_io_Serializable, object_array_class->GetInterface(1));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700212
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700213 // Setup the primitive type classes.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700214 SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass("Z"));
215 SetClassRoot(kPrimitiveByte, CreatePrimitiveClass("B"));
216 SetClassRoot(kPrimitiveChar, CreatePrimitiveClass("C"));
217 SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass("D"));
218 SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass("F"));
219 SetClassRoot(kPrimitiveInt, CreatePrimitiveClass("I"));
220 SetClassRoot(kPrimitiveLong, CreatePrimitiveClass("J"));
221 SetClassRoot(kPrimitiveShort, CreatePrimitiveClass("S"));
222 SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass("V"));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700223 // now we can use FindSystemClass for anything, including for "[C"
224
Jesse Wilson7833bd22011-08-09 18:31:44 -0400225 // run char[], int[] and long[] through FindClass to complete initialization
Jesse Wilson8989d992011-08-02 13:39:42 -0700226 Class* found_char_array_class = FindSystemClass("[C");
227 CHECK_EQ(char_array_class, found_char_array_class);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400228 Class* found_int_array_class = FindSystemClass("[I");
229 CHECK_EQ(int_array_class, found_int_array_class);
230 Class* found_long_array_class = FindSystemClass("[J");
231 CHECK_EQ(long_array_class, found_long_array_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700232
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700233 // Initialize all the other primitive array types for PrimitiveArray::Alloc.
234 // These are easy because everything we need has already been set up.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700235 SetClassRoot(kBooleanArrayClass, FindSystemClass("[Z"));
236 SetClassRoot(kByteArrayClass, FindSystemClass("[B"));
237 SetClassRoot(kDoubleArrayClass, FindSystemClass("[D"));
238 SetClassRoot(kFloatArrayClass, FindSystemClass("[F"));
239 SetClassRoot(kShortArrayClass, FindSystemClass("[S"));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700240 BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass));
241 ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass));
242 DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass));
243 FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass));
244 ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass));
245
Brian Carlstroma663ea52011-08-19 23:33:41 -0700246 FinishInit();
247}
248
249void ClassLinker::FinishInit() {
250 // ensure all class_roots_ are initialized
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700251 for (size_t i = 0; i < kClassRootsMax; i++) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700252 ClassRoot class_root = static_cast<ClassRoot>(i);
253 Class* klass = GetClassRoot(class_root);
254 CHECK(klass != NULL);
255 DCHECK(klass->IsArray() || klass->IsPrimitive() || klass->dex_cache_ != NULL);
256 // note SetClassRoot does additional validation.
257 // if possible add new checks there to catch errors early
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700258 }
259
260 // disable the slow paths in FindClass and CreatePrimitiveClass now
261 // that Object, Class, and Object[] are setup
262 init_done_ = true;
263}
264
Brian Carlstroma663ea52011-08-19 23:33:41 -0700265struct ClassLinker::InitCallbackState {
266 ClassLinker* class_linker;
267
268 Class* class_roots[kClassRootsMax];
269
270 typedef std::tr1::unordered_map<std::string, ClassRoot> Table;
271 Table descriptor_to_class_root;
272
273 struct DexCacheHash {
274 size_t operator()(art::DexCache* const& obj) const {
275 return reinterpret_cast<size_t>(&obj);
276 }
277 };
278 typedef std::tr1::unordered_set<DexCache*, DexCacheHash> Set;
279 Set dex_caches;
280};
281
282void ClassLinker::Init(const std::vector<DexFile*>& boot_class_path, Space* space) {
283 CHECK(!init_done_);
284
285 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
286 DCHECK(heap_bitmap != NULL);
287
288 InitCallbackState state;
289 state.class_linker = this;
290 for (size_t i = 0; i < kClassRootsMax; i++) {
291 ClassRoot class_root = static_cast<ClassRoot>(i);
292 state.descriptor_to_class_root[GetClassRootDescriptor(class_root)] = class_root;
293 }
294
295 // reinit clases_ table
296 heap_bitmap->Walk(InitCallback, &state);
297
298 // reinit class_roots_
299 Class* object_array_class = state.class_roots[kObjectArrayClass];
300 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
301 for (size_t i = 0; i < kClassRootsMax; i++) {
302 ClassRoot class_root = static_cast<ClassRoot>(i);
303 SetClassRoot(class_root, state.class_roots[class_root]);
304 }
305
306 // reinit intern_table_
307 ObjectArray<Object>* interned_array = space->GetImageHeader().GetInternedArray();
308 for (int32_t i = 0; i < interned_array->GetLength(); i++) {
309 String* string = interned_array->Get(i)->AsString();
310 intern_table_.Register(string);
311 }
312
313 // reinit array_interfaces_ from any array class instance, they should all be ==
314 array_interfaces_ = GetClassRoot(kObjectArrayClass)->interfaces_;
315 DCHECK(array_interfaces_ == GetClassRoot(kBooleanArrayClass)->interfaces_);
316
317 // build a map from location to DexCache to match up with DexFile::GetLocation
318 std::tr1::unordered_map<std::string, DexCache*> location_to_dex_cache;
319 typedef InitCallbackState::Set::const_iterator It; // TODO: C++0x auto
320 for (It it = state.dex_caches.begin(), end = state.dex_caches.end(); it != end; ++it) {
321 DexCache* dex_cache = *it;
322 std::string location = dex_cache->GetLocation()->ToModifiedUtf8();
323 location_to_dex_cache[location] = dex_cache;
324 }
325
326 // reinit boot_class_path with DexFile arguments and found DexCaches
327 for (size_t i = 0; i != boot_class_path.size(); ++i) {
328 DexFile* dex_file = boot_class_path[i];
329 DexCache* dex_cache = location_to_dex_cache[dex_file->GetLocation()];
330 AppendToBootClassPath(dex_file, dex_cache);
331 }
332
333 String::SetClass(GetClassRoot(kJavaLangString));
334 BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass));
335 ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass));
336 CharArray::SetArrayClass(GetClassRoot(kCharArrayClass));
337 DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass));
338 FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass));
339 IntArray::SetArrayClass(GetClassRoot(kIntArrayClass));
340 LongArray::SetArrayClass(GetClassRoot(kLongArrayClass));
341 ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass));
342
343 FinishInit();
344}
345
346void ClassLinker::InitCallback(Object *obj, void *arg) {
347 DCHECK(obj != NULL);
348 DCHECK(arg != NULL);
349 InitCallbackState* state = reinterpret_cast<InitCallbackState*>(arg);
350
351 if (!obj->IsClass()) {
352 return;
353 }
354 Class* klass = obj->AsClass();
355 CHECK(klass->class_loader_ == NULL);
356
357 std::string descriptor = klass->GetDescriptor()->ToModifiedUtf8();
358
359 // restore class to ClassLinker::classes_ table
360 state->class_linker->InsertClass(descriptor, klass);
361
362 // note DexCache to match with DexFile later
363 DexCache* dex_cache = klass->GetDexCache();
364 if (dex_cache != NULL) {
365 state->dex_caches.insert(dex_cache);
366 } else {
367 DCHECK(klass->IsArray() || klass->IsPrimitive());
368 }
369
370 // check if this is a root, if so, register it
371 typedef InitCallbackState::Table::const_iterator It; // TODO: C++0x auto
372 It it = state->descriptor_to_class_root.find(descriptor);
373 if (it != state->descriptor_to_class_root.end()) {
374 ClassRoot class_root = it->second;
375 state->class_roots[class_root] = klass;
376 }
377}
378
379// Keep in sync with InitCallback. Anything we visit, we need to
380// reinit references to when reinitializing a ClassLinker from a
381// mapped image.
382void ClassLinker::VisitRoots(Heap::RootVistor* root_visitor, void* arg) const {
383
Brian Carlstromb88e9442011-07-28 15:15:51 -0700384 root_visitor(class_roots_, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700385
386 for (size_t i = 0; i < dex_caches_.size(); i++) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700387 root_visitor(dex_caches_[i], arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700388 }
389
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700390 {
391 MutexLock mu(classes_lock_);
392 typedef Table::const_iterator It; // TODO: C++0x auto
393 for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
394 root_visitor(it->second, arg);
395 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700396 }
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700397
398 intern_table_.VisitRoots(root_visitor, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700399
Brian Carlstromb88e9442011-07-28 15:15:51 -0700400 root_visitor(array_interfaces_, arg);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700401}
402
Brian Carlstroma663ea52011-08-19 23:33:41 -0700403DexCache* ClassLinker::AllocDexCache(const DexFile* dex_file) {
404 DexCache* dex_cache = down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::kMax));
405 dex_cache->Init(String::AllocFromModifiedUtf8(dex_file->GetLocation().c_str()),
406 AllocObjectArray<String>(dex_file->NumStringIds()),
407 AllocObjectArray<Class>(dex_file->NumTypeIds()),
408 AllocObjectArray<Method>(dex_file->NumMethodIds()),
409 AllocObjectArray<Field>(dex_file->NumFieldIds()));
410 return dex_cache;
Brian Carlstroma0808032011-07-18 00:39:23 -0700411}
412
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700413Class* ClassLinker::AllocClass(Class* java_lang_Class) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700414 return java_lang_Class->NewInstance()->AsClass();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700415}
416
417Class* ClassLinker::AllocClass() {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700418 return AllocClass(GetClassRoot(kJavaLangClass));
Brian Carlstroma0808032011-07-18 00:39:23 -0700419}
420
Jesse Wilson35baaab2011-08-10 16:18:03 -0400421Field* ClassLinker::AllocField() {
422 return down_cast<Field*>(GetClassRoot(kJavaLangReflectField)->NewInstance());
Brian Carlstroma0808032011-07-18 00:39:23 -0700423}
424
425Method* ClassLinker::AllocMethod() {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700426 return down_cast<Method*>(GetClassRoot(kJavaLangReflectMethod)->NewInstance());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700427}
428
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700429// TODO: remove once we can use java.lang.Class.getSystemClassLoader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700430PathClassLoader* ClassLinker::AllocPathClassLoader(std::vector<const DexFile*> dex_files) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700431 PathClassLoader* cl = down_cast<PathClassLoader*>(GetClassRoot(kDalvikSystemPathClassLoader)->NewInstance());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700432 cl->SetClassPath(dex_files);
433 return cl;
Carl Shapiro565f5072011-07-10 13:39:43 -0700434}
435
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700436Class* ClassLinker::FindClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700437 ClassLoader* class_loader) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700438 // TODO: remove this contrived parent class loader check when we have a real ClassLoader.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700439 if (class_loader != NULL) {
440 Class* klass = FindClass(descriptor, NULL);
441 if (klass != NULL) {
442 return klass;
443 }
444 }
445
Carl Shapirob5573532011-07-12 18:22:59 -0700446 Thread* self = Thread::Current();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700447 DCHECK(self != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700448 CHECK(!self->IsExceptionPending());
449 // Find the class in the loaded classes table.
450 Class* klass = LookupClass(descriptor, class_loader);
451 if (klass == NULL) {
452 // Class is not yet loaded.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700453 if (descriptor[0] == '[') {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700454 return CreateArrayClass(descriptor, class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700455 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700456 DexFile::ClassPath& class_path = ((class_loader != NULL) ? class_loader->GetClassPath() : boot_class_path_);
457 DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, class_path);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700458 if (pair.second == NULL) {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700459 LG << "Class " << PrintableString(descriptor) << " not found in class loader " << class_loader; // TODO: NoClassDefFoundError
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700460 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700461 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700462 const DexFile& dex_file = *pair.first;
463 const DexFile::ClassDef& dex_class_def = *pair.second;
464 DexCache* dex_cache = FindDexCache(pair.first);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700465 // Load the class from the dex file.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700466 if (!init_done_) {
467 // finish up init of hand crafted class_roots_
468 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700469 klass = GetClassRoot(kJavaLangObject);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700470 } else if (descriptor == "Ljava/lang/Class;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700471 klass = GetClassRoot(kJavaLangClass);
Jesse Wilson14150742011-07-29 19:04:44 -0400472 } else if (descriptor == "Ljava/lang/String;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700473 klass = GetClassRoot(kJavaLangString);
474 } else if (descriptor == "Ljava/lang/reflect/Field;") {
475 klass = GetClassRoot(kJavaLangReflectField);
476 } else if (descriptor == "Ljava/lang/reflect/Method;") {
477 klass = GetClassRoot(kJavaLangReflectMethod);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700478 } else {
479 klass = AllocClass();
480 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700481 } else {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700482 klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700483 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700484 klass->dex_cache_ = dex_cache;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700485 LoadClass(dex_file, dex_class_def, klass, class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700486 // Check for a pending exception during load
487 if (self->IsExceptionPending()) {
488 // TODO: free native allocations in klass
489 return NULL;
490 }
491 {
492 ObjectLock lock(klass);
Carl Shapirob5573532011-07-12 18:22:59 -0700493 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700494 // Add the newly loaded class to the loaded classes table.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700495 bool success = InsertClass(descriptor, klass); // TODO: just return collision
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700496 if (!success) {
497 // We may fail to insert if we raced with another thread.
498 klass->clinit_thread_id_ = 0;
499 // TODO: free native allocations in klass
500 klass = LookupClass(descriptor, class_loader);
501 CHECK(klass != NULL);
502 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700503 // Finish loading (if necessary) by finding parents
504 if (!klass->IsLoaded() && !LoadSuperAndInterfaces(klass, dex_file)) {
505 // Loading failed.
506 // TODO: CHECK(self->IsExceptionPending());
507 lock.NotifyAll();
508 return NULL;
509 }
510 CHECK(klass->IsLoaded());
511 // Link the class (if necessary)
512 if (!klass->IsLinked() && !LinkClass(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700513 // Linking failed.
514 // TODO: CHECK(self->IsExceptionPending());
515 lock.NotifyAll();
516 return NULL;
517 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700518 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700519 }
520 }
521 }
522 // Link the class if it has not already been linked.
523 if (!klass->IsLinked() && !klass->IsErroneous()) {
524 ObjectLock lock(klass);
525 // Check for circular dependencies between classes.
Carl Shapirob5573532011-07-12 18:22:59 -0700526 if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700527 LG << "Recursive link"; // TODO: ClassCircularityError
528 return NULL;
529 }
530 // Wait for the pending initialization to complete.
531 while (!klass->IsLinked() && !klass->IsErroneous()) {
532 lock.Wait();
533 }
534 }
535 if (klass->IsErroneous()) {
536 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
537 return NULL;
538 }
539 // Return the loaded class. No exceptions should be pending.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700540 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700541 CHECK(!self->IsExceptionPending());
542 return klass;
543}
544
Brian Carlstromf615a612011-07-23 12:50:34 -0700545void ClassLinker::LoadClass(const DexFile& dex_file,
546 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700547 Class* klass,
548 ClassLoader* class_loader) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700549 CHECK(klass != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700550 CHECK(klass->dex_cache_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700551 CHECK_EQ(Class::kStatusNotReady, klass->status_);
Brian Carlstromf615a612011-07-23 12:50:34 -0700552 const byte* class_data = dex_file.GetClassData(dex_class_def);
553 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700554
Brian Carlstromf615a612011-07-23 12:50:34 -0700555 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700556 CHECK(descriptor != NULL);
557
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700558 klass->klass_ = GetClassRoot(kJavaLangClass);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700559 klass->descriptor_ = String::AllocFromModifiedUtf8(descriptor);
Brian Carlstromf615a612011-07-23 12:50:34 -0700560 klass->access_flags_ = dex_class_def.access_flags_;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700561 klass->class_loader_ = class_loader;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700562 klass->primitive_type_ = Class::kPrimNot;
563 klass->status_ = Class::kStatusIdx;
564
565 klass->super_class_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700566 klass->super_class_idx_ = dex_class_def.superclass_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700567
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700568 size_t num_static_fields = header.static_fields_size_;
569 size_t num_instance_fields = header.instance_fields_size_;
570 size_t num_direct_methods = header.direct_methods_size_;
571 size_t num_virtual_methods = header.virtual_methods_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700572
Brian Carlstromf615a612011-07-23 12:50:34 -0700573 klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700574
575 // Load class interfaces.
Brian Carlstromf615a612011-07-23 12:50:34 -0700576 LoadInterfaces(dex_file, dex_class_def, klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700577
578 // Load static fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700579 DCHECK(klass->sfields_ == NULL);
580 if (num_static_fields != 0) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400581 klass->sfields_ = AllocObjectArray<Field>(num_static_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700582 uint32_t last_idx = 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700583 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700584 DexFile::Field dex_field;
585 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400586 Field* sfield = AllocField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700587 klass->SetStaticField(i, sfield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700588 LoadField(dex_file, dex_field, klass, sfield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700589 }
590 }
591
592 // Load instance fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700593 DCHECK(klass->ifields_ == NULL);
594 if (num_instance_fields != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700595 // TODO: allocate on the object heap.
Jesse Wilson35baaab2011-08-10 16:18:03 -0400596 klass->ifields_ = AllocObjectArray<Field>(num_instance_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700597 uint32_t last_idx = 0;
598 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700599 DexFile::Field dex_field;
600 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400601 Field* ifield = AllocField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700602 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700603 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700604 }
605 }
606
607 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700608 DCHECK(klass->direct_methods_ == NULL);
609 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700610 // TODO: append direct methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700611 klass->direct_methods_ = AllocObjectArray<Method>(num_direct_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700612 uint32_t last_idx = 0;
613 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700614 DexFile::Method dex_method;
615 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700616 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700617 klass->SetDirectMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700618 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700619 // TODO: register maps
620 }
621 }
622
623 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700624 DCHECK(klass->virtual_methods_ == NULL);
625 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700626 // TODO: append virtual methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700627 klass->virtual_methods_ = AllocObjectArray<Method>(num_virtual_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700628 uint32_t last_idx = 0;
629 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700630 DexFile::Method dex_method;
631 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700632 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700633 klass->SetVirtualMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700634 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700635 // TODO: register maps
636 }
637 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700638}
639
Brian Carlstromf615a612011-07-23 12:50:34 -0700640void ClassLinker::LoadInterfaces(const DexFile& dex_file,
641 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700642 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700643 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700644 if (list != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700645 DCHECK(klass->interfaces_ == NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700646 klass->interfaces_ = AllocObjectArray<Class>(list->Size());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700647 DCHECK(klass->interfaces_idx_ == NULL);
648 klass->interfaces_idx_ = new uint32_t[list->Size()];
Brian Carlstrom934486c2011-07-12 23:42:50 -0700649 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700650 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700651 klass->interfaces_idx_[i] = type_item.type_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700652 }
653 }
654}
655
Brian Carlstromf615a612011-07-23 12:50:34 -0700656void ClassLinker::LoadField(const DexFile& dex_file,
657 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700658 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700659 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700660 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400661 dst->declaring_class_ = klass;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700662 dst->name_ = ResolveString(klass, field_id.name_idx_, dex_file);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700663 dst->descriptor_.set(dex_file.dexStringByTypeIdx(field_id.type_idx_));
Jesse Wilson35baaab2011-08-10 16:18:03 -0400664 // TODO: Assign dst->type_.
Brian Carlstrom934486c2011-07-12 23:42:50 -0700665 dst->access_flags_ = src.access_flags_;
666}
667
Brian Carlstromf615a612011-07-23 12:50:34 -0700668void ClassLinker::LoadMethod(const DexFile& dex_file,
669 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700670 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700671 Method* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700672 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400673 dst->declaring_class_ = klass;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700674 dst->name_ = ResolveString(klass, method_id.name_idx_, dex_file);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700675 {
676 int32_t utf16_length;
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700677 scoped_array<char> utf8(dex_file.CreateMethodDescriptor(method_id.proto_idx_,
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700678 &utf16_length));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700679 dst->signature_ = String::AllocFromModifiedUtf8(utf16_length, utf8.get());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700680 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700681 dst->proto_idx_ = method_id.proto_idx_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700682 dst->code_off_ = src.code_off_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700683 dst->shorty_ = dex_file.GetShorty(method_id.proto_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700684 dst->access_flags_ = src.access_flags_;
685
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700686 dst->dex_cache_strings_ = klass->dex_cache_->GetStrings();
687 dst->dex_cache_classes_ = klass->dex_cache_->GetClasses();
688 dst->dex_cache_methods_ = klass->dex_cache_->GetMethods();
689 dst->dex_cache_fields_ = klass->dex_cache_->GetFields();
690
Brian Carlstrom934486c2011-07-12 23:42:50 -0700691 // TODO: check for finalize method
692
Brian Carlstromf615a612011-07-23 12:50:34 -0700693 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700694 if (code_item != NULL) {
695 dst->num_registers_ = code_item->registers_size_;
696 dst->num_ins_ = code_item->ins_size_;
697 dst->num_outs_ = code_item->outs_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700698 } else {
699 uint16_t num_args = dst->NumArgRegisters();
700 if (!dst->IsStatic()) {
701 ++num_args;
702 }
703 dst->num_registers_ = dst->num_ins_ + num_args;
704 // TODO: native methods
705 }
706}
707
Brian Carlstroma663ea52011-08-19 23:33:41 -0700708void ClassLinker::AppendToBootClassPath(const DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700709 CHECK(dex_file != NULL);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700710 AppendToBootClassPath(dex_file, AllocDexCache(dex_file));
711}
712
713void ClassLinker::AppendToBootClassPath(const DexFile* dex_file, DexCache* dex_cache) {
714 CHECK(dex_file != NULL);
715 CHECK(dex_cache != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700716 boot_class_path_.push_back(dex_file);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700717 RegisterDexFile(dex_file, dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700718}
719
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700720void ClassLinker::RegisterDexFile(const DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700721 CHECK(dex_file != NULL);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700722 RegisterDexFile(dex_file, AllocDexCache(dex_file));
723}
724
725void ClassLinker::RegisterDexFile(const DexFile* dex_file, DexCache* dex_cache) {
726 CHECK(dex_file != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700727 CHECK(dex_cache != NULL);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700728 dex_files_.push_back(dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700729 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700730}
731
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700732const DexFile& ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700733 for (size_t i = 0; i != dex_caches_.size(); ++i) {
734 if (dex_caches_[i] == dex_cache) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700735 return *dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700736 }
737 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700738 CHECK(false) << "Could not find DexFile";
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700739 return *dex_files_[-1];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700740}
741
Brian Carlstromf615a612011-07-23 12:50:34 -0700742DexCache* ClassLinker::FindDexCache(const DexFile* dex_file) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700743 for (size_t i = 0; i != dex_files_.size(); ++i) {
744 if (dex_files_[i] == dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700745 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700746 }
747 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700748 CHECK(false) << "Could not find DexCache";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700749 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700750}
751
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700752Class* ClassLinker::CreatePrimitiveClass(const char* descriptor) {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700753 Class* klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700754 CHECK(klass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700755 klass->super_class_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700756 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700757 klass->descriptor_ = String::AllocFromModifiedUtf8(descriptor);
Carl Shapiro565f5072011-07-10 13:39:43 -0700758 klass->status_ = Class::kStatusInitialized;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700759 bool success = InsertClass(descriptor, klass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700760 CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed";
Carl Shapiro565f5072011-07-10 13:39:43 -0700761 return klass;
762}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700763
Brian Carlstrombe977852011-07-19 14:54:54 -0700764// Create an array class (i.e. the class object for the array, not the
765// array itself). "descriptor" looks like "[C" or "[[[[B" or
766// "[Ljava/lang/String;".
767//
768// If "descriptor" refers to an array of primitives, look up the
769// primitive type's internally-generated class object.
770//
771// "loader" is the class loader of the class that's referring to us. It's
772// used to ensure that we're looking for the element type in the right
773// context. It does NOT become the class loader for the array class; that
774// always comes from the base element class.
775//
776// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700777Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700778 ClassLoader* class_loader)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700779{
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700780 CHECK(descriptor[0] == '[');
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700781
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700782 // Identify the underlying element class and the array dimension depth.
783 Class* component_type_ = NULL;
784 int array_rank;
785 if (descriptor[1] == '[') {
786 // array of arrays; keep descriptor and grab stuff from parent
787 Class* outer = FindClass(descriptor.substr(1), class_loader);
788 if (outer != NULL) {
789 // want the base class, not "outer", in our component_type_
790 component_type_ = outer->component_type_;
791 array_rank = outer->array_rank_ + 1;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700792 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700793 DCHECK(component_type_ == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700794 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700795 } else {
796 array_rank = 1;
797 if (descriptor[1] == 'L') {
798 // array of objects; strip off "[" and look up descriptor.
799 const StringPiece subDescriptor = descriptor.substr(1);
800 component_type_ = FindClass(subDescriptor, class_loader);
801 } else {
802 // array of a primitive type
803 component_type_ = FindPrimitiveClass(descriptor[1]);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700804 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700805 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700806
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700807 if (component_type_ == NULL) {
808 // failed
809 // DCHECK(Thread::Current()->IsExceptionPending()); // TODO
810 return NULL;
811 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700812
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700813 // See if the component type is already loaded. Array classes are
814 // always associated with the class loader of their underlying
815 // element type -- an array of Strings goes with the loader for
816 // java/lang/String -- so we need to look for it there. (The
817 // caller should have checked for the existence of the class
818 // before calling here, but they did so with *their* class loader,
819 // not the component type's loader.)
820 //
821 // If we find it, the caller adds "loader" to the class' initiating
822 // loader list, which should prevent us from going through this again.
823 //
824 // This call is unnecessary if "loader" and "component_type_->class_loader_"
825 // are the same, because our caller (FindClass) just did the
826 // lookup. (Even if we get this wrong we still have correct behavior,
827 // because we effectively do this lookup again when we add the new
828 // class to the hash table --- necessary because of possible races with
829 // other threads.)
830 if (class_loader != component_type_->class_loader_) {
831 Class* new_class = LookupClass(descriptor, component_type_->class_loader_);
832 if (new_class != NULL) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700833 return new_class;
834 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700835 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700836
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700837 // Fill out the fields in the Class.
838 //
839 // It is possible to execute some methods against arrays, because
840 // all arrays are subclasses of java_lang_Object_, so we need to set
841 // up a vtable. We can just point at the one in java_lang_Object_.
842 //
843 // Array classes are simple enough that we don't need to do a full
844 // link step.
845
846 Class* new_class = NULL;
847 if (!init_done_) {
848 if (descriptor == "[Ljava/lang/Object;") {
849 new_class = GetClassRoot(kObjectArrayClass);
850 } else if (descriptor == "[C") {
851 new_class = GetClassRoot(kCharArrayClass);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400852 } else if (descriptor == "[I") {
853 new_class = GetClassRoot(kIntArrayClass);
854 } else if (descriptor == "[J") {
855 new_class = GetClassRoot(kLongArrayClass);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700856 }
857 }
858 if (new_class == NULL) {
859 new_class = AllocClass();
860 if (new_class == NULL) {
861 return NULL;
862 }
863 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700864 new_class->descriptor_ = String::AllocFromModifiedUtf8(descriptor.ToString().c_str());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700865 Class* java_lang_Object = GetClassRoot(kJavaLangObject);
866 new_class->super_class_ = java_lang_Object;
867 new_class->vtable_ = java_lang_Object->vtable_;
868 new_class->primitive_type_ = Class::kPrimNot;
869 new_class->component_type_ = component_type_;
870 new_class->class_loader_ = component_type_->class_loader_;
871 new_class->array_rank_ = array_rank;
872 new_class->status_ = Class::kStatusInitialized;
873 // don't need to set new_class->object_size_
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700874 // because Object::SizeOf delegates to Array::SizeOf
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700875
876
877 // All arrays have java/lang/Cloneable and java/io/Serializable as
878 // interfaces. We need to set that up here, so that stuff like
879 // "instanceof" works right.
880 //
881 // Note: The GC could run during the call to FindSystemClass,
882 // so we need to make sure the class object is GC-valid while we're in
883 // there. Do this by clearing the interface list so the GC will just
884 // think that the entries are null.
885
886
887 // Use the single, global copies of "interfaces" and "iftable"
888 // (remember not to free them for arrays).
889 DCHECK(array_interfaces_ != NULL);
890 new_class->interfaces_ = array_interfaces_;
891 new_class->iftable_count_ = 2;
892 DCHECK(array_iftable_ != NULL);
893 new_class->iftable_ = array_iftable_;
894
895 // Inherit access flags from the component type. Arrays can't be
896 // used as a superclass or interface, so we want to add "final"
897 // and remove "interface".
898 //
899 // Don't inherit any non-standard flags (e.g., kAccFinal)
900 // from component_type_. We assume that the array class does not
901 // override finalize().
902 new_class->access_flags_ = ((new_class->component_type_->access_flags_ &
903 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask;
904
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700905 if (InsertClass(descriptor, new_class)) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700906 return new_class;
907 }
908 // Another thread must have loaded the class after we
909 // started but before we finished. Abandon what we've
910 // done.
911 //
912 // (Yes, this happens.)
913
914 // Grab the winning class.
915 Class* other_class = LookupClass(descriptor, component_type_->class_loader_);
916 DCHECK(other_class != NULL);
917 return other_class;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700918}
919
920Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700921 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700922 case 'B':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700923 return GetClassRoot(kPrimitiveByte);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700924 case 'C':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700925 return GetClassRoot(kPrimitiveChar);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700926 case 'D':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700927 return GetClassRoot(kPrimitiveDouble);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700928 case 'F':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700929 return GetClassRoot(kPrimitiveFloat);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700930 case 'I':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700931 return GetClassRoot(kPrimitiveInt);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700932 case 'J':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700933 return GetClassRoot(kPrimitiveLong);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700934 case 'S':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700935 return GetClassRoot(kPrimitiveShort);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700936 case 'Z':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700937 return GetClassRoot(kPrimitiveBoolean);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700938 case 'V':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700939 return GetClassRoot(kPrimitiveVoid);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700940 case 'L':
941 case '[':
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700942 LOG(ERROR) << "Not a primitive type " << PrintableChar(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700943 default:
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700944 LOG(ERROR) << "Unknown primitive type " << PrintableChar(type);
Carl Shapiro744ad052011-08-06 15:53:36 -0700945 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700946 return NULL; // Not reachable.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700947}
948
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700949bool ClassLinker::InsertClass(const StringPiece& descriptor, Class* klass) {
950 size_t hash = StringPieceHash()(descriptor);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700951 MutexLock mu(classes_lock_);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700952 Table::iterator it = classes_.insert(std::make_pair(hash, klass));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700953 return ((*it).second == klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700954}
955
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700956Class* ClassLinker::LookupClass(const StringPiece& descriptor, ClassLoader* class_loader) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700957 size_t hash = StringPieceHash()(descriptor);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700958 MutexLock mu(classes_lock_);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700959 typedef Table::const_iterator It; // TODO: C++0x auto
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700960 for (It it = classes_.find(hash), end = classes_.end(); it != end; ++it) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700961 Class* klass = it->second;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700962 if (klass->descriptor_->Equals(descriptor) && klass->class_loader_ == class_loader) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700963 return klass;
964 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700965 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700966 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700967}
968
969bool ClassLinker::InitializeClass(Class* klass) {
970 CHECK(klass->GetStatus() == Class::kStatusResolved ||
971 klass->GetStatus() == Class::kStatusError);
972
Carl Shapirob5573532011-07-12 18:22:59 -0700973 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700974
975 {
976 ObjectLock lock(klass);
977
978 if (klass->GetStatus() < Class::kStatusVerified) {
979 if (klass->IsErroneous()) {
980 LG << "re-initializing failed class"; // TODO: throw
981 return false;
982 }
983
984 CHECK(klass->GetStatus() == Class::kStatusResolved);
985
986 klass->status_ = Class::kStatusVerifying;
987 if (!DexVerify::VerifyClass(klass)) {
988 LG << "Verification failed"; // TODO: ThrowVerifyError
989 Object* exception = self->GetException();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700990 klass->SetVerifyErrorClass(exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700991 klass->SetStatus(Class::kStatusError);
992 return false;
993 }
994
995 klass->SetStatus(Class::kStatusVerified);
996 }
997
998 if (klass->status_ == Class::kStatusInitialized) {
999 return true;
1000 }
1001
1002 while (klass->status_ == Class::kStatusInitializing) {
1003 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -07001004 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001005 LG << "recursive <clinit>";
1006 return true;
1007 }
1008
1009 CHECK(!self->IsExceptionPending());
1010
1011 lock.Wait(); // TODO: check for interruption
1012
1013 // When we wake up, repeat the test for init-in-progress. If
1014 // there's an exception pending (only possible if
1015 // "interruptShouldThrow" was set), bail out.
1016 if (self->IsExceptionPending()) {
1017 CHECK(false);
1018 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
1019 klass->SetStatus(Class::kStatusError);
1020 return false;
1021 }
1022 if (klass->GetStatus() == Class::kStatusInitializing) {
1023 continue;
1024 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001025 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001026 klass->GetStatus() == Class::kStatusError);
1027 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001028 // The caller wants an exception, but it was thrown in a
1029 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001030 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
1031 return false;
1032 }
1033 return true; // otherwise, initialized
1034 }
1035
1036 // see if we failed previously
1037 if (klass->IsErroneous()) {
1038 // might be wise to unlock before throwing; depends on which class
1039 // it is that we have locked
1040
1041 // TODO: throwEarlierClassFailure(klass);
1042 return false;
1043 }
1044
1045 if (!ValidateSuperClassDescriptors(klass)) {
1046 klass->SetStatus(Class::kStatusError);
1047 return false;
1048 }
1049
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001050 DCHECK(klass->status_ < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001051
Carl Shapirob5573532011-07-12 18:22:59 -07001052 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001053 klass->status_ = Class::kStatusInitializing;
1054 }
1055
1056 if (!InitializeSuperClass(klass)) {
1057 return false;
1058 }
1059
1060 InitializeStaticFields(klass);
1061
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001062 Method* clinit = klass->FindDeclaredDirectMethod("<clinit>", "()V");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001063 if (clinit != NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001064 // JValue unused;
1065 // TODO: dvmCallMethod(self, method, NULL, &unused);
Elliott Hughes53b61312011-08-12 18:28:20 -07001066 // UNIMPLEMENTED(FATAL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001067 }
1068
1069 {
1070 ObjectLock lock(klass);
1071
1072 if (self->IsExceptionPending()) {
1073 klass->SetStatus(Class::kStatusError);
1074 } else {
1075 klass->SetStatus(Class::kStatusInitialized);
1076 }
1077 lock.NotifyAll();
1078 }
1079
1080 return true;
1081}
1082
1083bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
1084 if (klass->IsInterface()) {
1085 return true;
1086 }
1087 // begin with the methods local to the superclass
1088 if (klass->HasSuperClass() &&
1089 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
1090 const Class* super = klass->GetSuperClass();
1091 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001092 const Method* method = super->GetVirtualMethod(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001093 if (method != super->GetVirtualMethod(i) &&
1094 !HasSameMethodDescriptorClasses(method, super, klass)) {
1095 LG << "Classes resolve differently in superclass";
1096 return false;
1097 }
1098 }
1099 }
1100 for (size_t i = 0; i < klass->iftable_count_; ++i) {
1101 const InterfaceEntry* iftable = &klass->iftable_[i];
1102 Class* interface = iftable->GetClass();
1103 if (klass->GetClassLoader() != interface->GetClassLoader()) {
1104 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1105 uint32_t vtable_index = iftable->method_index_array_[j];
1106 const Method* method = klass->GetVirtualMethod(vtable_index);
1107 if (!HasSameMethodDescriptorClasses(method, interface,
1108 method->GetClass())) {
1109 LG << "Classes resolve differently in interface"; // TODO: LinkageError
1110 return false;
1111 }
1112 }
1113 }
1114 }
1115 return true;
1116}
1117
1118bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -07001119 const Class* klass1,
1120 const Class* klass2) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001121 const DexFile& dex_file = FindDexFile(method->GetClass()->GetDexCache());
1122 const DexFile::ProtoId& proto_id = dex_file.GetProtoId(method->proto_idx_);
Brian Carlstromf615a612011-07-23 12:50:34 -07001123 DexFile::ParameterIterator *it;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001124 for (it = dex_file.GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001125 const char* descriptor = it->GetDescriptor();
1126 if (descriptor == NULL) {
1127 break;
1128 }
1129 if (descriptor[0] == 'L' || descriptor[0] == '[') {
1130 // Found a non-primitive type.
1131 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
1132 return false;
1133 }
1134 }
1135 }
1136 // Check the return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001137 const char* descriptor = dex_file.GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001138 if (descriptor[0] == 'L' || descriptor[0] == '[') {
1139 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
1140 return false;
1141 }
1142 }
1143 return true;
1144}
1145
1146// Returns true if classes referenced by the descriptor are the
1147// same classes in klass1 as they are in klass2.
1148bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -07001149 const Class* klass1,
1150 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001151 CHECK(descriptor != NULL);
1152 CHECK(klass1 != NULL);
1153 CHECK(klass2 != NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001154 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001155 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001156 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001157 // TODO: found2 == NULL
1158 // TODO: lookup found1 in initiating loader list
1159 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -07001160 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001161 if (found1 == found2) {
1162 return true;
1163 } else {
1164 return false;
1165 }
1166 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001167 return true;
1168}
1169
1170bool ClassLinker::InitializeSuperClass(Class* klass) {
1171 CHECK(klass != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001172 if (!klass->IsInterface() && klass->HasSuperClass()) {
1173 Class* super_class = klass->GetSuperClass();
1174 if (super_class->GetStatus() != Class::kStatusInitialized) {
1175 CHECK(!super_class->IsInterface());
1176 klass->MonitorExit();
1177 bool super_initialized = InitializeClass(super_class);
1178 klass->MonitorEnter();
1179 // TODO: check for a pending exception
1180 if (!super_initialized) {
1181 klass->SetStatus(Class::kStatusError);
1182 klass->NotifyAll();
1183 return false;
1184 }
1185 }
1186 }
1187 return true;
1188}
1189
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001190bool ClassLinker::EnsureInitialized(Class* c) {
1191 CHECK(c != NULL);
1192 if (c->IsInitialized()) {
1193 return true;
1194 }
1195
1196 c->MonitorExit();
1197 InitializeClass(c);
1198 c->MonitorEnter();
1199 return !Thread::Current()->IsExceptionPending();
1200}
1201
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001202void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001203 size_t num_static_fields = klass->NumStaticFields();
1204 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001205 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001206 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001207 DexCache* dex_cache = klass->GetDexCache();
1208 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001209 return;
1210 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001211 const std::string descriptor(klass->GetDescriptor()->ToModifiedUtf8());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001212 const DexFile& dex_file = FindDexFile(dex_cache);
1213 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Brian Carlstromf615a612011-07-23 12:50:34 -07001214 CHECK(dex_class_def != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001215 const byte* addr = dex_file.GetEncodedArray(*dex_class_def);
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001216 if (addr == NULL) {
1217 // All this class' static fields have default values.
1218 return;
1219 }
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001220 size_t array_size = DecodeUnsignedLeb128(&addr);
1221 for (size_t i = 0; i < array_size; ++i) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001222 Field* field = klass->GetStaticField(i);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001223 JValue value;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001224 DexFile::ValueType type = dex_file.ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001225 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001226 case DexFile::kByte:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001227 field->SetByte(value.b);
1228 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001229 case DexFile::kShort:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001230 field->SetShort(value.s);
1231 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001232 case DexFile::kChar:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001233 field->SetChar(value.c);
1234 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001235 case DexFile::kInt:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001236 field->SetInt(value.i);
1237 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001238 case DexFile::kLong:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001239 field->SetLong(value.j);
1240 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001241 case DexFile::kFloat:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001242 field->SetFloat(value.f);
1243 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001244 case DexFile::kDouble:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001245 field->SetDouble(value.d);
1246 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001247 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001248 uint32_t string_idx = value.i;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001249 String* resolved = ResolveString(klass, string_idx, dex_file);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001250 field->SetObject(resolved);
1251 break;
1252 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001253 case DexFile::kBoolean:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001254 field->SetBoolean(value.z);
1255 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001256 case DexFile::kNull:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001257 field->SetObject(value.l);
1258 break;
1259 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001260 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001261 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001262 }
1263}
1264
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001265bool ClassLinker::LinkClass(Class* klass, const DexFile& dex_file) {
1266 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001267 if (!LinkSuperClass(klass)) {
1268 return false;
1269 }
1270 if (!LinkMethods(klass)) {
1271 return false;
1272 }
Jesse Wilson7833bd22011-08-09 18:31:44 -04001273 if (!LinkStaticFields(klass)) {
1274 return false;
1275 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001276 if (!LinkInstanceFields(klass)) {
1277 return false;
1278 }
1279 CreateReferenceOffsets(klass);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001280 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001281 klass->status_ = Class::kStatusResolved;
1282 return true;
1283}
1284
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001285bool ClassLinker::LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file) {
1286 CHECK_EQ(Class::kStatusIdx, klass->status_);
Brian Carlstromf615a612011-07-23 12:50:34 -07001287 if (klass->super_class_idx_ != DexFile::kDexNoIndex) {
1288 Class* super_class = ResolveClass(klass, klass->super_class_idx_, dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001289 if (super_class == NULL) {
1290 LG << "Failed to resolve superclass";
1291 return false;
1292 }
1293 klass->super_class_ = super_class; // TODO: write barrier
1294 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001295 if (klass->NumInterfaces() > 0) {
1296 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
1297 uint32_t idx = klass->interfaces_idx_[i];
1298 klass->SetInterface(i, ResolveClass(klass, idx, dex_file));
1299 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001300 LG << "Failed to resolve interface";
1301 return false;
1302 }
1303 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001304 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001305 LG << "Inaccessible interface";
1306 return false;
1307 }
1308 }
1309 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001310 // Mark the class as loaded.
1311 klass->status_ = Class::kStatusLoaded;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001312 return true;
1313}
1314
1315bool ClassLinker::LinkSuperClass(Class* klass) {
1316 CHECK(!klass->IsPrimitive());
1317 const Class* super = klass->GetSuperClass();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001318 if (klass->GetDescriptor()->Equals("Ljava/lang/Object;")) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001319 if (super != NULL) {
1320 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1321 return false;
1322 }
1323 // TODO: clear finalize attribute
1324 return true;
1325 }
1326 if (super == NULL) {
1327 LG << "No superclass defined"; // TODO: LinkageError
1328 return false;
1329 }
1330 // Verify
1331 if (super->IsFinal()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001332 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is declared final"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001333 return false;
1334 }
1335 if (super->IsInterface()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001336 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is an interface"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001337 return false;
1338 }
1339 if (!klass->CanAccess(super)) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001340 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is inaccessible"; // TODO: IllegalAccessError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001341 return false;
1342 }
1343 return true;
1344}
1345
1346// Populate the class vtable and itable.
1347bool ClassLinker::LinkMethods(Class* klass) {
1348 if (klass->IsInterface()) {
1349 // No vtable.
1350 size_t count = klass->NumVirtualMethods();
1351 if (!IsUint(16, count)) {
1352 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1353 return false;
1354 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001355 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001356 klass->GetVirtualMethod(i)->method_index_ = i;
1357 }
1358 } else {
1359 // Link virtual method tables
1360 LinkVirtualMethods(klass);
1361
1362 // Link interface method tables
1363 LinkInterfaceMethods(klass);
1364
1365 // Insert stubs.
1366 LinkAbstractMethods(klass);
1367 }
1368 return true;
1369}
1370
1371bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001372 if (klass->HasSuperClass()) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001373 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001374 size_t actual_count = klass->GetSuperClass()->vtable_->GetLength();
1375 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001376 // TODO: do not assign to the vtable field until it is fully constructed.
1377 klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001378 // See if any of our virtual methods override the superclass.
1379 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1380 Method* local_method = klass->GetVirtualMethod(i);
1381 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001382 for (; j < actual_count; ++j) {
1383 Method* super_method = klass->vtable_->Get(j);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001384 if (local_method->HasSameNameAndDescriptor(super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001385 // Verify
1386 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001387 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001388 return false;
1389 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001390 klass->vtable_->Set(j, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001391 local_method->method_index_ = j;
1392 break;
1393 }
1394 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001395 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001396 // Not overriding, append.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001397 klass->vtable_->Set(actual_count, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001398 local_method->method_index_ = actual_count;
1399 actual_count += 1;
1400 }
1401 }
1402 if (!IsUint(16, actual_count)) {
1403 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1404 return false;
1405 }
1406 CHECK_LE(actual_count, max_count);
1407 if (actual_count < max_count) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001408 // TODO: do not assign to the vtable field until it is fully constructed.
1409 klass->vtable_ = klass->vtable_->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001410 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001411 } else {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001412 CHECK(klass->GetDescriptor()->Equals("Ljava/lang/Object;"));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001413 uint32_t num_virtual_methods = klass->NumVirtualMethods();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001414 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001415 LG << "Too many methods"; // TODO: VirtualMachineError
1416 return false;
1417 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001418 // TODO: do not assign to the vtable field until it is fully constructed.
1419 klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods);
1420 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001421 klass->vtable_->Set(i, klass->GetVirtualMethod(i));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001422 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1423 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001424 }
1425 return true;
1426}
1427
1428bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1429 int pool_offset = 0;
1430 int pool_size = 0;
1431 int miranda_count = 0;
1432 int miranda_alloc = 0;
1433 size_t super_ifcount;
1434 if (klass->HasSuperClass()) {
1435 super_ifcount = klass->GetSuperClass()->iftable_count_;
1436 } else {
1437 super_ifcount = 0;
1438 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001439 size_t ifcount = super_ifcount;
1440 ifcount += klass->NumInterfaces();
1441 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1442 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001443 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001444 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001445 DCHECK(klass->iftable_count_ == 0);
1446 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001447 return true;
1448 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001449 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1450 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001451 if (super_ifcount != 0) {
1452 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1453 sizeof(InterfaceEntry) * super_ifcount);
1454 }
1455 // Flatten the interface inheritance hierarchy.
1456 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001457 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1458 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001459 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001460 if (!interf->IsInterface()) {
1461 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1462 return false;
1463 }
1464 klass->iftable_[idx++].SetClass(interf);
1465 for (size_t j = 0; j < interf->iftable_count_; j++) {
1466 klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
1467 }
1468 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001469 CHECK_EQ(idx, ifcount);
1470 klass->iftable_count_ = ifcount;
1471 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001472 return true;
1473 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001474 for (size_t i = super_ifcount; i < ifcount; i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001475 pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
1476 }
1477 if (pool_size == 0) {
1478 return true;
1479 }
1480 klass->ifvi_pool_count_ = pool_size;
1481 klass->ifvi_pool_ = new uint32_t[pool_size];
1482 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001483 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001484 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
1485 Class* interface = klass->iftable_[i].GetClass();
1486 pool_offset += interface->NumVirtualMethods(); // end here
1487 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1488 Method* interface_method = interface->GetVirtualMethod(j);
1489 int k; // must be signed
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001490 for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001491 Method* vtable_method = klass->vtable_->Get(k);
1492 if (interface_method->HasSameNameAndDescriptor(vtable_method)) {
1493 if (!vtable_method->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001494 LG << "Implementation not public";
1495 return false;
1496 }
1497 klass->iftable_[i].method_index_array_[j] = k;
1498 break;
1499 }
1500 }
1501 if (k < 0) {
1502 if (miranda_count == miranda_alloc) {
1503 miranda_alloc += 8;
1504 if (miranda_list.empty()) {
1505 miranda_list.resize(miranda_alloc);
1506 } else {
1507 miranda_list.resize(miranda_alloc);
1508 }
1509 }
1510 int mir;
1511 for (mir = 0; mir < miranda_count; mir++) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001512 Method* miranda_method = miranda_list[mir];
1513 if (miranda_method->HasSameNameAndDescriptor(interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001514 break;
1515 }
1516 }
1517 // point the interface table at a phantom slot index
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001518 klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001519 if (mir == miranda_count) {
1520 miranda_list[miranda_count++] = interface_method;
1521 }
1522 }
1523 }
1524 }
1525 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001526 int old_method_count = klass->NumVirtualMethods();
1527 int new_method_count = old_method_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001528 klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001529
1530 CHECK(klass->vtable_ != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001531 int old_vtable_count = klass->vtable_->GetLength();
1532 int new_vtable_count = old_vtable_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001533 // TODO: do not assign to the vtable field until it is fully constructed.
1534 klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001535
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001536 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001537 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001538 memcpy(meth, miranda_list[i], sizeof(Method));
1539 meth->klass_ = klass;
1540 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001541 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1542 klass->SetVirtualMethod(old_method_count + i, meth);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001543 klass->vtable_->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001544 }
1545 }
1546 return true;
1547}
1548
1549void ClassLinker::LinkAbstractMethods(Class* klass) {
1550 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1551 Method* method = klass->GetVirtualMethod(i);
1552 if (method->IsAbstract()) {
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001553 LG << "AbstractMethodError";
1554 method->code_off_ = 0xFFFFFFFF;
1555 // TODO: throw AbstractMethodError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001556 }
1557 }
1558}
1559
Jesse Wilson7833bd22011-08-09 18:31:44 -04001560// Each static field will be stored in one of three arrays: static_references_,
1561// static_32bit_primitives_, or static_64bit_primitives_. This assigns each
1562// field a slot in its array and create the arrays.
1563bool ClassLinker::LinkStaticFields(Class* klass) {
1564 size_t next_reference_slot = 0;
1565 size_t next_32bit_primitive_slot = 0;
1566 size_t next_64bit_primitive_slot = 0;
1567
1568 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001569 Field* field = klass->GetStaticField(i);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001570 char type = field->GetType();
1571 if (type == '[' || type == 'L') {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001572 field->offset_ = next_reference_slot++;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001573 } else if (type == 'J' || type == 'D') {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001574 field->offset_ = next_64bit_primitive_slot++;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001575 } else {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001576 field->offset_ = next_32bit_primitive_slot++;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001577 }
1578 }
1579
1580 if (next_reference_slot > 0) {
1581 Class* array_class = GetClassRoot(kObjectArrayClass);
1582 klass->static_references_ = ObjectArray<Object>::Alloc(array_class, next_reference_slot);
1583 }
1584 if (next_32bit_primitive_slot > 0) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001585 klass->static_32bit_primitives_ = IntArray::Alloc(next_32bit_primitive_slot);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001586 }
1587 if (next_64bit_primitive_slot > 0) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001588 klass->static_64bit_primitives_ = LongArray::Alloc(next_64bit_primitive_slot);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001589 }
1590
1591 return true;
1592}
1593
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001594bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001595 int field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001596 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001597 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001598 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001599 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001600 }
1601 // Move references to the front.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001602 klass->num_reference_instance_fields_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001603 size_t i = 0;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001604 for ( ; i < klass->NumInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001605 Field* pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001606 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001607 if (c != '[' && c != 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001608 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001609 Field* refField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001610 char rc = refField->GetType();
1611 if (rc == '[' || rc == 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001612 klass->SetInstanceField(i, refField);
1613 klass->SetInstanceField(j, pField);
1614 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001615 c = rc;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001616 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001617 break;
1618 }
1619 }
1620 } else {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001621 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001622 }
1623 if (c != '[' && c != 'L') {
1624 break;
1625 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001626 pField->SetOffset(field_offset);
1627 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001628 }
1629
1630 // Now we want to pack all of the double-wide fields together. If
1631 // we're not aligned, though, we want to shuffle one 32-bit field
1632 // into place. If we can't find one, we'll have to pad it.
Brian Carlstroma0808032011-07-18 00:39:23 -07001633 if (i != klass->NumInstanceFields() && (field_offset & 0x04) != 0) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001634 Field* pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001635 char c = pField->GetType();
1636
1637 if (c != 'J' && c != 'D') {
1638 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001639 DCHECK(c != '[');
1640 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001641 pField->SetOffset(field_offset);
1642 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001643 i++;
1644 } else {
1645 // Next field is 64-bit, so search for a 32-bit field we can
1646 // swap into it.
1647 bool found = false;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001648 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001649 Field* singleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001650 char rc = singleField->GetType();
1651 if (rc != 'J' && rc != 'D') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001652 klass->SetInstanceField(i, singleField);
1653 klass->SetInstanceField(j, pField);
1654 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001655 pField->SetOffset(field_offset);
1656 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001657 found = true;
1658 i++;
1659 break;
1660 }
1661 }
1662 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001663 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001664 }
1665 }
1666 }
1667
1668 // Alignment is good, shuffle any double-wide fields forward, and
1669 // finish assigning field offsets to all fields.
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001670 DCHECK(i == klass->NumInstanceFields() || (field_offset & 0x04) == 0);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001671 for ( ; i < klass->NumInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001672 Field* pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001673 char c = pField->GetType();
1674 if (c != 'D' && c != 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001675 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001676 Field* doubleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001677 char rc = doubleField->GetType();
1678 if (rc == 'D' || rc == 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001679 klass->SetInstanceField(i, doubleField);
1680 klass->SetInstanceField(j, pField);
1681 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001682 c = rc;
1683 break;
1684 }
1685 }
1686 } else {
1687 // This is a double-wide field, leave it be.
1688 }
1689
Brian Carlstroma0808032011-07-18 00:39:23 -07001690 pField->SetOffset(field_offset);
1691 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001692 if (c == 'J' || c == 'D')
Brian Carlstroma0808032011-07-18 00:39:23 -07001693 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001694 }
1695
1696#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001697 // Make sure that all reference fields appear before
1698 // non-reference fields, and all double-wide fields are aligned.
1699 bool seen_non_ref = false;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001700 for (i = 0; i < klass->NumInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001701 Field *pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001702 char c = pField->GetType();
1703
1704 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001705 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001706 }
1707
1708 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001709 if (!seen_non_ref) {
1710 seen_non_ref = true;
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001711 DCHECK_EQ(klass->NumReferenceInstanceFields(), i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001712 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001713 } else {
1714 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001715 }
1716 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001717 if (!seen_non_ref) {
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001718 DCHECK_EQ(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001719 }
1720#endif
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001721 klass->object_size_ = field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001722 return true;
1723}
1724
1725// Set the bitmap of reference offsets, refOffsets, from the ifields
1726// list.
1727void ClassLinker::CreateReferenceOffsets(Class* klass) {
1728 uint32_t reference_offsets = 0;
1729 if (klass->HasSuperClass()) {
1730 reference_offsets = klass->GetSuperClass()->GetReferenceOffsets();
1731 }
1732 // If our superclass overflowed, we don't stand a chance.
1733 if (reference_offsets != CLASS_WALK_SUPER) {
1734 // All of the fields that contain object references are guaranteed
1735 // to be at the beginning of the ifields list.
1736 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) {
1737 // Note that, per the comment on struct InstField, f->byteOffset
1738 // is the offset from the beginning of obj, not the offset into
1739 // obj->instanceData.
Jesse Wilson35baaab2011-08-10 16:18:03 -04001740 const Field* field = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001741 size_t byte_offset = field->GetOffset();
1742 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001743 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001744 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1745 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001746 CHECK_NE(new_bit, 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001747 reference_offsets |= new_bit;
1748 } else {
1749 reference_offsets = CLASS_WALK_SUPER;
1750 break;
1751 }
1752 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001753 }
Brian Carlstromae3ac012011-07-27 01:30:28 -07001754 klass->SetReferenceOffsets(reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001755}
1756
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001757Class* ClassLinker::ResolveClass(const Class* referrer,
1758 uint32_t class_idx,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001759 const DexFile& dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001760 DexCache* dex_cache = referrer->GetDexCache();
1761 Class* resolved = dex_cache->GetResolvedClass(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001762 if (resolved != NULL) {
1763 return resolved;
1764 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001765 const char* descriptor = dex_file.dexStringByTypeIdx(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001766 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001767 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001768 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001769 resolved = FindClass(descriptor, referrer->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001770 }
1771 if (resolved != NULL) {
1772 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001773 if (referrer->GetDexCache() != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001774 if (check->GetClassLoader() != NULL) {
1775 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1776 return NULL;
1777 }
1778 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001779 dex_cache->SetResolvedClass(class_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001780 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001781 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001782 }
1783 return resolved;
1784}
1785
1786Method* ResolveMethod(const Class* referrer, uint32_t method_idx,
1787 /*MethodType*/ int method_type) {
1788 CHECK(false);
1789 return NULL;
1790}
1791
Carl Shapiro69759ea2011-07-21 18:13:35 -07001792String* ClassLinker::ResolveString(const Class* referring,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001793 uint32_t string_idx,
1794 const DexFile& dex_file) {
1795 const DexFile::StringId& string_id = dex_file.GetStringId(string_idx);
1796 int32_t utf16_length = dex_file.GetStringLength(string_id);
1797 const char* utf8_data = dex_file.GetStringData(string_id);
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001798 String* string = intern_table_.Intern(utf16_length, utf8_data);
1799 referring->GetDexCache()->SetResolvedString(string_idx, string);
1800 return string;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001801}
1802
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001803} // namespace art