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