Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "class_linker.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5 | #include <deque> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6 | #include <string> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7 | #include <utility> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 8 | #include <vector> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 9 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 10 | #include "UniquePtr.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 11 | #include "casts.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 12 | #include "class_loader.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 13 | #include "dex_cache.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 14 | #include "dex_file.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 15 | #include "dex_verifier.h" |
| 16 | #include "heap.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 17 | #include "intern_table.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 18 | #include "logging.h" |
| 19 | #include "monitor.h" |
| 20 | #include "object.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 21 | #include "runtime.h" |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 22 | #include "space.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 23 | #include "thread.h" |
| 24 | #include "utils.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 28 | const char* ClassLinker::class_roots_descriptors_[kClassRootsMax] = { |
| 29 | "Ljava/lang/Class;", |
| 30 | "Ljava/lang/Object;", |
| 31 | "[Ljava/lang/Object;", |
| 32 | "Ljava/lang/String;", |
| 33 | "Ljava/lang/reflect/Field;", |
| 34 | "Ljava/lang/reflect/Method;", |
| 35 | "Ljava/lang/ClassLoader;", |
| 36 | "Ldalvik/system/BaseDexClassLoader;", |
| 37 | "Ldalvik/system/PathClassLoader;", |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 38 | "Ljava/lang/StackTraceElement;", |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 39 | "Z", |
| 40 | "B", |
| 41 | "C", |
| 42 | "D", |
| 43 | "F", |
| 44 | "I", |
| 45 | "J", |
| 46 | "S", |
| 47 | "V", |
| 48 | "[Z", |
| 49 | "[B", |
| 50 | "[C", |
| 51 | "[D", |
| 52 | "[F", |
| 53 | "[I", |
| 54 | "[J", |
| 55 | "[S", |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 56 | "[Ljava/lang/StackTraceElement;", |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 59 | ClassLinker* ClassLinker::Create(const std::vector<const DexFile*>& boot_class_path, |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 60 | const std::vector<const DexFile*>& class_path, |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 61 | InternTable* intern_table, bool image) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 62 | CHECK_NE(boot_class_path.size(), 0U); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 63 | UniquePtr<ClassLinker> class_linker(new ClassLinker(intern_table)); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 64 | if (image) { |
| 65 | class_linker->InitFromImage(boot_class_path, class_path); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 66 | } else { |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 67 | class_linker->Init(boot_class_path, class_path); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 68 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 69 | // TODO: check for failure during initialization |
| 70 | return class_linker.release(); |
| 71 | } |
| 72 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 73 | ClassLinker::ClassLinker(InternTable* intern_table) |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 74 | : classes_lock_("ClassLinker lock"), |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 75 | class_roots_(NULL), |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 76 | array_interfaces_(NULL), |
| 77 | array_iftable_(NULL), |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 78 | init_done_(false), |
| 79 | intern_table_(intern_table) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 80 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 81 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 82 | void ClassLinker::Init(const std::vector<const DexFile*>& boot_class_path, |
| 83 | const std::vector<const DexFile*>& class_path) { |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 84 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 85 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 86 | // java_lang_Class comes first, its needed for AllocClass |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 87 | Class* java_lang_Class = down_cast<Class*>( |
| 88 | Heap::AllocObject(NULL, sizeof(ClassClass))); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 89 | CHECK(java_lang_Class != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 90 | java_lang_Class->SetClass(java_lang_Class); |
| 91 | java_lang_Class->SetClassSize(sizeof(ClassClass)); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 92 | // AllocClass(Class*) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 93 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 94 | // java_lang_Object comes next so that object_array_class can be created |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 95 | Class* java_lang_Object = AllocClass(java_lang_Class, sizeof(Class)); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 96 | CHECK(java_lang_Object != NULL); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 97 | // backfill Object as the super class of Class |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 98 | java_lang_Class->SetSuperClass(java_lang_Object); |
| 99 | java_lang_Object->SetStatus(Class::kStatusLoaded); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 100 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 101 | // Object[] next to hold class roots |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 102 | Class* object_array_class = AllocClass(java_lang_Class, sizeof(Class)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 103 | object_array_class->SetArrayRank(1); |
| 104 | object_array_class->SetComponentType(java_lang_Object); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 105 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 106 | // Setup the char[] class to be used for String |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 107 | Class* char_array_class = AllocClass(java_lang_Class, sizeof(Class)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 108 | char_array_class->SetArrayRank(1); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 109 | CharArray::SetArrayClass(char_array_class); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 110 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 111 | // Setup String |
| 112 | Class* java_lang_String = AllocClass(java_lang_Class, sizeof(StringClass)); |
| 113 | String::SetClass(java_lang_String); |
| 114 | java_lang_String->SetObjectSize(sizeof(String)); |
| 115 | java_lang_String->SetStatus(Class::kStatusResolved); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 116 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 117 | // Backfill Class descriptors missing until this point |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 118 | java_lang_Class->SetDescriptor(intern_table_->InternStrong("Ljava/lang/Class;")); |
| 119 | java_lang_Object->SetDescriptor(intern_table_->InternStrong("Ljava/lang/Object;")); |
| 120 | object_array_class->SetDescriptor(intern_table_->InternStrong("[Ljava/lang/Object;")); |
| 121 | java_lang_String->SetDescriptor(intern_table_->InternStrong("Ljava/lang/String;")); |
| 122 | char_array_class->SetDescriptor(intern_table_->InternStrong("[C")); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 123 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 124 | // Create storage for root classes, save away our work so far (requires |
| 125 | // descriptors) |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 126 | class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 127 | SetClassRoot(kJavaLangClass, java_lang_Class); |
| 128 | SetClassRoot(kJavaLangObject, java_lang_Object); |
| 129 | SetClassRoot(kObjectArrayClass, object_array_class); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 130 | SetClassRoot(kCharArrayClass, char_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 131 | SetClassRoot(kJavaLangString, java_lang_String); |
| 132 | |
| 133 | // Setup the primitive type classes. |
| 134 | SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass("Z", Class::kPrimBoolean)); |
| 135 | SetClassRoot(kPrimitiveByte, CreatePrimitiveClass("B", Class::kPrimByte)); |
| 136 | SetClassRoot(kPrimitiveChar, CreatePrimitiveClass("C", Class::kPrimChar)); |
| 137 | SetClassRoot(kPrimitiveShort, CreatePrimitiveClass("S", Class::kPrimShort)); |
| 138 | SetClassRoot(kPrimitiveInt, CreatePrimitiveClass("I", Class::kPrimInt)); |
| 139 | SetClassRoot(kPrimitiveLong, CreatePrimitiveClass("J", Class::kPrimLong)); |
| 140 | SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass("F", Class::kPrimFloat)); |
| 141 | SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass("D", Class::kPrimDouble)); |
| 142 | SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass("V", Class::kPrimVoid)); |
| 143 | |
| 144 | // Backfill component type of char[] |
| 145 | char_array_class->SetComponentType(GetClassRoot(kPrimitiveChar)); |
| 146 | |
| 147 | // Create array interface entries to populate once we can load system classes |
| 148 | array_interfaces_ = AllocObjectArray<Class>(2); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 149 | array_iftable_ = AllocObjectArray<InterfaceEntry>(2); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 150 | |
| 151 | // Create int array type for AllocDexCache (done in AppendToBootClassPath) |
| 152 | Class* int_array_class = AllocClass(java_lang_Class, sizeof(Class)); |
| 153 | int_array_class->SetArrayRank(1); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 154 | int_array_class->SetDescriptor(intern_table_->InternStrong("[I")); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 155 | int_array_class->SetComponentType(GetClassRoot(kPrimitiveInt)); |
| 156 | IntArray::SetArrayClass(int_array_class); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 157 | SetClassRoot(kIntArrayClass, int_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 158 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 159 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 160 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 161 | // setup boot_class_path_ and register class_path now that we can |
| 162 | // use AllocObjectArray to create DexCache instances |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 163 | for (size_t i = 0; i != boot_class_path.size(); ++i) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 164 | const DexFile* dex_file = boot_class_path[i]; |
| 165 | CHECK(dex_file != NULL); |
| 166 | AppendToBootClassPath(*dex_file); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 167 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 168 | for (size_t i = 0; i != class_path.size(); ++i) { |
| 169 | const DexFile* dex_file = class_path[i]; |
| 170 | CHECK(dex_file != NULL); |
| 171 | RegisterDexFile(*dex_file); |
| 172 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 173 | |
| 174 | // Field and Method are necessary so that FindClass can link members |
| 175 | Class* java_lang_reflect_Field = AllocClass(java_lang_Class, sizeof(FieldClass)); |
| 176 | CHECK(java_lang_reflect_Field != NULL); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 177 | java_lang_reflect_Field->SetDescriptor(intern_table_->InternStrong("Ljava/lang/reflect/Field;")); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 178 | java_lang_reflect_Field->SetObjectSize(sizeof(Field)); |
| 179 | SetClassRoot(kJavaLangReflectField, java_lang_reflect_Field); |
| 180 | java_lang_reflect_Field->SetStatus(Class::kStatusResolved); |
| 181 | Field::SetClass(java_lang_reflect_Field); |
| 182 | |
| 183 | Class* java_lang_reflect_Method = AllocClass(java_lang_Class, sizeof(MethodClass)); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 184 | java_lang_reflect_Method->SetDescriptor( |
| 185 | intern_table_->InternStrong("Ljava/lang/reflect/Method;")); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 186 | CHECK(java_lang_reflect_Method != NULL); |
| 187 | java_lang_reflect_Method->SetObjectSize(sizeof(Method)); |
| 188 | SetClassRoot(kJavaLangReflectMethod, java_lang_reflect_Method); |
| 189 | java_lang_reflect_Method->SetStatus(Class::kStatusResolved); |
| 190 | Method::SetClass(java_lang_reflect_Method); |
| 191 | |
| 192 | // now we can use FindSystemClass |
| 193 | |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 194 | // Object and String need to be rerun through FindSystemClass to finish init |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 195 | java_lang_Object->SetStatus(Class::kStatusNotReady); |
| 196 | Class* Object_class = FindSystemClass("Ljava/lang/Object;"); |
| 197 | CHECK_EQ(java_lang_Object, Object_class); |
| 198 | CHECK_EQ(java_lang_Object->GetObjectSize(), sizeof(Object)); |
| 199 | java_lang_String->SetStatus(Class::kStatusNotReady); |
| 200 | Class* String_class = FindSystemClass("Ljava/lang/String;"); |
| 201 | CHECK_EQ(java_lang_String, String_class); |
| 202 | CHECK_EQ(java_lang_String->GetObjectSize(), sizeof(String)); |
| 203 | |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 204 | // Setup the primitive array type classes - can't be done until Object has a vtable |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 205 | SetClassRoot(kBooleanArrayClass, FindSystemClass("[Z")); |
| 206 | BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
| 207 | |
| 208 | SetClassRoot(kByteArrayClass, FindSystemClass("[B")); |
| 209 | ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
| 210 | |
| 211 | Class* found_char_array_class = FindSystemClass("[C"); |
| 212 | CHECK_EQ(char_array_class, found_char_array_class); |
| 213 | |
| 214 | SetClassRoot(kShortArrayClass, FindSystemClass("[S")); |
| 215 | ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
| 216 | |
| 217 | Class* found_int_array_class = FindSystemClass("[I"); |
| 218 | CHECK_EQ(int_array_class, found_int_array_class); |
| 219 | |
| 220 | SetClassRoot(kLongArrayClass, FindSystemClass("[J")); |
| 221 | LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
| 222 | |
| 223 | SetClassRoot(kFloatArrayClass, FindSystemClass("[F")); |
| 224 | FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
| 225 | |
| 226 | SetClassRoot(kDoubleArrayClass, FindSystemClass("[D")); |
| 227 | DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
| 228 | |
| 229 | Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;"); |
| 230 | CHECK_EQ(object_array_class, found_object_array_class); |
| 231 | |
| 232 | // Setup the single, global copies of "interfaces" and "iftable" |
| 233 | Class* java_lang_Cloneable = FindSystemClass("Ljava/lang/Cloneable;"); |
| 234 | CHECK(java_lang_Cloneable != NULL); |
| 235 | Class* java_io_Serializable = FindSystemClass("Ljava/io/Serializable;"); |
| 236 | CHECK(java_io_Serializable != NULL); |
| 237 | CHECK(array_interfaces_ != NULL); |
| 238 | array_interfaces_->Set(0, java_lang_Cloneable); |
| 239 | array_interfaces_->Set(1, java_io_Serializable); |
| 240 | // We assume that Cloneable/Serializable don't have superinterfaces -- |
| 241 | // normally we'd have to crawl up and explicitly list all of the |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 242 | // supers as well. |
| 243 | array_iftable_->Set(0, AllocInterfaceEntry(array_interfaces_->Get(0))); |
| 244 | array_iftable_->Set(1, AllocInterfaceEntry(array_interfaces_->Get(1))); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 245 | |
| 246 | // Sanity check Object[]'s interfaces |
| 247 | CHECK_EQ(java_lang_Cloneable, object_array_class->GetInterface(0)); |
| 248 | CHECK_EQ(java_io_Serializable, object_array_class->GetInterface(1)); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 249 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 250 | // run Class, Field, and Method through FindSystemClass. |
| 251 | // this initializes their dex_cache_ fields and register them in classes_. |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 252 | Class* Class_class = FindSystemClass("Ljava/lang/Class;"); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 253 | CHECK_EQ(java_lang_Class, Class_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 254 | // No sanity check on size as Class is variably sized |
| 255 | |
| 256 | java_lang_reflect_Field->SetStatus(Class::kStatusNotReady); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 257 | Class* Field_class = FindSystemClass("Ljava/lang/reflect/Field;"); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 258 | CHECK_EQ(java_lang_reflect_Field, Field_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 259 | |
| 260 | java_lang_reflect_Method->SetStatus(Class::kStatusNotReady); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 261 | Class* Method_class = FindSystemClass("Ljava/lang/reflect/Method;"); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 262 | CHECK_EQ(java_lang_reflect_Method, Method_class); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 263 | |
| 264 | // java.lang.ref classes need to be specially flagged, but otherwise are normal classes |
| 265 | Class* java_lang_ref_FinalizerReference = FindSystemClass("Ljava/lang/ref/FinalizerReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 266 | java_lang_ref_FinalizerReference->SetAccessFlags( |
| 267 | java_lang_ref_FinalizerReference->GetAccessFlags() | |
| 268 | kAccClassIsReference | kAccClassIsFinalizerReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 269 | Class* java_lang_ref_PhantomReference = FindSystemClass("Ljava/lang/ref/PhantomReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 270 | java_lang_ref_PhantomReference->SetAccessFlags( |
| 271 | java_lang_ref_PhantomReference->GetAccessFlags() | |
| 272 | kAccClassIsReference | kAccClassIsPhantomReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 273 | Class* java_lang_ref_SoftReference = FindSystemClass("Ljava/lang/ref/SoftReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 274 | java_lang_ref_SoftReference->SetAccessFlags( |
| 275 | java_lang_ref_SoftReference->GetAccessFlags() | kAccClassIsReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 276 | Class* java_lang_ref_WeakReference = FindSystemClass("Ljava/lang/ref/WeakReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 277 | java_lang_ref_WeakReference->SetAccessFlags( |
| 278 | java_lang_ref_WeakReference->GetAccessFlags() | |
| 279 | kAccClassIsReference | kAccClassIsWeakReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 280 | |
| 281 | // Let the heap know some key offsets into java.lang.ref instances |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 282 | // NB we hard code the field indexes here rather than using FindInstanceField |
| 283 | // as the types of the field can't be resolved prior to the runtime being |
| 284 | // fully initialized |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 285 | Class* java_lang_ref_Reference = FindSystemClass("Ljava/lang/ref/Reference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 286 | |
| 287 | Field* pendingNext = java_lang_ref_Reference->GetInstanceField(0); |
| 288 | CHECK(pendingNext->GetName()->Equals("pendingNext")); |
| 289 | CHECK(ResolveType(pendingNext->GetTypeIdx(), pendingNext) == |
| 290 | java_lang_ref_Reference); |
| 291 | |
| 292 | Field* queue = java_lang_ref_Reference->GetInstanceField(1); |
| 293 | CHECK(queue->GetName()->Equals("queue")); |
| 294 | CHECK(ResolveType(queue->GetTypeIdx(), queue) == |
| 295 | FindSystemClass("Ljava/lang/ref/ReferenceQueue;")); |
| 296 | |
| 297 | Field* queueNext = java_lang_ref_Reference->GetInstanceField(2); |
| 298 | CHECK(queueNext->GetName()->Equals("queueNext")); |
| 299 | CHECK(ResolveType(queueNext->GetTypeIdx(), queueNext) == |
| 300 | java_lang_ref_Reference); |
| 301 | |
| 302 | Field* referent = java_lang_ref_Reference->GetInstanceField(3); |
| 303 | CHECK(referent->GetName()->Equals("referent")); |
| 304 | CHECK(ResolveType(referent->GetTypeIdx(), referent) == |
| 305 | java_lang_Object); |
| 306 | |
| 307 | Field* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2); |
| 308 | CHECK(zombie->GetName()->Equals("zombie")); |
| 309 | CHECK(ResolveType(zombie->GetTypeIdx(), zombie) == |
| 310 | java_lang_Object); |
| 311 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 312 | Heap::SetReferenceOffsets(referent->GetOffset(), |
| 313 | queue->GetOffset(), |
| 314 | queueNext->GetOffset(), |
| 315 | pendingNext->GetOffset(), |
| 316 | zombie->GetOffset()); |
| 317 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 318 | // Setup the ClassLoaders, adjusting the object_size_ as necessary |
| 319 | Class* java_lang_ClassLoader = FindSystemClass("Ljava/lang/ClassLoader;"); |
| 320 | CHECK_LT(java_lang_ClassLoader->GetObjectSize(), sizeof(ClassLoader)); |
| 321 | java_lang_ClassLoader->SetObjectSize(sizeof(ClassLoader)); |
| 322 | SetClassRoot(kJavaLangClassLoader, java_lang_ClassLoader); |
| 323 | |
| 324 | Class* dalvik_system_BaseDexClassLoader = FindSystemClass("Ldalvik/system/BaseDexClassLoader;"); |
| 325 | CHECK_EQ(dalvik_system_BaseDexClassLoader->GetObjectSize(), sizeof(BaseDexClassLoader)); |
| 326 | SetClassRoot(kDalvikSystemBaseDexClassLoader, dalvik_system_BaseDexClassLoader); |
| 327 | |
| 328 | Class* dalvik_system_PathClassLoader = FindSystemClass("Ldalvik/system/PathClassLoader;"); |
| 329 | CHECK_EQ(dalvik_system_PathClassLoader->GetObjectSize(), sizeof(PathClassLoader)); |
| 330 | SetClassRoot(kDalvikSystemPathClassLoader, dalvik_system_PathClassLoader); |
| 331 | PathClassLoader::SetClass(dalvik_system_PathClassLoader); |
| 332 | |
| 333 | // Set up java.lang.StackTraceElement as a convenience |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 334 | SetClassRoot(kJavaLangStackTraceElement, FindSystemClass("Ljava/lang/StackTraceElement;")); |
| 335 | SetClassRoot(kJavaLangStackTraceElementArrayClass, FindSystemClass("[Ljava/lang/StackTraceElement;")); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 336 | StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 337 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 338 | FinishInit(); |
| 339 | } |
| 340 | |
| 341 | void ClassLinker::FinishInit() { |
| 342 | // ensure all class_roots_ are initialized |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 343 | for (size_t i = 0; i < kClassRootsMax; i++) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 344 | ClassRoot class_root = static_cast<ClassRoot>(i); |
| 345 | Class* klass = GetClassRoot(class_root); |
| 346 | CHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 347 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != NULL); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 348 | // note SetClassRoot does additional validation. |
| 349 | // if possible add new checks there to catch errors early |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 353 | // that Object, Class, and Object[] are setup |
| 354 | init_done_ = true; |
| 355 | } |
| 356 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 357 | struct ClassLinker::InitFromImageCallbackState { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 358 | ClassLinker* class_linker; |
| 359 | |
| 360 | Class* class_roots[kClassRootsMax]; |
| 361 | |
| 362 | typedef std::tr1::unordered_map<std::string, ClassRoot> Table; |
| 363 | Table descriptor_to_class_root; |
| 364 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 365 | typedef std::tr1::unordered_set<DexCache*, DexCacheHash> Set; |
| 366 | Set dex_caches; |
| 367 | }; |
| 368 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 369 | void ClassLinker::InitFromImage(const std::vector<const DexFile*>& boot_class_path, |
| 370 | const std::vector<const DexFile*>& class_path) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 371 | CHECK(!init_done_); |
| 372 | |
| 373 | HeapBitmap* heap_bitmap = Heap::GetLiveBits(); |
| 374 | DCHECK(heap_bitmap != NULL); |
| 375 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 376 | InitFromImageCallbackState state; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 377 | state.class_linker = this; |
| 378 | for (size_t i = 0; i < kClassRootsMax; i++) { |
| 379 | ClassRoot class_root = static_cast<ClassRoot>(i); |
| 380 | state.descriptor_to_class_root[GetClassRootDescriptor(class_root)] = class_root; |
| 381 | } |
| 382 | |
| 383 | // reinit clases_ table |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 384 | heap_bitmap->Walk(InitFromImageCallback, &state); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 385 | |
| 386 | // reinit class_roots_ |
| 387 | Class* object_array_class = state.class_roots[kObjectArrayClass]; |
| 388 | class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax); |
| 389 | for (size_t i = 0; i < kClassRootsMax; i++) { |
| 390 | ClassRoot class_root = static_cast<ClassRoot>(i); |
| 391 | SetClassRoot(class_root, state.class_roots[class_root]); |
| 392 | } |
| 393 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 394 | // reinit array_interfaces_ from any array class instance, they should all be == |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 395 | array_interfaces_ = GetClassRoot(kObjectArrayClass)->GetInterfaces(); |
| 396 | DCHECK(array_interfaces_ == GetClassRoot(kBooleanArrayClass)->GetInterfaces()); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 397 | |
| 398 | // build a map from location to DexCache to match up with DexFile::GetLocation |
| 399 | std::tr1::unordered_map<std::string, DexCache*> location_to_dex_cache; |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 400 | typedef InitFromImageCallbackState::Set::const_iterator It; // TODO: C++0x auto |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 401 | for (It it = state.dex_caches.begin(), end = state.dex_caches.end(); it != end; ++it) { |
| 402 | DexCache* dex_cache = *it; |
| 403 | std::string location = dex_cache->GetLocation()->ToModifiedUtf8(); |
| 404 | location_to_dex_cache[location] = dex_cache; |
| 405 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 406 | CHECK_EQ(boot_class_path.size() + class_path.size(), |
| 407 | location_to_dex_cache.size()); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 408 | |
| 409 | // reinit boot_class_path with DexFile arguments and found DexCaches |
| 410 | for (size_t i = 0; i != boot_class_path.size(); ++i) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 411 | const DexFile* dex_file = boot_class_path[i]; |
| 412 | CHECK(dex_file != NULL); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 413 | DexCache* dex_cache = location_to_dex_cache[dex_file->GetLocation()]; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 414 | CHECK(dex_cache != NULL) << dex_file->GetLocation(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 415 | AppendToBootClassPath(*dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 416 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 417 | |
| 418 | // register class_path with DexFile arguments and found DexCaches |
| 419 | for (size_t i = 0; i != class_path.size(); ++i) { |
| 420 | const DexFile* dex_file = class_path[i]; |
| 421 | CHECK(dex_file != NULL); |
| 422 | DexCache* dex_cache = location_to_dex_cache[dex_file->GetLocation()]; |
| 423 | CHECK(dex_cache != NULL) << dex_file->GetLocation(); |
| 424 | RegisterDexFile(*dex_file, dex_cache); |
| 425 | } |
| 426 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 427 | String::SetClass(GetClassRoot(kJavaLangString)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 428 | Field::SetClass(GetClassRoot(kJavaLangReflectField)); |
| 429 | Method::SetClass(GetClassRoot(kJavaLangReflectMethod)); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 430 | BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
| 431 | ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
| 432 | CharArray::SetArrayClass(GetClassRoot(kCharArrayClass)); |
| 433 | DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
| 434 | FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
| 435 | IntArray::SetArrayClass(GetClassRoot(kIntArrayClass)); |
| 436 | LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
| 437 | ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 438 | PathClassLoader::SetClass(GetClassRoot(kDalvikSystemPathClassLoader)); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 439 | StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 440 | |
| 441 | FinishInit(); |
| 442 | } |
| 443 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 444 | void ClassLinker::InitFromImageCallback(Object* obj, void *arg) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 445 | DCHECK(obj != NULL); |
| 446 | DCHECK(arg != NULL); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 447 | InitFromImageCallbackState* state = reinterpret_cast<InitFromImageCallbackState*>(arg); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 448 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 449 | if (obj->IsString()) { |
| 450 | state->class_linker->intern_table_->RegisterStrong(obj->AsString()); |
| 451 | return; |
| 452 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 453 | if (!obj->IsClass()) { |
| 454 | return; |
| 455 | } |
| 456 | Class* klass = obj->AsClass(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 457 | // TODO: restore ClassLoader's list of DexFiles after image load |
| 458 | // CHECK(klass->GetClassLoader() == NULL); |
| 459 | const ClassLoader* class_loader = klass->GetClassLoader(); |
| 460 | if (class_loader != NULL) { |
| 461 | // TODO: replace this hack with something based on command line arguments |
| 462 | Thread::Current()->SetClassLoaderOverride(class_loader); |
| 463 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 464 | |
| 465 | std::string descriptor = klass->GetDescriptor()->ToModifiedUtf8(); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 466 | // restore class to ClassLinker::classes_ table |
| 467 | state->class_linker->InsertClass(descriptor, klass); |
| 468 | |
| 469 | // note DexCache to match with DexFile later |
| 470 | DexCache* dex_cache = klass->GetDexCache(); |
| 471 | if (dex_cache != NULL) { |
| 472 | state->dex_caches.insert(dex_cache); |
| 473 | } else { |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 474 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive()); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | // check if this is a root, if so, register it |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 478 | typedef InitFromImageCallbackState::Table::const_iterator It; // TODO: C++0x auto |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 479 | It it = state->descriptor_to_class_root.find(descriptor); |
| 480 | if (it != state->descriptor_to_class_root.end()) { |
| 481 | ClassRoot class_root = it->second; |
| 482 | state->class_roots[class_root] = klass; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 487 | // reinit references to when reinitializing a ClassLinker from a |
| 488 | // mapped image. |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 489 | void ClassLinker::VisitRoots(Heap::RootVisitor* visitor, void* arg) const { |
| 490 | visitor(class_roots_, arg); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 491 | |
| 492 | for (size_t i = 0; i < dex_caches_.size(); i++) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 493 | visitor(dex_caches_[i], arg); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 496 | { |
| 497 | MutexLock mu(classes_lock_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 498 | typedef Table::const_iterator It; // TODO: C++0x auto |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 499 | for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 500 | visitor(it->second, arg); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 501 | } |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 502 | } |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 503 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 504 | visitor(array_interfaces_, arg); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 507 | ClassLinker::~ClassLinker() { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 508 | String::ResetClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 509 | Field::ResetClass(); |
| 510 | Method::ResetClass(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 511 | BooleanArray::ResetArrayClass(); |
| 512 | ByteArray::ResetArrayClass(); |
| 513 | CharArray::ResetArrayClass(); |
| 514 | DoubleArray::ResetArrayClass(); |
| 515 | FloatArray::ResetArrayClass(); |
| 516 | IntArray::ResetArrayClass(); |
| 517 | LongArray::ResetArrayClass(); |
| 518 | ShortArray::ResetArrayClass(); |
| 519 | PathClassLoader::ResetClass(); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 520 | StackTraceElement::ResetClass(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | DexCache* ClassLinker::AllocDexCache(const DexFile& dex_file) { |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 524 | DexCache* dex_cache = down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::LengthAsArray())); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 525 | dex_cache->Init(intern_table_->InternStrong(dex_file.GetLocation().c_str()), |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 526 | AllocObjectArray<String>(dex_file.NumStringIds()), |
| 527 | AllocObjectArray<Class>(dex_file.NumTypeIds()), |
| 528 | AllocObjectArray<Method>(dex_file.NumMethodIds()), |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 529 | AllocObjectArray<Field>(dex_file.NumFieldIds()), |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 530 | AllocCodeAndDirectMethods(dex_file.NumMethodIds()), |
| 531 | AllocObjectArray<StaticStorageBase>(dex_file.NumTypeIds())); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 532 | return dex_cache; |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 535 | CodeAndDirectMethods* ClassLinker::AllocCodeAndDirectMethods(size_t length) { |
| 536 | return down_cast<CodeAndDirectMethods*>(IntArray::Alloc(CodeAndDirectMethods::LengthAsArray(length))); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 539 | InterfaceEntry* ClassLinker::AllocInterfaceEntry(Class* interface) { |
| 540 | DCHECK(interface->IsInterface()); |
| 541 | ObjectArray<Object>* array = AllocObjectArray<Object>(InterfaceEntry::LengthAsArray()); |
| 542 | InterfaceEntry* interface_entry = down_cast<InterfaceEntry*>(array); |
| 543 | interface_entry->SetInterface(interface); |
| 544 | return interface_entry; |
| 545 | } |
| 546 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 547 | Class* ClassLinker::AllocClass(Class* java_lang_Class, size_t class_size) { |
| 548 | DCHECK_GE(class_size, sizeof(Class)); |
| 549 | Class* klass = Heap::AllocObject(java_lang_Class, class_size)->AsClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 550 | klass->SetPrimitiveType(Class::kPrimNot); // default to not being primitive |
| 551 | klass->SetClassSize(class_size); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 552 | return klass; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 555 | Class* ClassLinker::AllocClass(size_t class_size) { |
| 556 | return AllocClass(GetClassRoot(kJavaLangClass), class_size); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 559 | Field* ClassLinker::AllocField() { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 560 | return down_cast<Field*>(GetClassRoot(kJavaLangReflectField)->AllocObject()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | Method* ClassLinker::AllocMethod() { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 564 | return down_cast<Method*>(GetClassRoot(kJavaLangReflectMethod)->AllocObject()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 567 | ObjectArray<StackTraceElement>* ClassLinker::AllocStackTraceElementArray(size_t length) { |
| 568 | return ObjectArray<StackTraceElement>::Alloc( |
| 569 | GetClassRoot(kJavaLangStackTraceElementArrayClass), |
| 570 | length); |
| 571 | } |
| 572 | |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 573 | Class* ClassLinker::FindClass(const StringPiece& descriptor, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 574 | const ClassLoader* class_loader) { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 575 | // TODO: remove this contrived parent class loader check when we have a real ClassLoader. |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 576 | if (class_loader != NULL) { |
| 577 | Class* klass = FindClass(descriptor, NULL); |
| 578 | if (klass != NULL) { |
| 579 | return klass; |
| 580 | } |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 581 | Thread::Current()->ClearException(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 584 | Thread* self = Thread::Current(); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 585 | DCHECK(self != NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 586 | CHECK(!self->IsExceptionPending()); |
| 587 | // Find the class in the loaded classes table. |
| 588 | Class* klass = LookupClass(descriptor, class_loader); |
| 589 | if (klass == NULL) { |
| 590 | // Class is not yet loaded. |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 591 | if (descriptor[0] == '[') { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 592 | return CreateArrayClass(descriptor, class_loader); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 593 | } |
Brian Carlstrom | 8a48741 | 2011-08-29 20:08:52 -0700 | [diff] [blame] | 594 | const DexFile::ClassPath& class_path = ((class_loader != NULL) |
| 595 | ? ClassLoader::GetClassPath(class_loader) |
| 596 | : boot_class_path_); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 597 | DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, class_path); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 598 | if (pair.second == NULL) { |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 599 | std::string name(PrintableString(descriptor)); |
| 600 | self->ThrowNewException("Ljava/lang/NoClassDefFoundError;", |
| 601 | "Class %s not found in class loader %p", name.c_str(), class_loader); |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 602 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 603 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 604 | const DexFile& dex_file = *pair.first; |
| 605 | const DexFile::ClassDef& dex_class_def = *pair.second; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 606 | DexCache* dex_cache = FindDexCache(dex_file); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 607 | // Load the class from the dex file. |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 608 | if (!init_done_) { |
| 609 | // finish up init of hand crafted class_roots_ |
| 610 | if (descriptor == "Ljava/lang/Object;") { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 611 | klass = GetClassRoot(kJavaLangObject); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 612 | } else if (descriptor == "Ljava/lang/Class;") { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 613 | klass = GetClassRoot(kJavaLangClass); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 614 | } else if (descriptor == "Ljava/lang/String;") { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 615 | klass = GetClassRoot(kJavaLangString); |
| 616 | } else if (descriptor == "Ljava/lang/reflect/Field;") { |
| 617 | klass = GetClassRoot(kJavaLangReflectField); |
| 618 | } else if (descriptor == "Ljava/lang/reflect/Method;") { |
| 619 | klass = GetClassRoot(kJavaLangReflectMethod); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 620 | } else { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 621 | klass = AllocClass(SizeOfClass(dex_file, dex_class_def)); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 622 | } |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 623 | } else { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 624 | klass = AllocClass(SizeOfClass(dex_file, dex_class_def)); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 625 | } |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 626 | if (!klass->IsResolved()) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 627 | klass->SetDexCache(dex_cache); |
| 628 | LoadClass(dex_file, dex_class_def, klass, class_loader); |
| 629 | // Check for a pending exception during load |
| 630 | if (self->IsExceptionPending()) { |
| 631 | // TODO: free native allocations in klass |
| 632 | return NULL; |
| 633 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 634 | ObjectLock lock(klass); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 635 | klass->SetClinitThreadId(self->GetTid()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 636 | // Add the newly loaded class to the loaded classes table. |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 637 | bool success = InsertClass(descriptor, klass); // TODO: just return collision |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 638 | if (!success) { |
| 639 | // We may fail to insert if we raced with another thread. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 640 | klass->SetClinitThreadId(0); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 641 | // TODO: free native allocations in klass |
| 642 | klass = LookupClass(descriptor, class_loader); |
| 643 | CHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 644 | return klass; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 645 | } else { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 646 | // Finish loading (if necessary) by finding parents |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 647 | CHECK(!klass->IsLoaded()); |
| 648 | if (!LoadSuperAndInterfaces(klass, dex_file)) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 649 | // Loading failed. |
| 650 | // TODO: CHECK(self->IsExceptionPending()); |
| 651 | lock.NotifyAll(); |
| 652 | return NULL; |
| 653 | } |
| 654 | CHECK(klass->IsLoaded()); |
| 655 | // Link the class (if necessary) |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 656 | CHECK(!klass->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 657 | if (!LinkClass(klass)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 658 | // Linking failed. |
| 659 | // TODO: CHECK(self->IsExceptionPending()); |
| 660 | lock.NotifyAll(); |
| 661 | return NULL; |
| 662 | } |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 663 | CHECK(klass->IsResolved()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | } |
| 667 | // Link the class if it has not already been linked. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 668 | if (!klass->IsResolved() && !klass->IsErroneous()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 669 | ObjectLock lock(klass); |
| 670 | // Check for circular dependencies between classes. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 671 | if (!klass->IsResolved() && klass->GetClinitThreadId() == self->GetTid()) { |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 672 | self->ThrowNewException("Ljava/lang/ClassCircularityError;", NULL); // TODO: detail |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 673 | return NULL; |
| 674 | } |
| 675 | // Wait for the pending initialization to complete. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 676 | while (!klass->IsResolved() && !klass->IsErroneous()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 677 | lock.Wait(); |
| 678 | } |
| 679 | } |
| 680 | if (klass->IsErroneous()) { |
| 681 | LG << "EarlierClassFailure"; // TODO: EarlierClassFailure |
| 682 | return NULL; |
| 683 | } |
| 684 | // Return the loaded class. No exceptions should be pending. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 685 | CHECK(klass->IsResolved()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 686 | CHECK(!self->IsExceptionPending()); |
| 687 | return klass; |
| 688 | } |
| 689 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 690 | // Precomputes size that will be needed for Class, matching LinkStaticFields |
| 691 | size_t ClassLinker::SizeOfClass(const DexFile& dex_file, |
| 692 | const DexFile::ClassDef& dex_class_def) { |
| 693 | const byte* class_data = dex_file.GetClassData(dex_class_def); |
| 694 | DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data); |
| 695 | size_t num_static_fields = header.static_fields_size_; |
| 696 | size_t num_ref = 0; |
| 697 | size_t num_32 = 0; |
| 698 | size_t num_64 = 0; |
| 699 | if (num_static_fields != 0) { |
| 700 | uint32_t last_idx = 0; |
| 701 | for (size_t i = 0; i < num_static_fields; ++i) { |
| 702 | DexFile::Field dex_field; |
| 703 | dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx); |
| 704 | const DexFile::FieldId& field_id = dex_file.GetFieldId(dex_field.field_idx_); |
| 705 | const char* descriptor = dex_file.dexStringByTypeIdx(field_id.type_idx_); |
| 706 | char c = descriptor[0]; |
| 707 | if (c == 'L' || c == '[') { |
| 708 | num_ref++; |
| 709 | } else if (c == 'J' || c == 'D') { |
| 710 | num_64++; |
| 711 | } else { |
| 712 | num_32++; |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | // start with generic class data |
| 718 | size_t size = sizeof(Class); |
| 719 | // follow with reference fields which must be contiguous at start |
| 720 | size += (num_ref * sizeof(uint32_t)); |
| 721 | // if there are 64-bit fields to add, make sure they are aligned |
| 722 | if (num_64 != 0 && size != RoundUp(size, 8)) { // for 64-bit alignment |
| 723 | if (num_32 != 0) { |
| 724 | // use an available 32-bit field for padding |
| 725 | num_32--; |
| 726 | } |
| 727 | size += sizeof(uint32_t); // either way, we are adding a word |
| 728 | DCHECK_EQ(size, RoundUp(size, 8)); |
| 729 | } |
| 730 | // tack on any 64-bit fields now that alignment is assured |
| 731 | size += (num_64 * sizeof(uint64_t)); |
| 732 | // tack on any remaining 32-bit fields |
| 733 | size += (num_32 * sizeof(uint32_t)); |
| 734 | return size; |
| 735 | } |
| 736 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 737 | void ClassLinker::LoadClass(const DexFile& dex_file, |
| 738 | const DexFile::ClassDef& dex_class_def, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 739 | Class* klass, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 740 | const ClassLoader* class_loader) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 741 | CHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 742 | CHECK(klass->GetDexCache() != NULL); |
| 743 | CHECK_EQ(Class::kStatusNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 744 | const byte* class_data = dex_file.GetClassData(dex_class_def); |
| 745 | DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 746 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 747 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 748 | CHECK(descriptor != NULL); |
| 749 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 750 | klass->SetClass(GetClassRoot(kJavaLangClass)); |
| 751 | if (klass->GetDescriptor() != NULL) { |
| 752 | DCHECK(klass->GetDescriptor()->Equals(descriptor)); |
| 753 | } else { |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 754 | klass->SetDescriptor(intern_table_->InternStrong(descriptor)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 755 | } |
| 756 | uint32_t access_flags = dex_class_def.access_flags_; |
| 757 | // Make sure there aren't any "bonus" flags set, since we use them for runtime |
| 758 | // state. |
| 759 | CHECK_EQ(access_flags & ~kAccClassFlagsMask, 0U); |
| 760 | klass->SetAccessFlags(access_flags); |
| 761 | klass->SetClassLoader(class_loader); |
| 762 | DCHECK(klass->GetPrimitiveType() == Class::kPrimNot); |
| 763 | klass->SetStatus(Class::kStatusIdx); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 764 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 765 | klass->SetSuperClassTypeIdx(dex_class_def.superclass_idx_); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 766 | |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 767 | size_t num_static_fields = header.static_fields_size_; |
| 768 | size_t num_instance_fields = header.instance_fields_size_; |
| 769 | size_t num_direct_methods = header.direct_methods_size_; |
| 770 | size_t num_virtual_methods = header.virtual_methods_size_; |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 771 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 772 | klass->SetSourceFile(intern_table_->InternStrong(dex_file.dexGetSourceFile(dex_class_def))); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 773 | |
| 774 | // Load class interfaces. |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 775 | LoadInterfaces(dex_file, dex_class_def, klass); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 776 | |
| 777 | // Load static fields. |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 778 | if (num_static_fields != 0) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 779 | klass->SetSFields(AllocObjectArray<Field>(num_static_fields)); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 780 | uint32_t last_idx = 0; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 781 | for (size_t i = 0; i < num_static_fields; ++i) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 782 | DexFile::Field dex_field; |
| 783 | dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx); |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 784 | Field* sfield = AllocField(); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 785 | klass->SetStaticField(i, sfield); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 786 | LoadField(dex_file, dex_field, klass, sfield); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | |
| 790 | // Load instance fields. |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 791 | if (num_instance_fields != 0) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 792 | klass->SetIFields(AllocObjectArray<Field>(num_instance_fields)); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 793 | uint32_t last_idx = 0; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 794 | for (size_t i = 0; i < num_instance_fields; ++i) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 795 | DexFile::Field dex_field; |
| 796 | dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx); |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 797 | Field* ifield = AllocField(); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 798 | klass->SetInstanceField(i, ifield); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 799 | LoadField(dex_file, dex_field, klass, ifield); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | |
| 803 | // Load direct methods. |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 804 | if (num_direct_methods != 0) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 805 | // TODO: append direct methods to class object |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 806 | klass->SetDirectMethods(AllocObjectArray<Method>(num_direct_methods)); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 807 | uint32_t last_idx = 0; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 808 | for (size_t i = 0; i < num_direct_methods; ++i) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 809 | DexFile::Method dex_method; |
| 810 | dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 811 | Method* meth = AllocMethod(); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 812 | klass->SetDirectMethod(i, meth); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 813 | LoadMethod(dex_file, dex_method, klass, meth); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 814 | // TODO: register maps |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | // Load virtual methods. |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 819 | if (num_virtual_methods != 0) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 820 | // TODO: append virtual methods to class object |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 821 | klass->SetVirtualMethods(AllocObjectArray<Method>(num_virtual_methods)); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 822 | uint32_t last_idx = 0; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 823 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 824 | DexFile::Method dex_method; |
| 825 | dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 826 | Method* meth = AllocMethod(); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 827 | klass->SetVirtualMethod(i, meth); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 828 | LoadMethod(dex_file, dex_method, klass, meth); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 829 | // TODO: register maps |
| 830 | } |
| 831 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 834 | void ClassLinker::LoadInterfaces(const DexFile& dex_file, |
| 835 | const DexFile::ClassDef& dex_class_def, |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 836 | Class* klass) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 837 | const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 838 | if (list != NULL) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 839 | klass->SetInterfaces(AllocObjectArray<Class>(list->Size())); |
| 840 | IntArray* interfaces_idx = IntArray::Alloc(list->Size()); |
| 841 | klass->SetInterfacesTypeIdx(interfaces_idx); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 842 | for (size_t i = 0; i < list->Size(); ++i) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 843 | const DexFile::TypeItem& type_item = list->GetTypeItem(i); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 844 | interfaces_idx->Set(i, type_item.type_idx_); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 845 | } |
| 846 | } |
| 847 | } |
| 848 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 849 | void ClassLinker::LoadField(const DexFile& dex_file, |
| 850 | const DexFile::Field& src, |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 851 | Class* klass, |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 852 | Field* dst) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 853 | const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 854 | dst->SetDeclaringClass(klass); |
| 855 | dst->SetName(ResolveString(dex_file, field_id.name_idx_, klass->GetDexCache())); |
| 856 | dst->SetTypeIdx(field_id.type_idx_); |
| 857 | dst->SetAccessFlags(src.access_flags_); |
| 858 | |
| 859 | // In order to access primitive types using GetTypeDuringLinking we need to |
| 860 | // ensure they are resolved into the dex cache |
| 861 | const char* descriptor = dex_file.dexStringByTypeIdx(field_id.type_idx_); |
| 862 | if (descriptor[1] == '\0') { |
| 863 | // only the descriptors of primitive types should be 1 character long |
| 864 | Class* resolved = ResolveType(dex_file, field_id.type_idx_, klass); |
| 865 | DCHECK(resolved->IsPrimitive()); |
| 866 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 869 | void ClassLinker::LoadMethod(const DexFile& dex_file, |
| 870 | const DexFile::Method& src, |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 871 | Class* klass, |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 872 | Method* dst) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 873 | const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 874 | dst->SetDeclaringClass(klass); |
| 875 | dst->SetName(ResolveString(dex_file, method_id.name_idx_, klass->GetDexCache())); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 876 | { |
| 877 | int32_t utf16_length; |
Elliott Hughes | 0c424cb | 2011-08-26 10:16:25 -0700 | [diff] [blame] | 878 | std::string utf8(dex_file.CreateMethodDescriptor(method_id.proto_idx_, &utf16_length)); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 879 | dst->SetSignature(intern_table_->InternStrong(utf16_length, utf8.c_str())); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 880 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 881 | dst->SetProtoIdx(method_id.proto_idx_); |
| 882 | dst->SetCodeItemOffset(src.code_off_); |
| 883 | const char* shorty = dex_file.GetShorty(method_id.proto_idx_); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 884 | dst->SetShorty(intern_table_->InternStrong(shorty)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 885 | dst->SetAccessFlags(src.access_flags_); |
| 886 | dst->SetReturnTypeIdx(dex_file.GetProtoId(method_id.proto_idx_).return_type_idx_); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 887 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 888 | dst->SetDexCacheStrings(klass->GetDexCache()->GetStrings()); |
| 889 | dst->SetDexCacheResolvedTypes(klass->GetDexCache()->GetResolvedTypes()); |
| 890 | dst->SetDexCacheResolvedMethods(klass->GetDexCache()->GetResolvedMethods()); |
| 891 | dst->SetDexCacheResolvedFields(klass->GetDexCache()->GetResolvedFields()); |
| 892 | dst->SetDexCacheCodeAndDirectMethods(klass->GetDexCache()->GetCodeAndDirectMethods()); |
| 893 | dst->SetDexCacheInitializedStaticStorage(klass->GetDexCache()->GetInitializedStaticStorage()); |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 894 | |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 895 | // TODO: check for finalize method |
| 896 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 897 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 898 | if (code_item != NULL) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 899 | dst->SetNumRegisters(code_item->registers_size_); |
| 900 | dst->SetNumIns(code_item->ins_size_); |
| 901 | dst->SetNumOuts(code_item->outs_size_); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 902 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 903 | uint16_t num_args = Method::NumArgRegisters(shorty); |
| 904 | if ((src.access_flags_ & kAccStatic) != 0) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 905 | ++num_args; |
| 906 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 907 | dst->SetNumRegisters(num_args); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 908 | // TODO: native methods |
| 909 | } |
| 910 | } |
| 911 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 912 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 913 | AppendToBootClassPath(dex_file, AllocDexCache(dex_file)); |
| 914 | } |
| 915 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 916 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 917 | CHECK(dex_cache != NULL) << dex_file.GetLocation(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 918 | boot_class_path_.push_back(&dex_file); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 919 | RegisterDexFile(dex_file, dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 920 | } |
| 921 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 922 | void ClassLinker::RegisterDexFile(const DexFile& dex_file) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 923 | RegisterDexFile(dex_file, AllocDexCache(dex_file)); |
| 924 | } |
| 925 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 926 | void ClassLinker::RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 927 | CHECK(dex_cache != NULL) << dex_file.GetLocation(); |
| 928 | CHECK(dex_cache->GetLocation()->Equals(dex_file.GetLocation())); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 929 | dex_files_.push_back(&dex_file); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 930 | dex_caches_.push_back(dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 931 | } |
| 932 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 933 | const DexFile& ClassLinker::FindDexFile(const DexCache* dex_cache) const { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 934 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
| 935 | if (dex_caches_[i] == dex_cache) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 936 | return *dex_files_[i]; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 937 | } |
| 938 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 939 | CHECK(false) << "Failed to find DexFile for DexCache " << dex_cache->GetLocation()->ToModifiedUtf8(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 940 | return *dex_files_[-1]; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 943 | DexCache* ClassLinker::FindDexCache(const DexFile& dex_file) const { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 944 | for (size_t i = 0; i != dex_files_.size(); ++i) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 945 | if (dex_files_[i] == &dex_file) { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 946 | return dex_caches_[i]; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 947 | } |
| 948 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 949 | CHECK(false) << "Failed to find DexCache for DexFile " << dex_file.GetLocation(); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 950 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 953 | Class* ClassLinker::CreatePrimitiveClass(const char* descriptor, |
| 954 | Class::PrimitiveType type) { |
| 955 | // TODO: deduce one argument from the other |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 956 | Class* klass = AllocClass(sizeof(Class)); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 957 | CHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 958 | klass->SetAccessFlags(kAccPublic | kAccFinal | kAccAbstract); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 959 | klass->SetDescriptor(intern_table_->InternStrong(descriptor)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 960 | klass->SetPrimitiveType(type); |
| 961 | klass->SetStatus(Class::kStatusInitialized); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 962 | bool success = InsertClass(descriptor, klass); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 963 | CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed"; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 964 | return klass; |
| 965 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 966 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 967 | // Create an array class (i.e. the class object for the array, not the |
| 968 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 969 | // "[Ljava/lang/String;". |
| 970 | // |
| 971 | // If "descriptor" refers to an array of primitives, look up the |
| 972 | // primitive type's internally-generated class object. |
| 973 | // |
| 974 | // "loader" is the class loader of the class that's referring to us. It's |
| 975 | // used to ensure that we're looking for the element type in the right |
| 976 | // context. It does NOT become the class loader for the array class; that |
| 977 | // always comes from the base element class. |
| 978 | // |
| 979 | // Returns NULL with an exception raised on failure. |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 980 | Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor, |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 981 | const ClassLoader* class_loader) { |
| 982 | CHECK_EQ('[', descriptor[0]); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 983 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 984 | // Identify the underlying element class and the array dimension depth. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 985 | Class* component_type = NULL; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 986 | int array_rank; |
| 987 | if (descriptor[1] == '[') { |
| 988 | // array of arrays; keep descriptor and grab stuff from parent |
| 989 | Class* outer = FindClass(descriptor.substr(1), class_loader); |
| 990 | if (outer != NULL) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 991 | // want the base class, not "outer", in our component_type |
| 992 | component_type = outer->GetComponentType(); |
| 993 | array_rank = outer->GetArrayRank() + 1; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 994 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 995 | DCHECK(component_type == NULL); // make sure we fail |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 996 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 997 | } else { |
| 998 | array_rank = 1; |
| 999 | if (descriptor[1] == 'L') { |
| 1000 | // array of objects; strip off "[" and look up descriptor. |
| 1001 | const StringPiece subDescriptor = descriptor.substr(1); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1002 | component_type = FindClass(subDescriptor, class_loader); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1003 | } else { |
| 1004 | // array of a primitive type |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1005 | component_type = FindPrimitiveClass(descriptor[1]); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1006 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1007 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1008 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1009 | if (component_type == NULL) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1010 | // failed |
| 1011 | // DCHECK(Thread::Current()->IsExceptionPending()); // TODO |
| 1012 | return NULL; |
| 1013 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1014 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1015 | // See if the component type is already loaded. Array classes are |
| 1016 | // always associated with the class loader of their underlying |
| 1017 | // element type -- an array of Strings goes with the loader for |
| 1018 | // java/lang/String -- so we need to look for it there. (The |
| 1019 | // caller should have checked for the existence of the class |
| 1020 | // before calling here, but they did so with *their* class loader, |
| 1021 | // not the component type's loader.) |
| 1022 | // |
| 1023 | // If we find it, the caller adds "loader" to the class' initiating |
| 1024 | // loader list, which should prevent us from going through this again. |
| 1025 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1026 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1027 | // are the same, because our caller (FindClass) just did the |
| 1028 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 1029 | // because we effectively do this lookup again when we add the new |
| 1030 | // class to the hash table --- necessary because of possible races with |
| 1031 | // other threads.) |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1032 | if (class_loader != component_type->GetClassLoader()) { |
| 1033 | Class* new_class = LookupClass(descriptor, component_type->GetClassLoader()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1034 | if (new_class != NULL) { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1035 | return new_class; |
| 1036 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1037 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1038 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1039 | // Fill out the fields in the Class. |
| 1040 | // |
| 1041 | // It is possible to execute some methods against arrays, because |
| 1042 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 1043 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 1044 | // |
| 1045 | // Array classes are simple enough that we don't need to do a full |
| 1046 | // link step. |
| 1047 | |
| 1048 | Class* new_class = NULL; |
| 1049 | if (!init_done_) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1050 | // Classes that were hand created, ie not by FindSystemClass |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1051 | if (descriptor == "[Ljava/lang/Object;") { |
| 1052 | new_class = GetClassRoot(kObjectArrayClass); |
| 1053 | } else if (descriptor == "[C") { |
| 1054 | new_class = GetClassRoot(kCharArrayClass); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 1055 | } else if (descriptor == "[I") { |
| 1056 | new_class = GetClassRoot(kIntArrayClass); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1057 | } |
| 1058 | } |
| 1059 | if (new_class == NULL) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1060 | new_class = AllocClass(sizeof(Class)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1061 | if (new_class == NULL) { |
| 1062 | return NULL; |
| 1063 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1064 | new_class->SetArrayRank(array_rank); |
| 1065 | new_class->SetComponentType(component_type); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1066 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1067 | DCHECK_LE(1, new_class->GetArrayRank()); |
| 1068 | DCHECK(new_class->GetComponentType() != NULL); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 1069 | if (new_class->GetDescriptor() != NULL) { |
| 1070 | DCHECK(new_class->GetDescriptor()->Equals(descriptor)); |
| 1071 | } else { |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame^] | 1072 | new_class->SetDescriptor(intern_table_->InternStrong(descriptor.ToString().c_str())); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 1073 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1074 | Class* java_lang_Object = GetClassRoot(kJavaLangObject); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1075 | new_class->SetSuperClass(java_lang_Object); |
| 1076 | new_class->SetVTable(java_lang_Object->GetVTable()); |
| 1077 | new_class->SetPrimitiveType(Class::kPrimNot); |
| 1078 | new_class->SetClassLoader(component_type->GetClassLoader()); |
| 1079 | new_class->SetStatus(Class::kStatusInitialized); |
| 1080 | // don't need to set new_class->SetObjectSize(..) |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1081 | // because Object::SizeOf delegates to Array::SizeOf |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1082 | |
| 1083 | |
| 1084 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 1085 | // interfaces. We need to set that up here, so that stuff like |
| 1086 | // "instanceof" works right. |
| 1087 | // |
| 1088 | // Note: The GC could run during the call to FindSystemClass, |
| 1089 | // so we need to make sure the class object is GC-valid while we're in |
| 1090 | // there. Do this by clearing the interface list so the GC will just |
| 1091 | // think that the entries are null. |
| 1092 | |
| 1093 | |
| 1094 | // Use the single, global copies of "interfaces" and "iftable" |
| 1095 | // (remember not to free them for arrays). |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1096 | new_class->SetInterfaces(array_interfaces_); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1097 | new_class->SetIfTable(array_iftable_); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1098 | |
| 1099 | // Inherit access flags from the component type. Arrays can't be |
| 1100 | // used as a superclass or interface, so we want to add "final" |
| 1101 | // and remove "interface". |
| 1102 | // |
| 1103 | // Don't inherit any non-standard flags (e.g., kAccFinal) |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1104 | // from component_type. We assume that the array class does not |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1105 | // override finalize(). |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1106 | new_class->SetAccessFlags(((new_class->GetComponentType()->GetAccessFlags() & |
| 1107 | ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1108 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1109 | if (InsertClass(descriptor, new_class)) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1110 | return new_class; |
| 1111 | } |
| 1112 | // Another thread must have loaded the class after we |
| 1113 | // started but before we finished. Abandon what we've |
| 1114 | // done. |
| 1115 | // |
| 1116 | // (Yes, this happens.) |
| 1117 | |
| 1118 | // Grab the winning class. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1119 | Class* other_class = LookupClass(descriptor, component_type->GetClassLoader()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1120 | DCHECK(other_class != NULL); |
| 1121 | return other_class; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | Class* ClassLinker::FindPrimitiveClass(char type) { |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 1125 | switch (type) { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1126 | case 'B': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1127 | return GetClassRoot(kPrimitiveByte); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1128 | case 'C': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1129 | return GetClassRoot(kPrimitiveChar); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1130 | case 'D': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1131 | return GetClassRoot(kPrimitiveDouble); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1132 | case 'F': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1133 | return GetClassRoot(kPrimitiveFloat); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1134 | case 'I': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1135 | return GetClassRoot(kPrimitiveInt); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1136 | case 'J': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1137 | return GetClassRoot(kPrimitiveLong); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1138 | case 'S': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1139 | return GetClassRoot(kPrimitiveShort); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1140 | case 'Z': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1141 | return GetClassRoot(kPrimitiveBoolean); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1142 | case 'V': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1143 | return GetClassRoot(kPrimitiveVoid); |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 1144 | } |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1145 | std::string printable_type(PrintableChar(type)); |
| 1146 | Thread::Current()->ThrowNewException("Ljava/lang/NoClassDefFoundError;", |
| 1147 | "Not a primitive type: %s", printable_type.c_str()); |
| 1148 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1149 | } |
| 1150 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1151 | bool ClassLinker::InsertClass(const StringPiece& descriptor, Class* klass) { |
| 1152 | size_t hash = StringPieceHash()(descriptor); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 1153 | MutexLock mu(classes_lock_); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1154 | Table::iterator it = classes_.insert(std::make_pair(hash, klass)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1155 | return ((*it).second == klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1158 | Class* ClassLinker::LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader) { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1159 | size_t hash = StringPieceHash()(descriptor); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 1160 | MutexLock mu(classes_lock_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1161 | typedef Table::const_iterator It; // TODO: C++0x auto |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1162 | for (It it = classes_.find(hash), end = classes_.end(); it != end; ++it) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1163 | Class* klass = it->second; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1164 | if (klass->GetDescriptor()->Equals(descriptor) && klass->GetClassLoader() == class_loader) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1165 | return klass; |
| 1166 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1167 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1168 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | bool ClassLinker::InitializeClass(Class* klass) { |
| 1172 | CHECK(klass->GetStatus() == Class::kStatusResolved || |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 1173 | klass->GetStatus() == Class::kStatusInitializing || |
| 1174 | klass->GetStatus() == Class::kStatusError) |
| 1175 | << PrettyDescriptor(klass->GetDescriptor()) << " is " << klass->GetStatus(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1176 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 1177 | Thread* self = Thread::Current(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1178 | |
| 1179 | { |
| 1180 | ObjectLock lock(klass); |
| 1181 | |
| 1182 | if (klass->GetStatus() < Class::kStatusVerified) { |
| 1183 | if (klass->IsErroneous()) { |
| 1184 | LG << "re-initializing failed class"; // TODO: throw |
| 1185 | return false; |
| 1186 | } |
| 1187 | |
| 1188 | CHECK(klass->GetStatus() == Class::kStatusResolved); |
| 1189 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1190 | klass->SetStatus(Class::kStatusVerifying); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1191 | if (!DexVerifier::VerifyClass(klass)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1192 | LG << "Verification failed"; // TODO: ThrowVerifyError |
| 1193 | Object* exception = self->GetException(); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 1194 | klass->SetVerifyErrorClass(exception->GetClass()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1195 | klass->SetStatus(Class::kStatusError); |
| 1196 | return false; |
| 1197 | } |
| 1198 | |
| 1199 | klass->SetStatus(Class::kStatusVerified); |
| 1200 | } |
| 1201 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1202 | if (klass->GetStatus() == Class::kStatusInitialized) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1203 | return true; |
| 1204 | } |
| 1205 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1206 | while (klass->GetStatus() == Class::kStatusInitializing) { |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 1207 | // We caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 1208 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 1209 | // Yes. That's fine. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1210 | return true; |
| 1211 | } |
| 1212 | |
| 1213 | CHECK(!self->IsExceptionPending()); |
| 1214 | |
| 1215 | lock.Wait(); // TODO: check for interruption |
| 1216 | |
| 1217 | // When we wake up, repeat the test for init-in-progress. If |
| 1218 | // there's an exception pending (only possible if |
| 1219 | // "interruptShouldThrow" was set), bail out. |
| 1220 | if (self->IsExceptionPending()) { |
| 1221 | CHECK(false); |
| 1222 | LG << "Exception in initialization."; // TODO: ExceptionInInitializerError |
| 1223 | klass->SetStatus(Class::kStatusError); |
| 1224 | return false; |
| 1225 | } |
| 1226 | if (klass->GetStatus() == Class::kStatusInitializing) { |
| 1227 | continue; |
| 1228 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1229 | DCHECK(klass->GetStatus() == Class::kStatusInitialized || |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1230 | klass->GetStatus() == Class::kStatusError); |
| 1231 | if (klass->IsErroneous()) { |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1232 | // The caller wants an exception, but it was thrown in a |
| 1233 | // different thread. Synthesize one here. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1234 | LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError |
| 1235 | return false; |
| 1236 | } |
| 1237 | return true; // otherwise, initialized |
| 1238 | } |
| 1239 | |
| 1240 | // see if we failed previously |
| 1241 | if (klass->IsErroneous()) { |
| 1242 | // might be wise to unlock before throwing; depends on which class |
| 1243 | // it is that we have locked |
| 1244 | |
| 1245 | // TODO: throwEarlierClassFailure(klass); |
| 1246 | return false; |
| 1247 | } |
| 1248 | |
| 1249 | if (!ValidateSuperClassDescriptors(klass)) { |
| 1250 | klass->SetStatus(Class::kStatusError); |
| 1251 | return false; |
| 1252 | } |
| 1253 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1254 | DCHECK(klass->GetStatus() < Class::kStatusInitializing); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1255 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 1256 | klass->SetClinitThreadId(self->GetTid()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1257 | klass->SetStatus(Class::kStatusInitializing); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | if (!InitializeSuperClass(klass)) { |
| 1261 | return false; |
| 1262 | } |
| 1263 | |
| 1264 | InitializeStaticFields(klass); |
| 1265 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1266 | Method* clinit = klass->FindDeclaredDirectMethod("<clinit>", "()V"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1267 | if (clinit != NULL) { |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 1268 | clinit->Invoke(self, NULL, NULL, NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | { |
| 1272 | ObjectLock lock(klass); |
| 1273 | |
| 1274 | if (self->IsExceptionPending()) { |
| 1275 | klass->SetStatus(Class::kStatusError); |
| 1276 | } else { |
| 1277 | klass->SetStatus(Class::kStatusInitialized); |
| 1278 | } |
| 1279 | lock.NotifyAll(); |
| 1280 | } |
| 1281 | |
| 1282 | return true; |
| 1283 | } |
| 1284 | |
| 1285 | bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) { |
| 1286 | if (klass->IsInterface()) { |
| 1287 | return true; |
| 1288 | } |
| 1289 | // begin with the methods local to the superclass |
| 1290 | if (klass->HasSuperClass() && |
| 1291 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
| 1292 | const Class* super = klass->GetSuperClass(); |
| 1293 | for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) { |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 1294 | const Method* method = super->GetVirtualMethod(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1295 | if (method != super->GetVirtualMethod(i) && |
| 1296 | !HasSameMethodDescriptorClasses(method, super, klass)) { |
| 1297 | LG << "Classes resolve differently in superclass"; |
| 1298 | return false; |
| 1299 | } |
| 1300 | } |
| 1301 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1302 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 1303 | InterfaceEntry* interface_entry = klass->GetIfTable()->Get(i); |
| 1304 | Class* interface = interface_entry->GetInterface(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1305 | if (klass->GetClassLoader() != interface->GetClassLoader()) { |
| 1306 | for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) { |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1307 | const Method* method = interface_entry->GetMethodArray()->Get(j); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1308 | if (!HasSameMethodDescriptorClasses(method, interface, |
| 1309 | method->GetClass())) { |
| 1310 | LG << "Classes resolve differently in interface"; // TODO: LinkageError |
| 1311 | return false; |
| 1312 | } |
| 1313 | } |
| 1314 | } |
| 1315 | } |
| 1316 | return true; |
| 1317 | } |
| 1318 | |
| 1319 | bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method, |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1320 | const Class* klass1, |
| 1321 | const Class* klass2) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1322 | const DexFile& dex_file = FindDexFile(method->GetClass()->GetDexCache()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1323 | const DexFile::ProtoId& proto_id = dex_file.GetProtoId(method->GetProtoIdx()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1324 | DexFile::ParameterIterator *it; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1325 | for (it = dex_file.GetParameterIterator(proto_id); it->HasNext(); it->Next()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1326 | const char* descriptor = it->GetDescriptor(); |
| 1327 | if (descriptor == NULL) { |
| 1328 | break; |
| 1329 | } |
| 1330 | if (descriptor[0] == 'L' || descriptor[0] == '[') { |
| 1331 | // Found a non-primitive type. |
| 1332 | if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) { |
| 1333 | return false; |
| 1334 | } |
| 1335 | } |
| 1336 | } |
| 1337 | // Check the return type |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1338 | const char* descriptor = dex_file.GetReturnTypeDescriptor(proto_id); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1339 | if (descriptor[0] == 'L' || descriptor[0] == '[') { |
| 1340 | if (HasSameDescriptorClasses(descriptor, klass1, klass2)) { |
| 1341 | return false; |
| 1342 | } |
| 1343 | } |
| 1344 | return true; |
| 1345 | } |
| 1346 | |
| 1347 | // Returns true if classes referenced by the descriptor are the |
| 1348 | // same classes in klass1 as they are in klass2. |
| 1349 | bool ClassLinker::HasSameDescriptorClasses(const char* descriptor, |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1350 | const Class* klass1, |
| 1351 | const Class* klass2) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1352 | CHECK(descriptor != NULL); |
| 1353 | CHECK(klass1 != NULL); |
| 1354 | CHECK(klass2 != NULL); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1355 | Class* found1 = FindClass(descriptor, klass1->GetClassLoader()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1356 | // TODO: found1 == NULL |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1357 | Class* found2 = FindClass(descriptor, klass2->GetClassLoader()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1358 | // TODO: found2 == NULL |
| 1359 | // TODO: lookup found1 in initiating loader list |
| 1360 | if (found1 == NULL || found2 == NULL) { |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 1361 | Thread::Current()->ClearException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1362 | if (found1 == found2) { |
| 1363 | return true; |
| 1364 | } else { |
| 1365 | return false; |
| 1366 | } |
| 1367 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1368 | return true; |
| 1369 | } |
| 1370 | |
| 1371 | bool ClassLinker::InitializeSuperClass(Class* klass) { |
| 1372 | CHECK(klass != NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1373 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
| 1374 | Class* super_class = klass->GetSuperClass(); |
| 1375 | if (super_class->GetStatus() != Class::kStatusInitialized) { |
| 1376 | CHECK(!super_class->IsInterface()); |
| 1377 | klass->MonitorExit(); |
| 1378 | bool super_initialized = InitializeClass(super_class); |
| 1379 | klass->MonitorEnter(); |
| 1380 | // TODO: check for a pending exception |
| 1381 | if (!super_initialized) { |
| 1382 | klass->SetStatus(Class::kStatusError); |
| 1383 | klass->NotifyAll(); |
| 1384 | return false; |
| 1385 | } |
| 1386 | } |
| 1387 | } |
| 1388 | return true; |
| 1389 | } |
| 1390 | |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 1391 | bool ClassLinker::EnsureInitialized(Class* c) { |
| 1392 | CHECK(c != NULL); |
| 1393 | if (c->IsInitialized()) { |
| 1394 | return true; |
| 1395 | } |
| 1396 | |
| 1397 | c->MonitorExit(); |
| 1398 | InitializeClass(c); |
| 1399 | c->MonitorEnter(); |
| 1400 | return !Thread::Current()->IsExceptionPending(); |
| 1401 | } |
| 1402 | |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 1403 | StaticStorageBase* ClassLinker::InitializeStaticStorageFromCode(uint32_t type_idx, |
| 1404 | const Method* referrer) { |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 1405 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1406 | Class* klass = class_linker->ResolveType(type_idx, referrer); |
| 1407 | if (klass == NULL) { |
| 1408 | UNIMPLEMENTED(FATAL) << "throw exception due to unresolved class"; |
| 1409 | } |
Brian Carlstrom | 193a44d | 2011-09-04 12:01:42 -0700 | [diff] [blame] | 1410 | // If we are the <clinit> of this class, just return our storage. |
| 1411 | // |
| 1412 | // Do not set the DexCache InitializedStaticStorage, since that |
| 1413 | // implies <clinit> has finished running. |
| 1414 | if (klass == referrer->GetDeclaringClass() && referrer->GetName()->Equals("<clinit>")) { |
| 1415 | return klass; |
| 1416 | } |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 1417 | if (!class_linker->EnsureInitialized(klass)) { |
| 1418 | CHECK(Thread::Current()->IsExceptionPending()); |
Brian Carlstrom | 193a44d | 2011-09-04 12:01:42 -0700 | [diff] [blame] | 1419 | UNIMPLEMENTED(FATAL) << "throw exception due to class initialization problem"; |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 1420 | } |
Brian Carlstrom | 848a4b3 | 2011-09-04 11:29:27 -0700 | [diff] [blame] | 1421 | referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass); |
Brian Carlstrom | 1caa2c2 | 2011-08-28 13:02:33 -0700 | [diff] [blame] | 1422 | return klass; |
| 1423 | } |
| 1424 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 1425 | void ClassLinker::ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def, |
| 1426 | Class* c, std::map<int, Field*>& field_map) { |
| 1427 | const ClassLoader* cl = c->GetClassLoader(); |
| 1428 | const byte* class_data = dex_file.GetClassData(dex_class_def); |
| 1429 | DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data); |
| 1430 | uint32_t last_idx = 0; |
| 1431 | for (size_t i = 0; i < header.static_fields_size_; ++i) { |
| 1432 | DexFile::Field dex_field; |
| 1433 | dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx); |
| 1434 | field_map[i] = ResolveField(dex_file, dex_field.field_idx_, c->GetDexCache(), cl, true); |
| 1435 | } |
| 1436 | } |
| 1437 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1438 | void ClassLinker::InitializeStaticFields(Class* klass) { |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1439 | size_t num_static_fields = klass->NumStaticFields(); |
| 1440 | if (num_static_fields == 0) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1441 | return; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1442 | } |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1443 | DexCache* dex_cache = klass->GetDexCache(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1444 | // TODO: this seems like the wrong check. do we really want !IsPrimitive && !IsArray? |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1445 | if (dex_cache == NULL) { |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1446 | return; |
| 1447 | } |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1448 | const std::string descriptor(klass->GetDescriptor()->ToModifiedUtf8()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1449 | const DexFile& dex_file = FindDexFile(dex_cache); |
| 1450 | const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1451 | CHECK(dex_class_def != NULL); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 1452 | |
| 1453 | // We reordered the fields, so we need to be able to map the field indexes to the right fields. |
| 1454 | std::map<int, Field*> field_map; |
| 1455 | ConstructFieldMap(dex_file, *dex_class_def, klass, field_map); |
| 1456 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1457 | const byte* addr = dex_file.GetEncodedArray(*dex_class_def); |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 1458 | if (addr == NULL) { |
| 1459 | // All this class' static fields have default values. |
| 1460 | return; |
| 1461 | } |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1462 | size_t array_size = DecodeUnsignedLeb128(&addr); |
| 1463 | for (size_t i = 0; i < array_size; ++i) { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 1464 | Field* field = field_map[i]; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1465 | JValue value; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1466 | DexFile::ValueType type = dex_file.ReadEncodedValue(&addr, &value); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1467 | switch (type) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1468 | case DexFile::kByte: |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1469 | field->SetByte(NULL, value.b); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1470 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1471 | case DexFile::kShort: |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1472 | field->SetShort(NULL, value.s); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1473 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1474 | case DexFile::kChar: |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1475 | field->SetChar(NULL, value.c); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1476 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1477 | case DexFile::kInt: |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1478 | field->SetInt(NULL, value.i); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1479 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1480 | case DexFile::kLong: |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1481 | field->SetLong(NULL, value.j); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1482 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1483 | case DexFile::kFloat: |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1484 | field->SetFloat(NULL, value.f); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1485 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1486 | case DexFile::kDouble: |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1487 | field->SetDouble(NULL, value.d); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1488 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1489 | case DexFile::kString: { |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1490 | uint32_t string_idx = value.i; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 1491 | const String* resolved = ResolveString(dex_file, string_idx, klass->GetDexCache()); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1492 | field->SetObject(NULL, resolved); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1493 | break; |
| 1494 | } |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1495 | case DexFile::kBoolean: |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1496 | field->SetBoolean(NULL, value.z); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1497 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1498 | case DexFile::kNull: |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1499 | field->SetObject(NULL, value.l); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1500 | break; |
| 1501 | default: |
Carl Shapiro | 606258b | 2011-07-09 16:09:09 -0700 | [diff] [blame] | 1502 | LOG(FATAL) << "Unknown type " << static_cast<int>(type); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1503 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1504 | } |
| 1505 | } |
| 1506 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1507 | bool ClassLinker::LinkClass(Class* klass) { |
| 1508 | CHECK_EQ(Class::kStatusLoaded, klass->GetStatus()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1509 | if (!LinkSuperClass(klass)) { |
| 1510 | return false; |
| 1511 | } |
| 1512 | if (!LinkMethods(klass)) { |
| 1513 | return false; |
| 1514 | } |
| 1515 | if (!LinkInstanceFields(klass)) { |
| 1516 | return false; |
| 1517 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1518 | if (!LinkStaticFields(klass)) { |
| 1519 | return false; |
| 1520 | } |
| 1521 | CreateReferenceInstanceOffsets(klass); |
| 1522 | CreateReferenceStaticOffsets(klass); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1523 | CHECK_EQ(Class::kStatusLoaded, klass->GetStatus()); |
| 1524 | klass->SetStatus(Class::kStatusResolved); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1525 | return true; |
| 1526 | } |
| 1527 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1528 | bool ClassLinker::LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1529 | CHECK_EQ(Class::kStatusIdx, klass->GetStatus()); |
| 1530 | if (klass->GetSuperClassTypeIdx() != DexFile::kDexNoIndex) { |
| 1531 | Class* super_class = ResolveType(dex_file, klass->GetSuperClassTypeIdx(), klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1532 | if (super_class == NULL) { |
| 1533 | LG << "Failed to resolve superclass"; |
| 1534 | return false; |
| 1535 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1536 | klass->SetSuperClass(super_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1537 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1538 | for (size_t i = 0; i < klass->NumInterfaces(); ++i) { |
| 1539 | uint32_t idx = klass->GetInterfacesTypeIdx()->Get(i); |
| 1540 | Class *interface = ResolveType(dex_file, idx, klass); |
| 1541 | klass->SetInterface(i, interface); |
| 1542 | if (interface == NULL) { |
| 1543 | LG << "Failed to resolve interface"; |
| 1544 | return false; |
| 1545 | } |
| 1546 | // Verify |
| 1547 | if (!klass->CanAccess(interface)) { |
| 1548 | LG << "Inaccessible interface"; |
| 1549 | return false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1550 | } |
| 1551 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1552 | // Mark the class as loaded. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1553 | klass->SetStatus(Class::kStatusLoaded); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1554 | return true; |
| 1555 | } |
| 1556 | |
| 1557 | bool ClassLinker::LinkSuperClass(Class* klass) { |
| 1558 | CHECK(!klass->IsPrimitive()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1559 | Class* super = klass->GetSuperClass(); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1560 | if (klass->GetDescriptor()->Equals("Ljava/lang/Object;")) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1561 | if (super != NULL) { |
| 1562 | LG << "Superclass must not be defined"; // TODO: ClassFormatError |
| 1563 | return false; |
| 1564 | } |
| 1565 | // TODO: clear finalize attribute |
| 1566 | return true; |
| 1567 | } |
| 1568 | if (super == NULL) { |
| 1569 | LG << "No superclass defined"; // TODO: LinkageError |
| 1570 | return false; |
| 1571 | } |
| 1572 | // Verify |
| 1573 | if (super->IsFinal()) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1574 | LG << "Superclass " << super->GetDescriptor()->ToModifiedUtf8() << " is declared final"; // TODO: IncompatibleClassChangeError |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1575 | return false; |
| 1576 | } |
| 1577 | if (super->IsInterface()) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1578 | LG << "Superclass " << super->GetDescriptor()->ToModifiedUtf8() << " is an interface"; // TODO: IncompatibleClassChangeError |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1579 | return false; |
| 1580 | } |
| 1581 | if (!klass->CanAccess(super)) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 1582 | LG << "Superclass " << super->GetDescriptor()->ToModifiedUtf8() << " is inaccessible"; // TODO: IllegalAccessError |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1583 | return false; |
| 1584 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1585 | #ifndef NDEBUG |
| 1586 | // Ensure super classes are fully resolved prior to resolving fields.. |
| 1587 | while (super != NULL) { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 1588 | CHECK(super->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1589 | super = super->GetSuperClass(); |
| 1590 | } |
| 1591 | #endif |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1592 | return true; |
| 1593 | } |
| 1594 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1595 | // Populate the class vtable and itable. Compute return type indices. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1596 | bool ClassLinker::LinkMethods(Class* klass) { |
| 1597 | if (klass->IsInterface()) { |
| 1598 | // No vtable. |
| 1599 | size_t count = klass->NumVirtualMethods(); |
| 1600 | if (!IsUint(16, count)) { |
| 1601 | LG << "Too many methods on interface"; // TODO: VirtualMachineError |
| 1602 | return false; |
| 1603 | } |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 1604 | for (size_t i = 0; i < count; ++i) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1605 | klass->GetVirtualMethodDuringLinking(i)->SetMethodIndex(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1606 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1607 | // Link interface method tables |
| 1608 | LinkInterfaceMethods(klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1609 | } else { |
| 1610 | // Link virtual method tables |
| 1611 | LinkVirtualMethods(klass); |
| 1612 | |
| 1613 | // Link interface method tables |
| 1614 | LinkInterfaceMethods(klass); |
| 1615 | |
| 1616 | // Insert stubs. |
| 1617 | LinkAbstractMethods(klass); |
| 1618 | } |
| 1619 | return true; |
| 1620 | } |
| 1621 | |
| 1622 | bool ClassLinker::LinkVirtualMethods(Class* klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1623 | if (klass->HasSuperClass()) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1624 | uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->GetVTable()->GetLength(); |
| 1625 | size_t actual_count = klass->GetSuperClass()->GetVTable()->GetLength(); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1626 | CHECK_LE(actual_count, max_count); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1627 | // TODO: do not assign to the vtable field until it is fully constructed. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1628 | ObjectArray<Method>* vtable = klass->GetSuperClass()->GetVTable()->CopyOf(max_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1629 | // See if any of our virtual methods override the superclass. |
| 1630 | for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1631 | Method* local_method = klass->GetVirtualMethodDuringLinking(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1632 | size_t j = 0; |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1633 | for (; j < actual_count; ++j) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1634 | Method* super_method = vtable->Get(j); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1635 | if (local_method->HasSameNameAndDescriptor(super_method)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1636 | // Verify |
| 1637 | if (super_method->IsFinal()) { |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1638 | LG << "Method overrides final method"; // TODO: VirtualMachineError |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1639 | return false; |
| 1640 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1641 | vtable->Set(j, local_method); |
| 1642 | local_method->SetMethodIndex(j); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1643 | break; |
| 1644 | } |
| 1645 | } |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1646 | if (j == actual_count) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1647 | // Not overriding, append. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1648 | vtable->Set(actual_count, local_method); |
| 1649 | local_method->SetMethodIndex(actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1650 | actual_count += 1; |
| 1651 | } |
| 1652 | } |
| 1653 | if (!IsUint(16, actual_count)) { |
| 1654 | LG << "Too many methods defined on class"; // TODO: VirtualMachineError |
| 1655 | return false; |
| 1656 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1657 | // Shrink vtable if possible |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1658 | CHECK_LE(actual_count, max_count); |
| 1659 | if (actual_count < max_count) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1660 | vtable = vtable->CopyOf(actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1661 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1662 | klass->SetVTable(vtable); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1663 | } else { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1664 | CHECK(klass->GetDescriptor()->Equals("Ljava/lang/Object;")); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1665 | uint32_t num_virtual_methods = klass->NumVirtualMethods(); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1666 | if (!IsUint(16, num_virtual_methods)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1667 | LG << "Too many methods"; // TODO: VirtualMachineError |
| 1668 | return false; |
| 1669 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1670 | ObjectArray<Method>* vtable = AllocObjectArray<Method>(num_virtual_methods); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1671 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1672 | Method* virtual_method = klass->GetVirtualMethodDuringLinking(i); |
| 1673 | vtable->Set(i, virtual_method); |
| 1674 | virtual_method->SetMethodIndex(i & 0xFFFF); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1675 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1676 | klass->SetVTable(vtable); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1677 | } |
| 1678 | return true; |
| 1679 | } |
| 1680 | |
| 1681 | bool ClassLinker::LinkInterfaceMethods(Class* klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1682 | int miranda_count = 0; |
| 1683 | int miranda_alloc = 0; |
| 1684 | size_t super_ifcount; |
| 1685 | if (klass->HasSuperClass()) { |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1686 | super_ifcount = klass->GetSuperClass()->GetIfTableCount(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1687 | } else { |
| 1688 | super_ifcount = 0; |
| 1689 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1690 | size_t ifcount = super_ifcount; |
| 1691 | ifcount += klass->NumInterfaces(); |
| 1692 | for (size_t i = 0; i < klass->NumInterfaces(); i++) { |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1693 | ifcount += klass->GetInterface(i)->GetIfTableCount(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1694 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1695 | if (ifcount == 0) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1696 | // TODO: enable these asserts with klass status validation |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1697 | // DCHECK(klass->GetIfTableCount() == 0); |
| 1698 | // DCHECK(klass->GetIfTable() == NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1699 | return true; |
| 1700 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1701 | ObjectArray<InterfaceEntry>* iftable = AllocObjectArray<InterfaceEntry>(ifcount); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1702 | if (super_ifcount != 0) { |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1703 | ObjectArray<InterfaceEntry>* super_iftable = klass->GetSuperClass()->GetIfTable(); |
| 1704 | for (size_t i = 0; i < super_ifcount; i++) { |
| 1705 | iftable->Set(i, AllocInterfaceEntry(super_iftable->Get(i)->GetInterface())); |
| 1706 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1707 | } |
| 1708 | // Flatten the interface inheritance hierarchy. |
| 1709 | size_t idx = super_ifcount; |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1710 | for (size_t i = 0; i < klass->NumInterfaces(); i++) { |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1711 | Class* interface = klass->GetInterface(i); |
| 1712 | DCHECK(interface != NULL); |
| 1713 | if (!interface->IsInterface()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1714 | LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError |
| 1715 | return false; |
| 1716 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1717 | iftable->Set(idx++, AllocInterfaceEntry(interface)); |
| 1718 | for (int32_t j = 0; j < interface->GetIfTableCount(); j++) { |
| 1719 | iftable->Set(idx++, AllocInterfaceEntry(interface->GetIfTable()->Get(j)->GetInterface())); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1720 | } |
| 1721 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1722 | klass->SetIfTable(iftable); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1723 | CHECK_EQ(idx, ifcount); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1724 | if (klass->IsInterface() || super_ifcount == ifcount) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1725 | return true; |
| 1726 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1727 | std::vector<Method*> miranda_list; |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1728 | for (size_t i = 0; i < ifcount; ++i) { |
| 1729 | InterfaceEntry* interface_entry = iftable->Get(i); |
| 1730 | Class* interface = interface_entry->GetInterface(); |
| 1731 | ObjectArray<Method>* method_array = AllocObjectArray<Method>(interface->NumVirtualMethods()); |
| 1732 | interface_entry->SetMethodArray(method_array); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1733 | ObjectArray<Method>* vtable = klass->GetVTableDuringLinking(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1734 | for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) { |
| 1735 | Method* interface_method = interface->GetVirtualMethod(j); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1736 | int32_t k; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1737 | for (k = vtable->GetLength() - 1; k >= 0; --k) { |
| 1738 | Method* vtable_method = vtable->Get(k); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1739 | if (interface_method->HasSameNameAndDescriptor(vtable_method)) { |
| 1740 | if (!vtable_method->IsPublic()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1741 | LG << "Implementation not public"; |
| 1742 | return false; |
| 1743 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1744 | method_array->Set(j, vtable_method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1745 | break; |
| 1746 | } |
| 1747 | } |
| 1748 | if (k < 0) { |
| 1749 | if (miranda_count == miranda_alloc) { |
| 1750 | miranda_alloc += 8; |
| 1751 | if (miranda_list.empty()) { |
| 1752 | miranda_list.resize(miranda_alloc); |
| 1753 | } else { |
| 1754 | miranda_list.resize(miranda_alloc); |
| 1755 | } |
| 1756 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1757 | Method* miranda_method = NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1758 | int mir; |
| 1759 | for (mir = 0; mir < miranda_count; mir++) { |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1760 | miranda_method = miranda_list[mir]; |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1761 | if (miranda_method->HasSameNameAndDescriptor(interface_method)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1762 | break; |
| 1763 | } |
| 1764 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1765 | // point the interface table at a phantom slot |
| 1766 | method_array->Set(j, miranda_method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1767 | if (mir == miranda_count) { |
| 1768 | miranda_list[miranda_count++] = interface_method; |
| 1769 | } |
| 1770 | } |
| 1771 | } |
| 1772 | } |
| 1773 | if (miranda_count != 0) { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1774 | int old_method_count = klass->NumVirtualMethods(); |
| 1775 | int new_method_count = old_method_count + miranda_count; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1776 | klass->SetVirtualMethods( |
| 1777 | klass->GetVirtualMethods()->CopyOf(new_method_count)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1778 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1779 | ObjectArray<Method>* vtable = klass->GetVTableDuringLinking(); |
| 1780 | CHECK(vtable != NULL); |
| 1781 | int old_vtable_count = vtable->GetLength(); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1782 | int new_vtable_count = old_vtable_count + miranda_count; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1783 | vtable = vtable->CopyOf(new_vtable_count); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1784 | for (int i = 0; i < miranda_count; i++) { |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1785 | Method* meth = AllocMethod(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1786 | // TODO: this shouldn't be a memcpy |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1787 | memcpy(meth, miranda_list[i], sizeof(Method)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1788 | meth->SetDeclaringClass(klass); |
| 1789 | meth->SetAccessFlags(meth->GetAccessFlags() | kAccMiranda); |
| 1790 | meth->SetMethodIndex(0xFFFF & (old_vtable_count + i)); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1791 | klass->SetVirtualMethod(old_method_count + i, meth); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1792 | vtable->Set(old_vtable_count + i, meth); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1793 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1794 | // TODO: do not assign to the vtable field until it is fully constructed. |
| 1795 | klass->SetVTable(vtable); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1796 | } |
| 1797 | return true; |
| 1798 | } |
| 1799 | |
| 1800 | void ClassLinker::LinkAbstractMethods(Class* klass) { |
| 1801 | for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1802 | Method* method = klass->GetVirtualMethodDuringLinking(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1803 | if (method->IsAbstract()) { |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 1804 | LG << "AbstractMethodError"; |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 1805 | // TODO: throw AbstractMethodError |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1806 | } |
| 1807 | } |
| 1808 | } |
| 1809 | |
| 1810 | bool ClassLinker::LinkInstanceFields(Class* klass) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1811 | CHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1812 | return LinkFields(klass, true); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | bool ClassLinker::LinkStaticFields(Class* klass) { |
| 1816 | CHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1817 | size_t allocated_class_size = klass->GetClassSize(); |
| 1818 | bool success = LinkFields(klass, false); |
| 1819 | CHECK_EQ(allocated_class_size, klass->GetClassSize()); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1820 | return success; |
| 1821 | } |
| 1822 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 1823 | struct LinkFieldsComparator { |
| 1824 | bool operator()(const Field* field1, const Field* field2){ |
| 1825 | |
| 1826 | // First come reference fields, then 64-bit, and finally 32-bit |
| 1827 | const Class* type1 = field1->GetTypeDuringLinking(); |
| 1828 | const Class* type2 = field2->GetTypeDuringLinking(); |
| 1829 | bool isPrimitive1 = type1 != NULL && type1->IsPrimitive(); |
| 1830 | bool isPrimitive2 = type2 != NULL && type2->IsPrimitive(); |
| 1831 | bool is64bit1 = isPrimitive1 && (type1->IsPrimitiveLong() || type1->IsPrimitiveDouble()); |
| 1832 | bool is64bit2 = isPrimitive2 && (type2->IsPrimitiveLong() || type2->IsPrimitiveDouble()); |
| 1833 | int order1 = (!isPrimitive1 ? 0 : (is64bit1 ? 1 : 2)); |
| 1834 | int order2 = (!isPrimitive2 ? 0 : (is64bit2 ? 1 : 2)); |
| 1835 | if (order1 != order2) { |
| 1836 | return order1 < order2; |
| 1837 | } |
| 1838 | |
| 1839 | // same basic group? then sort by string. |
| 1840 | std::string name1 = field1->GetName()->ToModifiedUtf8(); |
| 1841 | std::string name2 = field2->GetName()->ToModifiedUtf8(); |
| 1842 | return name1 < name2; |
| 1843 | } |
| 1844 | }; |
| 1845 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 1846 | bool ClassLinker::LinkFields(Class* klass, bool instance) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1847 | size_t num_fields = |
| 1848 | instance ? klass->NumInstanceFields() : klass->NumStaticFields(); |
| 1849 | |
| 1850 | ObjectArray<Field>* fields = |
| 1851 | instance ? klass->GetIFields() : klass->GetSFields(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1852 | |
| 1853 | // Initialize size and field_offset |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 1854 | size_t size; |
| 1855 | MemberOffset field_offset(0); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1856 | if (instance) { |
| 1857 | Class* super_class = klass->GetSuperClass(); |
| 1858 | if (super_class != NULL) { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 1859 | CHECK(super_class->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1860 | field_offset = MemberOffset(super_class->GetObjectSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1861 | } |
| 1862 | size = field_offset.Uint32Value(); |
| 1863 | } else { |
| 1864 | size = klass->GetClassSize(); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 1865 | field_offset = Class::FieldsOffset(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1866 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1867 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 1868 | CHECK_EQ(num_fields == 0, fields == NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1869 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 1870 | // we want a relatively stable order so that adding new fields |
| 1871 | // minimizes distruption of C++ version such as Class and Method. |
| 1872 | std::deque<Field*> grouped_and_sorted_fields; |
| 1873 | for (size_t i = 0; i < num_fields; i++) { |
| 1874 | grouped_and_sorted_fields.push_back(fields->Get(i)); |
| 1875 | } |
| 1876 | std::sort(grouped_and_sorted_fields.begin(), |
| 1877 | grouped_and_sorted_fields.end(), |
| 1878 | LinkFieldsComparator()); |
| 1879 | |
| 1880 | // References should be at the front. |
| 1881 | size_t current_field = 0; |
| 1882 | size_t num_reference_fields = 0; |
| 1883 | for (; current_field < num_fields; current_field++) { |
| 1884 | Field* field = grouped_and_sorted_fields.front(); |
| 1885 | const Class* type = field->GetTypeDuringLinking(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1886 | // if a field's type at this point is NULL it isn't primitive |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 1887 | bool isPrimitive = type != NULL && type->IsPrimitive(); |
| 1888 | if (isPrimitive) { |
| 1889 | break; // past last reference, move on to the next phase |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1890 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 1891 | grouped_and_sorted_fields.pop_front(); |
| 1892 | num_reference_fields++; |
| 1893 | fields->Set(current_field, field); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1894 | field->SetOffset(field_offset); |
| 1895 | field_offset = MemberOffset(field_offset.Uint32Value() + sizeof(uint32_t)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1896 | } |
| 1897 | |
| 1898 | // Now we want to pack all of the double-wide fields together. If |
| 1899 | // we're not aligned, though, we want to shuffle one 32-bit field |
| 1900 | // into place. If we can't find one, we'll have to pad it. |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 1901 | if (current_field != num_fields && !IsAligned(field_offset.Uint32Value(), 8)) { |
| 1902 | for (size_t i = 0; i < grouped_and_sorted_fields.size(); i++) { |
| 1903 | Field* field = grouped_and_sorted_fields[i]; |
| 1904 | const Class* type = field->GetTypeDuringLinking(); |
| 1905 | CHECK(type != NULL); // should only be working on primitive types |
| 1906 | DCHECK(type->IsPrimitive()); |
| 1907 | if (type->IsPrimitiveLong() || type->IsPrimitiveDouble()) { |
| 1908 | continue; |
| 1909 | } |
| 1910 | fields->Set(current_field++, field); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1911 | field->SetOffset(field_offset); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 1912 | // drop the consumed field |
| 1913 | grouped_and_sorted_fields.erase(grouped_and_sorted_fields.begin() + i); |
| 1914 | break; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1915 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 1916 | // whether we found a 32-bit field for padding or not, we advance |
| 1917 | field_offset = MemberOffset(field_offset.Uint32Value() + sizeof(uint32_t)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | // Alignment is good, shuffle any double-wide fields forward, and |
| 1921 | // finish assigning field offsets to all fields. |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 1922 | DCHECK(current_field == num_fields || IsAligned(field_offset.Uint32Value(), 8)); |
| 1923 | while (!grouped_and_sorted_fields.empty()) { |
| 1924 | Field* field = grouped_and_sorted_fields.front(); |
| 1925 | grouped_and_sorted_fields.pop_front(); |
| 1926 | const Class* type = field->GetTypeDuringLinking(); |
| 1927 | CHECK(type != NULL); // should only be working on primitive types |
| 1928 | DCHECK(type->IsPrimitive()); |
| 1929 | fields->Set(current_field, field); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1930 | field->SetOffset(field_offset); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 1931 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 1932 | ((type->IsPrimitiveLong() || type->IsPrimitiveDouble()) |
| 1933 | ? sizeof(uint64_t) |
| 1934 | : sizeof(uint32_t))); |
| 1935 | current_field++; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | #ifndef NDEBUG |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1939 | // Make sure that all reference fields appear before |
| 1940 | // non-reference fields, and all double-wide fields are aligned. |
| 1941 | bool seen_non_ref = false; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 1942 | for (size_t i = 0; i < num_fields; i++) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1943 | Field* field = fields->Get(i); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 1944 | if (false) { // enable to debug field layout |
| 1945 | LOG(INFO) << "LinkFields:" |
| 1946 | << " class=" << klass->GetDescriptor()->ToModifiedUtf8() |
| 1947 | << " field=" << field->GetName()->ToModifiedUtf8() |
| 1948 | << " offset=" << field->GetField32(MemberOffset(Field::OffsetOffset()), false); |
| 1949 | } |
| 1950 | const Class* type = field->GetTypeDuringLinking(); |
| 1951 | if (type != NULL && type->IsPrimitive()) { |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1952 | if (!seen_non_ref) { |
| 1953 | seen_non_ref = true; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1954 | DCHECK_EQ(num_reference_fields, i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1955 | } |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1956 | } else { |
| 1957 | DCHECK(!seen_non_ref); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1958 | } |
| 1959 | } |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1960 | if (!seen_non_ref) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1961 | DCHECK_EQ(num_fields, num_reference_fields); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1962 | } |
| 1963 | #endif |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1964 | size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1965 | // Update klass |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 1966 | if (instance) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1967 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 1968 | if (!klass->IsVariableSize()) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1969 | klass->SetObjectSize(size); |
| 1970 | } |
| 1971 | } else { |
| 1972 | klass->SetNumReferenceStaticFields(num_reference_fields); |
| 1973 | klass->SetClassSize(size); |
| 1974 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1975 | return true; |
| 1976 | } |
| 1977 | |
| 1978 | // Set the bitmap of reference offsets, refOffsets, from the ifields |
| 1979 | // list. |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1980 | void ClassLinker::CreateReferenceInstanceOffsets(Class* klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1981 | uint32_t reference_offsets = 0; |
| 1982 | Class* super_class = klass->GetSuperClass(); |
| 1983 | if (super_class != NULL) { |
| 1984 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1985 | // If our superclass overflowed, we don't stand a chance. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1986 | if (reference_offsets == CLASS_WALK_SUPER) { |
| 1987 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1988 | return; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1989 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1990 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1991 | CreateReferenceOffsets(klass, true, reference_offsets); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | void ClassLinker::CreateReferenceStaticOffsets(Class* klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1995 | CreateReferenceOffsets(klass, false, 0); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1996 | } |
| 1997 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1998 | void ClassLinker::CreateReferenceOffsets(Class* klass, bool instance, |
| 1999 | uint32_t reference_offsets) { |
| 2000 | size_t num_reference_fields = |
| 2001 | instance ? klass->NumReferenceInstanceFieldsDuringLinking() |
| 2002 | : klass->NumReferenceStaticFieldsDuringLinking(); |
| 2003 | const ObjectArray<Field>* fields = |
| 2004 | instance ? klass->GetIFields() : klass->GetSFields(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2005 | // All of the fields that contain object references are guaranteed |
| 2006 | // to be at the beginning of the fields list. |
| 2007 | for (size_t i = 0; i < num_reference_fields; ++i) { |
| 2008 | // Note that byte_offset is the offset from the beginning of |
| 2009 | // object, not the offset into instance data |
| 2010 | const Field* field = fields->Get(i); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2011 | MemberOffset byte_offset = field->GetOffsetDuringLinking(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2012 | CHECK_EQ(byte_offset.Uint32Value() & (CLASS_OFFSET_ALIGNMENT - 1), 0U); |
| 2013 | if (CLASS_CAN_ENCODE_OFFSET(byte_offset.Uint32Value())) { |
| 2014 | uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset.Uint32Value()); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2015 | CHECK_NE(new_bit, 0U); |
| 2016 | reference_offsets |= new_bit; |
| 2017 | } else { |
| 2018 | reference_offsets = CLASS_WALK_SUPER; |
| 2019 | break; |
| 2020 | } |
| 2021 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2022 | // Update fields in klass |
| 2023 | if (instance) { |
| 2024 | klass->SetReferenceInstanceOffsets(reference_offsets); |
| 2025 | } else { |
| 2026 | klass->SetReferenceStaticOffsets(reference_offsets); |
| 2027 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2028 | } |
| 2029 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2030 | String* ClassLinker::ResolveString(const DexFile& dex_file, |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 2031 | uint32_t string_idx, DexCache* dex_cache) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2032 | String* resolved = dex_cache->GetResolvedString(string_idx); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2033 | if (resolved != NULL) { |
| 2034 | return resolved; |
| 2035 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2036 | const DexFile::StringId& string_id = dex_file.GetStringId(string_idx); |
| 2037 | int32_t utf16_length = dex_file.GetStringLength(string_id); |
| 2038 | const char* utf8_data = dex_file.GetStringData(string_id); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2039 | // TODO: remote the const_cast below |
| 2040 | String* string = const_cast<String*>(intern_table_->InternStrong(utf16_length, utf8_data)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2041 | dex_cache->SetResolvedString(string_idx, string); |
| 2042 | return string; |
| 2043 | } |
| 2044 | |
| 2045 | Class* ClassLinker::ResolveType(const DexFile& dex_file, |
| 2046 | uint32_t type_idx, |
| 2047 | DexCache* dex_cache, |
| 2048 | const ClassLoader* class_loader) { |
| 2049 | Class* resolved = dex_cache->GetResolvedType(type_idx); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2050 | if (resolved == NULL) { |
| 2051 | const char* descriptor = dex_file.dexStringByTypeIdx(type_idx); |
| 2052 | if (descriptor[1] == '\0') { |
| 2053 | // only the descriptors of primitive types should be 1 character long |
| 2054 | resolved = FindPrimitiveClass(descriptor[0]); |
| 2055 | } else { |
| 2056 | resolved = FindClass(descriptor, class_loader); |
| 2057 | } |
| 2058 | if (resolved != NULL) { |
| 2059 | Class* check = resolved->IsArrayClass() ? resolved->GetComponentType() : resolved; |
| 2060 | if (dex_cache != check->GetDexCache()) { |
| 2061 | if (check->GetClassLoader() != NULL) { |
| 2062 | LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError |
| 2063 | resolved = NULL; |
| 2064 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2065 | } |
| 2066 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2067 | if (resolved != NULL) { |
| 2068 | dex_cache->SetResolvedType(type_idx, resolved); |
| 2069 | } else { |
| 2070 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 2071 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2072 | } |
| 2073 | return resolved; |
| 2074 | } |
| 2075 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2076 | Method* ClassLinker::ResolveMethod(const DexFile& dex_file, |
| 2077 | uint32_t method_idx, |
| 2078 | DexCache* dex_cache, |
| 2079 | const ClassLoader* class_loader, |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 2080 | bool is_direct) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2081 | Method* resolved = dex_cache->GetResolvedMethod(method_idx); |
| 2082 | if (resolved != NULL) { |
| 2083 | return resolved; |
| 2084 | } |
| 2085 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
| 2086 | Class* klass = ResolveType(dex_file, method_id.class_idx_, dex_cache, class_loader); |
| 2087 | if (klass == NULL) { |
| 2088 | return NULL; |
| 2089 | } |
| 2090 | |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 2091 | const char* name = dex_file.dexStringById(method_id.name_idx_); |
Elliott Hughes | 0c424cb | 2011-08-26 10:16:25 -0700 | [diff] [blame] | 2092 | std::string signature(dex_file.CreateMethodDescriptor(method_id.proto_idx_, NULL)); |
Brian Carlstrom | 7540ff4 | 2011-09-04 16:38:46 -0700 | [diff] [blame] | 2093 | if (is_direct) { |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 2094 | resolved = klass->FindDirectMethod(name, signature); |
Brian Carlstrom | 7540ff4 | 2011-09-04 16:38:46 -0700 | [diff] [blame] | 2095 | } else if (klass->IsInterface()) { |
| 2096 | resolved = klass->FindInterfaceMethod(name, signature); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 2097 | } else { |
| 2098 | resolved = klass->FindVirtualMethod(name, signature); |
| 2099 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2100 | if (resolved != NULL) { |
| 2101 | dex_cache->SetResolvedMethod(method_idx, resolved); |
| 2102 | } else { |
| 2103 | // DCHECK(Thread::Current()->IsExceptionPending()); |
| 2104 | } |
| 2105 | return resolved; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2106 | } |
| 2107 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2108 | Field* ClassLinker::ResolveField(const DexFile& dex_file, |
| 2109 | uint32_t field_idx, |
| 2110 | DexCache* dex_cache, |
| 2111 | const ClassLoader* class_loader, |
| 2112 | bool is_static) { |
| 2113 | Field* resolved = dex_cache->GetResolvedField(field_idx); |
| 2114 | if (resolved != NULL) { |
| 2115 | return resolved; |
| 2116 | } |
| 2117 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
| 2118 | Class* klass = ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader); |
| 2119 | if (klass == NULL) { |
| 2120 | return NULL; |
| 2121 | } |
| 2122 | |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 2123 | const char* name = dex_file.dexStringById(field_id.name_idx_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2124 | Class* field_type = ResolveType(dex_file, field_id.type_idx_, dex_cache, class_loader); |
| 2125 | // TODO: LinkageError? |
| 2126 | CHECK(field_type != NULL); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 2127 | if (is_static) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2128 | resolved = klass->FindStaticField(name, field_type); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 2129 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2130 | resolved = klass->FindInstanceField(name, field_type); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 2131 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2132 | if (resolved != NULL) { |
| 2133 | dex_cache->SetResolvedfield(field_idx, resolved); |
| 2134 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2135 | // TODO: DCHECK(Thread::Current()->IsExceptionPending()); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2136 | } |
| 2137 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 2138 | } |
| 2139 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 2140 | size_t ClassLinker::NumLoadedClasses() const { |
| 2141 | MutexLock mu(classes_lock_); |
| 2142 | return classes_.size(); |
| 2143 | } |
| 2144 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2145 | } // namespace art |