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