blob: 9bbcb0cb93515cb8ec73738dae06204b0b828835 [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
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005#include <utility>
Elliott Hughes90a33692011-08-30 13:27:07 -07006#include <vector>
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07007
Elliott Hughes90a33692011-08-30 13:27:07 -07008#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "casts.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070010#include "dex_cache.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070011#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070012#include "dex_verifier.h"
13#include "heap.h"
14#include "logging.h"
15#include "monitor.h"
16#include "object.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;",
Shih-wei Liao55df06b2011-08-26 14:39:27 -070033 "Ljava/lang/StackTraceElement;",
Brian Carlstroma663ea52011-08-19 23:33:41 -070034 "Z",
35 "B",
36 "C",
37 "D",
38 "F",
39 "I",
40 "J",
41 "S",
42 "V",
43 "[Z",
44 "[B",
45 "[C",
46 "[D",
47 "[F",
48 "[I",
49 "[J",
50 "[S",
Shih-wei Liao55df06b2011-08-26 14:39:27 -070051 "[Ljava/lang/StackTraceElement;",
Brian Carlstroma663ea52011-08-19 23:33:41 -070052};
53
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070054ClassLinker* ClassLinker::Create(const std::vector<const DexFile*>& boot_class_path, Space* space) {
Elliott Hughes90a33692011-08-30 13:27:07 -070055 UniquePtr<ClassLinker> class_linker(new ClassLinker);
Brian Carlstroma663ea52011-08-19 23:33:41 -070056 if (space == NULL) {
57 class_linker->Init(boot_class_path);
58 } else {
59 class_linker->Init(boot_class_path, space);
60 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070061 // TODO: check for failure during initialization
62 return class_linker.release();
63}
64
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070065ClassLinker::ClassLinker()
66 : classes_lock_(Mutex::Create("ClassLinker::Lock")),
67 class_roots_(NULL),
68 init_done_(false) {
69}
Brian Carlstroma663ea52011-08-19 23:33:41 -070070
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070071void ClassLinker::Init(const std::vector<const DexFile*>& boot_class_path) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070072 CHECK(!init_done_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070073
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070074 // java_lang_Class comes first, its needed for AllocClass
Brian Carlstrom4873d462011-08-21 15:23:39 -070075 Class* java_lang_Class = down_cast<Class*>(Heap::AllocObject(NULL, sizeof(ClassClass)));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070076 CHECK(java_lang_Class != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -070077 java_lang_Class->class_size_ = sizeof(ClassClass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070078 java_lang_Class->klass_ = java_lang_Class;
79 // AllocClass(Class*) can now be used
Brian Carlstroma0808032011-07-18 00:39:23 -070080
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070081 // java_lang_Object comes next so that object_array_class can be created
Brian Carlstrom4873d462011-08-21 15:23:39 -070082 Class* java_lang_Object = AllocClass(java_lang_Class, sizeof(Class));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070083 CHECK(java_lang_Object != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070084 // backfill Object as the super class of Class
85 java_lang_Class->super_class_ = java_lang_Object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070086 // mark as non-primitive for object_array_class
87 java_lang_Object->primitive_type_ = Class::kPrimNot;
Brian Carlstroma0808032011-07-18 00:39:23 -070088
Elliott Hughesc1674ed2011-08-25 18:09:09 -070089 // Object[] is for DexCache and int[] is for various Class members.
Brian Carlstrom4873d462011-08-21 15:23:39 -070090 Class* object_array_class = AllocClass(java_lang_Class, sizeof(Class));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070091 CHECK(object_array_class != NULL);
Brian Carlstromb63ec392011-08-27 17:38:27 -070092 object_array_class->array_rank_ = 1;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070093 object_array_class->component_type_ = java_lang_Object;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070094 Class* int_array_class = AllocClass(java_lang_Class, sizeof(Class));
95 CHECK(int_array_class != NULL);
Brian Carlstromb63ec392011-08-27 17:38:27 -070096 int_array_class->array_rank_ = 1;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070097 IntArray::SetArrayClass(int_array_class);
Brian Carlstroma0808032011-07-18 00:39:23 -070098
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070099 // String and char[] are necessary so that FindClass can assign names to members
Brian Carlstrom4873d462011-08-21 15:23:39 -0700100 Class* java_lang_String = AllocClass(java_lang_Class, sizeof(StringClass));
Jesse Wilson14150742011-07-29 19:04:44 -0400101 CHECK(java_lang_String != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700102 CHECK_LT(java_lang_String->object_size_, sizeof(String));
Jesse Wilson14150742011-07-29 19:04:44 -0400103 java_lang_String->object_size_ = sizeof(String);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700104 String::SetClass(java_lang_String);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700105 Class* char_array_class = AllocClass(java_lang_Class, sizeof(Class));
Jesse Wilson8989d992011-08-02 13:39:42 -0700106 CHECK(char_array_class != NULL);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700107 char_array_class->array_rank_ = 1;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700108 CharArray::SetArrayClass(char_array_class);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700109 // Now String::Alloc* can be used
110
111 // backfill Class descriptors missing until this point
112 java_lang_Class->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/Class;");
113 java_lang_Object->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/Object;");
114 object_array_class->descriptor_ = String::AllocFromModifiedUtf8("[Ljava/lang/Object;");
115 java_lang_String->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/String;");
116 char_array_class->descriptor_ = String::AllocFromModifiedUtf8("[C");
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700117 int_array_class->descriptor_ = String::AllocFromModifiedUtf8("[I");
Jesse Wilson14150742011-07-29 19:04:44 -0400118
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700119 // Field and Method are necessary so that FindClass can link members
Brian Carlstrom4873d462011-08-21 15:23:39 -0700120 Class* java_lang_reflect_Field = AllocClass(java_lang_Class, sizeof(FieldClass));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700121 CHECK(java_lang_reflect_Field != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700122 java_lang_reflect_Field->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/reflect/Field;");
Jesse Wilson35baaab2011-08-10 16:18:03 -0400123 CHECK_LT(java_lang_reflect_Field->object_size_, sizeof(Field));
124 java_lang_reflect_Field->object_size_ = sizeof(Field);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700125 Class* java_lang_reflect_Method = AllocClass(java_lang_Class, sizeof(MethodClass));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700126 java_lang_reflect_Method->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/reflect/Method;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700127 CHECK(java_lang_reflect_Method != NULL);
128 CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method));
129 java_lang_reflect_Method->object_size_ = sizeof(Method);
130
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700131 // create storage for root classes, save away our work so far
132 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700133 SetClassRoot(kJavaLangClass, java_lang_Class);
134 SetClassRoot(kJavaLangObject, java_lang_Object);
135 SetClassRoot(kObjectArrayClass, object_array_class);
136 SetClassRoot(kJavaLangString, java_lang_String);
137 SetClassRoot(kCharArrayClass, char_array_class);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700138 SetClassRoot(kIntArrayClass, int_array_class);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700139 SetClassRoot(kJavaLangReflectField, java_lang_reflect_Field);
140 SetClassRoot(kJavaLangReflectMethod, java_lang_reflect_Method);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700141 // now that these are registered, we can use AllocClass() and AllocObjectArray
Brian Carlstroma0808032011-07-18 00:39:23 -0700142
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700143 // setup boot_class_path_ now that we can use AllocObjectArray to
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700144 // create DexCache instances
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700145 for (size_t i = 0; i != boot_class_path.size(); ++i) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700146 const DexFile* dex_file = boot_class_path[i];
147 CHECK(dex_file != NULL);
148 AppendToBootClassPath(*dex_file);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700149 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700150 // now we can use FindSystemClass, at least for non-arrays classes.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700151
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700152 // run Class, Field, and Method through FindSystemClass.
153 // this initializes their dex_cache_ fields and register them in classes_.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700154 // we also override their object_size_ values to accommodate the extra C++ fields.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700155 Class* Class_class = FindSystemClass("Ljava/lang/Class;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700156 CHECK_EQ(java_lang_Class, Class_class);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700157 CHECK_LT(java_lang_Class->object_size_, sizeof(Class));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700158 java_lang_Class->object_size_ = sizeof(Class);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700159 Class* Field_class = FindSystemClass("Ljava/lang/reflect/Field;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700160 CHECK_EQ(java_lang_reflect_Field, Field_class);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400161 CHECK_LT(java_lang_reflect_Field->object_size_, sizeof(Field));
162 java_lang_reflect_Field->object_size_ = sizeof(Field);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700163 Class* Method_class = FindSystemClass("Ljava/lang/reflect/Method;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700164 CHECK_EQ(java_lang_reflect_Method, Method_class);
165 CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700166 java_lang_reflect_Method->object_size_ = sizeof(Method);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700167
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700168 // Object and String just need more minimal setup, since they do not have extra C++ fields.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700169 Class* Object_class = FindSystemClass("Ljava/lang/Object;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700170 CHECK_EQ(java_lang_Object, Object_class);
171 CHECK_EQ(java_lang_Object->object_size_, sizeof(Object));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700172 Class* String_class = FindSystemClass("Ljava/lang/String;");
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700173 CHECK_EQ(java_lang_String, String_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700174 CHECK_EQ(java_lang_String->object_size_, sizeof(String));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700175
176 // Setup the ClassLoaders, adjusting the object_size_ as necessary
177 Class* java_lang_ClassLoader = FindSystemClass("Ljava/lang/ClassLoader;");
178 CHECK(java_lang_ClassLoader != NULL);
179 CHECK_LT(java_lang_ClassLoader->object_size_, sizeof(ClassLoader));
180 java_lang_ClassLoader->object_size_ = sizeof(ClassLoader);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700181 SetClassRoot(kJavaLangClassLoader, java_lang_ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700182 Class* dalvik_system_BaseDexClassLoader = FindSystemClass("Ldalvik/system/BaseDexClassLoader;");
183 CHECK(dalvik_system_BaseDexClassLoader != NULL);
184 CHECK_EQ(dalvik_system_BaseDexClassLoader->object_size_, sizeof(BaseDexClassLoader));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700185 SetClassRoot(kDalvikSystemBaseDexClassLoader, dalvik_system_BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700186 Class* dalvik_system_PathClassLoader = FindSystemClass("Ldalvik/system/PathClassLoader;");
187 CHECK(dalvik_system_PathClassLoader != NULL);
188 CHECK_EQ(dalvik_system_PathClassLoader->object_size_, sizeof(PathClassLoader));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700189 SetClassRoot(kDalvikSystemPathClassLoader, dalvik_system_PathClassLoader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700190 PathClassLoader::SetClass(dalvik_system_PathClassLoader);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700191
192 // Setup a single, global copy of "interfaces" and "iftable" for
193 // reuse across array classes
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700194 Class* java_lang_Cloneable = FindSystemClass("Ljava/lang/Cloneable;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700195 CHECK(java_lang_Cloneable != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700196 Class* java_io_Serializable = FindSystemClass("Ljava/io/Serializable;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700197 CHECK(java_io_Serializable != NULL);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700198
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700199 array_interfaces_ = AllocObjectArray<Class>(2);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700200 CHECK(array_interfaces_ != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700201 array_interfaces_->Set(0, java_lang_Cloneable);
202 array_interfaces_->Set(1, java_io_Serializable);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700203
204 // We assume that Cloneable/Serializable don't have superinterfaces --
205 // normally we'd have to crawl up and explicitly list all of the
206 // supers as well. These interfaces don't have any methods, so we
207 // don't have to worry about the ifviPool either.
208 array_iftable_ = new InterfaceEntry[2];
Brian Carlstrom30b94452011-08-25 21:35:26 -0700209 array_iftable_[0].SetInterface(array_interfaces_->Get(0));
210 array_iftable_[1].SetInterface(array_interfaces_->Get(1));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700211 // now FindClass can be used for non-primitive array classes
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700212
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700213 // run Object[] through FindClass to complete initialization
Jesse Wilson8989d992011-08-02 13:39:42 -0700214 Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;");
215 CHECK_EQ(object_array_class, found_object_array_class);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700216 CHECK_EQ(java_lang_Cloneable, object_array_class->GetInterface(0));
217 CHECK_EQ(java_io_Serializable, object_array_class->GetInterface(1));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700218
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700219 // Setup the primitive type classes.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700220 SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass("Z"));
221 SetClassRoot(kPrimitiveByte, CreatePrimitiveClass("B"));
222 SetClassRoot(kPrimitiveChar, CreatePrimitiveClass("C"));
223 SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass("D"));
224 SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass("F"));
225 SetClassRoot(kPrimitiveInt, CreatePrimitiveClass("I"));
226 SetClassRoot(kPrimitiveLong, CreatePrimitiveClass("J"));
227 SetClassRoot(kPrimitiveShort, CreatePrimitiveClass("S"));
228 SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass("V"));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700229 // now we can use FindSystemClass for anything, including for "[C"
230
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700231 // run char[] and int[] through FindClass to complete initialization
Jesse Wilson8989d992011-08-02 13:39:42 -0700232 Class* found_char_array_class = FindSystemClass("[C");
233 CHECK_EQ(char_array_class, found_char_array_class);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700234 Class* found_int_array_class = FindSystemClass("[I");
235 CHECK_EQ(int_array_class, found_int_array_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700236
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700237 // Initialize all the other primitive array types for PrimitiveArray::Alloc.
238 // These are easy because everything we need has already been set up.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700239 SetClassRoot(kBooleanArrayClass, FindSystemClass("[Z"));
240 SetClassRoot(kByteArrayClass, FindSystemClass("[B"));
241 SetClassRoot(kDoubleArrayClass, FindSystemClass("[D"));
242 SetClassRoot(kFloatArrayClass, FindSystemClass("[F"));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700243 SetClassRoot(kLongArrayClass, FindSystemClass("[J"));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700244 SetClassRoot(kShortArrayClass, FindSystemClass("[S"));
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700245 SetClassRoot(kJavaLangStackTraceElement, FindSystemClass("Ljava/lang/StackTraceElement;"));
246 SetClassRoot(kJavaLangStackTraceElementArrayClass, FindSystemClass("[Ljava/lang/StackTraceElement;"));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700247 BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass));
248 ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass));
249 DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass));
250 FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700251 LongArray::SetArrayClass(GetClassRoot(kLongArrayClass));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700252 ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass));
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700253 StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700254
Brian Carlstroma663ea52011-08-19 23:33:41 -0700255 FinishInit();
256}
257
258void ClassLinker::FinishInit() {
259 // ensure all class_roots_ are initialized
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700260 for (size_t i = 0; i < kClassRootsMax; i++) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700261 ClassRoot class_root = static_cast<ClassRoot>(i);
262 Class* klass = GetClassRoot(class_root);
263 CHECK(klass != NULL);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700264 DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->dex_cache_ != NULL);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700265 // note SetClassRoot does additional validation.
266 // if possible add new checks there to catch errors early
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700267 }
268
269 // disable the slow paths in FindClass and CreatePrimitiveClass now
270 // that Object, Class, and Object[] are setup
271 init_done_ = true;
272}
273
Brian Carlstroma663ea52011-08-19 23:33:41 -0700274struct ClassLinker::InitCallbackState {
275 ClassLinker* class_linker;
276
277 Class* class_roots[kClassRootsMax];
278
279 typedef std::tr1::unordered_map<std::string, ClassRoot> Table;
280 Table descriptor_to_class_root;
281
282 struct DexCacheHash {
283 size_t operator()(art::DexCache* const& obj) const {
284 return reinterpret_cast<size_t>(&obj);
285 }
286 };
287 typedef std::tr1::unordered_set<DexCache*, DexCacheHash> Set;
288 Set dex_caches;
289};
290
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700291void ClassLinker::Init(const std::vector<const DexFile*>& boot_class_path, Space* space) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700292 CHECK(!init_done_);
293
294 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
295 DCHECK(heap_bitmap != NULL);
296
297 InitCallbackState state;
298 state.class_linker = this;
299 for (size_t i = 0; i < kClassRootsMax; i++) {
300 ClassRoot class_root = static_cast<ClassRoot>(i);
301 state.descriptor_to_class_root[GetClassRootDescriptor(class_root)] = class_root;
302 }
303
304 // reinit clases_ table
305 heap_bitmap->Walk(InitCallback, &state);
306
307 // reinit class_roots_
308 Class* object_array_class = state.class_roots[kObjectArrayClass];
309 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
310 for (size_t i = 0; i < kClassRootsMax; i++) {
311 ClassRoot class_root = static_cast<ClassRoot>(i);
312 SetClassRoot(class_root, state.class_roots[class_root]);
313 }
314
315 // reinit intern_table_
316 ObjectArray<Object>* interned_array = space->GetImageHeader().GetInternedArray();
317 for (int32_t i = 0; i < interned_array->GetLength(); i++) {
318 String* string = interned_array->Get(i)->AsString();
319 intern_table_.Register(string);
320 }
321
322 // reinit array_interfaces_ from any array class instance, they should all be ==
323 array_interfaces_ = GetClassRoot(kObjectArrayClass)->interfaces_;
324 DCHECK(array_interfaces_ == GetClassRoot(kBooleanArrayClass)->interfaces_);
325
326 // build a map from location to DexCache to match up with DexFile::GetLocation
327 std::tr1::unordered_map<std::string, DexCache*> location_to_dex_cache;
328 typedef InitCallbackState::Set::const_iterator It; // TODO: C++0x auto
329 for (It it = state.dex_caches.begin(), end = state.dex_caches.end(); it != end; ++it) {
330 DexCache* dex_cache = *it;
331 std::string location = dex_cache->GetLocation()->ToModifiedUtf8();
332 location_to_dex_cache[location] = dex_cache;
333 }
334
335 // reinit boot_class_path with DexFile arguments and found DexCaches
336 for (size_t i = 0; i != boot_class_path.size(); ++i) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700337 const DexFile* dex_file = boot_class_path[i];
338 CHECK(dex_file != NULL);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700339 DexCache* dex_cache = location_to_dex_cache[dex_file->GetLocation()];
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700340 AppendToBootClassPath(*dex_file, dex_cache);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700341 }
342
343 String::SetClass(GetClassRoot(kJavaLangString));
344 BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass));
345 ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass));
346 CharArray::SetArrayClass(GetClassRoot(kCharArrayClass));
347 DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass));
348 FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass));
349 IntArray::SetArrayClass(GetClassRoot(kIntArrayClass));
350 LongArray::SetArrayClass(GetClassRoot(kLongArrayClass));
351 ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700352 PathClassLoader::SetClass(GetClassRoot(kDalvikSystemPathClassLoader));
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700353 StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700354
355 FinishInit();
356}
357
Brian Carlstrom4873d462011-08-21 15:23:39 -0700358void ClassLinker::InitCallback(Object* obj, void *arg) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700359 DCHECK(obj != NULL);
360 DCHECK(arg != NULL);
361 InitCallbackState* state = reinterpret_cast<InitCallbackState*>(arg);
362
363 if (!obj->IsClass()) {
364 return;
365 }
366 Class* klass = obj->AsClass();
367 CHECK(klass->class_loader_ == NULL);
368
369 std::string descriptor = klass->GetDescriptor()->ToModifiedUtf8();
370
371 // restore class to ClassLinker::classes_ table
372 state->class_linker->InsertClass(descriptor, klass);
373
374 // note DexCache to match with DexFile later
375 DexCache* dex_cache = klass->GetDexCache();
376 if (dex_cache != NULL) {
377 state->dex_caches.insert(dex_cache);
378 } else {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700379 DCHECK(klass->IsArrayClass() || klass->IsPrimitive());
Brian Carlstroma663ea52011-08-19 23:33:41 -0700380 }
381
382 // check if this is a root, if so, register it
383 typedef InitCallbackState::Table::const_iterator It; // TODO: C++0x auto
384 It it = state->descriptor_to_class_root.find(descriptor);
385 if (it != state->descriptor_to_class_root.end()) {
386 ClassRoot class_root = it->second;
387 state->class_roots[class_root] = klass;
388 }
389}
390
391// Keep in sync with InitCallback. Anything we visit, we need to
392// reinit references to when reinitializing a ClassLinker from a
393// mapped image.
394void ClassLinker::VisitRoots(Heap::RootVistor* root_visitor, void* arg) const {
395
Brian Carlstromb88e9442011-07-28 15:15:51 -0700396 root_visitor(class_roots_, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700397
398 for (size_t i = 0; i < dex_caches_.size(); i++) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700399 root_visitor(dex_caches_[i], arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700400 }
401
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700402 {
403 MutexLock mu(classes_lock_);
404 typedef Table::const_iterator It; // TODO: C++0x auto
405 for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
406 root_visitor(it->second, arg);
407 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700408 }
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700409
410 intern_table_.VisitRoots(root_visitor, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700411
Brian Carlstromb88e9442011-07-28 15:15:51 -0700412 root_visitor(array_interfaces_, arg);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700413}
414
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700415ClassLinker::~ClassLinker() {
416 delete classes_lock_;
417 String::ResetClass();
418 BooleanArray::ResetArrayClass();
419 ByteArray::ResetArrayClass();
420 CharArray::ResetArrayClass();
421 DoubleArray::ResetArrayClass();
422 FloatArray::ResetArrayClass();
423 IntArray::ResetArrayClass();
424 LongArray::ResetArrayClass();
425 ShortArray::ResetArrayClass();
426 PathClassLoader::ResetClass();
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700427 StackTraceElement::ResetClass();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700428}
429
430DexCache* ClassLinker::AllocDexCache(const DexFile& dex_file) {
Brian Carlstrom83db7722011-08-26 17:32:56 -0700431 DexCache* dex_cache = down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::LengthAsArray()));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700432 dex_cache->Init(String::AllocFromModifiedUtf8(dex_file.GetLocation().c_str()),
433 AllocObjectArray<String>(dex_file.NumStringIds()),
434 AllocObjectArray<Class>(dex_file.NumTypeIds()),
435 AllocObjectArray<Method>(dex_file.NumMethodIds()),
Brian Carlstrom83db7722011-08-26 17:32:56 -0700436 AllocObjectArray<Field>(dex_file.NumFieldIds()),
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700437 AllocCodeAndDirectMethods(dex_file.NumMethodIds()),
438 AllocObjectArray<StaticStorageBase>(dex_file.NumTypeIds()));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700439 return dex_cache;
Brian Carlstroma0808032011-07-18 00:39:23 -0700440}
441
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700442CodeAndDirectMethods* ClassLinker::AllocCodeAndDirectMethods(size_t length) {
443 return down_cast<CodeAndDirectMethods*>(IntArray::Alloc(CodeAndDirectMethods::LengthAsArray(length)));
Brian Carlstrom83db7722011-08-26 17:32:56 -0700444}
445
Brian Carlstrom4873d462011-08-21 15:23:39 -0700446Class* ClassLinker::AllocClass(Class* java_lang_Class, size_t class_size) {
447 DCHECK_GE(class_size, sizeof(Class));
448 Class* klass = Heap::AllocObject(java_lang_Class, class_size)->AsClass();
449 klass->class_size_ = class_size;
450 return klass;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700451}
452
Brian Carlstrom4873d462011-08-21 15:23:39 -0700453Class* ClassLinker::AllocClass(size_t class_size) {
454 return AllocClass(GetClassRoot(kJavaLangClass), class_size);
Brian Carlstroma0808032011-07-18 00:39:23 -0700455}
456
Jesse Wilson35baaab2011-08-10 16:18:03 -0400457Field* ClassLinker::AllocField() {
458 return down_cast<Field*>(GetClassRoot(kJavaLangReflectField)->NewInstance());
Brian Carlstroma0808032011-07-18 00:39:23 -0700459}
460
461Method* ClassLinker::AllocMethod() {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700462 return down_cast<Method*>(GetClassRoot(kJavaLangReflectMethod)->NewInstance());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700463}
464
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700465ObjectArray<StackTraceElement>* ClassLinker::AllocStackTraceElementArray(size_t length) {
466 return ObjectArray<StackTraceElement>::Alloc(
467 GetClassRoot(kJavaLangStackTraceElementArrayClass),
468 length);
469}
470
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700471Class* ClassLinker::FindClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700472 const ClassLoader* class_loader) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700473 // TODO: remove this contrived parent class loader check when we have a real ClassLoader.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700474 if (class_loader != NULL) {
475 Class* klass = FindClass(descriptor, NULL);
476 if (klass != NULL) {
477 return klass;
478 }
Elliott Hughesbd935992011-08-22 11:59:34 -0700479 Thread::Current()->ClearException();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700480 }
481
Carl Shapirob5573532011-07-12 18:22:59 -0700482 Thread* self = Thread::Current();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700483 DCHECK(self != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700484 CHECK(!self->IsExceptionPending());
485 // Find the class in the loaded classes table.
486 Class* klass = LookupClass(descriptor, class_loader);
487 if (klass == NULL) {
488 // Class is not yet loaded.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700489 if (descriptor[0] == '[') {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700490 return CreateArrayClass(descriptor, class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700491 }
Brian Carlstrom8a487412011-08-29 20:08:52 -0700492 const DexFile::ClassPath& class_path = ((class_loader != NULL)
493 ? ClassLoader::GetClassPath(class_loader)
494 : boot_class_path_);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700495 DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, class_path);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700496 if (pair.second == NULL) {
Elliott Hughesbd935992011-08-22 11:59:34 -0700497 std::string name(PrintableString(descriptor));
498 self->ThrowNewException("Ljava/lang/NoClassDefFoundError;",
499 "Class %s not found in class loader %p", name.c_str(), class_loader);
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700500 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700501 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700502 const DexFile& dex_file = *pair.first;
503 const DexFile::ClassDef& dex_class_def = *pair.second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700504 DexCache* dex_cache = FindDexCache(dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700505 // Load the class from the dex file.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700506 if (!init_done_) {
507 // finish up init of hand crafted class_roots_
508 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700509 klass = GetClassRoot(kJavaLangObject);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700510 } else if (descriptor == "Ljava/lang/Class;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700511 klass = GetClassRoot(kJavaLangClass);
Jesse Wilson14150742011-07-29 19:04:44 -0400512 } else if (descriptor == "Ljava/lang/String;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700513 klass = GetClassRoot(kJavaLangString);
514 } else if (descriptor == "Ljava/lang/reflect/Field;") {
515 klass = GetClassRoot(kJavaLangReflectField);
516 } else if (descriptor == "Ljava/lang/reflect/Method;") {
517 klass = GetClassRoot(kJavaLangReflectMethod);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700518 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700519 klass = AllocClass(SizeOfClass(dex_file, dex_class_def));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700520 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700521 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700522 klass = AllocClass(SizeOfClass(dex_file, dex_class_def));
Carl Shapiro565f5072011-07-10 13:39:43 -0700523 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700524 klass->dex_cache_ = dex_cache;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700525 LoadClass(dex_file, dex_class_def, klass, class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700526 // Check for a pending exception during load
527 if (self->IsExceptionPending()) {
528 // TODO: free native allocations in klass
529 return NULL;
530 }
531 {
532 ObjectLock lock(klass);
Carl Shapirob5573532011-07-12 18:22:59 -0700533 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700534 // Add the newly loaded class to the loaded classes table.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700535 bool success = InsertClass(descriptor, klass); // TODO: just return collision
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700536 if (!success) {
537 // We may fail to insert if we raced with another thread.
538 klass->clinit_thread_id_ = 0;
539 // TODO: free native allocations in klass
540 klass = LookupClass(descriptor, class_loader);
541 CHECK(klass != NULL);
542 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700543 // Finish loading (if necessary) by finding parents
544 if (!klass->IsLoaded() && !LoadSuperAndInterfaces(klass, dex_file)) {
545 // Loading failed.
546 // TODO: CHECK(self->IsExceptionPending());
547 lock.NotifyAll();
548 return NULL;
549 }
550 CHECK(klass->IsLoaded());
551 // Link the class (if necessary)
552 if (!klass->IsLinked() && !LinkClass(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700553 // Linking failed.
554 // TODO: CHECK(self->IsExceptionPending());
555 lock.NotifyAll();
556 return NULL;
557 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700558 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700559 }
560 }
561 }
562 // Link the class if it has not already been linked.
563 if (!klass->IsLinked() && !klass->IsErroneous()) {
564 ObjectLock lock(klass);
565 // Check for circular dependencies between classes.
Carl Shapirob5573532011-07-12 18:22:59 -0700566 if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) {
Elliott Hughesbd935992011-08-22 11:59:34 -0700567 self->ThrowNewException("Ljava/lang/ClassCircularityError;", NULL); // TODO: detail
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700568 return NULL;
569 }
570 // Wait for the pending initialization to complete.
571 while (!klass->IsLinked() && !klass->IsErroneous()) {
572 lock.Wait();
573 }
574 }
575 if (klass->IsErroneous()) {
576 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
577 return NULL;
578 }
579 // Return the loaded class. No exceptions should be pending.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700580 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700581 CHECK(!self->IsExceptionPending());
582 return klass;
583}
584
Brian Carlstrom4873d462011-08-21 15:23:39 -0700585// Precomputes size that will be needed for Class, matching LinkStaticFields
586size_t ClassLinker::SizeOfClass(const DexFile& dex_file,
587 const DexFile::ClassDef& dex_class_def) {
588 const byte* class_data = dex_file.GetClassData(dex_class_def);
589 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
590 size_t num_static_fields = header.static_fields_size_;
591 size_t num_ref = 0;
592 size_t num_32 = 0;
593 size_t num_64 = 0;
594 if (num_static_fields != 0) {
595 uint32_t last_idx = 0;
596 for (size_t i = 0; i < num_static_fields; ++i) {
597 DexFile::Field dex_field;
598 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
599 const DexFile::FieldId& field_id = dex_file.GetFieldId(dex_field.field_idx_);
600 const char* descriptor = dex_file.dexStringByTypeIdx(field_id.type_idx_);
601 char c = descriptor[0];
602 if (c == 'L' || c == '[') {
603 num_ref++;
604 } else if (c == 'J' || c == 'D') {
605 num_64++;
606 } else {
607 num_32++;
608 }
609 }
610 }
611
612 // start with generic class data
613 size_t size = sizeof(Class);
614 // follow with reference fields which must be contiguous at start
615 size += (num_ref * sizeof(uint32_t));
616 // if there are 64-bit fields to add, make sure they are aligned
617 if (num_64 != 0 && size != RoundUp(size, 8)) { // for 64-bit alignment
618 if (num_32 != 0) {
619 // use an available 32-bit field for padding
620 num_32--;
621 }
622 size += sizeof(uint32_t); // either way, we are adding a word
623 DCHECK_EQ(size, RoundUp(size, 8));
624 }
625 // tack on any 64-bit fields now that alignment is assured
626 size += (num_64 * sizeof(uint64_t));
627 // tack on any remaining 32-bit fields
628 size += (num_32 * sizeof(uint32_t));
629 return size;
630}
631
Brian Carlstromf615a612011-07-23 12:50:34 -0700632void ClassLinker::LoadClass(const DexFile& dex_file,
633 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700634 Class* klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700635 const ClassLoader* class_loader) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700636 CHECK(klass != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700637 CHECK(klass->dex_cache_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700638 CHECK_EQ(Class::kStatusNotReady, klass->status_);
Brian Carlstromf615a612011-07-23 12:50:34 -0700639 const byte* class_data = dex_file.GetClassData(dex_class_def);
640 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700641
Brian Carlstromf615a612011-07-23 12:50:34 -0700642 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700643 CHECK(descriptor != NULL);
644
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700645 klass->klass_ = GetClassRoot(kJavaLangClass);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700646 klass->descriptor_ = String::AllocFromModifiedUtf8(descriptor);
Brian Carlstromf615a612011-07-23 12:50:34 -0700647 klass->access_flags_ = dex_class_def.access_flags_;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700648 klass->class_loader_ = class_loader;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700649 klass->primitive_type_ = Class::kPrimNot;
650 klass->status_ = Class::kStatusIdx;
651
652 klass->super_class_ = NULL;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700653 klass->super_class_type_idx_ = dex_class_def.superclass_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700654
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700655 size_t num_static_fields = header.static_fields_size_;
656 size_t num_instance_fields = header.instance_fields_size_;
657 size_t num_direct_methods = header.direct_methods_size_;
658 size_t num_virtual_methods = header.virtual_methods_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700659
Brian Carlstromf615a612011-07-23 12:50:34 -0700660 klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700661
662 // Load class interfaces.
Brian Carlstromf615a612011-07-23 12:50:34 -0700663 LoadInterfaces(dex_file, dex_class_def, klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700664
665 // Load static fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700666 DCHECK(klass->sfields_ == NULL);
667 if (num_static_fields != 0) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400668 klass->sfields_ = AllocObjectArray<Field>(num_static_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700669 uint32_t last_idx = 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700670 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700671 DexFile::Field dex_field;
672 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400673 Field* sfield = AllocField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700674 klass->SetStaticField(i, sfield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700675 LoadField(dex_file, dex_field, klass, sfield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700676 }
677 }
678
679 // Load instance fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700680 DCHECK(klass->ifields_ == NULL);
681 if (num_instance_fields != 0) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400682 klass->ifields_ = AllocObjectArray<Field>(num_instance_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700683 uint32_t last_idx = 0;
684 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700685 DexFile::Field dex_field;
686 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400687 Field* ifield = AllocField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700688 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700689 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700690 }
691 }
692
693 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700694 DCHECK(klass->direct_methods_ == NULL);
695 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700696 // TODO: append direct methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700697 klass->direct_methods_ = AllocObjectArray<Method>(num_direct_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700698 uint32_t last_idx = 0;
699 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700700 DexFile::Method dex_method;
701 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700702 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700703 klass->SetDirectMethod(i, meth);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700704 LoadMethod(dex_file, dex_method, klass, meth, true);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700705 // TODO: register maps
706 }
707 }
708
709 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700710 DCHECK(klass->virtual_methods_ == NULL);
711 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700712 // TODO: append virtual methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700713 klass->virtual_methods_ = AllocObjectArray<Method>(num_virtual_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700714 uint32_t last_idx = 0;
715 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700716 DexFile::Method dex_method;
717 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700718 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700719 klass->SetVirtualMethod(i, meth);
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700720 LoadMethod(dex_file, dex_method, klass, meth, false);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700721 // TODO: register maps
722 }
723 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700724}
725
Brian Carlstromf615a612011-07-23 12:50:34 -0700726void ClassLinker::LoadInterfaces(const DexFile& dex_file,
727 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700728 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700729 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700730 if (list != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700731 DCHECK(klass->interfaces_ == NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700732 klass->interfaces_ = AllocObjectArray<Class>(list->Size());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700733 DCHECK(klass->interfaces_type_idx_ == NULL);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700734 klass->interfaces_type_idx_ = IntArray::Alloc(list->Size());
Brian Carlstrom934486c2011-07-12 23:42:50 -0700735 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700736 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700737 klass->interfaces_type_idx_->Set(i, type_item.type_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700738 }
739 }
740}
741
Brian Carlstromf615a612011-07-23 12:50:34 -0700742void ClassLinker::LoadField(const DexFile& dex_file,
743 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700744 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700745 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700746 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400747 dst->declaring_class_ = klass;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700748 dst->name_ = ResolveString(dex_file, field_id.name_idx_, klass->GetDexCache());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700749 dst->descriptor_.set(dex_file.dexStringByTypeIdx(field_id.type_idx_));
Jesse Wilson35baaab2011-08-10 16:18:03 -0400750 // TODO: Assign dst->type_.
Brian Carlstrom934486c2011-07-12 23:42:50 -0700751 dst->access_flags_ = src.access_flags_;
752}
753
Brian Carlstromf615a612011-07-23 12:50:34 -0700754void ClassLinker::LoadMethod(const DexFile& dex_file,
755 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700756 Class* klass,
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700757 Method* dst,
758 bool is_direct) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700759 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400760 dst->declaring_class_ = klass;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700761 dst->name_ = ResolveString(dex_file, method_id.name_idx_, klass->GetDexCache());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700762 {
763 int32_t utf16_length;
Elliott Hughes0c424cb2011-08-26 10:16:25 -0700764 std::string utf8(dex_file.CreateMethodDescriptor(method_id.proto_idx_, &utf16_length));
765 dst->signature_ = String::AllocFromModifiedUtf8(utf16_length, utf8.c_str());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700766 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700767 dst->proto_idx_ = method_id.proto_idx_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700768 dst->code_off_ = src.code_off_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700769 dst->shorty_ = dex_file.GetShorty(method_id.proto_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700770 dst->access_flags_ = src.access_flags_;
771
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700772 dst->dex_cache_strings_ = klass->dex_cache_->GetStrings();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700773 dst->dex_cache_resolved_types_ = klass->dex_cache_->GetResolvedTypes();
774 dst->dex_cache_resolved_methods_ = klass->dex_cache_->GetResolvedMethods();
775 dst->dex_cache_resolved_fields_ = klass->dex_cache_->GetResolvedFields();
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700776 dst->dex_cache_code_and_direct_methods_ = klass->dex_cache_->GetCodeAndDirectMethods();
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700777 dst->dex_cache_initialized_static_storage_ = klass->dex_cache_->GetInitializedStaticStorage();
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700778
779 dst->is_direct_ = is_direct;
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700780
Brian Carlstrom934486c2011-07-12 23:42:50 -0700781 // TODO: check for finalize method
782
Brian Carlstromf615a612011-07-23 12:50:34 -0700783 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700784 if (code_item != NULL) {
785 dst->num_registers_ = code_item->registers_size_;
786 dst->num_ins_ = code_item->ins_size_;
787 dst->num_outs_ = code_item->outs_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700788 } else {
789 uint16_t num_args = dst->NumArgRegisters();
790 if (!dst->IsStatic()) {
791 ++num_args;
792 }
793 dst->num_registers_ = dst->num_ins_ + num_args;
794 // TODO: native methods
795 }
796}
797
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700798void ClassLinker::AppendToBootClassPath(const DexFile& dex_file) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700799 AppendToBootClassPath(dex_file, AllocDexCache(dex_file));
800}
801
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700802void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700803 CHECK(dex_cache != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700804 boot_class_path_.push_back(&dex_file);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700805 RegisterDexFile(dex_file, dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700806}
807
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700808void ClassLinker::RegisterDexFile(const DexFile& dex_file) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700809 RegisterDexFile(dex_file, AllocDexCache(dex_file));
810}
811
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700812void ClassLinker::RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700813 CHECK(dex_cache != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700814 dex_files_.push_back(&dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700815 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700816}
817
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700818const DexFile& ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700819 for (size_t i = 0; i != dex_caches_.size(); ++i) {
820 if (dex_caches_[i] == dex_cache) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700821 return *dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700822 }
823 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700824 CHECK(false) << "Could not find DexFile";
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700825 return *dex_files_[-1];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700826}
827
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700828DexCache* ClassLinker::FindDexCache(const DexFile& dex_file) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700829 for (size_t i = 0; i != dex_files_.size(); ++i) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700830 if (dex_files_[i] == &dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700831 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700832 }
833 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700834 CHECK(false) << "Could not find DexCache";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700835 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700836}
837
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700838Class* ClassLinker::CreatePrimitiveClass(const char* descriptor) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700839 Class* klass = AllocClass(sizeof(Class));
Carl Shapiro565f5072011-07-10 13:39:43 -0700840 CHECK(klass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700841 klass->super_class_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700842 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700843 klass->descriptor_ = String::AllocFromModifiedUtf8(descriptor);
Carl Shapiro565f5072011-07-10 13:39:43 -0700844 klass->status_ = Class::kStatusInitialized;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700845 bool success = InsertClass(descriptor, klass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700846 CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed";
Carl Shapiro565f5072011-07-10 13:39:43 -0700847 return klass;
848}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700849
Brian Carlstrombe977852011-07-19 14:54:54 -0700850// Create an array class (i.e. the class object for the array, not the
851// array itself). "descriptor" looks like "[C" or "[[[[B" or
852// "[Ljava/lang/String;".
853//
854// If "descriptor" refers to an array of primitives, look up the
855// primitive type's internally-generated class object.
856//
857// "loader" is the class loader of the class that's referring to us. It's
858// used to ensure that we're looking for the element type in the right
859// context. It does NOT become the class loader for the array class; that
860// always comes from the base element class.
861//
862// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700863Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700864 const ClassLoader* class_loader)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700865{
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700866 CHECK(descriptor[0] == '[');
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700867
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700868 // Identify the underlying element class and the array dimension depth.
869 Class* component_type_ = NULL;
870 int array_rank;
871 if (descriptor[1] == '[') {
872 // array of arrays; keep descriptor and grab stuff from parent
873 Class* outer = FindClass(descriptor.substr(1), class_loader);
874 if (outer != NULL) {
875 // want the base class, not "outer", in our component_type_
876 component_type_ = outer->component_type_;
877 array_rank = outer->array_rank_ + 1;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700878 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700879 DCHECK(component_type_ == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700880 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700881 } else {
882 array_rank = 1;
883 if (descriptor[1] == 'L') {
884 // array of objects; strip off "[" and look up descriptor.
885 const StringPiece subDescriptor = descriptor.substr(1);
886 component_type_ = FindClass(subDescriptor, class_loader);
887 } else {
888 // array of a primitive type
889 component_type_ = FindPrimitiveClass(descriptor[1]);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700890 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700891 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700892
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700893 if (component_type_ == NULL) {
894 // failed
895 // DCHECK(Thread::Current()->IsExceptionPending()); // TODO
896 return NULL;
897 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700898
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700899 // See if the component type is already loaded. Array classes are
900 // always associated with the class loader of their underlying
901 // element type -- an array of Strings goes with the loader for
902 // java/lang/String -- so we need to look for it there. (The
903 // caller should have checked for the existence of the class
904 // before calling here, but they did so with *their* class loader,
905 // not the component type's loader.)
906 //
907 // If we find it, the caller adds "loader" to the class' initiating
908 // loader list, which should prevent us from going through this again.
909 //
910 // This call is unnecessary if "loader" and "component_type_->class_loader_"
911 // are the same, because our caller (FindClass) just did the
912 // lookup. (Even if we get this wrong we still have correct behavior,
913 // because we effectively do this lookup again when we add the new
914 // class to the hash table --- necessary because of possible races with
915 // other threads.)
916 if (class_loader != component_type_->class_loader_) {
917 Class* new_class = LookupClass(descriptor, component_type_->class_loader_);
918 if (new_class != NULL) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700919 return new_class;
920 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700921 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700922
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700923 // Fill out the fields in the Class.
924 //
925 // It is possible to execute some methods against arrays, because
926 // all arrays are subclasses of java_lang_Object_, so we need to set
927 // up a vtable. We can just point at the one in java_lang_Object_.
928 //
929 // Array classes are simple enough that we don't need to do a full
930 // link step.
931
932 Class* new_class = NULL;
933 if (!init_done_) {
934 if (descriptor == "[Ljava/lang/Object;") {
935 new_class = GetClassRoot(kObjectArrayClass);
936 } else if (descriptor == "[C") {
937 new_class = GetClassRoot(kCharArrayClass);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700938 } else if (descriptor == "[I") {
939 new_class = GetClassRoot(kIntArrayClass);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700940 }
941 }
942 if (new_class == NULL) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700943 new_class = AllocClass(sizeof(Class));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700944 if (new_class == NULL) {
945 return NULL;
946 }
947 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700948 new_class->descriptor_ = String::AllocFromModifiedUtf8(descriptor.ToString().c_str());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700949 Class* java_lang_Object = GetClassRoot(kJavaLangObject);
950 new_class->super_class_ = java_lang_Object;
951 new_class->vtable_ = java_lang_Object->vtable_;
952 new_class->primitive_type_ = Class::kPrimNot;
953 new_class->component_type_ = component_type_;
954 new_class->class_loader_ = component_type_->class_loader_;
955 new_class->array_rank_ = array_rank;
956 new_class->status_ = Class::kStatusInitialized;
957 // don't need to set new_class->object_size_
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700958 // because Object::SizeOf delegates to Array::SizeOf
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700959
960
961 // All arrays have java/lang/Cloneable and java/io/Serializable as
962 // interfaces. We need to set that up here, so that stuff like
963 // "instanceof" works right.
964 //
965 // Note: The GC could run during the call to FindSystemClass,
966 // so we need to make sure the class object is GC-valid while we're in
967 // there. Do this by clearing the interface list so the GC will just
968 // think that the entries are null.
969
970
971 // Use the single, global copies of "interfaces" and "iftable"
972 // (remember not to free them for arrays).
973 DCHECK(array_interfaces_ != NULL);
974 new_class->interfaces_ = array_interfaces_;
975 new_class->iftable_count_ = 2;
976 DCHECK(array_iftable_ != NULL);
977 new_class->iftable_ = array_iftable_;
978
979 // Inherit access flags from the component type. Arrays can't be
980 // used as a superclass or interface, so we want to add "final"
981 // and remove "interface".
982 //
983 // Don't inherit any non-standard flags (e.g., kAccFinal)
984 // from component_type_. We assume that the array class does not
985 // override finalize().
986 new_class->access_flags_ = ((new_class->component_type_->access_flags_ &
987 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask;
988
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700989 if (InsertClass(descriptor, new_class)) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700990 return new_class;
991 }
992 // Another thread must have loaded the class after we
993 // started but before we finished. Abandon what we've
994 // done.
995 //
996 // (Yes, this happens.)
997
998 // Grab the winning class.
999 Class* other_class = LookupClass(descriptor, component_type_->class_loader_);
1000 DCHECK(other_class != NULL);
1001 return other_class;
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001002}
1003
1004Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -07001005 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001006 case 'B':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001007 return GetClassRoot(kPrimitiveByte);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001008 case 'C':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001009 return GetClassRoot(kPrimitiveChar);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001010 case 'D':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001011 return GetClassRoot(kPrimitiveDouble);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001012 case 'F':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001013 return GetClassRoot(kPrimitiveFloat);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001014 case 'I':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001015 return GetClassRoot(kPrimitiveInt);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001016 case 'J':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001017 return GetClassRoot(kPrimitiveLong);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001018 case 'S':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001019 return GetClassRoot(kPrimitiveShort);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001020 case 'Z':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001021 return GetClassRoot(kPrimitiveBoolean);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001022 case 'V':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001023 return GetClassRoot(kPrimitiveVoid);
Carl Shapiro744ad052011-08-06 15:53:36 -07001024 }
Elliott Hughesbd935992011-08-22 11:59:34 -07001025 std::string printable_type(PrintableChar(type));
1026 Thread::Current()->ThrowNewException("Ljava/lang/NoClassDefFoundError;",
1027 "Not a primitive type: %s", printable_type.c_str());
1028 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001029}
1030
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001031bool ClassLinker::InsertClass(const StringPiece& descriptor, Class* klass) {
1032 size_t hash = StringPieceHash()(descriptor);
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001033 MutexLock mu(classes_lock_);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001034 Table::iterator it = classes_.insert(std::make_pair(hash, klass));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001035 return ((*it).second == klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001036}
1037
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001038Class* ClassLinker::LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001039 size_t hash = StringPieceHash()(descriptor);
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001040 MutexLock mu(classes_lock_);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001041 typedef Table::const_iterator It; // TODO: C++0x auto
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001042 for (It it = classes_.find(hash), end = classes_.end(); it != end; ++it) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001043 Class* klass = it->second;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001044 if (klass->descriptor_->Equals(descriptor) && klass->class_loader_ == class_loader) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001045 return klass;
1046 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001047 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001048 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001049}
1050
1051bool ClassLinker::InitializeClass(Class* klass) {
1052 CHECK(klass->GetStatus() == Class::kStatusResolved ||
1053 klass->GetStatus() == Class::kStatusError);
1054
Carl Shapirob5573532011-07-12 18:22:59 -07001055 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001056
1057 {
1058 ObjectLock lock(klass);
1059
1060 if (klass->GetStatus() < Class::kStatusVerified) {
1061 if (klass->IsErroneous()) {
1062 LG << "re-initializing failed class"; // TODO: throw
1063 return false;
1064 }
1065
1066 CHECK(klass->GetStatus() == Class::kStatusResolved);
1067
1068 klass->status_ = Class::kStatusVerifying;
1069 if (!DexVerify::VerifyClass(klass)) {
1070 LG << "Verification failed"; // TODO: ThrowVerifyError
1071 Object* exception = self->GetException();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001072 klass->SetVerifyErrorClass(exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001073 klass->SetStatus(Class::kStatusError);
1074 return false;
1075 }
1076
1077 klass->SetStatus(Class::kStatusVerified);
1078 }
1079
1080 if (klass->status_ == Class::kStatusInitialized) {
1081 return true;
1082 }
1083
1084 while (klass->status_ == Class::kStatusInitializing) {
1085 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -07001086 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001087 LG << "recursive <clinit>";
1088 return true;
1089 }
1090
1091 CHECK(!self->IsExceptionPending());
1092
1093 lock.Wait(); // TODO: check for interruption
1094
1095 // When we wake up, repeat the test for init-in-progress. If
1096 // there's an exception pending (only possible if
1097 // "interruptShouldThrow" was set), bail out.
1098 if (self->IsExceptionPending()) {
1099 CHECK(false);
1100 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
1101 klass->SetStatus(Class::kStatusError);
1102 return false;
1103 }
1104 if (klass->GetStatus() == Class::kStatusInitializing) {
1105 continue;
1106 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001107 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001108 klass->GetStatus() == Class::kStatusError);
1109 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001110 // The caller wants an exception, but it was thrown in a
1111 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001112 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
1113 return false;
1114 }
1115 return true; // otherwise, initialized
1116 }
1117
1118 // see if we failed previously
1119 if (klass->IsErroneous()) {
1120 // might be wise to unlock before throwing; depends on which class
1121 // it is that we have locked
1122
1123 // TODO: throwEarlierClassFailure(klass);
1124 return false;
1125 }
1126
1127 if (!ValidateSuperClassDescriptors(klass)) {
1128 klass->SetStatus(Class::kStatusError);
1129 return false;
1130 }
1131
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001132 DCHECK(klass->status_ < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001133
Carl Shapirob5573532011-07-12 18:22:59 -07001134 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001135 klass->status_ = Class::kStatusInitializing;
1136 }
1137
1138 if (!InitializeSuperClass(klass)) {
1139 return false;
1140 }
1141
1142 InitializeStaticFields(klass);
1143
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001144 Method* clinit = klass->FindDeclaredDirectMethod("<clinit>", "()V");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001145 if (clinit != NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001146 // JValue unused;
1147 // TODO: dvmCallMethod(self, method, NULL, &unused);
Elliott Hughes53b61312011-08-12 18:28:20 -07001148 // UNIMPLEMENTED(FATAL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001149 }
1150
1151 {
1152 ObjectLock lock(klass);
1153
1154 if (self->IsExceptionPending()) {
1155 klass->SetStatus(Class::kStatusError);
1156 } else {
1157 klass->SetStatus(Class::kStatusInitialized);
1158 }
1159 lock.NotifyAll();
1160 }
1161
1162 return true;
1163}
1164
1165bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
1166 if (klass->IsInterface()) {
1167 return true;
1168 }
1169 // begin with the methods local to the superclass
1170 if (klass->HasSuperClass() &&
1171 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
1172 const Class* super = klass->GetSuperClass();
1173 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001174 const Method* method = super->GetVirtualMethod(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001175 if (method != super->GetVirtualMethod(i) &&
1176 !HasSameMethodDescriptorClasses(method, super, klass)) {
1177 LG << "Classes resolve differently in superclass";
1178 return false;
1179 }
1180 }
1181 }
1182 for (size_t i = 0; i < klass->iftable_count_; ++i) {
1183 const InterfaceEntry* iftable = &klass->iftable_[i];
Brian Carlstrom30b94452011-08-25 21:35:26 -07001184 Class* interface = iftable->GetInterface();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001185 if (klass->GetClassLoader() != interface->GetClassLoader()) {
1186 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1187 uint32_t vtable_index = iftable->method_index_array_[j];
1188 const Method* method = klass->GetVirtualMethod(vtable_index);
1189 if (!HasSameMethodDescriptorClasses(method, interface,
1190 method->GetClass())) {
1191 LG << "Classes resolve differently in interface"; // TODO: LinkageError
1192 return false;
1193 }
1194 }
1195 }
1196 }
1197 return true;
1198}
1199
1200bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -07001201 const Class* klass1,
1202 const Class* klass2) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001203 const DexFile& dex_file = FindDexFile(method->GetClass()->GetDexCache());
1204 const DexFile::ProtoId& proto_id = dex_file.GetProtoId(method->proto_idx_);
Brian Carlstromf615a612011-07-23 12:50:34 -07001205 DexFile::ParameterIterator *it;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001206 for (it = dex_file.GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001207 const char* descriptor = it->GetDescriptor();
1208 if (descriptor == NULL) {
1209 break;
1210 }
1211 if (descriptor[0] == 'L' || descriptor[0] == '[') {
1212 // Found a non-primitive type.
1213 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
1214 return false;
1215 }
1216 }
1217 }
1218 // Check the return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001219 const char* descriptor = dex_file.GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001220 if (descriptor[0] == 'L' || descriptor[0] == '[') {
1221 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
1222 return false;
1223 }
1224 }
1225 return true;
1226}
1227
1228// Returns true if classes referenced by the descriptor are the
1229// same classes in klass1 as they are in klass2.
1230bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -07001231 const Class* klass1,
1232 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001233 CHECK(descriptor != NULL);
1234 CHECK(klass1 != NULL);
1235 CHECK(klass2 != NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001236 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001237 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001238 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001239 // TODO: found2 == NULL
1240 // TODO: lookup found1 in initiating loader list
1241 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -07001242 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001243 if (found1 == found2) {
1244 return true;
1245 } else {
1246 return false;
1247 }
1248 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001249 return true;
1250}
1251
1252bool ClassLinker::InitializeSuperClass(Class* klass) {
1253 CHECK(klass != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001254 if (!klass->IsInterface() && klass->HasSuperClass()) {
1255 Class* super_class = klass->GetSuperClass();
1256 if (super_class->GetStatus() != Class::kStatusInitialized) {
1257 CHECK(!super_class->IsInterface());
1258 klass->MonitorExit();
1259 bool super_initialized = InitializeClass(super_class);
1260 klass->MonitorEnter();
1261 // TODO: check for a pending exception
1262 if (!super_initialized) {
1263 klass->SetStatus(Class::kStatusError);
1264 klass->NotifyAll();
1265 return false;
1266 }
1267 }
1268 }
1269 return true;
1270}
1271
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001272bool ClassLinker::EnsureInitialized(Class* c) {
1273 CHECK(c != NULL);
1274 if (c->IsInitialized()) {
1275 return true;
1276 }
1277
1278 c->MonitorExit();
1279 InitializeClass(c);
1280 c->MonitorEnter();
1281 return !Thread::Current()->IsExceptionPending();
1282}
1283
Brian Carlstromb9edb842011-08-28 16:31:06 -07001284StaticStorageBase* ClassLinker::InitializeStaticStorageFromCode(uint32_t type_idx,
1285 const Method* referrer) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001286 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1287 Class* klass = class_linker->ResolveType(type_idx, referrer);
1288 if (klass == NULL) {
1289 UNIMPLEMENTED(FATAL) << "throw exception due to unresolved class";
1290 }
1291 if (!class_linker->EnsureInitialized(klass)) {
1292 CHECK(Thread::Current()->IsExceptionPending());
1293 UNIMPLEMENTED(FATAL) << "throw exception due to class initializtion problem";
1294 }
1295 return klass;
1296}
1297
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001298void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001299 size_t num_static_fields = klass->NumStaticFields();
1300 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001301 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001302 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001303 DexCache* dex_cache = klass->GetDexCache();
Brian Carlstrom4873d462011-08-21 15:23:39 -07001304 // TODO: this seems like the wrong check. do we really want !IsPrimitive && !IsArray?
Brian Carlstromf615a612011-07-23 12:50:34 -07001305 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001306 return;
1307 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001308 const std::string descriptor(klass->GetDescriptor()->ToModifiedUtf8());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001309 const DexFile& dex_file = FindDexFile(dex_cache);
1310 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Brian Carlstromf615a612011-07-23 12:50:34 -07001311 CHECK(dex_class_def != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001312 const byte* addr = dex_file.GetEncodedArray(*dex_class_def);
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001313 if (addr == NULL) {
1314 // All this class' static fields have default values.
1315 return;
1316 }
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001317 size_t array_size = DecodeUnsignedLeb128(&addr);
1318 for (size_t i = 0; i < array_size; ++i) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001319 Field* field = klass->GetStaticField(i);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001320 JValue value;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001321 DexFile::ValueType type = dex_file.ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001322 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001323 case DexFile::kByte:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001324 field->SetByte(NULL, value.b);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001325 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001326 case DexFile::kShort:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001327 field->SetShort(NULL, value.s);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001328 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001329 case DexFile::kChar:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001330 field->SetChar(NULL, value.c);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001331 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001332 case DexFile::kInt:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001333 field->SetInt(NULL, value.i);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001334 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001335 case DexFile::kLong:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001336 field->SetLong(NULL, value.j);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001337 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001338 case DexFile::kFloat:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001339 field->SetFloat(NULL, value.f);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001340 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001341 case DexFile::kDouble:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001342 field->SetDouble(NULL, value.d);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001343 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001344 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001345 uint32_t string_idx = value.i;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001346 String* resolved = ResolveString(dex_file, string_idx, klass->GetDexCache());
Brian Carlstrom4873d462011-08-21 15:23:39 -07001347 field->SetObject(NULL, resolved);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001348 break;
1349 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001350 case DexFile::kBoolean:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001351 field->SetBoolean(NULL, value.z);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001352 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001353 case DexFile::kNull:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001354 field->SetObject(NULL, value.l);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001355 break;
1356 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001357 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001358 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001359 }
1360}
1361
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001362bool ClassLinker::LinkClass(Class* klass, const DexFile& dex_file) {
1363 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001364 if (!LinkSuperClass(klass)) {
1365 return false;
1366 }
1367 if (!LinkMethods(klass)) {
1368 return false;
1369 }
1370 if (!LinkInstanceFields(klass)) {
1371 return false;
1372 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001373 if (!LinkStaticFields(klass)) {
1374 return false;
1375 }
1376 CreateReferenceInstanceOffsets(klass);
1377 CreateReferenceStaticOffsets(klass);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001378 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001379 klass->status_ = Class::kStatusResolved;
1380 return true;
1381}
1382
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001383bool ClassLinker::LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file) {
1384 CHECK_EQ(Class::kStatusIdx, klass->status_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001385 if (klass->super_class_type_idx_ != DexFile::kDexNoIndex) {
1386 Class* super_class = ResolveType(dex_file, klass->super_class_type_idx_, klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001387 if (super_class == NULL) {
1388 LG << "Failed to resolve superclass";
1389 return false;
1390 }
1391 klass->super_class_ = super_class; // TODO: write barrier
1392 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001393 if (klass->NumInterfaces() > 0) {
1394 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001395 uint32_t type_idx = klass->interfaces_type_idx_->Get(i);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001396 klass->SetInterface(i, ResolveType(dex_file, type_idx, klass));
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001397 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001398 LG << "Failed to resolve interface";
1399 return false;
1400 }
1401 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001402 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001403 LG << "Inaccessible interface";
1404 return false;
1405 }
1406 }
1407 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001408 // Mark the class as loaded.
1409 klass->status_ = Class::kStatusLoaded;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001410 return true;
1411}
1412
1413bool ClassLinker::LinkSuperClass(Class* klass) {
1414 CHECK(!klass->IsPrimitive());
1415 const Class* super = klass->GetSuperClass();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001416 if (klass->GetDescriptor()->Equals("Ljava/lang/Object;")) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001417 if (super != NULL) {
1418 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1419 return false;
1420 }
1421 // TODO: clear finalize attribute
1422 return true;
1423 }
1424 if (super == NULL) {
1425 LG << "No superclass defined"; // TODO: LinkageError
1426 return false;
1427 }
1428 // Verify
1429 if (super->IsFinal()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001430 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is declared final"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001431 return false;
1432 }
1433 if (super->IsInterface()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001434 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is an interface"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001435 return false;
1436 }
1437 if (!klass->CanAccess(super)) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001438 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is inaccessible"; // TODO: IllegalAccessError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001439 return false;
1440 }
1441 return true;
1442}
1443
1444// Populate the class vtable and itable.
1445bool ClassLinker::LinkMethods(Class* klass) {
1446 if (klass->IsInterface()) {
1447 // No vtable.
1448 size_t count = klass->NumVirtualMethods();
1449 if (!IsUint(16, count)) {
1450 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1451 return false;
1452 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001453 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001454 klass->GetVirtualMethod(i)->method_index_ = i;
1455 }
1456 } else {
1457 // Link virtual method tables
1458 LinkVirtualMethods(klass);
1459
1460 // Link interface method tables
1461 LinkInterfaceMethods(klass);
1462
1463 // Insert stubs.
1464 LinkAbstractMethods(klass);
1465 }
1466 return true;
1467}
1468
1469bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001470 if (klass->HasSuperClass()) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001471 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001472 size_t actual_count = klass->GetSuperClass()->vtable_->GetLength();
1473 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001474 // TODO: do not assign to the vtable field until it is fully constructed.
1475 klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001476 // See if any of our virtual methods override the superclass.
1477 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1478 Method* local_method = klass->GetVirtualMethod(i);
1479 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001480 for (; j < actual_count; ++j) {
1481 Method* super_method = klass->vtable_->Get(j);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001482 if (local_method->HasSameNameAndDescriptor(super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001483 // Verify
1484 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001485 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001486 return false;
1487 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001488 klass->vtable_->Set(j, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001489 local_method->method_index_ = j;
1490 break;
1491 }
1492 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001493 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001494 // Not overriding, append.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001495 klass->vtable_->Set(actual_count, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001496 local_method->method_index_ = actual_count;
1497 actual_count += 1;
1498 }
1499 }
1500 if (!IsUint(16, actual_count)) {
1501 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1502 return false;
1503 }
1504 CHECK_LE(actual_count, max_count);
1505 if (actual_count < max_count) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001506 // TODO: do not assign to the vtable field until it is fully constructed.
1507 klass->vtable_ = klass->vtable_->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001508 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001509 } else {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001510 CHECK(klass->GetDescriptor()->Equals("Ljava/lang/Object;"));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001511 uint32_t num_virtual_methods = klass->NumVirtualMethods();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001512 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001513 LG << "Too many methods"; // TODO: VirtualMachineError
1514 return false;
1515 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001516 // TODO: do not assign to the vtable field until it is fully constructed.
1517 klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods);
1518 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001519 klass->vtable_->Set(i, klass->GetVirtualMethod(i));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001520 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1521 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001522 }
1523 return true;
1524}
1525
1526bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1527 int pool_offset = 0;
1528 int pool_size = 0;
1529 int miranda_count = 0;
1530 int miranda_alloc = 0;
1531 size_t super_ifcount;
1532 if (klass->HasSuperClass()) {
1533 super_ifcount = klass->GetSuperClass()->iftable_count_;
1534 } else {
1535 super_ifcount = 0;
1536 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001537 size_t ifcount = super_ifcount;
1538 ifcount += klass->NumInterfaces();
1539 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1540 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001541 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001542 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001543 DCHECK(klass->iftable_count_ == 0);
1544 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001545 return true;
1546 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001547 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1548 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001549 if (super_ifcount != 0) {
1550 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1551 sizeof(InterfaceEntry) * super_ifcount);
1552 }
1553 // Flatten the interface inheritance hierarchy.
1554 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001555 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1556 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001557 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001558 if (!interf->IsInterface()) {
1559 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1560 return false;
1561 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001562 klass->iftable_[idx++].SetInterface(interf);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001563 for (size_t j = 0; j < interf->iftable_count_; j++) {
Brian Carlstrom30b94452011-08-25 21:35:26 -07001564 klass->iftable_[idx++].SetInterface(interf->iftable_[j].GetInterface());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001565 }
1566 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001567 CHECK_EQ(idx, ifcount);
1568 klass->iftable_count_ = ifcount;
1569 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001570 return true;
1571 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001572 for (size_t i = super_ifcount; i < ifcount; i++) {
Brian Carlstrom30b94452011-08-25 21:35:26 -07001573 pool_size += klass->iftable_[i].GetInterface()->NumVirtualMethods();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001574 }
1575 if (pool_size == 0) {
1576 return true;
1577 }
1578 klass->ifvi_pool_count_ = pool_size;
1579 klass->ifvi_pool_ = new uint32_t[pool_size];
1580 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001581 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001582 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
Brian Carlstrom30b94452011-08-25 21:35:26 -07001583 Class* interface = klass->iftable_[i].GetInterface();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001584 pool_offset += interface->NumVirtualMethods(); // end here
1585 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1586 Method* interface_method = interface->GetVirtualMethod(j);
1587 int k; // must be signed
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001588 for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001589 Method* vtable_method = klass->vtable_->Get(k);
1590 if (interface_method->HasSameNameAndDescriptor(vtable_method)) {
1591 if (!vtable_method->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001592 LG << "Implementation not public";
1593 return false;
1594 }
1595 klass->iftable_[i].method_index_array_[j] = k;
1596 break;
1597 }
1598 }
1599 if (k < 0) {
1600 if (miranda_count == miranda_alloc) {
1601 miranda_alloc += 8;
1602 if (miranda_list.empty()) {
1603 miranda_list.resize(miranda_alloc);
1604 } else {
1605 miranda_list.resize(miranda_alloc);
1606 }
1607 }
1608 int mir;
1609 for (mir = 0; mir < miranda_count; mir++) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001610 Method* miranda_method = miranda_list[mir];
1611 if (miranda_method->HasSameNameAndDescriptor(interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001612 break;
1613 }
1614 }
1615 // point the interface table at a phantom slot index
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001616 klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001617 if (mir == miranda_count) {
1618 miranda_list[miranda_count++] = interface_method;
1619 }
1620 }
1621 }
1622 }
1623 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001624 int old_method_count = klass->NumVirtualMethods();
1625 int new_method_count = old_method_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001626 klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001627
1628 CHECK(klass->vtable_ != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001629 int old_vtable_count = klass->vtable_->GetLength();
1630 int new_vtable_count = old_vtable_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001631 // TODO: do not assign to the vtable field until it is fully constructed.
1632 klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001633
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001634 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001635 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001636 memcpy(meth, miranda_list[i], sizeof(Method));
1637 meth->klass_ = klass;
1638 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001639 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1640 klass->SetVirtualMethod(old_method_count + i, meth);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001641 klass->vtable_->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001642 }
1643 }
1644 return true;
1645}
1646
1647void ClassLinker::LinkAbstractMethods(Class* klass) {
1648 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1649 Method* method = klass->GetVirtualMethod(i);
1650 if (method->IsAbstract()) {
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001651 LG << "AbstractMethodError";
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001652 // TODO: throw AbstractMethodError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001653 }
1654 }
1655}
1656
1657bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001658 CHECK(klass != NULL);
1659 size_t field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001660 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001661 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001662 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001663 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001664 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001665 return LinkFields(field_offset,
1666 klass->num_reference_instance_fields_,
1667 klass->NumInstanceFields(),
1668 klass->ifields_,
1669 klass->object_size_);
1670}
1671
1672bool ClassLinker::LinkStaticFields(Class* klass) {
1673 CHECK(klass != NULL);
1674 size_t allocated_class_size = klass->class_size_;
1675 size_t field_offset = OFFSETOF_MEMBER(Class, fields_);
1676 bool success = LinkFields(field_offset,
1677 klass->num_reference_static_fields_,
1678 klass->NumStaticFields(),
1679 klass->sfields_,
1680 klass->class_size_);
1681 CHECK_EQ(allocated_class_size, klass->class_size_);
1682 return success;
1683}
1684
1685bool ClassLinker::LinkFields(size_t field_offset,
1686 size_t& num_reference_fields,
1687 size_t num_fields,
1688 ObjectArray<Field>* fields,
1689 size_t& size) {
1690 CHECK((num_fields == 0) == (fields == NULL));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001691 // Move references to the front.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001692 num_reference_fields = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001693 size_t i = 0;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001694 for ( ; i < num_fields; i++) {
1695 Field* pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001696 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001697 if (c != '[' && c != 'L') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001698 for (size_t j = num_fields - 1; j > i; j--) {
1699 Field* refField = fields->Get(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001700 char rc = refField->GetType();
1701 if (rc == '[' || rc == 'L') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001702 fields->Set(i, refField);
1703 fields->Set(j, pField);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001704 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001705 c = rc;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001706 num_reference_fields++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001707 break;
1708 }
1709 }
1710 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001711 num_reference_fields++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001712 }
1713 if (c != '[' && c != 'L') {
1714 break;
1715 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001716 pField->SetOffset(field_offset);
1717 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001718 }
1719
1720 // Now we want to pack all of the double-wide fields together. If
1721 // we're not aligned, though, we want to shuffle one 32-bit field
1722 // into place. If we can't find one, we'll have to pad it.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001723 if (i != num_fields && (field_offset & 0x04) != 0) {
1724 Field* pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001725 char c = pField->GetType();
1726
1727 if (c != 'J' && c != 'D') {
1728 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001729 DCHECK(c != '[');
1730 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001731 pField->SetOffset(field_offset);
1732 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001733 i++;
1734 } else {
1735 // Next field is 64-bit, so search for a 32-bit field we can
1736 // swap into it.
1737 bool found = false;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001738 for (size_t j = num_fields - 1; j > i; j--) {
1739 Field* singleField = fields->Get(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001740 char rc = singleField->GetType();
1741 if (rc != 'J' && rc != 'D') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001742 fields->Set(i, singleField);
1743 fields->Set(j, pField);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001744 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001745 pField->SetOffset(field_offset);
1746 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001747 found = true;
1748 i++;
1749 break;
1750 }
1751 }
1752 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001753 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001754 }
1755 }
1756 }
1757
1758 // Alignment is good, shuffle any double-wide fields forward, and
1759 // finish assigning field offsets to all fields.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001760 DCHECK(i == num_fields || (field_offset & 0x04) == 0);
1761 for ( ; i < num_fields; i++) {
1762 Field* pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001763 char c = pField->GetType();
1764 if (c != 'D' && c != 'J') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001765 for (size_t j = num_fields - 1; j > i; j--) {
1766 Field* doubleField = fields->Get(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001767 char rc = doubleField->GetType();
1768 if (rc == 'D' || rc == 'J') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001769 fields->Set(i, doubleField);
1770 fields->Set(j, pField);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001771 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001772 c = rc;
1773 break;
1774 }
1775 }
1776 } else {
1777 // This is a double-wide field, leave it be.
1778 }
1779
Brian Carlstroma0808032011-07-18 00:39:23 -07001780 pField->SetOffset(field_offset);
1781 field_offset += sizeof(uint32_t);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001782 if (c == 'J' || c == 'D') {
Brian Carlstroma0808032011-07-18 00:39:23 -07001783 field_offset += sizeof(uint32_t);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001784 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001785 }
1786
1787#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001788 // Make sure that all reference fields appear before
1789 // non-reference fields, and all double-wide fields are aligned.
1790 bool seen_non_ref = false;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001791 for (i = 0; i < num_fields; i++) {
1792 Field *pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001793 char c = pField->GetType();
1794
1795 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001796 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001797 }
1798
1799 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001800 if (!seen_non_ref) {
1801 seen_non_ref = true;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001802 DCHECK_EQ(num_reference_fields, i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001803 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001804 } else {
1805 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001806 }
1807 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001808 if (!seen_non_ref) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001809 DCHECK_EQ(num_fields, num_reference_fields);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001810 }
1811#endif
Brian Carlstrom4873d462011-08-21 15:23:39 -07001812 size = field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001813 return true;
1814}
1815
1816// Set the bitmap of reference offsets, refOffsets, from the ifields
1817// list.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001818void ClassLinker::CreateReferenceInstanceOffsets(Class* klass) {
1819 klass->reference_instance_offsets_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001820 if (klass->HasSuperClass()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001821 klass->reference_instance_offsets_ = klass->GetSuperClass()->GetReferenceInstanceOffsets();
1822 // If our superclass overflowed, we don't stand a chance.
1823 if (klass->reference_instance_offsets_ == CLASS_WALK_SUPER) {
1824 return;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001825 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001826 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001827 CreateReferenceOffsets(klass->reference_instance_offsets_,
1828 klass->NumReferenceInstanceFields(),
1829 klass->ifields_);
1830}
1831
1832void ClassLinker::CreateReferenceStaticOffsets(Class* klass) {
1833 klass->reference_static_offsets_ = 0;
1834 CreateReferenceOffsets(klass->reference_static_offsets_,
1835 klass->NumReferenceStaticFields(),
1836 klass->sfields_);
1837}
1838
1839void ClassLinker::CreateReferenceOffsets(uint32_t& reference_offsets,
1840 size_t num_reference_fields,
1841 const ObjectArray<Field>* fields) {
1842 // All of the fields that contain object references are guaranteed
1843 // to be at the beginning of the fields list.
1844 for (size_t i = 0; i < num_reference_fields; ++i) {
1845 // Note that byte_offset is the offset from the beginning of
1846 // object, not the offset into instance data
1847 const Field* field = fields->Get(i);
1848 size_t byte_offset = field->GetOffset();
1849 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
1850 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
1851 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1852 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
1853 CHECK_NE(new_bit, 0U);
1854 reference_offsets |= new_bit;
1855 } else {
1856 reference_offsets = CLASS_WALK_SUPER;
1857 break;
1858 }
1859 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001860}
1861
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001862String* ClassLinker::ResolveString(const DexFile& dex_file,
1863 uint32_t string_idx,
1864 DexCache* dex_cache) {
1865 String* resolved = dex_cache->GetResolvedString(string_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001866 if (resolved != NULL) {
1867 return resolved;
1868 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001869 const DexFile::StringId& string_id = dex_file.GetStringId(string_idx);
1870 int32_t utf16_length = dex_file.GetStringLength(string_id);
1871 const char* utf8_data = dex_file.GetStringData(string_id);
1872 String* string = intern_table_.Intern(utf16_length, utf8_data);
1873 dex_cache->SetResolvedString(string_idx, string);
1874 return string;
1875}
1876
1877Class* ClassLinker::ResolveType(const DexFile& dex_file,
1878 uint32_t type_idx,
1879 DexCache* dex_cache,
1880 const ClassLoader* class_loader) {
1881 Class* resolved = dex_cache->GetResolvedType(type_idx);
1882 if (resolved != NULL) {
1883 return resolved;
1884 }
1885 const char* descriptor = dex_file.dexStringByTypeIdx(type_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001886 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001887 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001888 } else {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001889 resolved = FindClass(descriptor, class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001890 }
1891 if (resolved != NULL) {
Brian Carlstromb63ec392011-08-27 17:38:27 -07001892 Class* check = resolved->IsArrayClass() ? resolved->component_type_ : resolved;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001893 if (dex_cache != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001894 if (check->GetClassLoader() != NULL) {
1895 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1896 return NULL;
1897 }
1898 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001899 dex_cache->SetResolvedType(type_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001900 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001901 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001902 }
1903 return resolved;
1904}
1905
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001906Method* ClassLinker::ResolveMethod(const DexFile& dex_file,
1907 uint32_t method_idx,
1908 DexCache* dex_cache,
1909 const ClassLoader* class_loader,
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07001910 bool is_direct) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001911 Method* resolved = dex_cache->GetResolvedMethod(method_idx);
1912 if (resolved != NULL) {
1913 return resolved;
1914 }
1915 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
1916 Class* klass = ResolveType(dex_file, method_id.class_idx_, dex_cache, class_loader);
1917 if (klass == NULL) {
1918 return NULL;
1919 }
1920
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07001921 const char* name = dex_file.dexStringById(method_id.name_idx_);
Elliott Hughes0c424cb2011-08-26 10:16:25 -07001922 std::string signature(dex_file.CreateMethodDescriptor(method_id.proto_idx_, NULL));
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07001923 if (is_direct) {
1924 resolved = klass->FindDirectMethod(name, signature);
1925 } else {
1926 resolved = klass->FindVirtualMethod(name, signature);
1927 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001928 if (resolved != NULL) {
1929 dex_cache->SetResolvedMethod(method_idx, resolved);
1930 } else {
1931 // DCHECK(Thread::Current()->IsExceptionPending());
1932 }
1933 return resolved;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001934}
1935
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001936Field* ClassLinker::ResolveField(const DexFile& dex_file,
1937 uint32_t field_idx,
1938 DexCache* dex_cache,
1939 const ClassLoader* class_loader,
1940 bool is_static) {
1941 Field* resolved = dex_cache->GetResolvedField(field_idx);
1942 if (resolved != NULL) {
1943 return resolved;
1944 }
1945 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx);
1946 Class* klass = ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader);
1947 if (klass == NULL) {
1948 return NULL;
1949 }
1950
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07001951 const char* name = dex_file.dexStringById(field_id.name_idx_);
1952 const char* type = dex_file.dexStringByTypeIdx(field_id.type_idx_);
1953 if (is_static) {
1954 resolved = klass->FindStaticField(name, type);
1955 } else {
1956 resolved = klass->FindInstanceField(name, type);
1957 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001958 if (resolved != NULL) {
1959 dex_cache->SetResolvedfield(field_idx, resolved);
1960 } else {
1961 // DCHECK(Thread::Current()->IsExceptionPending());
1962 }
1963 return resolved;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001964}
1965
Elliott Hughese27955c2011-08-26 15:21:24 -07001966size_t ClassLinker::NumLoadedClasses() const {
1967 MutexLock mu(classes_lock_);
1968 return classes_.size();
1969}
1970
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001971} // namespace art