blob: 55aaff4888963fb6dd086bf9b7491bb4d64826c2 [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"
17#include "thread.h"
18#include "utils.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070019
20namespace art {
21
Carl Shapiro2ed144c2011-07-26 16:52:08 -070022ClassLinker* ClassLinker::Create(const std::vector<DexFile*>& boot_class_path) {
Carl Shapiro61e019d2011-07-14 16:53:09 -070023 scoped_ptr<ClassLinker> class_linker(new ClassLinker);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070024 class_linker->Init(boot_class_path);
Carl Shapiro61e019d2011-07-14 16:53:09 -070025 // TODO: check for failure during initialization
26 return class_linker.release();
27}
28
Carl Shapiro2ed144c2011-07-26 16:52:08 -070029void ClassLinker::Init(const std::vector<DexFile*>& boot_class_path) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070030 CHECK(!init_done_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070031
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070032 // java_lang_Class comes first, its needed for AllocClass
33 Class* java_lang_Class = down_cast<Class*>(Heap::AllocObject(NULL, sizeof(Class)));
34 CHECK(java_lang_Class != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070035 // java_lang_Class->descriptor_ = "Ljava/lang/Class;"; // XXX bdc can these be created later?
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070036 java_lang_Class->object_size_ = sizeof(Class);
37 java_lang_Class->klass_ = java_lang_Class;
38 // AllocClass(Class*) can now be used
Brian Carlstroma0808032011-07-18 00:39:23 -070039
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070040 // java_lang_Object comes next so that object_array_class can be created
41 Class* java_lang_Object = AllocClass(java_lang_Class);
42 CHECK(java_lang_Object != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070043 // java_lang_Object->descriptor_ = "Ljava/lang/Object;"; // XXX bdc can these be created later?
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070044 // backfill Object as the super class of Class
45 java_lang_Class->super_class_ = java_lang_Object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070046 // mark as non-primitive for object_array_class
47 java_lang_Object->primitive_type_ = Class::kPrimNot;
Brian Carlstroma0808032011-07-18 00:39:23 -070048
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070049 // object_array_class is for root_classes to provide the storage for these classes
50 Class* object_array_class = AllocClass(java_lang_Class);
51 CHECK(object_array_class != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070052 // object_array_class->descriptor_ = "[Ljava/lang/Object;"; // XXX bdc can these be created later?
Brian Carlstromdb4d5402011-08-09 12:18:28 -070053 object_array_class->component_type_ = java_lang_Object;
Brian Carlstroma0808032011-07-18 00:39:23 -070054
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070055 // String and char[] are necessary so that FindClass can assign names to members
Jesse Wilson14150742011-07-29 19:04:44 -040056 Class* java_lang_String = AllocClass(java_lang_Class);
57 CHECK(java_lang_String != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070058 // java_lang_String->descriptor_ = "Ljava/lang/String;"; // XXX can these be created later?
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070059 CHECK_LT(java_lang_String->object_size_, sizeof(String));
Jesse Wilson14150742011-07-29 19:04:44 -040060 java_lang_String->object_size_ = sizeof(String);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070061 String::InitClass(java_lang_String);
Jesse Wilson8989d992011-08-02 13:39:42 -070062 Class* char_array_class = AllocClass(java_lang_Class);
63 CHECK(char_array_class != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070064 // char_array_class->descriptor_ = "[C"; // XXX can these be created later?
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070065 CharArray::SetArrayClass(char_array_class);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070066 // Now String::Alloc* can be used
67
68 // backfill Class descriptors missing until this point
69 java_lang_Class->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/Class;");
70 java_lang_Object->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/Object;");
71 object_array_class->descriptor_ = String::AllocFromModifiedUtf8("[Ljava/lang/Object;");
72 java_lang_String->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/String;");
73 char_array_class->descriptor_ = String::AllocFromModifiedUtf8("[C");
Jesse Wilson14150742011-07-29 19:04:44 -040074
Jesse Wilson7833bd22011-08-09 18:31:44 -040075 // int[] and long[] are used for static field storage
76 Class* int_array_class = AllocClass(java_lang_Class);
77 CHECK(int_array_class != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070078 int_array_class->descriptor_ = String::AllocFromModifiedUtf8("[I");
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070079 IntArray::SetArrayClass(int_array_class);
Jesse Wilson7833bd22011-08-09 18:31:44 -040080 Class* long_array_class = AllocClass(java_lang_Class);
81 CHECK(long_array_class != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070082 long_array_class->descriptor_ = String::AllocFromModifiedUtf8("[J");
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070083 LongArray::SetArrayClass(long_array_class);
Jesse Wilson7833bd22011-08-09 18:31:44 -040084
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070085 // Field and Method are necessary so that FindClass can link members
86 Class* java_lang_reflect_Field = AllocClass(java_lang_Class);
87 CHECK(java_lang_reflect_Field != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070088 java_lang_reflect_Field->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/reflect/Field;");
Jesse Wilson35baaab2011-08-10 16:18:03 -040089 CHECK_LT(java_lang_reflect_Field->object_size_, sizeof(Field));
90 java_lang_reflect_Field->object_size_ = sizeof(Field);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070091 Class* java_lang_reflect_Method = AllocClass(java_lang_Class);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070092 java_lang_reflect_Method->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/reflect/Method;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070093 CHECK(java_lang_reflect_Method != NULL);
94 CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method));
95 java_lang_reflect_Method->object_size_ = sizeof(Method);
96
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070097 // create storage for root classes, save away our work so far
98 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
99 class_roots_->Set(kJavaLangClass, java_lang_Class);
100 class_roots_->Set(kJavaLangObject, java_lang_Object);
101 class_roots_->Set(kObjectArrayClass, object_array_class);
Jesse Wilson14150742011-07-29 19:04:44 -0400102 class_roots_->Set(kJavaLangString, java_lang_String);
Jesse Wilson8989d992011-08-02 13:39:42 -0700103 class_roots_->Set(kCharArrayClass, char_array_class);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400104 class_roots_->Set(kIntArrayClass, int_array_class);
105 class_roots_->Set(kLongArrayClass, long_array_class);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700106 class_roots_->Set(kJavaLangReflectField, java_lang_reflect_Field);
107 class_roots_->Set(kJavaLangReflectMethod, java_lang_reflect_Method);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700108 // now that these are registered, we can use AllocClass() and AllocObjectArray
Brian Carlstroma0808032011-07-18 00:39:23 -0700109
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700110 // setup boot_class_path_ now that we can use AllocObjectArray to
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700111 // create DexCache instances
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700112 for (size_t i = 0; i != boot_class_path.size(); ++i) {
113 AppendToBootClassPath(boot_class_path[i]);
114 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700115 // now we can use FindSystemClass, at least for non-arrays classes.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700116
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700117 // run Class, Field, and Method through FindSystemClass.
118 // this initializes their dex_cache_ fields and register them in classes_.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700119 // we also override their object_size_ values to accommodate the extra C++ fields.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700120 Class* Class_class = FindSystemClass("Ljava/lang/Class;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700121 CHECK_EQ(java_lang_Class, Class_class);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700122 CHECK_LT(java_lang_Class->object_size_, sizeof(Class));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700123 java_lang_Class->object_size_ = sizeof(Class);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700124 Class* Field_class = FindSystemClass("Ljava/lang/reflect/Field;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700125 CHECK_EQ(java_lang_reflect_Field, Field_class);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400126 CHECK_LT(java_lang_reflect_Field->object_size_, sizeof(Field));
127 java_lang_reflect_Field->object_size_ = sizeof(Field);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700128 Class* Method_class = FindSystemClass("Ljava/lang/reflect/Method;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700129 CHECK_EQ(java_lang_reflect_Method, Method_class);
130 CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700131 java_lang_reflect_Method->object_size_ = sizeof(Method);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700132
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700133 // Object and String just need more minimal setup, since they do not have extra C++ fields.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700134 Class* Object_class = FindSystemClass("Ljava/lang/Object;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700135 CHECK_EQ(java_lang_Object, Object_class);
136 CHECK_EQ(java_lang_Object->object_size_, sizeof(Object));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700137 Class* String_class = FindSystemClass("Ljava/lang/String;");
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700138 CHECK_EQ(java_lang_String, String_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700139 CHECK_EQ(java_lang_String->object_size_, sizeof(String));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700140
141 // Setup the ClassLoaders, adjusting the object_size_ as necessary
142 Class* java_lang_ClassLoader = FindSystemClass("Ljava/lang/ClassLoader;");
143 CHECK(java_lang_ClassLoader != NULL);
144 CHECK_LT(java_lang_ClassLoader->object_size_, sizeof(ClassLoader));
145 java_lang_ClassLoader->object_size_ = sizeof(ClassLoader);
146 class_roots_->Set(kJavaLangClassLoader, java_lang_ClassLoader);
147 Class* dalvik_system_BaseDexClassLoader = FindSystemClass("Ldalvik/system/BaseDexClassLoader;");
148 CHECK(dalvik_system_BaseDexClassLoader != NULL);
149 CHECK_EQ(dalvik_system_BaseDexClassLoader->object_size_, sizeof(BaseDexClassLoader));
150 class_roots_->Set(kDalvikSystemBaseDexClassLoader, dalvik_system_BaseDexClassLoader);
151 Class* dalvik_system_PathClassLoader = FindSystemClass("Ldalvik/system/PathClassLoader;");
152 CHECK(dalvik_system_PathClassLoader != NULL);
153 CHECK_EQ(dalvik_system_PathClassLoader->object_size_, sizeof(PathClassLoader));
154 class_roots_->Set(kDalvikSystemPathClassLoader, dalvik_system_PathClassLoader);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700155
156 // Setup a single, global copy of "interfaces" and "iftable" for
157 // reuse across array classes
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700158 Class* java_lang_Cloneable = FindSystemClass("Ljava/lang/Cloneable;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700159 CHECK(java_lang_Cloneable != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700160 Class* java_io_Serializable = FindSystemClass("Ljava/io/Serializable;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700161 CHECK(java_io_Serializable != NULL);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700162
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700163 array_interfaces_ = AllocObjectArray<Class>(2);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700164 CHECK(array_interfaces_ != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700165 array_interfaces_->Set(0, java_lang_Cloneable);
166 array_interfaces_->Set(1, java_io_Serializable);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700167
168 // We assume that Cloneable/Serializable don't have superinterfaces --
169 // normally we'd have to crawl up and explicitly list all of the
170 // supers as well. These interfaces don't have any methods, so we
171 // don't have to worry about the ifviPool either.
172 array_iftable_ = new InterfaceEntry[2];
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700173 array_iftable_[0].SetClass(array_interfaces_->Get(0));
174 array_iftable_[1].SetClass(array_interfaces_->Get(1));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700175 // now FindClass can be used for non-primitive array classes
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700176
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700177 // run Object[] through FindClass to complete initialization
Jesse Wilson8989d992011-08-02 13:39:42 -0700178 Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;");
179 CHECK_EQ(object_array_class, found_object_array_class);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700180 CHECK_EQ(java_lang_Cloneable, object_array_class->GetInterface(0));
181 CHECK_EQ(java_io_Serializable, object_array_class->GetInterface(1));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700182
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700183 // Setup the primitive type classes.
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700184 class_roots_->Set(kPrimitiveBoolean, CreatePrimitiveClass("Z"));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700185 class_roots_->Set(kPrimitiveByte, CreatePrimitiveClass("B"));
186 class_roots_->Set(kPrimitiveChar, CreatePrimitiveClass("C"));
187 class_roots_->Set(kPrimitiveDouble, CreatePrimitiveClass("D"));
188 class_roots_->Set(kPrimitiveFloat, CreatePrimitiveClass("F"));
189 class_roots_->Set(kPrimitiveInt, CreatePrimitiveClass("I"));
190 class_roots_->Set(kPrimitiveLong, CreatePrimitiveClass("J"));
191 class_roots_->Set(kPrimitiveShort, CreatePrimitiveClass("S"));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700192 class_roots_->Set(kPrimitiveVoid, CreatePrimitiveClass("V"));
193 // now we can use FindSystemClass for anything, including for "[C"
194
Jesse Wilson7833bd22011-08-09 18:31:44 -0400195 // run char[], int[] and long[] through FindClass to complete initialization
Jesse Wilson8989d992011-08-02 13:39:42 -0700196 Class* found_char_array_class = FindSystemClass("[C");
197 CHECK_EQ(char_array_class, found_char_array_class);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400198 Class* found_int_array_class = FindSystemClass("[I");
199 CHECK_EQ(int_array_class, found_int_array_class);
200 Class* found_long_array_class = FindSystemClass("[J");
201 CHECK_EQ(long_array_class, found_long_array_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700202
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700203 // Initialize all the other primitive array types for PrimitiveArray::Alloc.
204 // These are easy because everything we need has already been set up.
205 class_roots_->Set(kBooleanArrayClass, FindSystemClass("[Z"));
206 class_roots_->Set(kByteArrayClass, FindSystemClass("[B"));
207 class_roots_->Set(kDoubleArrayClass, FindSystemClass("[D"));
208 class_roots_->Set(kFloatArrayClass, FindSystemClass("[F"));
209 class_roots_->Set(kShortArrayClass, FindSystemClass("[S"));
210 BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass));
211 ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass));
212 DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass));
213 FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass));
214 ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass));
215
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700216 // ensure all class_roots_ were initialized
217 for (size_t i = 0; i < kClassRootsMax; i++) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700218 CHECK(GetClassRoot(static_cast<ClassRoot>(i)));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700219 }
220
221 // disable the slow paths in FindClass and CreatePrimitiveClass now
222 // that Object, Class, and Object[] are setup
223 init_done_ = true;
224}
225
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700226void ClassLinker::VisitRoots(Heap::RootVistor* root_visitor, void* arg) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700227 root_visitor(class_roots_, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700228
229 for (size_t i = 0; i < dex_caches_.size(); i++) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700230 root_visitor(dex_caches_[i], arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700231 }
232
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700233 {
234 MutexLock mu(classes_lock_);
235 typedef Table::const_iterator It; // TODO: C++0x auto
236 for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
237 root_visitor(it->second, arg);
238 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700239 }
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700240
241 intern_table_.VisitRoots(root_visitor, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700242
Brian Carlstromb88e9442011-07-28 15:15:51 -0700243 root_visitor(array_interfaces_, arg);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700244}
245
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700246DexCache* ClassLinker::AllocDexCache() {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700247 return down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::kMax));
Brian Carlstroma0808032011-07-18 00:39:23 -0700248}
249
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700250Class* ClassLinker::AllocClass(Class* java_lang_Class) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700251 return java_lang_Class->NewInstance()->AsClass();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700252}
253
254Class* ClassLinker::AllocClass() {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700255 return AllocClass(GetClassRoot(kJavaLangClass));
Brian Carlstroma0808032011-07-18 00:39:23 -0700256}
257
Jesse Wilson35baaab2011-08-10 16:18:03 -0400258Field* ClassLinker::AllocField() {
259 return down_cast<Field*>(GetClassRoot(kJavaLangReflectField)->NewInstance());
Brian Carlstroma0808032011-07-18 00:39:23 -0700260}
261
262Method* ClassLinker::AllocMethod() {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700263 return down_cast<Method*>(GetClassRoot(kJavaLangReflectMethod)->NewInstance());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700264}
265
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700266// TODO: remove once we can use java.lang.Class.getSystemClassLoader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700267PathClassLoader* ClassLinker::AllocPathClassLoader(std::vector<const DexFile*> dex_files) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700268 PathClassLoader* cl = down_cast<PathClassLoader*>(GetClassRoot(kDalvikSystemPathClassLoader)->NewInstance());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700269 cl->SetClassPath(dex_files);
270 return cl;
Carl Shapiro565f5072011-07-10 13:39:43 -0700271}
272
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700273Class* ClassLinker::FindClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700274 ClassLoader* class_loader) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700275 // TODO: remove this contrived parent class loader check when we have a real ClassLoader.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700276 if (class_loader != NULL) {
277 Class* klass = FindClass(descriptor, NULL);
278 if (klass != NULL) {
279 return klass;
280 }
281 }
282
Carl Shapirob5573532011-07-12 18:22:59 -0700283 Thread* self = Thread::Current();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700284 DCHECK(self != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700285 CHECK(!self->IsExceptionPending());
286 // Find the class in the loaded classes table.
287 Class* klass = LookupClass(descriptor, class_loader);
288 if (klass == NULL) {
289 // Class is not yet loaded.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700290 if (descriptor[0] == '[') {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700291 return CreateArrayClass(descriptor, class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700292 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700293 DexFile::ClassPath& class_path = ((class_loader != NULL) ? class_loader->GetClassPath() : boot_class_path_);
294 DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, class_path);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700295 if (pair.second == NULL) {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700296 LG << "Class " << PrintableString(descriptor) << " not found in class loader " << class_loader; // TODO: NoClassDefFoundError
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700297 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700298 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700299 const DexFile& dex_file = *pair.first;
300 const DexFile::ClassDef& dex_class_def = *pair.second;
301 DexCache* dex_cache = FindDexCache(pair.first);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700302 // Load the class from the dex file.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700303 if (!init_done_) {
304 // finish up init of hand crafted class_roots_
305 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700306 klass = GetClassRoot(kJavaLangObject);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700307 } else if (descriptor == "Ljava/lang/Class;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700308 klass = GetClassRoot(kJavaLangClass);
Jesse Wilson14150742011-07-29 19:04:44 -0400309 } else if (descriptor == "Ljava/lang/String;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700310 klass = GetClassRoot(kJavaLangString);
311 } else if (descriptor == "Ljava/lang/reflect/Field;") {
312 klass = GetClassRoot(kJavaLangReflectField);
313 } else if (descriptor == "Ljava/lang/reflect/Method;") {
314 klass = GetClassRoot(kJavaLangReflectMethod);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700315 } else {
316 klass = AllocClass();
317 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700318 } else {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700319 klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700320 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700321 klass->dex_cache_ = dex_cache;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700322 LoadClass(dex_file, dex_class_def, klass, class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700323 // Check for a pending exception during load
324 if (self->IsExceptionPending()) {
325 // TODO: free native allocations in klass
326 return NULL;
327 }
328 {
329 ObjectLock lock(klass);
Carl Shapirob5573532011-07-12 18:22:59 -0700330 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700331 // Add the newly loaded class to the loaded classes table.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700332 bool success = InsertClass(descriptor, klass); // TODO: just return collision
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700333 if (!success) {
334 // We may fail to insert if we raced with another thread.
335 klass->clinit_thread_id_ = 0;
336 // TODO: free native allocations in klass
337 klass = LookupClass(descriptor, class_loader);
338 CHECK(klass != NULL);
339 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700340 // Finish loading (if necessary) by finding parents
341 if (!klass->IsLoaded() && !LoadSuperAndInterfaces(klass, dex_file)) {
342 // Loading failed.
343 // TODO: CHECK(self->IsExceptionPending());
344 lock.NotifyAll();
345 return NULL;
346 }
347 CHECK(klass->IsLoaded());
348 // Link the class (if necessary)
349 if (!klass->IsLinked() && !LinkClass(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700350 // Linking failed.
351 // TODO: CHECK(self->IsExceptionPending());
352 lock.NotifyAll();
353 return NULL;
354 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700355 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700356 }
357 }
358 }
359 // Link the class if it has not already been linked.
360 if (!klass->IsLinked() && !klass->IsErroneous()) {
361 ObjectLock lock(klass);
362 // Check for circular dependencies between classes.
Carl Shapirob5573532011-07-12 18:22:59 -0700363 if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700364 LG << "Recursive link"; // TODO: ClassCircularityError
365 return NULL;
366 }
367 // Wait for the pending initialization to complete.
368 while (!klass->IsLinked() && !klass->IsErroneous()) {
369 lock.Wait();
370 }
371 }
372 if (klass->IsErroneous()) {
373 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
374 return NULL;
375 }
376 // Return the loaded class. No exceptions should be pending.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700377 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700378 CHECK(!self->IsExceptionPending());
379 return klass;
380}
381
Brian Carlstromf615a612011-07-23 12:50:34 -0700382void ClassLinker::LoadClass(const DexFile& dex_file,
383 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700384 Class* klass,
385 ClassLoader* class_loader) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700386 CHECK(klass != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700387 CHECK(klass->dex_cache_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700388 CHECK_EQ(Class::kStatusNotReady, klass->status_);
Brian Carlstromf615a612011-07-23 12:50:34 -0700389 const byte* class_data = dex_file.GetClassData(dex_class_def);
390 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700391
Brian Carlstromf615a612011-07-23 12:50:34 -0700392 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700393 CHECK(descriptor != NULL);
394
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700395 klass->klass_ = GetClassRoot(kJavaLangClass);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700396 klass->descriptor_ = String::AllocFromModifiedUtf8(descriptor);
Brian Carlstromf615a612011-07-23 12:50:34 -0700397 klass->access_flags_ = dex_class_def.access_flags_;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700398 klass->class_loader_ = class_loader;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700399 klass->primitive_type_ = Class::kPrimNot;
400 klass->status_ = Class::kStatusIdx;
401
402 klass->super_class_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700403 klass->super_class_idx_ = dex_class_def.superclass_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700404
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700405 size_t num_static_fields = header.static_fields_size_;
406 size_t num_instance_fields = header.instance_fields_size_;
407 size_t num_direct_methods = header.direct_methods_size_;
408 size_t num_virtual_methods = header.virtual_methods_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700409
Brian Carlstromf615a612011-07-23 12:50:34 -0700410 klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700411
412 // Load class interfaces.
Brian Carlstromf615a612011-07-23 12:50:34 -0700413 LoadInterfaces(dex_file, dex_class_def, klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700414
415 // Load static fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700416 DCHECK(klass->sfields_ == NULL);
417 if (num_static_fields != 0) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400418 klass->sfields_ = AllocObjectArray<Field>(num_static_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700419 uint32_t last_idx = 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700420 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700421 DexFile::Field dex_field;
422 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400423 Field* sfield = AllocField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700424 klass->SetStaticField(i, sfield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700425 LoadField(dex_file, dex_field, klass, sfield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700426 }
427 }
428
429 // Load instance fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700430 DCHECK(klass->ifields_ == NULL);
431 if (num_instance_fields != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700432 // TODO: allocate on the object heap.
Jesse Wilson35baaab2011-08-10 16:18:03 -0400433 klass->ifields_ = AllocObjectArray<Field>(num_instance_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700434 uint32_t last_idx = 0;
435 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700436 DexFile::Field dex_field;
437 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400438 Field* ifield = AllocField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700439 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700440 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700441 }
442 }
443
444 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700445 DCHECK(klass->direct_methods_ == NULL);
446 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700447 // TODO: append direct methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700448 klass->direct_methods_ = AllocObjectArray<Method>(num_direct_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700449 uint32_t last_idx = 0;
450 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700451 DexFile::Method dex_method;
452 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700453 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700454 klass->SetDirectMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700455 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700456 // TODO: register maps
457 }
458 }
459
460 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700461 DCHECK(klass->virtual_methods_ == NULL);
462 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700463 // TODO: append virtual methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700464 klass->virtual_methods_ = AllocObjectArray<Method>(num_virtual_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700465 uint32_t last_idx = 0;
466 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700467 DexFile::Method dex_method;
468 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700469 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700470 klass->SetVirtualMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700471 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700472 // TODO: register maps
473 }
474 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700475}
476
Brian Carlstromf615a612011-07-23 12:50:34 -0700477void ClassLinker::LoadInterfaces(const DexFile& dex_file,
478 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700479 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700480 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700481 if (list != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700482 DCHECK(klass->interfaces_ == NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700483 klass->interfaces_ = AllocObjectArray<Class>(list->Size());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700484 DCHECK(klass->interfaces_idx_ == NULL);
485 klass->interfaces_idx_ = new uint32_t[list->Size()];
Brian Carlstrom934486c2011-07-12 23:42:50 -0700486 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700487 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700488 klass->interfaces_idx_[i] = type_item.type_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700489 }
490 }
491}
492
Brian Carlstromf615a612011-07-23 12:50:34 -0700493void ClassLinker::LoadField(const DexFile& dex_file,
494 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700495 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700496 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700497 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400498 dst->declaring_class_ = klass;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700499 dst->name_ = ResolveString(klass, field_id.name_idx_, dex_file);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700500 dst->descriptor_.set(dex_file.dexStringByTypeIdx(field_id.type_idx_));
Jesse Wilson35baaab2011-08-10 16:18:03 -0400501 // TODO: Assign dst->type_.
Brian Carlstrom934486c2011-07-12 23:42:50 -0700502 dst->access_flags_ = src.access_flags_;
503}
504
Brian Carlstromf615a612011-07-23 12:50:34 -0700505void ClassLinker::LoadMethod(const DexFile& dex_file,
506 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700507 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700508 Method* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700509 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400510 dst->declaring_class_ = klass;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700511 dst->name_ = ResolveString(klass, method_id.name_idx_, dex_file);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700512 {
513 int32_t utf16_length;
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700514 scoped_array<char> utf8(dex_file.CreateMethodDescriptor(method_id.proto_idx_,
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700515 &utf16_length));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700516 dst->signature_ = String::AllocFromModifiedUtf8(utf16_length, utf8.get());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700517 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700518 dst->proto_idx_ = method_id.proto_idx_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700519 dst->code_off_ = src.code_off_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700520 dst->shorty_ = dex_file.GetShorty(method_id.proto_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700521 dst->access_flags_ = src.access_flags_;
522
523 // TODO: check for finalize method
524
Brian Carlstromf615a612011-07-23 12:50:34 -0700525 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700526 if (code_item != NULL) {
527 dst->num_registers_ = code_item->registers_size_;
528 dst->num_ins_ = code_item->ins_size_;
529 dst->num_outs_ = code_item->outs_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700530 } else {
531 uint16_t num_args = dst->NumArgRegisters();
532 if (!dst->IsStatic()) {
533 ++num_args;
534 }
535 dst->num_registers_ = dst->num_ins_ + num_args;
536 // TODO: native methods
537 }
538}
539
Brian Carlstromf615a612011-07-23 12:50:34 -0700540void ClassLinker::AppendToBootClassPath(DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700541 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700542 boot_class_path_.push_back(dex_file);
543 RegisterDexFile(dex_file);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700544}
545
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700546void ClassLinker::RegisterDexFile(const DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700547 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700548 dex_files_.push_back(dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700549 DexCache* dex_cache = AllocDexCache();
550 CHECK(dex_cache != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700551 dex_cache->Init(AllocObjectArray<String>(dex_file->NumStringIds()),
552 AllocObjectArray<Class>(dex_file->NumTypeIds()),
553 AllocObjectArray<Method>(dex_file->NumMethodIds()),
554 AllocObjectArray<Field>(dex_file->NumFieldIds()));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700555 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700556}
557
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700558const DexFile& ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700559 for (size_t i = 0; i != dex_caches_.size(); ++i) {
560 if (dex_caches_[i] == dex_cache) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700561 return *dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700562 }
563 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700564 CHECK(false) << "Could not find DexFile";
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700565 return *dex_files_[-1];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700566}
567
Brian Carlstromf615a612011-07-23 12:50:34 -0700568DexCache* ClassLinker::FindDexCache(const DexFile* dex_file) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700569 for (size_t i = 0; i != dex_files_.size(); ++i) {
570 if (dex_files_[i] == dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700571 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700572 }
573 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700574 CHECK(false) << "Could not find DexCache";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700575 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700576}
577
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700578Class* ClassLinker::CreatePrimitiveClass(const char* descriptor) {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700579 Class* klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700580 CHECK(klass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700581 klass->super_class_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700582 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700583 klass->descriptor_ = String::AllocFromModifiedUtf8(descriptor);
Carl Shapiro565f5072011-07-10 13:39:43 -0700584 klass->status_ = Class::kStatusInitialized;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700585 bool success = InsertClass(descriptor, klass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700586 CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed";
Carl Shapiro565f5072011-07-10 13:39:43 -0700587 return klass;
588}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700589
Brian Carlstrombe977852011-07-19 14:54:54 -0700590// Create an array class (i.e. the class object for the array, not the
591// array itself). "descriptor" looks like "[C" or "[[[[B" or
592// "[Ljava/lang/String;".
593//
594// If "descriptor" refers to an array of primitives, look up the
595// primitive type's internally-generated class object.
596//
597// "loader" is the class loader of the class that's referring to us. It's
598// used to ensure that we're looking for the element type in the right
599// context. It does NOT become the class loader for the array class; that
600// always comes from the base element class.
601//
602// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700603Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700604 ClassLoader* class_loader)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700605{
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700606 CHECK(descriptor[0] == '[');
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700607
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700608 // Identify the underlying element class and the array dimension depth.
609 Class* component_type_ = NULL;
610 int array_rank;
611 if (descriptor[1] == '[') {
612 // array of arrays; keep descriptor and grab stuff from parent
613 Class* outer = FindClass(descriptor.substr(1), class_loader);
614 if (outer != NULL) {
615 // want the base class, not "outer", in our component_type_
616 component_type_ = outer->component_type_;
617 array_rank = outer->array_rank_ + 1;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700618 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700619 DCHECK(component_type_ == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700620 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700621 } else {
622 array_rank = 1;
623 if (descriptor[1] == 'L') {
624 // array of objects; strip off "[" and look up descriptor.
625 const StringPiece subDescriptor = descriptor.substr(1);
626 component_type_ = FindClass(subDescriptor, class_loader);
627 } else {
628 // array of a primitive type
629 component_type_ = FindPrimitiveClass(descriptor[1]);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700630 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700631 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700632
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700633 if (component_type_ == NULL) {
634 // failed
635 // DCHECK(Thread::Current()->IsExceptionPending()); // TODO
636 return NULL;
637 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700638
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700639 // See if the component type is already loaded. Array classes are
640 // always associated with the class loader of their underlying
641 // element type -- an array of Strings goes with the loader for
642 // java/lang/String -- so we need to look for it there. (The
643 // caller should have checked for the existence of the class
644 // before calling here, but they did so with *their* class loader,
645 // not the component type's loader.)
646 //
647 // If we find it, the caller adds "loader" to the class' initiating
648 // loader list, which should prevent us from going through this again.
649 //
650 // This call is unnecessary if "loader" and "component_type_->class_loader_"
651 // are the same, because our caller (FindClass) just did the
652 // lookup. (Even if we get this wrong we still have correct behavior,
653 // because we effectively do this lookup again when we add the new
654 // class to the hash table --- necessary because of possible races with
655 // other threads.)
656 if (class_loader != component_type_->class_loader_) {
657 Class* new_class = LookupClass(descriptor, component_type_->class_loader_);
658 if (new_class != NULL) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700659 return new_class;
660 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700661 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700662
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700663 // Fill out the fields in the Class.
664 //
665 // It is possible to execute some methods against arrays, because
666 // all arrays are subclasses of java_lang_Object_, so we need to set
667 // up a vtable. We can just point at the one in java_lang_Object_.
668 //
669 // Array classes are simple enough that we don't need to do a full
670 // link step.
671
672 Class* new_class = NULL;
673 if (!init_done_) {
674 if (descriptor == "[Ljava/lang/Object;") {
675 new_class = GetClassRoot(kObjectArrayClass);
676 } else if (descriptor == "[C") {
677 new_class = GetClassRoot(kCharArrayClass);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400678 } else if (descriptor == "[I") {
679 new_class = GetClassRoot(kIntArrayClass);
680 } else if (descriptor == "[J") {
681 new_class = GetClassRoot(kLongArrayClass);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700682 }
683 }
684 if (new_class == NULL) {
685 new_class = AllocClass();
686 if (new_class == NULL) {
687 return NULL;
688 }
689 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700690 new_class->descriptor_ = String::AllocFromModifiedUtf8(descriptor.ToString().c_str());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700691 Class* java_lang_Object = GetClassRoot(kJavaLangObject);
692 new_class->super_class_ = java_lang_Object;
693 new_class->vtable_ = java_lang_Object->vtable_;
694 new_class->primitive_type_ = Class::kPrimNot;
695 new_class->component_type_ = component_type_;
696 new_class->class_loader_ = component_type_->class_loader_;
697 new_class->array_rank_ = array_rank;
698 new_class->status_ = Class::kStatusInitialized;
699 // don't need to set new_class->object_size_
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700700 // because Object::SizeOf delegates to Array::SizeOf
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700701
702
703 // All arrays have java/lang/Cloneable and java/io/Serializable as
704 // interfaces. We need to set that up here, so that stuff like
705 // "instanceof" works right.
706 //
707 // Note: The GC could run during the call to FindSystemClass,
708 // so we need to make sure the class object is GC-valid while we're in
709 // there. Do this by clearing the interface list so the GC will just
710 // think that the entries are null.
711
712
713 // Use the single, global copies of "interfaces" and "iftable"
714 // (remember not to free them for arrays).
715 DCHECK(array_interfaces_ != NULL);
716 new_class->interfaces_ = array_interfaces_;
717 new_class->iftable_count_ = 2;
718 DCHECK(array_iftable_ != NULL);
719 new_class->iftable_ = array_iftable_;
720
721 // Inherit access flags from the component type. Arrays can't be
722 // used as a superclass or interface, so we want to add "final"
723 // and remove "interface".
724 //
725 // Don't inherit any non-standard flags (e.g., kAccFinal)
726 // from component_type_. We assume that the array class does not
727 // override finalize().
728 new_class->access_flags_ = ((new_class->component_type_->access_flags_ &
729 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask;
730
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700731 if (InsertClass(descriptor, new_class)) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700732 return new_class;
733 }
734 // Another thread must have loaded the class after we
735 // started but before we finished. Abandon what we've
736 // done.
737 //
738 // (Yes, this happens.)
739
740 // Grab the winning class.
741 Class* other_class = LookupClass(descriptor, component_type_->class_loader_);
742 DCHECK(other_class != NULL);
743 return other_class;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700744}
745
746Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700747 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700748 case 'B':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700749 return GetClassRoot(kPrimitiveByte);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700750 case 'C':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700751 return GetClassRoot(kPrimitiveChar);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700752 case 'D':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700753 return GetClassRoot(kPrimitiveDouble);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700754 case 'F':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700755 return GetClassRoot(kPrimitiveFloat);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700756 case 'I':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700757 return GetClassRoot(kPrimitiveInt);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700758 case 'J':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700759 return GetClassRoot(kPrimitiveLong);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700760 case 'S':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700761 return GetClassRoot(kPrimitiveShort);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700762 case 'Z':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700763 return GetClassRoot(kPrimitiveBoolean);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700764 case 'V':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700765 return GetClassRoot(kPrimitiveVoid);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700766 case 'L':
767 case '[':
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700768 LOG(ERROR) << "Not a primitive type " << PrintableChar(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700769 default:
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700770 LOG(ERROR) << "Unknown primitive type " << PrintableChar(type);
Carl Shapiro744ad052011-08-06 15:53:36 -0700771 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700772 return NULL; // Not reachable.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700773}
774
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700775bool ClassLinker::InsertClass(const StringPiece& descriptor, Class* klass) {
776 size_t hash = StringPieceHash()(descriptor);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700777 MutexLock mu(classes_lock_);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700778 Table::iterator it = classes_.insert(std::make_pair(hash, klass));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700779 return ((*it).second == klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700780}
781
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700782Class* ClassLinker::LookupClass(const StringPiece& descriptor, ClassLoader* class_loader) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700783 size_t hash = StringPieceHash()(descriptor);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700784 MutexLock mu(classes_lock_);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700785 typedef Table::const_iterator It; // TODO: C++0x auto
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700786 for (It it = classes_.find(hash), end = classes_.end(); it != end; ++it) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700787 Class* klass = it->second;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700788 if (klass->descriptor_->Equals(descriptor) && klass->class_loader_ == class_loader) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700789 return klass;
790 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700791 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700792 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700793}
794
795bool ClassLinker::InitializeClass(Class* klass) {
796 CHECK(klass->GetStatus() == Class::kStatusResolved ||
797 klass->GetStatus() == Class::kStatusError);
798
Carl Shapirob5573532011-07-12 18:22:59 -0700799 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700800
801 {
802 ObjectLock lock(klass);
803
804 if (klass->GetStatus() < Class::kStatusVerified) {
805 if (klass->IsErroneous()) {
806 LG << "re-initializing failed class"; // TODO: throw
807 return false;
808 }
809
810 CHECK(klass->GetStatus() == Class::kStatusResolved);
811
812 klass->status_ = Class::kStatusVerifying;
813 if (!DexVerify::VerifyClass(klass)) {
814 LG << "Verification failed"; // TODO: ThrowVerifyError
815 Object* exception = self->GetException();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700816 klass->SetVerifyErrorClass(exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700817 klass->SetStatus(Class::kStatusError);
818 return false;
819 }
820
821 klass->SetStatus(Class::kStatusVerified);
822 }
823
824 if (klass->status_ == Class::kStatusInitialized) {
825 return true;
826 }
827
828 while (klass->status_ == Class::kStatusInitializing) {
829 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -0700830 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700831 LG << "recursive <clinit>";
832 return true;
833 }
834
835 CHECK(!self->IsExceptionPending());
836
837 lock.Wait(); // TODO: check for interruption
838
839 // When we wake up, repeat the test for init-in-progress. If
840 // there's an exception pending (only possible if
841 // "interruptShouldThrow" was set), bail out.
842 if (self->IsExceptionPending()) {
843 CHECK(false);
844 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
845 klass->SetStatus(Class::kStatusError);
846 return false;
847 }
848 if (klass->GetStatus() == Class::kStatusInitializing) {
849 continue;
850 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700851 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700852 klass->GetStatus() == Class::kStatusError);
853 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700854 // The caller wants an exception, but it was thrown in a
855 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700856 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
857 return false;
858 }
859 return true; // otherwise, initialized
860 }
861
862 // see if we failed previously
863 if (klass->IsErroneous()) {
864 // might be wise to unlock before throwing; depends on which class
865 // it is that we have locked
866
867 // TODO: throwEarlierClassFailure(klass);
868 return false;
869 }
870
871 if (!ValidateSuperClassDescriptors(klass)) {
872 klass->SetStatus(Class::kStatusError);
873 return false;
874 }
875
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700876 DCHECK(klass->status_ < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700877
Carl Shapirob5573532011-07-12 18:22:59 -0700878 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700879 klass->status_ = Class::kStatusInitializing;
880 }
881
882 if (!InitializeSuperClass(klass)) {
883 return false;
884 }
885
886 InitializeStaticFields(klass);
887
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700888 Method* clinit = klass->FindDeclaredDirectMethod("<clinit>", "()V");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700889 if (clinit != NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700890 // JValue unused;
891 // TODO: dvmCallMethod(self, method, NULL, &unused);
Elliott Hughes53b61312011-08-12 18:28:20 -0700892 // UNIMPLEMENTED(FATAL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700893 }
894
895 {
896 ObjectLock lock(klass);
897
898 if (self->IsExceptionPending()) {
899 klass->SetStatus(Class::kStatusError);
900 } else {
901 klass->SetStatus(Class::kStatusInitialized);
902 }
903 lock.NotifyAll();
904 }
905
906 return true;
907}
908
909bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
910 if (klass->IsInterface()) {
911 return true;
912 }
913 // begin with the methods local to the superclass
914 if (klass->HasSuperClass() &&
915 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
916 const Class* super = klass->GetSuperClass();
917 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -0700918 const Method* method = super->GetVirtualMethod(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700919 if (method != super->GetVirtualMethod(i) &&
920 !HasSameMethodDescriptorClasses(method, super, klass)) {
921 LG << "Classes resolve differently in superclass";
922 return false;
923 }
924 }
925 }
926 for (size_t i = 0; i < klass->iftable_count_; ++i) {
927 const InterfaceEntry* iftable = &klass->iftable_[i];
928 Class* interface = iftable->GetClass();
929 if (klass->GetClassLoader() != interface->GetClassLoader()) {
930 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
931 uint32_t vtable_index = iftable->method_index_array_[j];
932 const Method* method = klass->GetVirtualMethod(vtable_index);
933 if (!HasSameMethodDescriptorClasses(method, interface,
934 method->GetClass())) {
935 LG << "Classes resolve differently in interface"; // TODO: LinkageError
936 return false;
937 }
938 }
939 }
940 }
941 return true;
942}
943
944bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700945 const Class* klass1,
946 const Class* klass2) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700947 const DexFile& dex_file = FindDexFile(method->GetClass()->GetDexCache());
948 const DexFile::ProtoId& proto_id = dex_file.GetProtoId(method->proto_idx_);
Brian Carlstromf615a612011-07-23 12:50:34 -0700949 DexFile::ParameterIterator *it;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700950 for (it = dex_file.GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700951 const char* descriptor = it->GetDescriptor();
952 if (descriptor == NULL) {
953 break;
954 }
955 if (descriptor[0] == 'L' || descriptor[0] == '[') {
956 // Found a non-primitive type.
957 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
958 return false;
959 }
960 }
961 }
962 // Check the return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700963 const char* descriptor = dex_file.GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700964 if (descriptor[0] == 'L' || descriptor[0] == '[') {
965 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
966 return false;
967 }
968 }
969 return true;
970}
971
972// Returns true if classes referenced by the descriptor are the
973// same classes in klass1 as they are in klass2.
974bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700975 const Class* klass1,
976 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700977 CHECK(descriptor != NULL);
978 CHECK(klass1 != NULL);
979 CHECK(klass2 != NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700980 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700981 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700982 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700983 // TODO: found2 == NULL
984 // TODO: lookup found1 in initiating loader list
985 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -0700986 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700987 if (found1 == found2) {
988 return true;
989 } else {
990 return false;
991 }
992 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700993 return true;
994}
995
996bool ClassLinker::InitializeSuperClass(Class* klass) {
997 CHECK(klass != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700998 if (!klass->IsInterface() && klass->HasSuperClass()) {
999 Class* super_class = klass->GetSuperClass();
1000 if (super_class->GetStatus() != Class::kStatusInitialized) {
1001 CHECK(!super_class->IsInterface());
1002 klass->MonitorExit();
1003 bool super_initialized = InitializeClass(super_class);
1004 klass->MonitorEnter();
1005 // TODO: check for a pending exception
1006 if (!super_initialized) {
1007 klass->SetStatus(Class::kStatusError);
1008 klass->NotifyAll();
1009 return false;
1010 }
1011 }
1012 }
1013 return true;
1014}
1015
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001016bool ClassLinker::EnsureInitialized(Class* c) {
1017 CHECK(c != NULL);
1018 if (c->IsInitialized()) {
1019 return true;
1020 }
1021
1022 c->MonitorExit();
1023 InitializeClass(c);
1024 c->MonitorEnter();
1025 return !Thread::Current()->IsExceptionPending();
1026}
1027
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001028void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001029 size_t num_static_fields = klass->NumStaticFields();
1030 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001031 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001032 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001033 DexCache* dex_cache = klass->GetDexCache();
1034 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001035 return;
1036 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001037 const std::string descriptor(klass->GetDescriptor()->ToModifiedUtf8());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001038 const DexFile& dex_file = FindDexFile(dex_cache);
1039 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Brian Carlstromf615a612011-07-23 12:50:34 -07001040 CHECK(dex_class_def != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001041 const byte* addr = dex_file.GetEncodedArray(*dex_class_def);
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001042 if (addr == NULL) {
1043 // All this class' static fields have default values.
1044 return;
1045 }
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001046 size_t array_size = DecodeUnsignedLeb128(&addr);
1047 for (size_t i = 0; i < array_size; ++i) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001048 Field* field = klass->GetStaticField(i);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001049 JValue value;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001050 DexFile::ValueType type = dex_file.ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001051 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001052 case DexFile::kByte:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001053 field->SetByte(value.b);
1054 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001055 case DexFile::kShort:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001056 field->SetShort(value.s);
1057 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001058 case DexFile::kChar:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001059 field->SetChar(value.c);
1060 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001061 case DexFile::kInt:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001062 field->SetInt(value.i);
1063 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001064 case DexFile::kLong:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001065 field->SetLong(value.j);
1066 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001067 case DexFile::kFloat:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001068 field->SetFloat(value.f);
1069 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001070 case DexFile::kDouble:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001071 field->SetDouble(value.d);
1072 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001073 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001074 uint32_t string_idx = value.i;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001075 String* resolved = ResolveString(klass, string_idx, dex_file);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001076 field->SetObject(resolved);
1077 break;
1078 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001079 case DexFile::kBoolean:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001080 field->SetBoolean(value.z);
1081 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001082 case DexFile::kNull:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001083 field->SetObject(value.l);
1084 break;
1085 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001086 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001087 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001088 }
1089}
1090
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001091bool ClassLinker::LinkClass(Class* klass, const DexFile& dex_file) {
1092 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001093 if (!LinkSuperClass(klass)) {
1094 return false;
1095 }
1096 if (!LinkMethods(klass)) {
1097 return false;
1098 }
Jesse Wilson7833bd22011-08-09 18:31:44 -04001099 if (!LinkStaticFields(klass)) {
1100 return false;
1101 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001102 if (!LinkInstanceFields(klass)) {
1103 return false;
1104 }
1105 CreateReferenceOffsets(klass);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001106 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001107 klass->status_ = Class::kStatusResolved;
1108 return true;
1109}
1110
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001111bool ClassLinker::LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file) {
1112 CHECK_EQ(Class::kStatusIdx, klass->status_);
Brian Carlstromf615a612011-07-23 12:50:34 -07001113 if (klass->super_class_idx_ != DexFile::kDexNoIndex) {
1114 Class* super_class = ResolveClass(klass, klass->super_class_idx_, dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001115 if (super_class == NULL) {
1116 LG << "Failed to resolve superclass";
1117 return false;
1118 }
1119 klass->super_class_ = super_class; // TODO: write barrier
1120 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001121 if (klass->NumInterfaces() > 0) {
1122 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
1123 uint32_t idx = klass->interfaces_idx_[i];
1124 klass->SetInterface(i, ResolveClass(klass, idx, dex_file));
1125 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001126 LG << "Failed to resolve interface";
1127 return false;
1128 }
1129 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001130 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001131 LG << "Inaccessible interface";
1132 return false;
1133 }
1134 }
1135 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001136 // Mark the class as loaded.
1137 klass->status_ = Class::kStatusLoaded;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001138 return true;
1139}
1140
1141bool ClassLinker::LinkSuperClass(Class* klass) {
1142 CHECK(!klass->IsPrimitive());
1143 const Class* super = klass->GetSuperClass();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001144 if (klass->GetDescriptor()->Equals("Ljava/lang/Object;")) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001145 if (super != NULL) {
1146 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1147 return false;
1148 }
1149 // TODO: clear finalize attribute
1150 return true;
1151 }
1152 if (super == NULL) {
1153 LG << "No superclass defined"; // TODO: LinkageError
1154 return false;
1155 }
1156 // Verify
1157 if (super->IsFinal()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001158 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is declared final"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001159 return false;
1160 }
1161 if (super->IsInterface()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001162 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is an interface"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001163 return false;
1164 }
1165 if (!klass->CanAccess(super)) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001166 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is inaccessible"; // TODO: IllegalAccessError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001167 return false;
1168 }
1169 return true;
1170}
1171
1172// Populate the class vtable and itable.
1173bool ClassLinker::LinkMethods(Class* klass) {
1174 if (klass->IsInterface()) {
1175 // No vtable.
1176 size_t count = klass->NumVirtualMethods();
1177 if (!IsUint(16, count)) {
1178 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1179 return false;
1180 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001181 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001182 klass->GetVirtualMethod(i)->method_index_ = i;
1183 }
1184 } else {
1185 // Link virtual method tables
1186 LinkVirtualMethods(klass);
1187
1188 // Link interface method tables
1189 LinkInterfaceMethods(klass);
1190
1191 // Insert stubs.
1192 LinkAbstractMethods(klass);
1193 }
1194 return true;
1195}
1196
1197bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001198 if (klass->HasSuperClass()) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001199 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001200 size_t actual_count = klass->GetSuperClass()->vtable_->GetLength();
1201 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001202 // TODO: do not assign to the vtable field until it is fully constructed.
1203 klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001204 // See if any of our virtual methods override the superclass.
1205 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1206 Method* local_method = klass->GetVirtualMethod(i);
1207 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001208 for (; j < actual_count; ++j) {
1209 Method* super_method = klass->vtable_->Get(j);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001210 if (local_method->HasSameNameAndDescriptor(super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001211 // Verify
1212 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001213 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001214 return false;
1215 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001216 klass->vtable_->Set(j, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001217 local_method->method_index_ = j;
1218 break;
1219 }
1220 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001221 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001222 // Not overriding, append.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001223 klass->vtable_->Set(actual_count, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001224 local_method->method_index_ = actual_count;
1225 actual_count += 1;
1226 }
1227 }
1228 if (!IsUint(16, actual_count)) {
1229 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1230 return false;
1231 }
1232 CHECK_LE(actual_count, max_count);
1233 if (actual_count < max_count) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001234 // TODO: do not assign to the vtable field until it is fully constructed.
1235 klass->vtable_ = klass->vtable_->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001236 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001237 } else {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001238 CHECK(klass->GetDescriptor()->Equals("Ljava/lang/Object;"));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001239 uint32_t num_virtual_methods = klass->NumVirtualMethods();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001240 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001241 LG << "Too many methods"; // TODO: VirtualMachineError
1242 return false;
1243 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001244 // TODO: do not assign to the vtable field until it is fully constructed.
1245 klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods);
1246 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001247 klass->vtable_->Set(i, klass->GetVirtualMethod(i));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001248 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1249 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001250 }
1251 return true;
1252}
1253
1254bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1255 int pool_offset = 0;
1256 int pool_size = 0;
1257 int miranda_count = 0;
1258 int miranda_alloc = 0;
1259 size_t super_ifcount;
1260 if (klass->HasSuperClass()) {
1261 super_ifcount = klass->GetSuperClass()->iftable_count_;
1262 } else {
1263 super_ifcount = 0;
1264 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001265 size_t ifcount = super_ifcount;
1266 ifcount += klass->NumInterfaces();
1267 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1268 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001269 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001270 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001271 DCHECK(klass->iftable_count_ == 0);
1272 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001273 return true;
1274 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001275 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1276 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001277 if (super_ifcount != 0) {
1278 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1279 sizeof(InterfaceEntry) * super_ifcount);
1280 }
1281 // Flatten the interface inheritance hierarchy.
1282 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001283 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1284 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001285 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001286 if (!interf->IsInterface()) {
1287 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1288 return false;
1289 }
1290 klass->iftable_[idx++].SetClass(interf);
1291 for (size_t j = 0; j < interf->iftable_count_; j++) {
1292 klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
1293 }
1294 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001295 CHECK_EQ(idx, ifcount);
1296 klass->iftable_count_ = ifcount;
1297 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001298 return true;
1299 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001300 for (size_t i = super_ifcount; i < ifcount; i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001301 pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
1302 }
1303 if (pool_size == 0) {
1304 return true;
1305 }
1306 klass->ifvi_pool_count_ = pool_size;
1307 klass->ifvi_pool_ = new uint32_t[pool_size];
1308 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001309 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001310 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
1311 Class* interface = klass->iftable_[i].GetClass();
1312 pool_offset += interface->NumVirtualMethods(); // end here
1313 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1314 Method* interface_method = interface->GetVirtualMethod(j);
1315 int k; // must be signed
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001316 for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001317 Method* vtable_method = klass->vtable_->Get(k);
1318 if (interface_method->HasSameNameAndDescriptor(vtable_method)) {
1319 if (!vtable_method->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001320 LG << "Implementation not public";
1321 return false;
1322 }
1323 klass->iftable_[i].method_index_array_[j] = k;
1324 break;
1325 }
1326 }
1327 if (k < 0) {
1328 if (miranda_count == miranda_alloc) {
1329 miranda_alloc += 8;
1330 if (miranda_list.empty()) {
1331 miranda_list.resize(miranda_alloc);
1332 } else {
1333 miranda_list.resize(miranda_alloc);
1334 }
1335 }
1336 int mir;
1337 for (mir = 0; mir < miranda_count; mir++) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001338 Method* miranda_method = miranda_list[mir];
1339 if (miranda_method->HasSameNameAndDescriptor(interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001340 break;
1341 }
1342 }
1343 // point the interface table at a phantom slot index
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001344 klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001345 if (mir == miranda_count) {
1346 miranda_list[miranda_count++] = interface_method;
1347 }
1348 }
1349 }
1350 }
1351 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001352 int old_method_count = klass->NumVirtualMethods();
1353 int new_method_count = old_method_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001354 klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001355
1356 CHECK(klass->vtable_ != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001357 int old_vtable_count = klass->vtable_->GetLength();
1358 int new_vtable_count = old_vtable_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001359 // TODO: do not assign to the vtable field until it is fully constructed.
1360 klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001361
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001362 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001363 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001364 memcpy(meth, miranda_list[i], sizeof(Method));
1365 meth->klass_ = klass;
1366 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001367 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1368 klass->SetVirtualMethod(old_method_count + i, meth);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001369 klass->vtable_->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001370 }
1371 }
1372 return true;
1373}
1374
1375void ClassLinker::LinkAbstractMethods(Class* klass) {
1376 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1377 Method* method = klass->GetVirtualMethod(i);
1378 if (method->IsAbstract()) {
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001379 LG << "AbstractMethodError";
1380 method->code_off_ = 0xFFFFFFFF;
1381 // TODO: throw AbstractMethodError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001382 }
1383 }
1384}
1385
Jesse Wilson7833bd22011-08-09 18:31:44 -04001386// Each static field will be stored in one of three arrays: static_references_,
1387// static_32bit_primitives_, or static_64bit_primitives_. This assigns each
1388// field a slot in its array and create the arrays.
1389bool ClassLinker::LinkStaticFields(Class* klass) {
1390 size_t next_reference_slot = 0;
1391 size_t next_32bit_primitive_slot = 0;
1392 size_t next_64bit_primitive_slot = 0;
1393
1394 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001395 Field* field = klass->GetStaticField(i);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001396 char type = field->GetType();
1397 if (type == '[' || type == 'L') {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001398 field->offset_ = next_reference_slot++;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001399 } else if (type == 'J' || type == 'D') {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001400 field->offset_ = next_64bit_primitive_slot++;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001401 } else {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001402 field->offset_ = next_32bit_primitive_slot++;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001403 }
1404 }
1405
1406 if (next_reference_slot > 0) {
1407 Class* array_class = GetClassRoot(kObjectArrayClass);
1408 klass->static_references_ = ObjectArray<Object>::Alloc(array_class, next_reference_slot);
1409 }
1410 if (next_32bit_primitive_slot > 0) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001411 klass->static_32bit_primitives_ = IntArray::Alloc(next_32bit_primitive_slot);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001412 }
1413 if (next_64bit_primitive_slot > 0) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001414 klass->static_64bit_primitives_ = LongArray::Alloc(next_64bit_primitive_slot);
Jesse Wilson7833bd22011-08-09 18:31:44 -04001415 }
1416
1417 return true;
1418}
1419
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001420bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001421 int field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001422 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001423 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001424 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001425 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001426 }
1427 // Move references to the front.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001428 klass->num_reference_instance_fields_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001429 size_t i = 0;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001430 for ( ; i < klass->NumInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001431 Field* pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001432 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001433 if (c != '[' && c != 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001434 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001435 Field* refField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001436 char rc = refField->GetType();
1437 if (rc == '[' || rc == 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001438 klass->SetInstanceField(i, refField);
1439 klass->SetInstanceField(j, pField);
1440 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001441 c = rc;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001442 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001443 break;
1444 }
1445 }
1446 } else {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001447 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001448 }
1449 if (c != '[' && c != 'L') {
1450 break;
1451 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001452 pField->SetOffset(field_offset);
1453 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001454 }
1455
1456 // Now we want to pack all of the double-wide fields together. If
1457 // we're not aligned, though, we want to shuffle one 32-bit field
1458 // into place. If we can't find one, we'll have to pad it.
Brian Carlstroma0808032011-07-18 00:39:23 -07001459 if (i != klass->NumInstanceFields() && (field_offset & 0x04) != 0) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001460 Field* pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001461 char c = pField->GetType();
1462
1463 if (c != 'J' && c != 'D') {
1464 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001465 DCHECK(c != '[');
1466 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001467 pField->SetOffset(field_offset);
1468 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001469 i++;
1470 } else {
1471 // Next field is 64-bit, so search for a 32-bit field we can
1472 // swap into it.
1473 bool found = false;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001474 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001475 Field* singleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001476 char rc = singleField->GetType();
1477 if (rc != 'J' && rc != 'D') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001478 klass->SetInstanceField(i, singleField);
1479 klass->SetInstanceField(j, pField);
1480 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001481 pField->SetOffset(field_offset);
1482 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001483 found = true;
1484 i++;
1485 break;
1486 }
1487 }
1488 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001489 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001490 }
1491 }
1492 }
1493
1494 // Alignment is good, shuffle any double-wide fields forward, and
1495 // finish assigning field offsets to all fields.
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001496 DCHECK(i == klass->NumInstanceFields() || (field_offset & 0x04) == 0);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001497 for ( ; i < klass->NumInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001498 Field* pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001499 char c = pField->GetType();
1500 if (c != 'D' && c != 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001501 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001502 Field* doubleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001503 char rc = doubleField->GetType();
1504 if (rc == 'D' || rc == 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001505 klass->SetInstanceField(i, doubleField);
1506 klass->SetInstanceField(j, pField);
1507 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001508 c = rc;
1509 break;
1510 }
1511 }
1512 } else {
1513 // This is a double-wide field, leave it be.
1514 }
1515
Brian Carlstroma0808032011-07-18 00:39:23 -07001516 pField->SetOffset(field_offset);
1517 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001518 if (c == 'J' || c == 'D')
Brian Carlstroma0808032011-07-18 00:39:23 -07001519 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001520 }
1521
1522#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001523 // Make sure that all reference fields appear before
1524 // non-reference fields, and all double-wide fields are aligned.
1525 bool seen_non_ref = false;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001526 for (i = 0; i < klass->NumInstanceFields(); i++) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001527 Field *pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001528 char c = pField->GetType();
1529
1530 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001531 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001532 }
1533
1534 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001535 if (!seen_non_ref) {
1536 seen_non_ref = true;
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001537 DCHECK_EQ(klass->NumReferenceInstanceFields(), i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001538 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001539 } else {
1540 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001541 }
1542 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001543 if (!seen_non_ref) {
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001544 DCHECK_EQ(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001545 }
1546#endif
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001547 klass->object_size_ = field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001548 return true;
1549}
1550
1551// Set the bitmap of reference offsets, refOffsets, from the ifields
1552// list.
1553void ClassLinker::CreateReferenceOffsets(Class* klass) {
1554 uint32_t reference_offsets = 0;
1555 if (klass->HasSuperClass()) {
1556 reference_offsets = klass->GetSuperClass()->GetReferenceOffsets();
1557 }
1558 // If our superclass overflowed, we don't stand a chance.
1559 if (reference_offsets != CLASS_WALK_SUPER) {
1560 // All of the fields that contain object references are guaranteed
1561 // to be at the beginning of the ifields list.
1562 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) {
1563 // Note that, per the comment on struct InstField, f->byteOffset
1564 // is the offset from the beginning of obj, not the offset into
1565 // obj->instanceData.
Jesse Wilson35baaab2011-08-10 16:18:03 -04001566 const Field* field = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001567 size_t byte_offset = field->GetOffset();
1568 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001569 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001570 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1571 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001572 CHECK_NE(new_bit, 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001573 reference_offsets |= new_bit;
1574 } else {
1575 reference_offsets = CLASS_WALK_SUPER;
1576 break;
1577 }
1578 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001579 }
Brian Carlstromae3ac012011-07-27 01:30:28 -07001580 klass->SetReferenceOffsets(reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001581}
1582
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001583Class* ClassLinker::ResolveClass(const Class* referrer,
1584 uint32_t class_idx,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001585 const DexFile& dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001586 DexCache* dex_cache = referrer->GetDexCache();
1587 Class* resolved = dex_cache->GetResolvedClass(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001588 if (resolved != NULL) {
1589 return resolved;
1590 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001591 const char* descriptor = dex_file.dexStringByTypeIdx(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001592 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001593 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001594 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001595 resolved = FindClass(descriptor, referrer->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001596 }
1597 if (resolved != NULL) {
1598 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001599 if (referrer->GetDexCache() != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001600 if (check->GetClassLoader() != NULL) {
1601 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1602 return NULL;
1603 }
1604 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001605 dex_cache->SetResolvedClass(class_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001606 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001607 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001608 }
1609 return resolved;
1610}
1611
1612Method* ResolveMethod(const Class* referrer, uint32_t method_idx,
1613 /*MethodType*/ int method_type) {
1614 CHECK(false);
1615 return NULL;
1616}
1617
Carl Shapiro69759ea2011-07-21 18:13:35 -07001618String* ClassLinker::ResolveString(const Class* referring,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001619 uint32_t string_idx,
1620 const DexFile& dex_file) {
1621 const DexFile::StringId& string_id = dex_file.GetStringId(string_idx);
1622 int32_t utf16_length = dex_file.GetStringLength(string_id);
1623 const char* utf8_data = dex_file.GetStringData(string_id);
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001624 String* string = intern_table_.Intern(utf16_length, utf8_data);
1625 referring->GetDexCache()->SetResolvedString(string_idx, string);
1626 return string;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001627}
1628
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001629} // namespace art