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