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