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