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