Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "class_linker.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 18 | |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 19 | #include <fcntl.h> |
| 20 | #include <sys/file.h> |
| 21 | #include <sys/stat.h> |
Brian Carlstrom | dbf05b7 | 2011-12-15 00:55:24 -0800 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | #include <sys/wait.h> |
| 24 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 25 | #include <deque> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 26 | #include <string> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 27 | #include <utility> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 28 | #include <vector> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 29 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 30 | #include "casts.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 31 | #include "class_loader.h" |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 32 | #include "debugger.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 33 | #include "dex_cache.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 34 | #include "dex_file.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 35 | #include "dex_verifier.h" |
| 36 | #include "heap.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 37 | #include "intern_table.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 38 | #include "leb128.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 39 | #include "logging.h" |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 40 | #include "oat_file.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 41 | #include "object.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 42 | #include "object_utils.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 43 | #include "runtime.h" |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 44 | #include "runtime_support.h" |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 45 | #include "ScopedLocalRef.h" |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 46 | #include "space.h" |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 47 | #include "stack_indirect_reference_table.h" |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 48 | #include "stl_util.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 49 | #include "thread.h" |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 50 | #include "UniquePtr.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 51 | #include "utils.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 52 | |
| 53 | namespace art { |
| 54 | |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 55 | namespace { |
| 56 | |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 57 | void ThrowNoClassDefFoundError(const char* fmt, ...) __attribute__((__format__(__printf__, 1, 2))); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 58 | void ThrowNoClassDefFoundError(const char* fmt, ...) { |
| 59 | va_list args; |
| 60 | va_start(args, fmt); |
| 61 | Thread::Current()->ThrowNewExceptionV("Ljava/lang/NoClassDefFoundError;", fmt, args); |
| 62 | va_end(args); |
| 63 | } |
| 64 | |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 65 | void ThrowClassFormatError(const char* fmt, ...) __attribute__((__format__(__printf__, 1, 2))); |
Elliott Hughes | e555dc0 | 2011-09-25 10:46:35 -0700 | [diff] [blame] | 66 | void ThrowClassFormatError(const char* fmt, ...) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 67 | va_list args; |
| 68 | va_start(args, fmt); |
Elliott Hughes | e555dc0 | 2011-09-25 10:46:35 -0700 | [diff] [blame] | 69 | Thread::Current()->ThrowNewExceptionV("Ljava/lang/ClassFormatError;", fmt, args); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 70 | va_end(args); |
| 71 | } |
| 72 | |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 73 | void ThrowLinkageError(const char* fmt, ...) __attribute__((__format__(__printf__, 1, 2))); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 74 | void ThrowLinkageError(const char* fmt, ...) { |
| 75 | va_list args; |
| 76 | va_start(args, fmt); |
| 77 | Thread::Current()->ThrowNewExceptionV("Ljava/lang/LinkageError;", fmt, args); |
| 78 | va_end(args); |
| 79 | } |
| 80 | |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 81 | void ThrowNoSuchMethodError(bool is_direct, Class* c, const StringPiece& name, |
| 82 | const StringPiece& signature) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 83 | ClassHelper kh(c); |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 84 | std::ostringstream msg; |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 85 | msg << "no " << (is_direct ? "direct" : "virtual") << " method " << name << "." << signature |
| 86 | << " in class " << kh.GetDescriptor() << " or its superclasses"; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 87 | std::string location(kh.GetLocation()); |
| 88 | if (!location.empty()) { |
| 89 | msg << " (defined in " << location << ")"; |
Elliott Hughes | cc5f9a9 | 2011-09-28 19:17:29 -0700 | [diff] [blame] | 90 | } |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 91 | Thread::Current()->ThrowNewException("Ljava/lang/NoSuchMethodError;", msg.str().c_str()); |
Elliott Hughes | cc5f9a9 | 2011-09-28 19:17:29 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 94 | void ThrowNoSuchFieldError(const StringPiece& scope, Class* c, const StringPiece& type, |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 95 | const StringPiece& name) { |
| 96 | ClassHelper kh(c); |
| 97 | std::ostringstream msg; |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 98 | msg << "no " << scope << "field " << name << " of type " << type |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 99 | << " in class " << kh.GetDescriptor() << " or its superclasses"; |
| 100 | std::string location(kh.GetLocation()); |
| 101 | if (!location.empty()) { |
| 102 | msg << " (defined in " << location << ")"; |
| 103 | } |
| 104 | Thread::Current()->ThrowNewException("Ljava/lang/NoSuchFieldError;", msg.str().c_str()); |
| 105 | } |
| 106 | |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 107 | void ThrowNullPointerException(const char* fmt, ...) __attribute__((__format__(__printf__, 1, 2))); |
| 108 | void ThrowNullPointerException(const char* fmt, ...) { |
| 109 | va_list args; |
| 110 | va_start(args, fmt); |
| 111 | Thread::Current()->ThrowNewExceptionV("Ljava/lang/NullPointerException;", fmt, args); |
| 112 | va_end(args); |
| 113 | } |
| 114 | |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 115 | void ThrowEarlierClassFailure(Class* c) { |
| 116 | /* |
| 117 | * The class failed to initialize on a previous attempt, so we want to throw |
| 118 | * a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we |
| 119 | * failed in verification, in which case v2 5.4.1 says we need to re-throw |
| 120 | * the previous error. |
| 121 | */ |
| 122 | LOG(INFO) << "Rejecting re-init on previously-failed class " << PrettyClass(c); |
| 123 | |
Brian Carlstrom | 4d9716c | 2012-01-30 01:49:33 -0800 | [diff] [blame] | 124 | CHECK(c->IsErroneous()) << PrettyClass(c); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 125 | if (c->GetVerifyErrorClass() != NULL) { |
| 126 | // TODO: change the verifier to store an _instance_, with a useful detail message? |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 127 | ClassHelper ve_ch(c->GetVerifyErrorClass()); |
| 128 | std::string error_descriptor(ve_ch.GetDescriptor()); |
| 129 | Thread::Current()->ThrowNewException(error_descriptor.c_str(), PrettyDescriptor(c).c_str()); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 130 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 131 | ThrowNoClassDefFoundError("%s", PrettyDescriptor(c).c_str()); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 135 | void WrapExceptionInInitializer() { |
| 136 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
| 137 | |
| 138 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
| 139 | CHECK(cause.get() != NULL); |
| 140 | |
| 141 | env->ExceptionClear(); |
| 142 | |
| 143 | // TODO: add java.lang.Error to JniConstants? |
| 144 | ScopedLocalRef<jclass> error_class(env, env->FindClass("java/lang/Error")); |
| 145 | CHECK(error_class.get() != NULL); |
| 146 | if (env->IsInstanceOf(cause.get(), error_class.get())) { |
| 147 | // We only wrap non-Error exceptions; an Error can just be used as-is. |
| 148 | env->Throw(cause.get()); |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | // TODO: add java.lang.ExceptionInInitializerError to JniConstants? |
| 153 | ScopedLocalRef<jclass> eiie_class(env, env->FindClass("java/lang/ExceptionInInitializerError")); |
| 154 | CHECK(eiie_class.get() != NULL); |
| 155 | |
| 156 | jmethodID mid = env->GetMethodID(eiie_class.get(), "<init>" , "(Ljava/lang/Throwable;)V"); |
| 157 | CHECK(mid != NULL); |
| 158 | |
| 159 | ScopedLocalRef<jthrowable> eiie(env, |
| 160 | reinterpret_cast<jthrowable>(env->NewObject(eiie_class.get(), mid, cause.get()))); |
| 161 | env->Throw(eiie.get()); |
| 162 | } |
| 163 | |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 164 | static size_t Hash(const char* s) { |
| 165 | // This is the java.lang.String hashcode for convenience, not interoperability. |
| 166 | size_t hash = 0; |
| 167 | for (; *s != '\0'; ++s) { |
| 168 | hash = hash * 31 + *s; |
| 169 | } |
| 170 | return hash; |
| 171 | } |
| 172 | |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 173 | } // namespace |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 174 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 175 | const char* ClassLinker::class_roots_descriptors_[] = { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 176 | "Ljava/lang/Class;", |
| 177 | "Ljava/lang/Object;", |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 178 | "[Ljava/lang/Class;", |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 179 | "[Ljava/lang/Object;", |
| 180 | "Ljava/lang/String;", |
Elliott Hughes | bf61ba3 | 2011-10-11 10:53:09 -0700 | [diff] [blame] | 181 | "Ljava/lang/ref/Reference;", |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 182 | "Ljava/lang/reflect/Constructor;", |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 183 | "Ljava/lang/reflect/Field;", |
| 184 | "Ljava/lang/reflect/Method;", |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 185 | "Ljava/lang/reflect/Proxy;", |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 186 | "Ljava/lang/ClassLoader;", |
| 187 | "Ldalvik/system/BaseDexClassLoader;", |
| 188 | "Ldalvik/system/PathClassLoader;", |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 189 | "Ljava/lang/StackTraceElement;", |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 190 | "Z", |
| 191 | "B", |
| 192 | "C", |
| 193 | "D", |
| 194 | "F", |
| 195 | "I", |
| 196 | "J", |
| 197 | "S", |
| 198 | "V", |
| 199 | "[Z", |
| 200 | "[B", |
| 201 | "[C", |
| 202 | "[D", |
| 203 | "[F", |
| 204 | "[I", |
| 205 | "[J", |
| 206 | "[S", |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 207 | "[Ljava/lang/StackTraceElement;", |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 208 | }; |
| 209 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 210 | ClassLinker* ClassLinker::Create(const std::string& boot_class_path, InternTable* intern_table) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 211 | CHECK_NE(boot_class_path.size(), 0U); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 212 | UniquePtr<ClassLinker> class_linker(new ClassLinker(intern_table)); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 213 | class_linker->Init(boot_class_path); |
| 214 | return class_linker.release(); |
| 215 | } |
| 216 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 217 | ClassLinker* ClassLinker::Create(InternTable* intern_table) { |
| 218 | UniquePtr<ClassLinker> class_linker(new ClassLinker(intern_table)); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 219 | class_linker->InitFromImage(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 220 | return class_linker.release(); |
| 221 | } |
| 222 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 223 | ClassLinker::ClassLinker(InternTable* intern_table) |
| 224 | : dex_lock_("ClassLinker dex lock"), |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 225 | classes_lock_("ClassLinker classes lock"), |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 226 | class_roots_(NULL), |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 227 | array_iftable_(NULL), |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 228 | init_done_(false), |
| 229 | intern_table_(intern_table) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 230 | CHECK_EQ(arraysize(class_roots_descriptors_), size_t(kClassRootsMax)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 231 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 232 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 233 | void CreateClassPath(const std::string& class_path, |
| 234 | std::vector<const DexFile*>& class_path_vector) { |
| 235 | std::vector<std::string> parsed; |
| 236 | Split(class_path, ':', parsed); |
| 237 | for (size_t i = 0; i < parsed.size(); ++i) { |
| 238 | const DexFile* dex_file = DexFile::Open(parsed[i], Runtime::Current()->GetHostPrefix()); |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 239 | if (dex_file == NULL) { |
| 240 | LOG(WARNING) << "Failed to open dex file " << parsed[i]; |
| 241 | } else { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 242 | class_path_vector.push_back(dex_file); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void ClassLinker::Init(const std::string& boot_class_path) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 248 | VLOG(startup) << "ClassLinker::InitFrom entering boot_class_path=" << boot_class_path; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 249 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 250 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 251 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 252 | // java_lang_Class comes first, it's needed for AllocClass |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 253 | SirtRef<Class> java_lang_Class(down_cast<Class*>(Heap::AllocObject(NULL, sizeof(ClassClass)))); |
| 254 | CHECK(java_lang_Class.get() != NULL); |
| 255 | java_lang_Class->SetClass(java_lang_Class.get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 256 | java_lang_Class->SetClassSize(sizeof(ClassClass)); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 257 | // AllocClass(Class*) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 258 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 259 | // Class[] is used for reflection support. |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 260 | SirtRef<Class> class_array_class(AllocClass(java_lang_Class.get(), sizeof(Class))); |
| 261 | class_array_class->SetComponentType(java_lang_Class.get()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 262 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 263 | // java_lang_Object comes next so that object_array_class can be created |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 264 | SirtRef<Class> java_lang_Object(AllocClass(java_lang_Class.get(), sizeof(Class))); |
| 265 | CHECK(java_lang_Object.get() != NULL); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 266 | // backfill Object as the super class of Class |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 267 | java_lang_Class->SetSuperClass(java_lang_Object.get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 268 | java_lang_Object->SetStatus(Class::kStatusLoaded); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 269 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 270 | // Object[] next to hold class roots |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 271 | SirtRef<Class> object_array_class(AllocClass(java_lang_Class.get(), sizeof(Class))); |
| 272 | object_array_class->SetComponentType(java_lang_Object.get()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 273 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 274 | // Setup the char class to be used for char[] |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 275 | SirtRef<Class> char_class(AllocClass(java_lang_Class.get(), sizeof(Class))); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 276 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 277 | // Setup the char[] class to be used for String |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 278 | SirtRef<Class> char_array_class(AllocClass(java_lang_Class.get(), sizeof(Class))); |
| 279 | char_array_class->SetComponentType(char_class.get()); |
| 280 | CharArray::SetArrayClass(char_array_class.get()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 281 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 282 | // Setup String |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 283 | SirtRef<Class> java_lang_String(AllocClass(java_lang_Class.get(), sizeof(StringClass))); |
| 284 | String::SetClass(java_lang_String.get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 285 | java_lang_String->SetObjectSize(sizeof(String)); |
| 286 | java_lang_String->SetStatus(Class::kStatusResolved); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 287 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 288 | // Create storage for root classes, save away our work so far (requires |
| 289 | // descriptors) |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 290 | class_roots_ = ObjectArray<Class>::Alloc(object_array_class.get(), kClassRootsMax); |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 291 | CHECK(class_roots_ != NULL); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 292 | SetClassRoot(kJavaLangClass, java_lang_Class.get()); |
| 293 | SetClassRoot(kJavaLangObject, java_lang_Object.get()); |
| 294 | SetClassRoot(kClassArrayClass, class_array_class.get()); |
| 295 | SetClassRoot(kObjectArrayClass, object_array_class.get()); |
| 296 | SetClassRoot(kCharArrayClass, char_array_class.get()); |
| 297 | SetClassRoot(kJavaLangString, java_lang_String.get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 298 | |
| 299 | // Setup the primitive type classes. |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 300 | SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass("Z", Primitive::kPrimBoolean)); |
| 301 | SetClassRoot(kPrimitiveByte, CreatePrimitiveClass("B", Primitive::kPrimByte)); |
| 302 | SetClassRoot(kPrimitiveShort, CreatePrimitiveClass("S", Primitive::kPrimShort)); |
| 303 | SetClassRoot(kPrimitiveInt, CreatePrimitiveClass("I", Primitive::kPrimInt)); |
| 304 | SetClassRoot(kPrimitiveLong, CreatePrimitiveClass("J", Primitive::kPrimLong)); |
| 305 | SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass("F", Primitive::kPrimFloat)); |
| 306 | SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass("D", Primitive::kPrimDouble)); |
| 307 | SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass("V", Primitive::kPrimVoid)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 308 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 309 | // Create array interface entries to populate once we can load system classes |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 310 | array_iftable_ = AllocObjectArray<InterfaceEntry>(2); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 311 | |
| 312 | // Create int array type for AllocDexCache (done in AppendToBootClassPath) |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 313 | SirtRef<Class> int_array_class(AllocClass(java_lang_Class.get(), sizeof(Class))); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 314 | int_array_class->SetComponentType(GetClassRoot(kPrimitiveInt)); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 315 | IntArray::SetArrayClass(int_array_class.get()); |
| 316 | SetClassRoot(kIntArrayClass, int_array_class.get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 317 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 318 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 319 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 320 | // setup boot_class_path_ and register class_path now that we can |
| 321 | // use AllocObjectArray to create DexCache instances |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 322 | std::vector<const DexFile*> boot_class_path_vector; |
| 323 | CreateClassPath(boot_class_path, boot_class_path_vector); |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 324 | CHECK_NE(0U, boot_class_path_vector.size()); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 325 | for (size_t i = 0; i != boot_class_path_vector.size(); ++i) { |
| 326 | const DexFile* dex_file = boot_class_path_vector[i]; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 327 | CHECK(dex_file != NULL); |
| 328 | AppendToBootClassPath(*dex_file); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 329 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 330 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 331 | // Constructor, Field, and Method are necessary so that FindClass can link members |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 332 | SirtRef<Class> java_lang_reflect_Constructor(AllocClass(java_lang_Class.get(), sizeof(MethodClass))); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 333 | CHECK(java_lang_reflect_Constructor.get() != NULL); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 334 | java_lang_reflect_Constructor->SetObjectSize(sizeof(Method)); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 335 | SetClassRoot(kJavaLangReflectConstructor, java_lang_reflect_Constructor.get()); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 336 | java_lang_reflect_Constructor->SetStatus(Class::kStatusResolved); |
| 337 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 338 | SirtRef<Class> java_lang_reflect_Field(AllocClass(java_lang_Class.get(), sizeof(FieldClass))); |
| 339 | CHECK(java_lang_reflect_Field.get() != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 340 | java_lang_reflect_Field->SetObjectSize(sizeof(Field)); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 341 | SetClassRoot(kJavaLangReflectField, java_lang_reflect_Field.get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 342 | java_lang_reflect_Field->SetStatus(Class::kStatusResolved); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 343 | Field::SetClass(java_lang_reflect_Field.get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 344 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 345 | SirtRef<Class> java_lang_reflect_Method(AllocClass(java_lang_Class.get(), sizeof(MethodClass))); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 346 | CHECK(java_lang_reflect_Method.get() != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 347 | java_lang_reflect_Method->SetObjectSize(sizeof(Method)); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 348 | SetClassRoot(kJavaLangReflectMethod, java_lang_reflect_Method.get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 349 | java_lang_reflect_Method->SetStatus(Class::kStatusResolved); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 350 | Method::SetClasses(java_lang_reflect_Constructor.get(), java_lang_reflect_Method.get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 351 | |
| 352 | // now we can use FindSystemClass |
| 353 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 354 | // run char class through InitializePrimitiveClass to finish init |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 355 | InitializePrimitiveClass(char_class.get(), "C", Primitive::kPrimChar); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 356 | SetClassRoot(kPrimitiveChar, char_class.get()); // needs descriptor |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 357 | |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 358 | // Object and String need to be rerun through FindSystemClass to finish init |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 359 | java_lang_Object->SetStatus(Class::kStatusNotReady); |
| 360 | Class* Object_class = FindSystemClass("Ljava/lang/Object;"); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 361 | CHECK_EQ(java_lang_Object.get(), Object_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 362 | CHECK_EQ(java_lang_Object->GetObjectSize(), sizeof(Object)); |
| 363 | java_lang_String->SetStatus(Class::kStatusNotReady); |
| 364 | Class* String_class = FindSystemClass("Ljava/lang/String;"); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 365 | CHECK_EQ(java_lang_String.get(), String_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 366 | CHECK_EQ(java_lang_String->GetObjectSize(), sizeof(String)); |
| 367 | |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 368 | // 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] | 369 | SetClassRoot(kBooleanArrayClass, FindSystemClass("[Z")); |
| 370 | BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
| 371 | |
| 372 | SetClassRoot(kByteArrayClass, FindSystemClass("[B")); |
| 373 | ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
| 374 | |
| 375 | Class* found_char_array_class = FindSystemClass("[C"); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 376 | CHECK_EQ(char_array_class.get(), found_char_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 377 | |
| 378 | SetClassRoot(kShortArrayClass, FindSystemClass("[S")); |
| 379 | ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
| 380 | |
| 381 | Class* found_int_array_class = FindSystemClass("[I"); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 382 | CHECK_EQ(int_array_class.get(), found_int_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 383 | |
| 384 | SetClassRoot(kLongArrayClass, FindSystemClass("[J")); |
| 385 | LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
| 386 | |
| 387 | SetClassRoot(kFloatArrayClass, FindSystemClass("[F")); |
| 388 | FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
| 389 | |
| 390 | SetClassRoot(kDoubleArrayClass, FindSystemClass("[D")); |
| 391 | DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
| 392 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 393 | Class* found_class_array_class = FindSystemClass("[Ljava/lang/Class;"); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 394 | CHECK_EQ(class_array_class.get(), found_class_array_class); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 395 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 396 | Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;"); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 397 | CHECK_EQ(object_array_class.get(), found_object_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 398 | |
| 399 | // Setup the single, global copies of "interfaces" and "iftable" |
| 400 | Class* java_lang_Cloneable = FindSystemClass("Ljava/lang/Cloneable;"); |
| 401 | CHECK(java_lang_Cloneable != NULL); |
| 402 | Class* java_io_Serializable = FindSystemClass("Ljava/io/Serializable;"); |
| 403 | CHECK(java_io_Serializable != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 404 | // We assume that Cloneable/Serializable don't have superinterfaces -- |
| 405 | // 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] | 406 | // supers as well. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 407 | array_iftable_->Set(0, AllocInterfaceEntry(java_lang_Cloneable)); |
| 408 | array_iftable_->Set(1, AllocInterfaceEntry(java_io_Serializable)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 409 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 410 | // Sanity check Class[] and Object[]'s interfaces |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 411 | ClassHelper kh(class_array_class.get(), this); |
| 412 | CHECK_EQ(java_lang_Cloneable, kh.GetInterface(0)); |
| 413 | CHECK_EQ(java_io_Serializable, kh.GetInterface(1)); |
| 414 | kh.ChangeClass(object_array_class.get()); |
| 415 | CHECK_EQ(java_lang_Cloneable, kh.GetInterface(0)); |
| 416 | CHECK_EQ(java_io_Serializable, kh.GetInterface(1)); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 417 | // run Class, Constructor, Field, and Method through FindSystemClass. |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 418 | // this initializes their dex_cache_ fields and register them in classes_. |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 419 | Class* Class_class = FindSystemClass("Ljava/lang/Class;"); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 420 | CHECK_EQ(java_lang_Class.get(), Class_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 421 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 422 | java_lang_reflect_Constructor->SetStatus(Class::kStatusNotReady); |
| 423 | Class* Constructor_class = FindSystemClass("Ljava/lang/reflect/Constructor;"); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 424 | CHECK_EQ(java_lang_reflect_Constructor.get(), Constructor_class); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 425 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 426 | java_lang_reflect_Field->SetStatus(Class::kStatusNotReady); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 427 | Class* Field_class = FindSystemClass("Ljava/lang/reflect/Field;"); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 428 | CHECK_EQ(java_lang_reflect_Field.get(), Field_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 429 | |
| 430 | java_lang_reflect_Method->SetStatus(Class::kStatusNotReady); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 431 | Class* Method_class = FindSystemClass("Ljava/lang/reflect/Method;"); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 432 | CHECK_EQ(java_lang_reflect_Method.get(), Method_class); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 433 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 434 | // End of special init trickery, subsequent classes may be loaded via FindSystemClass |
| 435 | |
| 436 | // Create java.lang.reflect.Proxy root |
| 437 | Class* java_lang_reflect_Proxy = FindSystemClass("Ljava/lang/reflect/Proxy;"); |
| 438 | SetClassRoot(kJavaLangReflectProxy, java_lang_reflect_Proxy); |
| 439 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 440 | // 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] | 441 | Class* java_lang_ref_Reference = FindSystemClass("Ljava/lang/ref/Reference;"); |
| 442 | SetClassRoot(kJavaLangRefReference, java_lang_ref_Reference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 443 | Class* java_lang_ref_FinalizerReference = FindSystemClass("Ljava/lang/ref/FinalizerReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 444 | java_lang_ref_FinalizerReference->SetAccessFlags( |
| 445 | java_lang_ref_FinalizerReference->GetAccessFlags() | |
| 446 | kAccClassIsReference | kAccClassIsFinalizerReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 447 | Class* java_lang_ref_PhantomReference = FindSystemClass("Ljava/lang/ref/PhantomReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 448 | java_lang_ref_PhantomReference->SetAccessFlags( |
| 449 | java_lang_ref_PhantomReference->GetAccessFlags() | |
| 450 | kAccClassIsReference | kAccClassIsPhantomReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 451 | Class* java_lang_ref_SoftReference = FindSystemClass("Ljava/lang/ref/SoftReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 452 | java_lang_ref_SoftReference->SetAccessFlags( |
| 453 | java_lang_ref_SoftReference->GetAccessFlags() | kAccClassIsReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 454 | Class* java_lang_ref_WeakReference = FindSystemClass("Ljava/lang/ref/WeakReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 455 | java_lang_ref_WeakReference->SetAccessFlags( |
| 456 | java_lang_ref_WeakReference->GetAccessFlags() | |
| 457 | kAccClassIsReference | kAccClassIsWeakReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 458 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 459 | // Setup the ClassLoaders, verifying the object_size_ |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 460 | Class* java_lang_ClassLoader = FindSystemClass("Ljava/lang/ClassLoader;"); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 461 | CHECK_EQ(java_lang_ClassLoader->GetObjectSize(), sizeof(ClassLoader)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 462 | SetClassRoot(kJavaLangClassLoader, java_lang_ClassLoader); |
| 463 | |
| 464 | Class* dalvik_system_BaseDexClassLoader = FindSystemClass("Ldalvik/system/BaseDexClassLoader;"); |
| 465 | CHECK_EQ(dalvik_system_BaseDexClassLoader->GetObjectSize(), sizeof(BaseDexClassLoader)); |
| 466 | SetClassRoot(kDalvikSystemBaseDexClassLoader, dalvik_system_BaseDexClassLoader); |
| 467 | |
| 468 | Class* dalvik_system_PathClassLoader = FindSystemClass("Ldalvik/system/PathClassLoader;"); |
| 469 | CHECK_EQ(dalvik_system_PathClassLoader->GetObjectSize(), sizeof(PathClassLoader)); |
| 470 | SetClassRoot(kDalvikSystemPathClassLoader, dalvik_system_PathClassLoader); |
| 471 | PathClassLoader::SetClass(dalvik_system_PathClassLoader); |
| 472 | |
| 473 | // Set up java.lang.StackTraceElement as a convenience |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 474 | SetClassRoot(kJavaLangStackTraceElement, FindSystemClass("Ljava/lang/StackTraceElement;")); |
| 475 | SetClassRoot(kJavaLangStackTraceElementArrayClass, FindSystemClass("[Ljava/lang/StackTraceElement;")); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 476 | StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 477 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 478 | FinishInit(); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 479 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 480 | VLOG(startup) << "ClassLinker::InitFrom exiting"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | void ClassLinker::FinishInit() { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 484 | VLOG(startup) << "ClassLinker::FinishInit entering"; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 485 | |
| 486 | // Let the heap know some key offsets into java.lang.ref instances |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 487 | // Note: we hard code the field indexes here rather than using FindInstanceField |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 488 | // as the types of the field can't be resolved prior to the runtime being |
| 489 | // fully initialized |
Elliott Hughes | bf61ba3 | 2011-10-11 10:53:09 -0700 | [diff] [blame] | 490 | Class* java_lang_ref_Reference = GetClassRoot(kJavaLangRefReference); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 491 | Class* java_lang_ref_ReferenceQueue = FindSystemClass("Ljava/lang/ref/ReferenceQueue;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 492 | Class* java_lang_ref_FinalizerReference = FindSystemClass("Ljava/lang/ref/FinalizerReference;"); |
| 493 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 494 | Heap::SetWellKnownClasses(java_lang_ref_FinalizerReference, java_lang_ref_ReferenceQueue); |
| 495 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 496 | const DexFile& java_lang_dex = FindDexFile(java_lang_ref_Reference->GetDexCache()); |
| 497 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 498 | Field* pendingNext = java_lang_ref_Reference->GetInstanceField(0); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 499 | FieldHelper fh(pendingNext, this); |
| 500 | CHECK_STREQ(fh.GetName(), "pendingNext"); |
| 501 | CHECK_EQ(java_lang_dex.GetFieldId(pendingNext->GetDexFieldIndex()).type_idx_, |
| 502 | java_lang_ref_Reference->GetDexTypeIndex()); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 503 | |
| 504 | Field* queue = java_lang_ref_Reference->GetInstanceField(1); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 505 | fh.ChangeField(queue); |
| 506 | CHECK_STREQ(fh.GetName(), "queue"); |
| 507 | CHECK_EQ(java_lang_dex.GetFieldId(queue->GetDexFieldIndex()).type_idx_, |
| 508 | java_lang_ref_ReferenceQueue->GetDexTypeIndex()); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 509 | |
| 510 | Field* queueNext = java_lang_ref_Reference->GetInstanceField(2); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 511 | fh.ChangeField(queueNext); |
| 512 | CHECK_STREQ(fh.GetName(), "queueNext"); |
| 513 | CHECK_EQ(java_lang_dex.GetFieldId(queueNext->GetDexFieldIndex()).type_idx_, |
| 514 | java_lang_ref_Reference->GetDexTypeIndex()); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 515 | |
| 516 | Field* referent = java_lang_ref_Reference->GetInstanceField(3); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 517 | fh.ChangeField(referent); |
| 518 | CHECK_STREQ(fh.GetName(), "referent"); |
| 519 | CHECK_EQ(java_lang_dex.GetFieldId(referent->GetDexFieldIndex()).type_idx_, |
| 520 | GetClassRoot(kJavaLangObject)->GetDexTypeIndex()); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 521 | |
| 522 | Field* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 523 | fh.ChangeField(zombie); |
| 524 | CHECK_STREQ(fh.GetName(), "zombie"); |
| 525 | CHECK_EQ(java_lang_dex.GetFieldId(zombie->GetDexFieldIndex()).type_idx_, |
| 526 | GetClassRoot(kJavaLangObject)->GetDexTypeIndex()); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 527 | |
| 528 | Heap::SetReferenceOffsets(referent->GetOffset(), |
| 529 | queue->GetOffset(), |
| 530 | queueNext->GetOffset(), |
| 531 | pendingNext->GetOffset(), |
| 532 | zombie->GetOffset()); |
| 533 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 534 | // ensure all class_roots_ are initialized |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 535 | for (size_t i = 0; i < kClassRootsMax; i++) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 536 | ClassRoot class_root = static_cast<ClassRoot>(i); |
| 537 | Class* klass = GetClassRoot(class_root); |
| 538 | CHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 539 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != NULL); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 540 | // note SetClassRoot does additional validation. |
| 541 | // if possible add new checks there to catch errors early |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 544 | CHECK(array_iftable_ != NULL); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 545 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 546 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 547 | // that Object, Class, and Object[] are setup |
| 548 | init_done_ = true; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 549 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 550 | VLOG(startup) << "ClassLinker::FinishInit exiting"; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 553 | void ClassLinker::RunRootClinits() { |
| 554 | Thread* self = Thread::Current(); |
| 555 | for (size_t i = 0; i < ClassLinker::kClassRootsMax; ++i) { |
| 556 | Class* c = GetClassRoot(ClassRoot(i)); |
| 557 | if (!c->IsArrayClass() && !c->IsPrimitive()) { |
| 558 | EnsureInitialized(GetClassRoot(ClassRoot(i)), true); |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 559 | CHECK(!self->IsExceptionPending()) << PrettyTypeOf(self->GetException()); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 564 | bool ClassLinker::GenerateOatFile(const std::string& dex_filename, |
| 565 | int oat_fd, |
| 566 | const std::string& oat_cache_filename) { |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 567 | std::string dex2oat_string("/system/bin/dex2oat"); |
| 568 | #ifndef NDEBUG |
| 569 | dex2oat_string += 'd'; |
| 570 | #endif |
| 571 | const char* dex2oat = dex2oat_string.c_str(); |
| 572 | |
| 573 | const char* class_path = Runtime::Current()->GetClassPath().c_str(); |
| 574 | |
| 575 | std::string boot_image_option_string("--boot-image="); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 576 | boot_image_option_string += Heap::GetSpaces()[0]->AsImageSpace()->GetImageFilename(); |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 577 | const char* boot_image_option = boot_image_option_string.c_str(); |
| 578 | |
| 579 | std::string dex_file_option_string("--dex-file="); |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 580 | dex_file_option_string += dex_filename; |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 581 | const char* dex_file_option = dex_file_option_string.c_str(); |
| 582 | |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 583 | std::string oat_fd_option_string("--oat-fd="); |
Brian Carlstrom | 866c862 | 2012-01-06 16:35:13 -0800 | [diff] [blame] | 584 | StringAppendF(&oat_fd_option_string, "%d", oat_fd); |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 585 | const char* oat_fd_option = oat_fd_option_string.c_str(); |
| 586 | |
| 587 | std::string oat_name_option_string("--oat-name="); |
| 588 | oat_name_option_string += oat_cache_filename; |
| 589 | const char* oat_name_option = oat_name_option_string.c_str(); |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 590 | |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 591 | // fork and exec dex2oat |
| 592 | pid_t pid = fork(); |
| 593 | if (pid == 0) { |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 594 | // no allocation allowed between fork and exec |
Ian Rogers | 725aee5 | 2012-01-11 11:56:56 -0800 | [diff] [blame] | 595 | |
| 596 | // change process groups, so we don't get reaped by ProcessManager |
| 597 | setpgid(0, 0); |
| 598 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 599 | VLOG(class_linker) << dex2oat |
| 600 | << " --runtime-arg -Xms64m" |
| 601 | << " --runtime-arg -Xmx64m" |
| 602 | << " --runtime-arg -classpath" |
| 603 | << " --runtime-arg " << class_path |
| 604 | << " " << boot_image_option |
| 605 | << " " << dex_file_option |
| 606 | << " " << oat_fd_option |
| 607 | << " " << oat_name_option; |
| 608 | |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 609 | execl(dex2oat, dex2oat, |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 610 | "--runtime-arg", "-Xms64m", |
| 611 | "--runtime-arg", "-Xmx64m", |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 612 | "--runtime-arg", "-classpath", |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 613 | "--runtime-arg", class_path, |
| 614 | boot_image_option, |
| 615 | dex_file_option, |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 616 | oat_fd_option, |
| 617 | oat_name_option, |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 618 | NULL); |
| 619 | |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 620 | PLOG(FATAL) << "execl(" << dex2oat << ") failed"; |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 621 | return false; |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 622 | } else { |
| 623 | // wait for dex2oat to finish |
| 624 | int status; |
| 625 | pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 626 | if (got_pid != pid) { |
| 627 | PLOG(ERROR) << "waitpid failed: wanted " << pid << ", got " << got_pid; |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 628 | return false; |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 629 | } |
| 630 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 631 | LOG(ERROR) << dex2oat << " failed with dex-file=" << dex_filename; |
| 632 | return false; |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 633 | } |
| 634 | } |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 635 | return true; |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Brian Carlstrom | 866c862 | 2012-01-06 16:35:13 -0800 | [diff] [blame] | 638 | void ClassLinker::RegisterOatFile(const OatFile& oat_file) { |
| 639 | MutexLock mu(dex_lock_); |
| 640 | RegisterOatFileLocked(oat_file); |
| 641 | } |
| 642 | |
| 643 | void ClassLinker::RegisterOatFileLocked(const OatFile& oat_file) { |
| 644 | dex_lock_.AssertHeld(); |
| 645 | oat_files_.push_back(&oat_file); |
| 646 | } |
| 647 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 648 | OatFile* ClassLinker::OpenOat(const ImageSpace* space) { |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 649 | MutexLock mu(dex_lock_); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 650 | const Runtime* runtime = Runtime::Current(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 651 | const ImageHeader& image_header = space->GetImageHeader(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 652 | // Grab location but don't use Object::AsString as we haven't yet initialized the roots to |
| 653 | // check the down cast |
| 654 | String* oat_location = down_cast<String*>(image_header.GetImageRoot(ImageHeader::kOatLocation)); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 655 | std::string oat_filename; |
| 656 | oat_filename += runtime->GetHostPrefix(); |
| 657 | oat_filename += oat_location->ToModifiedUtf8(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 658 | OatFile* oat_file = OatFile::Open(oat_filename, "", image_header.GetOatBegin()); |
| 659 | VLOG(startup) << "ClassLinker::OpenOat entering oat_filename=" << oat_filename; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 660 | if (oat_file == NULL) { |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 661 | LOG(ERROR) << "Failed to open oat file " << oat_filename << " referenced from image."; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 662 | return NULL; |
| 663 | } |
| 664 | uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum(); |
| 665 | uint32_t image_oat_checksum = image_header.GetOatChecksum(); |
| 666 | if (oat_checksum != image_oat_checksum) { |
Brian Carlstrom | 866c862 | 2012-01-06 16:35:13 -0800 | [diff] [blame] | 667 | LOG(ERROR) << "Failed to match oat file checksum " << std::hex << oat_checksum |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 668 | << " to expected oat checksum " << std::hex << oat_checksum |
| 669 | << " in image"; |
| 670 | return NULL; |
| 671 | } |
Brian Carlstrom | 866c862 | 2012-01-06 16:35:13 -0800 | [diff] [blame] | 672 | RegisterOatFileLocked(*oat_file); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 673 | VLOG(startup) << "ClassLinker::OpenOat exiting"; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 674 | return oat_file; |
| 675 | } |
| 676 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 677 | const OatFile* ClassLinker::FindOpenedOatFileForDexFile(const DexFile& dex_file) { |
| 678 | for (size_t i = 0; i < oat_files_.size(); i++) { |
| 679 | const OatFile* oat_file = oat_files_[i]; |
| 680 | DCHECK(oat_file != NULL); |
Ian Rogers | 7fe2c69 | 2011-12-06 16:35:59 -0800 | [diff] [blame] | 681 | if (oat_file->GetOatDexFile(dex_file.GetLocation(), false)) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 682 | return oat_file; |
| 683 | } |
| 684 | } |
| 685 | return NULL; |
| 686 | } |
| 687 | |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 688 | class LockedFd { |
| 689 | public: |
| 690 | static LockedFd* CreateAndLock(std::string& name, mode_t mode) { |
| 691 | int fd = open(name.c_str(), O_CREAT | O_RDWR, mode); |
| 692 | if (fd == -1) { |
| 693 | PLOG(ERROR) << "Failed to open file '" << name << "'"; |
| 694 | return NULL; |
| 695 | } |
| 696 | fchmod(fd, mode); |
| 697 | |
| 698 | LOG(INFO) << "locking file " << name << " (fd=" << fd << ")"; |
| 699 | // try to lock non-blocking so we can log if we need may need to block |
| 700 | int result = flock(fd, LOCK_EX | LOCK_NB); |
| 701 | if (result == -1) { |
| 702 | LOG(WARNING) << "sleeping while locking file " << name; |
| 703 | // retry blocking |
| 704 | result = flock(fd, LOCK_EX); |
| 705 | } |
| 706 | if (result == -1) { |
| 707 | PLOG(ERROR) << "Failed to lock file '" << name << "'"; |
| 708 | close(fd); |
| 709 | return NULL; |
| 710 | } |
| 711 | return new LockedFd(fd); |
| 712 | } |
| 713 | |
| 714 | int GetFd() const { |
| 715 | return fd_; |
| 716 | } |
| 717 | |
| 718 | ~LockedFd() { |
| 719 | if (fd_ != -1) { |
| 720 | int result = flock(fd_, LOCK_UN); |
| 721 | if (result == -1) { |
| 722 | PLOG(WARNING) << "flock(" << fd_ << ", LOCK_UN) failed"; |
| 723 | } |
| 724 | close(fd_); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | private: |
| 729 | explicit LockedFd(int fd) : fd_(fd) {} |
| 730 | |
| 731 | int fd_; |
| 732 | }; |
| 733 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 734 | const OatFile* ClassLinker::FindOatFileForDexFile(const DexFile& dex_file) { |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 735 | MutexLock mu(dex_lock_); |
Brian Carlstrom | 866c862 | 2012-01-06 16:35:13 -0800 | [diff] [blame] | 736 | const OatFile* open_oat_file = FindOpenedOatFileForDexFile(dex_file); |
| 737 | if (open_oat_file != NULL) { |
| 738 | return open_oat_file; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 739 | } |
| 740 | |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 741 | std::string oat_filename(OatFile::DexFilenameToOatFilename(dex_file.GetLocation())); |
Brian Carlstrom | 866c862 | 2012-01-06 16:35:13 -0800 | [diff] [blame] | 742 | open_oat_file = FindOpenedOatFileFromOatLocation(oat_filename); |
| 743 | if (open_oat_file != NULL) { |
| 744 | return open_oat_file; |
| 745 | } |
| 746 | |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 747 | while (true) { |
Brian Carlstrom | 866c862 | 2012-01-06 16:35:13 -0800 | [diff] [blame] | 748 | UniquePtr<const OatFile> oat_file(FindOatFileFromOatLocation(oat_filename)); |
| 749 | if (oat_file.get() != NULL) { |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 750 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation()); |
| 751 | if (dex_file.GetHeader().checksum_ == oat_dex_file->GetDexFileChecksum()) { |
Brian Carlstrom | 866c862 | 2012-01-06 16:35:13 -0800 | [diff] [blame] | 752 | return oat_file.release(); |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 753 | } |
| 754 | LOG(WARNING) << ".oat file " << oat_file->GetLocation() |
| 755 | << " checksum mismatch with " << dex_file.GetLocation() << " --- regenerating"; |
| 756 | if (TEMP_FAILURE_RETRY(unlink(oat_file->GetLocation().c_str())) != 0) { |
| 757 | PLOG(FATAL) << "Couldn't remove obsolete .oat file " << oat_file->GetLocation(); |
| 758 | } |
| 759 | // Fall through... |
Elliott Hughes | ed6d78e | 2011-10-25 17:35:14 -0700 | [diff] [blame] | 760 | } |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 761 | // Try to generate oat file if it wasn't found or was obsolete. |
| 762 | // Note we can be racing with another runtime to do this. |
| 763 | std::string oat_cache_filename(GetArtCacheFilenameOrDie(oat_filename)); |
| 764 | UniquePtr<LockedFd> locked_fd(LockedFd::CreateAndLock(oat_cache_filename, 0644)); |
| 765 | if (locked_fd.get() == NULL) { |
| 766 | LOG(ERROR) << "Failed to create and lock oat file " << oat_cache_filename; |
| 767 | return NULL; |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 768 | } |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 769 | // Check to see if the fd we opened and locked matches the file in |
| 770 | // the filesystem. If they don't, then somebody else unlinked ours |
| 771 | // and created a new file, and we need to use that one instead. (If |
| 772 | // we caught them between the unlink and the create, we'll get an |
| 773 | // ENOENT from the file stat.) |
| 774 | struct stat fd_stat; |
| 775 | int fd_stat_result = fstat(locked_fd->GetFd(), &fd_stat); |
| 776 | if (fd_stat_result != 0) { |
| 777 | PLOG(ERROR) << "Failed to fstat file descriptor of oat file " << oat_cache_filename; |
| 778 | return NULL; |
| 779 | } |
| 780 | struct stat file_stat; |
| 781 | int file_stat_result = stat(oat_cache_filename.c_str(), &file_stat); |
| 782 | if (file_stat_result != 0 |
| 783 | || fd_stat.st_dev != file_stat.st_dev |
| 784 | || fd_stat.st_ino != file_stat.st_ino) { |
| 785 | LOG(INFO) << "Opened oat file " << oat_cache_filename << " is stale; sleeping and retrying"; |
| 786 | usleep(250 * 1000); // if something is hosed, don't peg machine |
| 787 | continue; |
| 788 | } |
| 789 | |
| 790 | // We have the correct file open and locked. If the file size is |
| 791 | // zero, then it was just created by us and we can generate its |
| 792 | // contents. If not, someone else created it. Either way, we'll |
| 793 | // loop to retry opening the file. |
| 794 | if (fd_stat.st_size == 0) { |
| 795 | bool success = GenerateOatFile(dex_file.GetLocation(), |
| 796 | locked_fd->GetFd(), |
| 797 | oat_cache_filename); |
| 798 | if (!success) { |
| 799 | LOG(ERROR) << "Failed to generate oat file " << oat_cache_filename; |
| 800 | return NULL; |
| 801 | } |
| 802 | } |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 803 | } |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 804 | // Not reached |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 807 | const OatFile* ClassLinker::FindOpenedOatFileFromOatLocation(const std::string& oat_location) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 808 | for (size_t i = 0; i < oat_files_.size(); i++) { |
| 809 | const OatFile* oat_file = oat_files_[i]; |
| 810 | DCHECK(oat_file != NULL); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 811 | if (oat_file->GetLocation() == oat_location) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 812 | return oat_file; |
| 813 | } |
| 814 | } |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 815 | return NULL; |
| 816 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 817 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 818 | const OatFile* ClassLinker::FindOatFileFromOatLocation(const std::string& oat_location) { |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 819 | MutexLock mu(dex_lock_); |
| 820 | const OatFile* oat_file = FindOpenedOatFileFromOatLocation(oat_location); |
| 821 | if (oat_file != NULL) { |
| 822 | return oat_file; |
| 823 | } |
| 824 | |
| 825 | oat_file = OatFile::Open(oat_location, "", NULL); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 826 | if (oat_file == NULL) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 827 | if (oat_location.empty() || oat_location[0] != '/') { |
| 828 | LOG(ERROR) << "Failed to open oat file from " << oat_location; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 829 | return NULL; |
| 830 | } |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 831 | |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 832 | // not found in /foo/bar/baz.oat? try /data/art-cache/foo@bar@baz.oat |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 833 | std::string cache_location(GetArtCacheFilenameOrDie(oat_location)); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 834 | oat_file = FindOpenedOatFileFromOatLocation(cache_location); |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 835 | if (oat_file != NULL) { |
| 836 | return oat_file; |
| 837 | } |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 838 | oat_file = OatFile::Open(cache_location, "", NULL); |
| 839 | if (oat_file == NULL) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 840 | LOG(INFO) << "Failed to open oat file from " << oat_location << " or " << cache_location << "."; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 841 | return NULL; |
| 842 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 843 | } |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 844 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 845 | CHECK(oat_file != NULL) << oat_location; |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 846 | RegisterOatFileLocked(*oat_file); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 847 | return oat_file; |
| 848 | } |
| 849 | |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 850 | const DexFile* ClassLinker::FindDexFileFromDexLocation(const std::string& location) { |
| 851 | std::string oat_location(OatFile::DexFilenameToOatFilename(location)); |
| 852 | const OatFile* oat_file = FindOatFileFromOatLocation(oat_location); |
| 853 | if (oat_file == NULL) { |
| 854 | return NULL; |
| 855 | } |
| 856 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(location); |
| 857 | if (oat_dex_file == NULL) { |
| 858 | return NULL; |
| 859 | } |
| 860 | const DexFile* dex_file = oat_dex_file->OpenDexFile(); |
| 861 | if (dex_file == NULL) { |
| 862 | return NULL; |
| 863 | } |
| 864 | if (oat_dex_file->GetDexFileChecksum() != dex_file->GetHeader().checksum_) { |
| 865 | return NULL; |
| 866 | } |
| 867 | return dex_file; |
| 868 | } |
| 869 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 870 | void ClassLinker::InitFromImage() { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 871 | VLOG(startup) << "ClassLinker::InitFromImage entering"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 872 | CHECK(!init_done_); |
| 873 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 874 | const std::vector<Space*>& spaces = Heap::GetSpaces(); |
| 875 | for (size_t i = 0; i < spaces.size(); i++) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 876 | if (spaces[i]->IsImageSpace()) { |
| 877 | ImageSpace* space = spaces[i]->AsImageSpace(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 878 | OatFile* oat_file = OpenOat(space); |
| 879 | CHECK(oat_file != NULL) << "Failed to open oat file for image"; |
| 880 | Object* dex_caches_object = space->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
| 881 | ObjectArray<DexCache>* dex_caches = dex_caches_object->AsObjectArray<DexCache>(); |
| 882 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 883 | if (i == 0) { |
| 884 | // Special case of setting up the String class early so that we can test arbitrary objects |
| 885 | // as being Strings or not |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 886 | Class* java_lang_String = space->GetImageHeader().GetImageRoot(ImageHeader::kClassRoots) |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 887 | ->AsObjectArray<Class>()->Get(kJavaLangString); |
| 888 | String::SetClass(java_lang_String); |
| 889 | } |
| 890 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 891 | CHECK_EQ(oat_file->GetOatHeader().GetDexFileCount(), |
| 892 | static_cast<uint32_t>(dex_caches->GetLength())); |
| 893 | for (int i = 0; i < dex_caches->GetLength(); i++) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 894 | SirtRef<DexCache> dex_cache(dex_caches->Get(i)); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 895 | const std::string& dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 896 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file_location); |
| 897 | const DexFile* dex_file = oat_dex_file->OpenDexFile(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 898 | if (dex_file == NULL) { |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 899 | LOG(FATAL) << "Failed to open dex file " << dex_file_location |
| 900 | << " from within oat file " << oat_file->GetLocation(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 901 | } |
| 902 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 903 | CHECK_EQ(dex_file->GetHeader().checksum_, oat_dex_file->GetDexFileChecksum()); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 904 | |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 905 | AppendToBootClassPath(*dex_file, dex_cache); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | } |
| 909 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 910 | HeapBitmap* heap_bitmap = Heap::GetLiveBits(); |
| 911 | DCHECK(heap_bitmap != NULL); |
| 912 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 913 | // reinit clases_ table |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 914 | heap_bitmap->Walk(InitFromImageCallback, this); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 915 | |
| 916 | // reinit class_roots_ |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 917 | Object* class_roots_object = |
| 918 | spaces[0]->AsImageSpace()->GetImageHeader().GetImageRoot(ImageHeader::kClassRoots); |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 919 | class_roots_ = class_roots_object->AsObjectArray<Class>(); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 920 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 921 | // reinit array_iftable_ from any array class instance, they should be == |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 922 | array_iftable_ = GetClassRoot(kObjectArrayClass)->GetIfTable(); |
| 923 | DCHECK(array_iftable_ == GetClassRoot(kBooleanArrayClass)->GetIfTable()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 924 | // String class root was set above |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 925 | Field::SetClass(GetClassRoot(kJavaLangReflectField)); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 926 | Method::SetClasses(GetClassRoot(kJavaLangReflectConstructor), GetClassRoot(kJavaLangReflectMethod)); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 927 | BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
| 928 | ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
| 929 | CharArray::SetArrayClass(GetClassRoot(kCharArrayClass)); |
| 930 | DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
| 931 | FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
| 932 | IntArray::SetArrayClass(GetClassRoot(kIntArrayClass)); |
| 933 | LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
| 934 | ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 935 | PathClassLoader::SetClass(GetClassRoot(kDalvikSystemPathClassLoader)); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 936 | StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 937 | |
| 938 | FinishInit(); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 939 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 940 | VLOG(startup) << "ClassLinker::InitFromImage exiting"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 943 | void ClassLinker::InitFromImageCallback(Object* obj, void* arg) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 944 | DCHECK(obj != NULL); |
| 945 | DCHECK(arg != NULL); |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 946 | ClassLinker* class_linker = reinterpret_cast<ClassLinker*>(arg); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 947 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 948 | if (obj->GetClass()->IsStringClass()) { |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 949 | class_linker->intern_table_->RegisterStrong(obj->AsString()); |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 950 | return; |
| 951 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 952 | if (obj->IsClass()) { |
| 953 | // restore class to ClassLinker::classes_ table |
| 954 | Class* klass = obj->AsClass(); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 955 | ClassHelper kh(klass, class_linker); |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 956 | Class* existing = class_linker->InsertClass(kh.GetDescriptor(), klass, true); |
| 957 | DCHECK(existing == NULL) << kh.GetDescriptor(); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 958 | return; |
| 959 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 963 | // reinit references to when reinitializing a ClassLinker from a |
| 964 | // mapped image. |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 965 | void ClassLinker::VisitRoots(Heap::RootVisitor* visitor, void* arg) const { |
| 966 | visitor(class_roots_, arg); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 967 | |
| 968 | for (size_t i = 0; i < dex_caches_.size(); i++) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 969 | visitor(dex_caches_[i], arg); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 970 | } |
| 971 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 972 | { |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 973 | MutexLock mu(classes_lock_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 974 | typedef Table::const_iterator It; // TODO: C++0x auto |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 975 | for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 976 | visitor(it->second, arg); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 977 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 978 | // Note. we deliberately ignore the class roots in the image (held in image_classes_) |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 979 | } |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 980 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 981 | visitor(array_iftable_, arg); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 982 | } |
| 983 | |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 984 | void ClassLinker::VisitClasses(ClassVisitor* visitor, void* arg) const { |
| 985 | MutexLock mu(classes_lock_); |
| 986 | typedef Table::const_iterator It; // TODO: C++0x auto |
| 987 | for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) { |
| 988 | if (!visitor(it->second, arg)) { |
| 989 | return; |
| 990 | } |
| 991 | } |
| 992 | for (It it = image_classes_.begin(), end = image_classes_.end(); it != end; ++it) { |
| 993 | if (!visitor(it->second, arg)) { |
| 994 | return; |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 999 | ClassLinker::~ClassLinker() { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1000 | String::ResetClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1001 | Field::ResetClass(); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 1002 | Method::ResetClasses(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1003 | BooleanArray::ResetArrayClass(); |
| 1004 | ByteArray::ResetArrayClass(); |
| 1005 | CharArray::ResetArrayClass(); |
| 1006 | DoubleArray::ResetArrayClass(); |
| 1007 | FloatArray::ResetArrayClass(); |
| 1008 | IntArray::ResetArrayClass(); |
| 1009 | LongArray::ResetArrayClass(); |
| 1010 | ShortArray::ResetArrayClass(); |
| 1011 | PathClassLoader::ResetClass(); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1012 | StackTraceElement::ResetClass(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1013 | STLDeleteElements(&boot_class_path_); |
| 1014 | STLDeleteElements(&oat_files_); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | DexCache* ClassLinker::AllocDexCache(const DexFile& dex_file) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1018 | SirtRef<DexCache> dex_cache(down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::LengthAsArray()))); |
| 1019 | if (dex_cache.get() == NULL) { |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1020 | return NULL; |
| 1021 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1022 | SirtRef<String> location(intern_table_->InternStrong(dex_file.GetLocation().c_str())); |
| 1023 | if (location.get() == NULL) { |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1024 | return NULL; |
| 1025 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1026 | SirtRef<ObjectArray<String> > strings(AllocObjectArray<String>(dex_file.NumStringIds())); |
| 1027 | if (strings.get() == NULL) { |
| 1028 | return NULL; |
| 1029 | } |
| 1030 | SirtRef<ObjectArray<Class> > types(AllocClassArray(dex_file.NumTypeIds())); |
| 1031 | if (types.get() == NULL) { |
| 1032 | return NULL; |
| 1033 | } |
| 1034 | SirtRef<ObjectArray<Method> > methods(AllocObjectArray<Method>(dex_file.NumMethodIds())); |
| 1035 | if (methods.get() == NULL) { |
| 1036 | return NULL; |
| 1037 | } |
| 1038 | SirtRef<ObjectArray<Field> > fields(AllocObjectArray<Field>(dex_file.NumFieldIds())); |
| 1039 | if (fields.get() == NULL) { |
| 1040 | return NULL; |
| 1041 | } |
| 1042 | SirtRef<CodeAndDirectMethods> code_and_direct_methods(AllocCodeAndDirectMethods(dex_file.NumMethodIds())); |
| 1043 | if (code_and_direct_methods.get() == NULL) { |
| 1044 | return NULL; |
| 1045 | } |
| 1046 | SirtRef<ObjectArray<StaticStorageBase> > initialized_static_storage(AllocObjectArray<StaticStorageBase>(dex_file.NumTypeIds())); |
| 1047 | if (initialized_static_storage.get() == NULL) { |
| 1048 | return NULL; |
| 1049 | } |
| 1050 | |
| 1051 | dex_cache->Init(location.get(), |
| 1052 | strings.get(), |
| 1053 | types.get(), |
| 1054 | methods.get(), |
| 1055 | fields.get(), |
| 1056 | code_and_direct_methods.get(), |
| 1057 | initialized_static_storage.get()); |
| 1058 | return dex_cache.get(); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
Brian Carlstrom | 9cc262e | 2011-08-28 12:45:30 -0700 | [diff] [blame] | 1061 | CodeAndDirectMethods* ClassLinker::AllocCodeAndDirectMethods(size_t length) { |
| 1062 | return down_cast<CodeAndDirectMethods*>(IntArray::Alloc(CodeAndDirectMethods::LengthAsArray(length))); |
Brian Carlstrom | 83db772 | 2011-08-26 17:32:56 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1065 | InterfaceEntry* ClassLinker::AllocInterfaceEntry(Class* interface) { |
| 1066 | DCHECK(interface->IsInterface()); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1067 | SirtRef<ObjectArray<Object> > array(AllocObjectArray<Object>(InterfaceEntry::LengthAsArray())); |
| 1068 | SirtRef<InterfaceEntry> interface_entry(down_cast<InterfaceEntry*>(array.get())); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1069 | interface_entry->SetInterface(interface); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1070 | return interface_entry.get(); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1073 | Class* ClassLinker::AllocClass(Class* java_lang_Class, size_t class_size) { |
| 1074 | DCHECK_GE(class_size, sizeof(Class)); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1075 | SirtRef<Class> klass(Heap::AllocObject(java_lang_Class, class_size)->AsClass()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1076 | klass->SetPrimitiveType(Primitive::kPrimNot); // default to not being primitive |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1077 | klass->SetClassSize(class_size); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1078 | return klass.get(); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1081 | Class* ClassLinker::AllocClass(size_t class_size) { |
| 1082 | return AllocClass(GetClassRoot(kJavaLangClass), class_size); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1085 | Field* ClassLinker::AllocField() { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 1086 | return down_cast<Field*>(GetClassRoot(kJavaLangReflectField)->AllocObject()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | Method* ClassLinker::AllocMethod() { |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 1090 | return down_cast<Method*>(GetClassRoot(kJavaLangReflectMethod)->AllocObject()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1093 | ObjectArray<StackTraceElement>* ClassLinker::AllocStackTraceElementArray(size_t length) { |
| 1094 | return ObjectArray<StackTraceElement>::Alloc( |
| 1095 | GetClassRoot(kJavaLangStackTraceElementArrayClass), |
| 1096 | length); |
| 1097 | } |
| 1098 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1099 | Class* EnsureResolved(Class* klass) { |
| 1100 | DCHECK(klass != NULL); |
| 1101 | // Wait for the class if it has not already been linked. |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 1102 | Thread* self = Thread::Current(); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 1103 | if (!klass->IsResolved() && !klass->IsErroneous()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1104 | ObjectLock lock(klass); |
| 1105 | // Check for circular dependencies between classes. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 1106 | if (!klass->IsResolved() && klass->GetClinitThreadId() == self->GetTid()) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1107 | self->ThrowNewException("Ljava/lang/ClassCircularityError;", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1108 | PrettyDescriptor(klass).c_str()); |
Brian Carlstrom | 4d9716c | 2012-01-30 01:49:33 -0800 | [diff] [blame] | 1109 | klass->SetStatus(Class::kStatusError); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1110 | return NULL; |
| 1111 | } |
| 1112 | // Wait for the pending initialization to complete. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 1113 | while (!klass->IsResolved() && !klass->IsErroneous()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1114 | lock.Wait(); |
| 1115 | } |
| 1116 | } |
| 1117 | if (klass->IsErroneous()) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 1118 | ThrowEarlierClassFailure(klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1119 | return NULL; |
| 1120 | } |
| 1121 | // Return the loaded class. No exceptions should be pending. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1122 | CHECK(klass->IsResolved()) << PrettyClass(klass); |
| 1123 | CHECK(!self->IsExceptionPending()) |
| 1124 | << PrettyClass(klass) << " " << PrettyTypeOf(self->GetException()); |
| 1125 | return klass; |
| 1126 | } |
| 1127 | |
Elliott Hughes | db7d5e9 | 2011-12-16 18:47:37 -0800 | [diff] [blame] | 1128 | Class* ClassLinker::FindSystemClass(const char* descriptor) { |
| 1129 | return FindClass(descriptor, NULL); |
| 1130 | } |
| 1131 | |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 1132 | Class* ClassLinker::FindClass(const char* descriptor, const ClassLoader* class_loader) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 1133 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1134 | Thread* self = Thread::Current(); |
| 1135 | DCHECK(self != NULL); |
| 1136 | CHECK(!self->IsExceptionPending()) << PrettyTypeOf(self->GetException()); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 1137 | if (descriptor[1] == '\0') { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1138 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 1139 | // for primitive classes that aren't backed by dex files. |
| 1140 | return FindPrimitiveClass(descriptor[0]); |
| 1141 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1142 | // Find the class in the loaded classes table. |
| 1143 | Class* klass = LookupClass(descriptor, class_loader); |
| 1144 | if (klass != NULL) { |
| 1145 | return EnsureResolved(klass); |
| 1146 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1147 | // Class is not yet loaded. |
| 1148 | if (descriptor[0] == '[') { |
| 1149 | return CreateArrayClass(descriptor, class_loader); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1150 | |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1151 | } else if (class_loader == NULL) { |
| 1152 | DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, boot_class_path_); |
| 1153 | if (pair.second != NULL) { |
| 1154 | return DefineClass(descriptor, NULL, *pair.first, *pair.second); |
| 1155 | } |
| 1156 | |
| 1157 | } else if (ClassLoader::UseCompileTimeClassPath()) { |
| 1158 | // first try the boot class path |
| 1159 | Class* system_class = FindSystemClass(descriptor); |
| 1160 | if (system_class != NULL) { |
| 1161 | return system_class; |
| 1162 | } |
| 1163 | CHECK(self->IsExceptionPending()); |
| 1164 | self->ClearException(); |
| 1165 | |
| 1166 | // next try the compile time class path |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1167 | const std::vector<const DexFile*>& class_path |
| 1168 | = ClassLoader::GetCompileTimeClassPath(class_loader); |
| 1169 | DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, class_path); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1170 | if (pair.second != NULL) { |
| 1171 | return DefineClass(descriptor, class_loader, *pair.first, *pair.second); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1172 | } |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1173 | |
| 1174 | } else { |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 1175 | std::string class_name_string(DescriptorToDot(descriptor)); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1176 | ScopedThreadStateChange(self, Thread::kNative); |
Elliott Hughes | 748382f | 2012-01-26 18:07:38 -0800 | [diff] [blame] | 1177 | JNIEnv* env = self->GetJniEnv(); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1178 | ScopedLocalRef<jclass> c(env, AddLocalReference<jclass>(env, GetClassRoot(kJavaLangClassLoader))); |
| 1179 | CHECK(c.get() != NULL); |
| 1180 | // TODO: cache method? |
| 1181 | jmethodID mid = env->GetMethodID(c.get(), "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"); |
| 1182 | CHECK(mid != NULL); |
| 1183 | ScopedLocalRef<jobject> class_name_object(env, env->NewStringUTF(class_name_string.c_str())); |
| 1184 | if (class_name_object.get() == NULL) { |
| 1185 | return NULL; |
| 1186 | } |
| 1187 | ScopedLocalRef<jobject> class_loader_object(env, AddLocalReference<jobject>(env, class_loader)); |
Ian Rogers | 761bfa8 | 2012-01-11 10:14:05 -0800 | [diff] [blame] | 1188 | ScopedLocalRef<jobject> result(env, env->CallObjectMethod(class_loader_object.get(), mid, |
| 1189 | class_name_object.get())); |
Brian Carlstrom | 4d9716c | 2012-01-30 01:49:33 -0800 | [diff] [blame] | 1190 | if (env->ExceptionCheck()) { |
Elliott Hughes | 748382f | 2012-01-26 18:07:38 -0800 | [diff] [blame] | 1191 | // If the ClassLoader threw, pass that exception up. |
| 1192 | return NULL; |
Ian Rogers | 761bfa8 | 2012-01-11 10:14:05 -0800 | [diff] [blame] | 1193 | } else if (result.get() == NULL) { |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 1194 | // broken loader - throw NPE to be compatible with Dalvik |
| 1195 | ThrowNullPointerException("ClassLoader.loadClass returned null for %s", |
| 1196 | class_name_string.c_str()); |
| 1197 | return NULL; |
Ian Rogers | 761bfa8 | 2012-01-11 10:14:05 -0800 | [diff] [blame] | 1198 | } else { |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 1199 | // success, return Class* |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 1200 | return Decode<Class*>(env, result.get()); |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 1201 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1202 | } |
| 1203 | |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 1204 | ThrowNoClassDefFoundError("Class %s not found", PrintableString(StringPiece(descriptor)).c_str()); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1205 | return NULL; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 1208 | Class* ClassLinker::DefineClass(const StringPiece& descriptor, |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1209 | const ClassLoader* class_loader, |
| 1210 | const DexFile& dex_file, |
| 1211 | const DexFile::ClassDef& dex_class_def) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1212 | SirtRef<Class> klass(NULL); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1213 | // Load the class from the dex file. |
| 1214 | if (!init_done_) { |
| 1215 | // finish up init of hand crafted class_roots_ |
| 1216 | if (descriptor == "Ljava/lang/Object;") { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1217 | klass.reset(GetClassRoot(kJavaLangObject)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1218 | } else if (descriptor == "Ljava/lang/Class;") { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1219 | klass.reset(GetClassRoot(kJavaLangClass)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1220 | } else if (descriptor == "Ljava/lang/String;") { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1221 | klass.reset(GetClassRoot(kJavaLangString)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1222 | } else if (descriptor == "Ljava/lang/reflect/Constructor;") { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1223 | klass.reset(GetClassRoot(kJavaLangReflectConstructor)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1224 | } else if (descriptor == "Ljava/lang/reflect/Field;") { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1225 | klass.reset(GetClassRoot(kJavaLangReflectField)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1226 | } else if (descriptor == "Ljava/lang/reflect/Method;") { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1227 | klass.reset(GetClassRoot(kJavaLangReflectMethod)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1228 | } else { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1229 | klass.reset(AllocClass(SizeOfClass(dex_file, dex_class_def))); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1230 | } |
| 1231 | } else { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1232 | klass.reset(AllocClass(SizeOfClass(dex_file, dex_class_def))); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1233 | } |
| 1234 | klass->SetDexCache(FindDexCache(dex_file)); |
| 1235 | LoadClass(dex_file, dex_class_def, klass, class_loader); |
| 1236 | // Check for a pending exception during load |
| 1237 | Thread* self = Thread::Current(); |
| 1238 | if (self->IsExceptionPending()) { |
Brian Carlstrom | 4d9716c | 2012-01-30 01:49:33 -0800 | [diff] [blame] | 1239 | klass->SetStatus(Class::kStatusError); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1240 | return NULL; |
| 1241 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1242 | ObjectLock lock(klass.get()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1243 | klass->SetClinitThreadId(self->GetTid()); |
| 1244 | // Add the newly loaded class to the loaded classes table. |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 1245 | Class* existing = InsertClass(descriptor, klass.get(), false); |
| 1246 | if (existing != NULL) { |
| 1247 | // We failed to insert because we raced with another thread. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1248 | klass->SetClinitThreadId(0); |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 1249 | klass.reset(existing); |
| 1250 | return EnsureResolved(klass.get()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1251 | } |
| 1252 | // Finish loading (if necessary) by finding parents |
| 1253 | CHECK(!klass->IsLoaded()); |
| 1254 | if (!LoadSuperAndInterfaces(klass, dex_file)) { |
| 1255 | // Loading failed. |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1256 | klass->SetStatus(Class::kStatusError); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1257 | lock.NotifyAll(); |
| 1258 | return NULL; |
| 1259 | } |
| 1260 | CHECK(klass->IsLoaded()); |
| 1261 | // Link the class (if necessary) |
| 1262 | CHECK(!klass->IsResolved()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 1263 | if (!LinkClass(klass, NULL)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1264 | // Linking failed. |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 1265 | klass->SetStatus(Class::kStatusError); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1266 | lock.NotifyAll(); |
| 1267 | return NULL; |
| 1268 | } |
| 1269 | CHECK(klass->IsResolved()); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1270 | |
| 1271 | /* |
| 1272 | * We send CLASS_PREPARE events to the debugger from here. The |
| 1273 | * definition of "preparation" is creating the static fields for a |
| 1274 | * class and initializing them to the standard default values, but not |
| 1275 | * executing any code (that comes later, during "initialization"). |
| 1276 | * |
| 1277 | * We did the static preparation in LinkClass. |
| 1278 | * |
| 1279 | * The class has been prepared and resolved but possibly not yet verified |
| 1280 | * at this point. |
| 1281 | */ |
| 1282 | Dbg::PostClassPrepare(klass.get()); |
| 1283 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1284 | return klass.get(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1285 | } |
| 1286 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1287 | // Precomputes size that will be needed for Class, matching LinkStaticFields |
| 1288 | size_t ClassLinker::SizeOfClass(const DexFile& dex_file, |
| 1289 | const DexFile::ClassDef& dex_class_def) { |
| 1290 | const byte* class_data = dex_file.GetClassData(dex_class_def); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1291 | size_t num_ref = 0; |
| 1292 | size_t num_32 = 0; |
| 1293 | size_t num_64 = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1294 | if (class_data != NULL) { |
| 1295 | for (ClassDataItemIterator it(dex_file, class_data); it.HasNextStaticField(); it.Next()) { |
| 1296 | const DexFile::FieldId& field_id = dex_file.GetFieldId(it.GetMemberIndex()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1297 | const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1298 | char c = descriptor[0]; |
| 1299 | if (c == 'L' || c == '[') { |
| 1300 | num_ref++; |
| 1301 | } else if (c == 'J' || c == 'D') { |
| 1302 | num_64++; |
| 1303 | } else { |
| 1304 | num_32++; |
| 1305 | } |
| 1306 | } |
| 1307 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1308 | // start with generic class data |
| 1309 | size_t size = sizeof(Class); |
| 1310 | // follow with reference fields which must be contiguous at start |
| 1311 | size += (num_ref * sizeof(uint32_t)); |
| 1312 | // if there are 64-bit fields to add, make sure they are aligned |
| 1313 | if (num_64 != 0 && size != RoundUp(size, 8)) { // for 64-bit alignment |
| 1314 | if (num_32 != 0) { |
| 1315 | // use an available 32-bit field for padding |
| 1316 | num_32--; |
| 1317 | } |
| 1318 | size += sizeof(uint32_t); // either way, we are adding a word |
| 1319 | DCHECK_EQ(size, RoundUp(size, 8)); |
| 1320 | } |
| 1321 | // tack on any 64-bit fields now that alignment is assured |
| 1322 | size += (num_64 * sizeof(uint64_t)); |
| 1323 | // tack on any remaining 32-bit fields |
| 1324 | size += (num_32 * sizeof(uint32_t)); |
| 1325 | return size; |
| 1326 | } |
| 1327 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1328 | void LinkCode(SirtRef<Method>& method, const OatFile::OatClass* oat_class, uint32_t method_index) { |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 1329 | // Every kind of method should at least get an invoke stub from the oat_method. |
| 1330 | // non-abstract methods also get their code pointers. |
| 1331 | const OatFile::OatMethod oat_method = oat_class->GetOatMethod(method_index); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1332 | oat_method.LinkMethodPointers(method.get()); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 1333 | |
| 1334 | if (method->IsAbstract()) { |
| 1335 | method->SetCode(Runtime::Current()->GetAbstractMethodErrorStubArray()->GetData()); |
| 1336 | return; |
| 1337 | } |
| 1338 | if (method->IsNative()) { |
| 1339 | // unregistering restores the dlsym lookup stub |
| 1340 | method->UnregisterNative(); |
jeffhao | 26c0a1a | 2012-01-17 16:28:33 -0800 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | if (Runtime::Current()->IsMethodTracingActive()) { |
| 1344 | #if defined(__arm__) |
| 1345 | Trace* tracer = Runtime::Current()->GetTracer(); |
| 1346 | void* trace_stub = reinterpret_cast<void*>(art_trace_entry_from_code); |
| 1347 | tracer->SaveAndUpdateCode(method.get(), trace_stub); |
| 1348 | #else |
| 1349 | UNIMPLEMENTED(WARNING); |
| 1350 | #endif |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 1351 | } |
| 1352 | } |
| 1353 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1354 | void ClassLinker::LoadClass(const DexFile& dex_file, |
| 1355 | const DexFile::ClassDef& dex_class_def, |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1356 | SirtRef<Class>& klass, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1357 | const ClassLoader* class_loader) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1358 | CHECK(klass.get() != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1359 | CHECK(klass->GetDexCache() != NULL); |
| 1360 | CHECK_EQ(Class::kStatusNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1361 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1362 | CHECK(descriptor != NULL); |
| 1363 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1364 | klass->SetClass(GetClassRoot(kJavaLangClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1365 | uint32_t access_flags = dex_class_def.access_flags_; |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame] | 1366 | // Make sure that none of our runtime-only flags are set. |
| 1367 | CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1368 | klass->SetAccessFlags(access_flags); |
| 1369 | klass->SetClassLoader(class_loader); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 1370 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1371 | klass->SetStatus(Class::kStatusIdx); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1372 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1373 | klass->SetDexTypeIndex(dex_class_def.class_idx_); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1374 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1375 | // Load fields fields. |
| 1376 | const byte* class_data = dex_file.GetClassData(dex_class_def); |
| 1377 | if (class_data == NULL) { |
| 1378 | return; // no fields or methods - for example a marker interface |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1379 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1380 | ClassDataItemIterator it(dex_file, class_data); |
| 1381 | if (it.NumStaticFields() != 0) { |
| 1382 | klass->SetSFields(AllocObjectArray<Field>(it.NumStaticFields())); |
| 1383 | } |
| 1384 | if (it.NumInstanceFields() != 0) { |
| 1385 | klass->SetIFields(AllocObjectArray<Field>(it.NumInstanceFields())); |
| 1386 | } |
| 1387 | for (size_t i = 0; it.HasNextStaticField(); i++, it.Next()) { |
| 1388 | SirtRef<Field> sfield(AllocField()); |
| 1389 | klass->SetStaticField(i, sfield.get()); |
| 1390 | LoadField(dex_file, it, klass, sfield); |
| 1391 | } |
| 1392 | for (size_t i = 0; it.HasNextInstanceField(); i++, it.Next()) { |
| 1393 | SirtRef<Field> ifield(AllocField()); |
| 1394 | klass->SetInstanceField(i, ifield.get()); |
| 1395 | LoadField(dex_file, it, klass, ifield); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1396 | } |
| 1397 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1398 | UniquePtr<const OatFile::OatClass> oat_class; |
| 1399 | if (Runtime::Current()->IsStarted() && !ClassLoader::UseCompileTimeClassPath()) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1400 | const OatFile* oat_file = FindOatFileForDexFile(dex_file); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1401 | if (oat_file != NULL) { |
| 1402 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation()); |
| 1403 | if (oat_dex_file != NULL) { |
| 1404 | uint32_t class_def_index; |
| 1405 | bool found = dex_file.FindClassDefIndex(descriptor, class_def_index); |
| 1406 | CHECK(found) << descriptor; |
| 1407 | oat_class.reset(oat_dex_file->GetOatClass(class_def_index)); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 1408 | CHECK(oat_class.get() != NULL) << descriptor; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1409 | } |
| 1410 | } |
| 1411 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1412 | // Load methods. |
| 1413 | if (it.NumDirectMethods() != 0) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1414 | // TODO: append direct methods to class object |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1415 | klass->SetDirectMethods(AllocObjectArray<Method>(it.NumDirectMethods())); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1416 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1417 | if (it.NumVirtualMethods() != 0) { |
| 1418 | // TODO: append direct methods to class object |
| 1419 | klass->SetVirtualMethods(AllocObjectArray<Method>(it.NumVirtualMethods())); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1420 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1421 | size_t method_index = 0; |
| 1422 | for (size_t i = 0; it.HasNextDirectMethod(); i++, it.Next()) { |
| 1423 | SirtRef<Method> method(AllocMethod()); |
| 1424 | klass->SetDirectMethod(i, method.get()); |
| 1425 | LoadMethod(dex_file, it, klass, method); |
| 1426 | if (oat_class.get() != NULL) { |
| 1427 | LinkCode(method, oat_class.get(), method_index); |
| 1428 | } |
| 1429 | method_index++; |
| 1430 | } |
| 1431 | for (size_t i = 0; it.HasNextVirtualMethod(); i++, it.Next()) { |
| 1432 | SirtRef<Method> method(AllocMethod()); |
| 1433 | klass->SetVirtualMethod(i, method.get()); |
| 1434 | LoadMethod(dex_file, it, klass, method); |
| 1435 | if (oat_class.get() != NULL) { |
| 1436 | LinkCode(method, oat_class.get(), method_index); |
| 1437 | } |
| 1438 | method_index++; |
| 1439 | } |
| 1440 | DCHECK(!it.HasNext()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1441 | } |
| 1442 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1443 | void ClassLinker::LoadField(const DexFile& dex_file, const ClassDataItemIterator& it, |
| 1444 | SirtRef<Class>& klass, SirtRef<Field>& dst) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1445 | uint32_t field_idx = it.GetMemberIndex(); |
| 1446 | dst->SetDexFieldIndex(field_idx); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1447 | dst->SetDeclaringClass(klass.get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1448 | dst->SetAccessFlags(it.GetMemberAccessFlags()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1449 | } |
| 1450 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1451 | void ClassLinker::LoadMethod(const DexFile& dex_file, const ClassDataItemIterator& it, |
| 1452 | SirtRef<Class>& klass, SirtRef<Method>& dst) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1453 | uint32_t method_idx = it.GetMemberIndex(); |
| 1454 | dst->SetDexMethodIndex(method_idx); |
| 1455 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1456 | dst->SetDeclaringClass(klass.get()); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1457 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1458 | |
| 1459 | StringPiece method_name(dex_file.GetMethodName(method_id)); |
| 1460 | if (method_name == "<init>") { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 1461 | dst->SetClass(GetClassRoot(kJavaLangReflectConstructor)); |
| 1462 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1463 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1464 | if (method_name == "finalize") { |
| 1465 | // Create the prototype for a signature of "()V" |
| 1466 | const DexFile::StringId* void_string_id = dex_file.FindStringId("V"); |
| 1467 | if (void_string_id != NULL) { |
| 1468 | const DexFile::TypeId* void_type_id = |
| 1469 | dex_file.FindTypeId(dex_file.GetIndexForStringId(*void_string_id)); |
| 1470 | if (void_type_id != NULL) { |
| 1471 | std::vector<uint16_t> no_args; |
| 1472 | const DexFile::ProtoId* finalizer_proto = |
| 1473 | dex_file.FindProtoId(dex_file.GetIndexForTypeId(*void_type_id), no_args); |
| 1474 | if (finalizer_proto != NULL) { |
| 1475 | // We have the prototype in the dex file |
| 1476 | if (klass->GetClassLoader() != NULL) { // All non-boot finalizer methods are flagged |
| 1477 | klass->SetFinalizable(); |
| 1478 | } else { |
| 1479 | StringPiece klass_descriptor(dex_file.StringByTypeIdx(klass->GetDexTypeIndex())); |
| 1480 | // The Enum class declares a "final" finalize() method to prevent subclasses from |
| 1481 | // introducing a finalizer. We don't want to set the finalizable flag for Enum or its |
| 1482 | // subclasses, so we exclude it here. |
| 1483 | // We also want to avoid setting the flag on Object, where we know that finalize() is |
| 1484 | // empty. |
| 1485 | if (klass_descriptor != "Ljava/lang/Object;" && |
| 1486 | klass_descriptor != "Ljava/lang/Enum;") { |
| 1487 | klass->SetFinalizable(); |
| 1488 | } |
| 1489 | } |
| 1490 | } |
| 1491 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1492 | } |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1493 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1494 | dst->SetCodeItemOffset(it.GetMethodCodeItemOffset()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1495 | dst->SetAccessFlags(it.GetMemberAccessFlags()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1496 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1497 | dst->SetDexCacheStrings(klass->GetDexCache()->GetStrings()); |
| 1498 | dst->SetDexCacheResolvedTypes(klass->GetDexCache()->GetResolvedTypes()); |
| 1499 | dst->SetDexCacheResolvedMethods(klass->GetDexCache()->GetResolvedMethods()); |
| 1500 | dst->SetDexCacheResolvedFields(klass->GetDexCache()->GetResolvedFields()); |
| 1501 | dst->SetDexCacheCodeAndDirectMethods(klass->GetDexCache()->GetCodeAndDirectMethods()); |
| 1502 | dst->SetDexCacheInitializedStaticStorage(klass->GetDexCache()->GetInitializedStaticStorage()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1505 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1506 | SirtRef<DexCache> dex_cache(AllocDexCache(dex_file)); |
| 1507 | AppendToBootClassPath(dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1508 | } |
| 1509 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1510 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, SirtRef<DexCache>& dex_cache) { |
| 1511 | CHECK(dex_cache.get() != NULL) << dex_file.GetLocation(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1512 | boot_class_path_.push_back(&dex_file); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1513 | RegisterDexFile(dex_file, dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1514 | } |
| 1515 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1516 | bool ClassLinker::IsDexFileRegisteredLocked(const DexFile& dex_file) const { |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 1517 | dex_lock_.AssertHeld(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1518 | for (size_t i = 0; i != dex_files_.size(); ++i) { |
| 1519 | if (dex_files_[i] == &dex_file) { |
| 1520 | return true; |
| 1521 | } |
| 1522 | } |
| 1523 | return false; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1524 | } |
| 1525 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1526 | bool ClassLinker::IsDexFileRegistered(const DexFile& dex_file) const { |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 1527 | MutexLock mu(dex_lock_); |
Brian Carlstrom | 0691851 | 2011-10-16 23:39:12 -0700 | [diff] [blame] | 1528 | return IsDexFileRegisteredLocked(dex_file); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1529 | } |
| 1530 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1531 | void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, SirtRef<DexCache>& dex_cache) { |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 1532 | dex_lock_.AssertHeld(); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1533 | CHECK(dex_cache.get() != NULL) << dex_file.GetLocation(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 1534 | CHECK(dex_cache->GetLocation()->Equals(dex_file.GetLocation())); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1535 | dex_files_.push_back(&dex_file); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1536 | dex_caches_.push_back(dex_cache.get()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1537 | } |
| 1538 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1539 | void ClassLinker::RegisterDexFile(const DexFile& dex_file) { |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 1540 | { |
| 1541 | MutexLock mu(dex_lock_); |
| 1542 | if (IsDexFileRegisteredLocked(dex_file)) { |
| 1543 | return; |
| 1544 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1545 | } |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 1546 | // Don't alloc while holding the lock, since allocation may need to |
| 1547 | // suspend all threads and another thread may need the dex_lock_ to |
| 1548 | // get to a suspend point. |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1549 | SirtRef<DexCache> dex_cache(AllocDexCache(dex_file)); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 1550 | { |
| 1551 | MutexLock mu(dex_lock_); |
| 1552 | if (IsDexFileRegisteredLocked(dex_file)) { |
| 1553 | return; |
| 1554 | } |
| 1555 | RegisterDexFileLocked(dex_file, dex_cache); |
| 1556 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1557 | } |
| 1558 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1559 | void ClassLinker::RegisterDexFile(const DexFile& dex_file, SirtRef<DexCache>& dex_cache) { |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 1560 | MutexLock mu(dex_lock_); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1561 | RegisterDexFileLocked(dex_file, dex_cache); |
| 1562 | } |
| 1563 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1564 | const DexFile& ClassLinker::FindDexFile(const DexCache* dex_cache) const { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1565 | CHECK(dex_cache != NULL); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 1566 | MutexLock mu(dex_lock_); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1567 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
| 1568 | if (dex_caches_[i] == dex_cache) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1569 | return *dex_files_[i]; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1570 | } |
| 1571 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 1572 | CHECK(false) << "Failed to find DexFile for DexCache " << dex_cache->GetLocation()->ToModifiedUtf8(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1573 | return *dex_files_[-1]; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1574 | } |
| 1575 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1576 | DexCache* ClassLinker::FindDexCache(const DexFile& dex_file) const { |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 1577 | MutexLock mu(dex_lock_); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1578 | for (size_t i = 0; i != dex_files_.size(); ++i) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1579 | if (dex_files_[i] == &dex_file) { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1580 | return dex_caches_[i]; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1581 | } |
| 1582 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 1583 | CHECK(false) << "Failed to find DexCache for DexFile " << dex_file.GetLocation(); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1584 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1585 | } |
| 1586 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1587 | Class* ClassLinker::InitializePrimitiveClass(Class* primitive_class, |
| 1588 | const char* descriptor, |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1589 | Primitive::Type type) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1590 | // TODO: deduce one argument from the other |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1591 | CHECK(primitive_class != NULL); |
| 1592 | primitive_class->SetAccessFlags(kAccPublic | kAccFinal | kAccAbstract); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1593 | primitive_class->SetPrimitiveType(type); |
| 1594 | primitive_class->SetStatus(Class::kStatusInitialized); |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 1595 | Class* existing = InsertClass(descriptor, primitive_class, false); |
| 1596 | CHECK(existing == NULL) << "InitPrimitiveClass(" << descriptor << ") failed"; |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1597 | return primitive_class; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 1598 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1599 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1600 | // Create an array class (i.e. the class object for the array, not the |
| 1601 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 1602 | // "[Ljava/lang/String;". |
| 1603 | // |
| 1604 | // If "descriptor" refers to an array of primitives, look up the |
| 1605 | // primitive type's internally-generated class object. |
| 1606 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1607 | // "class_loader" is the class loader of the class that's referring to |
| 1608 | // us. It's used to ensure that we're looking for the element type in |
| 1609 | // the right context. It does NOT become the class loader for the |
| 1610 | // array class; that always comes from the base element class. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1611 | // |
| 1612 | // Returns NULL with an exception raised on failure. |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 1613 | Class* ClassLinker::CreateArrayClass(const std::string& descriptor, const ClassLoader* class_loader) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1614 | CHECK_EQ('[', descriptor[0]); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1615 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1616 | // Identify the underlying component type |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 1617 | Class* component_type = FindClass(descriptor.substr(1).c_str(), class_loader); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1618 | if (component_type == NULL) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 1619 | DCHECK(Thread::Current()->IsExceptionPending()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1620 | return NULL; |
| 1621 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1622 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1623 | // See if the component type is already loaded. Array classes are |
| 1624 | // always associated with the class loader of their underlying |
| 1625 | // element type -- an array of Strings goes with the loader for |
| 1626 | // java/lang/String -- so we need to look for it there. (The |
| 1627 | // caller should have checked for the existence of the class |
| 1628 | // before calling here, but they did so with *their* class loader, |
| 1629 | // not the component type's loader.) |
| 1630 | // |
| 1631 | // If we find it, the caller adds "loader" to the class' initiating |
| 1632 | // loader list, which should prevent us from going through this again. |
| 1633 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1634 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1635 | // are the same, because our caller (FindClass) just did the |
| 1636 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 1637 | // because we effectively do this lookup again when we add the new |
| 1638 | // class to the hash table --- necessary because of possible races with |
| 1639 | // other threads.) |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1640 | if (class_loader != component_type->GetClassLoader()) { |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 1641 | Class* new_class = LookupClass(descriptor.c_str(), component_type->GetClassLoader()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1642 | if (new_class != NULL) { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1643 | return new_class; |
| 1644 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1645 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1646 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1647 | // Fill out the fields in the Class. |
| 1648 | // |
| 1649 | // It is possible to execute some methods against arrays, because |
| 1650 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 1651 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 1652 | // |
| 1653 | // Array classes are simple enough that we don't need to do a full |
| 1654 | // link step. |
| 1655 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1656 | SirtRef<Class> new_class(NULL); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1657 | if (!init_done_) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1658 | // Classes that were hand created, ie not by FindSystemClass |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 1659 | if (descriptor == "[Ljava/lang/Class;") { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1660 | new_class.reset(GetClassRoot(kClassArrayClass)); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 1661 | } else if (descriptor == "[Ljava/lang/Object;") { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1662 | new_class.reset(GetClassRoot(kObjectArrayClass)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1663 | } else if (descriptor == "[C") { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1664 | new_class.reset(GetClassRoot(kCharArrayClass)); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 1665 | } else if (descriptor == "[I") { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1666 | new_class.reset(GetClassRoot(kIntArrayClass)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1667 | } |
| 1668 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1669 | if (new_class.get() == NULL) { |
| 1670 | new_class.reset(AllocClass(sizeof(Class))); |
| 1671 | if (new_class.get() == NULL) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1672 | return NULL; |
| 1673 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1674 | new_class->SetComponentType(component_type); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1675 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1676 | DCHECK(new_class->GetComponentType() != NULL); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1677 | Class* java_lang_Object = GetClassRoot(kJavaLangObject); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1678 | new_class->SetSuperClass(java_lang_Object); |
| 1679 | new_class->SetVTable(java_lang_Object->GetVTable()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1680 | new_class->SetPrimitiveType(Primitive::kPrimNot); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1681 | new_class->SetClassLoader(component_type->GetClassLoader()); |
| 1682 | new_class->SetStatus(Class::kStatusInitialized); |
| 1683 | // don't need to set new_class->SetObjectSize(..) |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1684 | // because Object::SizeOf delegates to Array::SizeOf |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1685 | |
| 1686 | |
| 1687 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 1688 | // interfaces. We need to set that up here, so that stuff like |
| 1689 | // "instanceof" works right. |
| 1690 | // |
| 1691 | // Note: The GC could run during the call to FindSystemClass, |
| 1692 | // so we need to make sure the class object is GC-valid while we're in |
| 1693 | // there. Do this by clearing the interface list so the GC will just |
| 1694 | // think that the entries are null. |
| 1695 | |
| 1696 | |
| 1697 | // Use the single, global copies of "interfaces" and "iftable" |
| 1698 | // (remember not to free them for arrays). |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 1699 | CHECK(array_iftable_ != NULL); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1700 | new_class->SetIfTable(array_iftable_); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1701 | |
| 1702 | // Inherit access flags from the component type. Arrays can't be |
| 1703 | // used as a superclass or interface, so we want to add "final" |
| 1704 | // and remove "interface". |
| 1705 | // |
| 1706 | // Don't inherit any non-standard flags (e.g., kAccFinal) |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1707 | // from component_type. We assume that the array class does not |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1708 | // override finalize(). |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1709 | new_class->SetAccessFlags(((new_class->GetComponentType()->GetAccessFlags() & |
| 1710 | ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1711 | |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 1712 | Class* existing = InsertClass(descriptor, new_class.get(), false); |
| 1713 | if (existing == NULL) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1714 | return new_class.get(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1715 | } |
| 1716 | // Another thread must have loaded the class after we |
| 1717 | // started but before we finished. Abandon what we've |
| 1718 | // done. |
| 1719 | // |
| 1720 | // (Yes, this happens.) |
| 1721 | |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 1722 | return existing; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | Class* ClassLinker::FindPrimitiveClass(char type) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1726 | switch (Primitive::GetType(type)) { |
| 1727 | case Primitive::kPrimByte: |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1728 | return GetClassRoot(kPrimitiveByte); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1729 | case Primitive::kPrimChar: |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1730 | return GetClassRoot(kPrimitiveChar); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1731 | case Primitive::kPrimDouble: |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1732 | return GetClassRoot(kPrimitiveDouble); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1733 | case Primitive::kPrimFloat: |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1734 | return GetClassRoot(kPrimitiveFloat); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1735 | case Primitive::kPrimInt: |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1736 | return GetClassRoot(kPrimitiveInt); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1737 | case Primitive::kPrimLong: |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1738 | return GetClassRoot(kPrimitiveLong); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1739 | case Primitive::kPrimShort: |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1740 | return GetClassRoot(kPrimitiveShort); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1741 | case Primitive::kPrimBoolean: |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1742 | return GetClassRoot(kPrimitiveBoolean); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1743 | case Primitive::kPrimVoid: |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1744 | return GetClassRoot(kPrimitiveVoid); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1745 | case Primitive::kPrimNot: |
| 1746 | break; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 1747 | } |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1748 | std::string printable_type(PrintableChar(type)); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 1749 | ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str()); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 1750 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1751 | } |
| 1752 | |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 1753 | Class* ClassLinker::InsertClass(const StringPiece& descriptor, Class* klass, bool image_class) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1754 | if (VLOG_IS_ON(class_linker)) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1755 | DexCache* dex_cache = klass->GetDexCache(); |
| 1756 | std::string source; |
| 1757 | if (dex_cache != NULL) { |
| 1758 | source += " from "; |
| 1759 | source += dex_cache->GetLocation()->ToModifiedUtf8(); |
| 1760 | } |
| 1761 | LOG(INFO) << "Loaded class " << descriptor << source; |
| 1762 | } |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1763 | size_t hash = StringPieceHash()(descriptor); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 1764 | MutexLock mu(classes_lock_); |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 1765 | Table& classes = image_class ? image_classes_ : classes_; |
| 1766 | Class* existing = LookupClass(descriptor.data(), klass->GetClassLoader(), hash, classes); |
| 1767 | #ifndef NDEBUG |
| 1768 | // Check we don't have the class in the other table in error |
| 1769 | Table& other_classes = image_class ? classes_ : image_classes_; |
| 1770 | CHECK(LookupClass(descriptor.data(), klass->GetClassLoader(), hash, other_classes) == NULL); |
| 1771 | #endif |
| 1772 | if (existing != NULL) { |
| 1773 | return existing; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 1774 | } |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 1775 | classes.insert(std::make_pair(hash, klass)); |
| 1776 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1777 | } |
| 1778 | |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 1779 | bool ClassLinker::RemoveClass(const char* descriptor, const ClassLoader* class_loader) { |
| 1780 | size_t hash = Hash(descriptor); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1781 | MutexLock mu(classes_lock_); |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 1782 | typedef Table::iterator It; // TODO: C++0x auto |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1783 | // TODO: determine if its better to search classes_ or image_classes_ first |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1784 | ClassHelper kh; |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 1785 | for (It it = classes_.lower_bound(hash), end = classes_.end(); it != end && it->first == hash; ++it) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1786 | Class* klass = it->second; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1787 | kh.ChangeClass(klass); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 1788 | if (strcmp(kh.GetDescriptor(), descriptor) == 0 && klass->GetClassLoader() == class_loader) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1789 | classes_.erase(it); |
| 1790 | return true; |
| 1791 | } |
| 1792 | } |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 1793 | for (It it = image_classes_.lower_bound(hash), end = classes_.end(); it != end && it->first == hash; ++it) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1794 | Class* klass = it->second; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1795 | kh.ChangeClass(klass); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 1796 | if (strcmp(kh.GetDescriptor(), descriptor) == 0 && klass->GetClassLoader() == class_loader) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1797 | image_classes_.erase(it); |
| 1798 | return true; |
| 1799 | } |
| 1800 | } |
| 1801 | return false; |
| 1802 | } |
| 1803 | |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 1804 | Class* ClassLinker::LookupClass(const char* descriptor, const ClassLoader* class_loader) { |
| 1805 | size_t hash = Hash(descriptor); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 1806 | MutexLock mu(classes_lock_); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 1807 | // TODO: determine if its better to search classes_ or image_classes_ first |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 1808 | Class* klass = LookupClass(descriptor, class_loader, hash, classes_); |
| 1809 | if (klass != NULL) { |
| 1810 | return klass; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1811 | } |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 1812 | return LookupClass(descriptor, class_loader, hash, image_classes_); |
| 1813 | } |
| 1814 | |
| 1815 | Class* ClassLinker::LookupClass(const char* descriptor, const ClassLoader* class_loader, |
| 1816 | size_t hash, const Table& classes) { |
| 1817 | ClassHelper kh(NULL, this); |
| 1818 | typedef Table::const_iterator It; // TODO: C++0x auto |
| 1819 | for (It it = classes.lower_bound(hash), end = classes_.end(); it != end && it->first == hash; ++it) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 1820 | Class* klass = it->second; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1821 | kh.ChangeClass(klass); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 1822 | if (strcmp(descriptor, kh.GetDescriptor()) == 0 && klass->GetClassLoader() == class_loader) { |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 1823 | #ifndef NDEBUG |
| 1824 | for (++it; it != end && it->first == hash; ++it) { |
| 1825 | kh.ChangeClass(it->second); |
| 1826 | CHECK(!(strcmp(descriptor, kh.GetDescriptor()) == 0 && klass->GetClassLoader() == class_loader)) |
| 1827 | << PrettyClass(klass) << " " << klass << " " << klass->GetClassLoader() << " " |
| 1828 | << PrettyClass(it->second) << " " << it->second << " " << it->second->GetClassLoader(); |
| 1829 | } |
| 1830 | #endif |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 1831 | return klass; |
| 1832 | } |
| 1833 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1834 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1835 | } |
| 1836 | |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 1837 | void ClassLinker::LookupClasses(const char* descriptor, std::vector<Class*>& classes) { |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1838 | classes.clear(); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 1839 | size_t hash = Hash(descriptor); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1840 | MutexLock mu(classes_lock_); |
| 1841 | typedef Table::const_iterator It; // TODO: C++0x auto |
| 1842 | // TODO: determine if its better to search classes_ or image_classes_ first |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1843 | ClassHelper kh(NULL, this); |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 1844 | for (It it = classes_.lower_bound(hash), end = classes_.end(); it != end && it->first == hash; ++it) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1845 | Class* klass = it->second; |
| 1846 | kh.ChangeClass(klass); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 1847 | if (strcmp(descriptor, kh.GetDescriptor()) == 0) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1848 | classes.push_back(klass); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1849 | } |
| 1850 | } |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 1851 | for (It it = image_classes_.lower_bound(hash), end = classes_.end(); it != end && it->first == hash; ++it) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1852 | Class* klass = it->second; |
| 1853 | kh.ChangeClass(klass); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 1854 | if (strcmp(descriptor, kh.GetDescriptor()) == 0) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1855 | classes.push_back(klass); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 1856 | } |
| 1857 | } |
| 1858 | } |
| 1859 | |
Ian Rogers | c20a83e | 2012-01-18 18:15:32 -0800 | [diff] [blame] | 1860 | #ifndef NDEBUG |
| 1861 | static void CheckMethodsHaveGcMaps(Class* klass) { |
| 1862 | if (!Runtime::Current()->IsStarted()) { |
| 1863 | return; |
| 1864 | } |
| 1865 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
| 1866 | Method* method = klass->GetDirectMethod(i); |
| 1867 | if (!method->IsNative() && !method->IsAbstract()) { |
| 1868 | CHECK(method->GetGcMap() != NULL) << PrettyMethod(method); |
| 1869 | } |
| 1870 | } |
| 1871 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
| 1872 | Method* method = klass->GetVirtualMethod(i); |
| 1873 | if (!method->IsNative() && !method->IsAbstract()) { |
| 1874 | CHECK(method->GetGcMap() != NULL) << PrettyMethod(method); |
| 1875 | } |
| 1876 | } |
| 1877 | } |
| 1878 | #else |
| 1879 | static void CheckMethodsHaveGcMaps(Class* klass) { |
| 1880 | } |
| 1881 | #endif |
| 1882 | |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 1883 | void ClassLinker::VerifyClass(Class* klass) { |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame^] | 1884 | ObjectLock lock(klass); |
| 1885 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1886 | // TODO: assert that the monitor on the Class is held |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 1887 | if (klass->IsVerified()) { |
| 1888 | return; |
| 1889 | } |
| 1890 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1891 | CHECK_EQ(klass->GetStatus(), Class::kStatusResolved) << PrettyClass(klass); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 1892 | klass->SetStatus(Class::kStatusVerifying); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 1893 | |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 1894 | // Verify super class |
| 1895 | Class* super = klass->GetSuperClass(); |
| 1896 | std::string error_msg; |
| 1897 | if (super != NULL) { |
| 1898 | // Acquire lock to prevent races on verifying the super class |
| 1899 | ObjectLock lock(super); |
| 1900 | |
| 1901 | if (!super->IsVerified() && !super->IsErroneous()) { |
| 1902 | Runtime::Current()->GetClassLinker()->VerifyClass(super); |
| 1903 | } |
| 1904 | if (!super->IsVerified()) { |
| 1905 | error_msg = "Rejecting class "; |
| 1906 | error_msg += PrettyDescriptor(klass); |
| 1907 | error_msg += " that attempts to sub-class erroneous class "; |
| 1908 | error_msg += PrettyDescriptor(super); |
| 1909 | LOG(ERROR) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
| 1910 | Thread* self = Thread::Current(); |
| 1911 | SirtRef<Throwable> cause(self->GetException()); |
| 1912 | if (cause.get() != NULL) { |
| 1913 | self->ClearException(); |
| 1914 | } |
| 1915 | self->ThrowNewException("Ljava/lang/VerifyError;", error_msg.c_str()); |
| 1916 | if (cause.get() != NULL) { |
| 1917 | self->GetException()->SetCause(cause.get()); |
| 1918 | } |
| 1919 | CHECK_EQ(klass->GetStatus(), Class::kStatusVerifying) << PrettyDescriptor(klass); |
| 1920 | klass->SetStatus(Class::kStatusError); |
| 1921 | return; |
| 1922 | } |
| 1923 | } |
| 1924 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1925 | // Try to use verification information from oat file, otherwise do runtime verification |
| 1926 | const DexFile& dex_file = FindDexFile(klass->GetDexCache()); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 1927 | if (VerifyClassUsingOatFile(dex_file, klass) || |
| 1928 | verifier::DexVerifier::VerifyClass(klass, error_msg)) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 1929 | DCHECK(!Thread::Current()->IsExceptionPending()); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1930 | // Make sure all classes referenced by catch blocks are resolved |
| 1931 | ResolveClassExceptionHandlerTypes(dex_file, klass); |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 1932 | klass->SetStatus(Class::kStatusVerified); |
Ian Rogers | c20a83e | 2012-01-18 18:15:32 -0800 | [diff] [blame] | 1933 | // Sanity check that a verified class has GC maps on all methods |
| 1934 | CheckMethodsHaveGcMaps(klass); |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 1935 | } else { |
Ian Rogers | 09f6b56 | 2012-01-31 21:58:52 -0800 | [diff] [blame] | 1936 | LOG(ERROR) << "Verification failed on class " << PrettyDescriptor(klass) |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 1937 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 1938 | << " because: " << error_msg; |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 1939 | Thread* self = Thread::Current(); |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 1940 | CHECK(!self->IsExceptionPending()); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 1941 | self->ThrowNewException("Ljava/lang/VerifyError;", error_msg.c_str()); |
| 1942 | CHECK_EQ(klass->GetStatus(), Class::kStatusVerifying) << PrettyDescriptor(klass); |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 1943 | klass->SetStatus(Class::kStatusError); |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 1944 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 1945 | } |
| 1946 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1947 | bool ClassLinker::VerifyClassUsingOatFile(const DexFile& dex_file, Class* klass) { |
| 1948 | if (!Runtime::Current()->IsStarted()) { |
| 1949 | return false; |
| 1950 | } |
| 1951 | if (ClassLoader::UseCompileTimeClassPath()) { |
| 1952 | return false; |
| 1953 | } |
| 1954 | const OatFile* oat_file = FindOatFileForDexFile(dex_file); |
| 1955 | if (oat_file == NULL) { |
| 1956 | return false; |
| 1957 | } |
| 1958 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation()); |
| 1959 | CHECK(oat_dex_file != NULL) << PrettyClass(klass); |
| 1960 | const char* descriptor = ClassHelper(klass).GetDescriptor(); |
| 1961 | uint32_t class_def_index; |
| 1962 | bool found = dex_file.FindClassDefIndex(descriptor, class_def_index); |
| 1963 | CHECK(found) << descriptor; |
| 1964 | UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index)); |
| 1965 | CHECK(oat_class.get() != NULL) << descriptor; |
| 1966 | Class::Status status = oat_class->GetStatus(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1967 | if (status == Class::kStatusVerified || status == Class::kStatusInitialized) { |
| 1968 | return true; |
| 1969 | } |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 1970 | if (status == Class::kStatusError) { |
| 1971 | // Compile time verification failed. Compile time verification can fail because we have |
| 1972 | // incomplete type information. Consider the following: |
| 1973 | // class ... { |
| 1974 | // Foo x; |
| 1975 | // .... () { |
| 1976 | // if (...) { |
| 1977 | // v1 gets assigned a type of resolved class Foo |
| 1978 | // } else { |
| 1979 | // v1 gets assigned a type of unresolved class Bar |
| 1980 | // } |
| 1981 | // iput x = v1 |
| 1982 | // } } |
| 1983 | // when we merge v1 following the if-the-else it results in Conflict |
| 1984 | // (see verifier::RegType::Merge) as we can't know the type of Bar and we could possibly be |
| 1985 | // allowing an unsafe assignment to the field x in the iput (javac may have compiled this as |
| 1986 | // it knew Bar was a sub-class of Foo, but for us this may have been moved into a separate apk |
| 1987 | // at compile time). |
| 1988 | return false; |
| 1989 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1990 | if (status == Class::kStatusNotReady) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 1991 | // Status is uninitialized if we couldn't determine the status at compile time, for example, |
| 1992 | // not loading the class. |
| 1993 | // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy |
| 1994 | // isn't a problem and this case shouldn't occur |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 1995 | return false; |
| 1996 | } |
| 1997 | LOG(FATAL) << "Unexpected class status: " << status; |
| 1998 | return false; |
| 1999 | } |
| 2000 | |
| 2001 | void ClassLinker::ResolveClassExceptionHandlerTypes(const DexFile& dex_file, Class* klass) { |
| 2002 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
| 2003 | ResolveMethodExceptionHandlerTypes(dex_file, klass->GetDirectMethod(i)); |
| 2004 | } |
| 2005 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
| 2006 | ResolveMethodExceptionHandlerTypes(dex_file, klass->GetVirtualMethod(i)); |
| 2007 | } |
| 2008 | } |
| 2009 | |
| 2010 | void ClassLinker::ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, Method* method) { |
| 2011 | // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod. |
| 2012 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset()); |
| 2013 | if (code_item == NULL) { |
| 2014 | return; // native or abstract method |
| 2015 | } |
| 2016 | if (code_item->tries_size_ == 0) { |
| 2017 | return; // nothing to process |
| 2018 | } |
| 2019 | const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item, 0); |
| 2020 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 2021 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 2022 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 2023 | CatchHandlerIterator iterator(handlers_ptr); |
| 2024 | for (; iterator.HasNext(); iterator.Next()) { |
| 2025 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 2026 | // unresolved exception types will be ignored by exception delivery |
| 2027 | if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) { |
| 2028 | Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method); |
| 2029 | if (exception_type == NULL) { |
| 2030 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 2031 | Thread::Current()->ClearException(); |
| 2032 | } |
| 2033 | } |
| 2034 | } |
| 2035 | handlers_ptr = iterator.EndDataPointer(); |
| 2036 | } |
| 2037 | } |
| 2038 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2039 | static void CheckProxyConstructor(Method* constructor); |
| 2040 | static void CheckProxyMethod(Method* method, SirtRef<Method>& prototype); |
| 2041 | |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2042 | Class* ClassLinker::CreateProxyClass(String* name, ObjectArray<Class>* interfaces, |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2043 | ClassLoader* loader, ObjectArray<Method>* methods, |
| 2044 | ObjectArray<ObjectArray<Class> >* throws) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2045 | SirtRef<Class> klass(AllocClass(GetClassRoot(kJavaLangClass), sizeof(SynthesizedProxyClass))); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2046 | CHECK(klass.get() != NULL); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2047 | DCHECK(klass->GetClass() != NULL); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2048 | klass->SetObjectSize(sizeof(Proxy)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2049 | klass->SetAccessFlags(kAccClassIsProxy | kAccPublic | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2050 | klass->SetClassLoader(loader); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2051 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2052 | klass->SetName(name); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2053 | Class* proxy_class = GetClassRoot(kJavaLangReflectProxy); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2054 | klass->SetDexCache(proxy_class->GetDexCache()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2055 | |
| 2056 | klass->SetStatus(Class::kStatusIdx); |
| 2057 | |
| 2058 | klass->SetDexTypeIndex(DexFile::kDexNoIndex16); |
| 2059 | |
| 2060 | // Create static field that holds throws, instance fields are inherited |
| 2061 | klass->SetSFields(AllocObjectArray<Field>(1)); |
| 2062 | SirtRef<Field> sfield(AllocField()); |
| 2063 | klass->SetStaticField(0, sfield.get()); |
| 2064 | sfield->SetDexFieldIndex(-1); |
| 2065 | sfield->SetDeclaringClass(klass.get()); |
| 2066 | sfield->SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2067 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2068 | // Proxies have 1 direct method, the constructor |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2069 | klass->SetDirectMethods(AllocObjectArray<Method>(1)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2070 | klass->SetDirectMethod(0, CreateProxyConstructor(klass, proxy_class)); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2071 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2072 | // Create virtual method using specified prototypes |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2073 | size_t num_virtual_methods = methods->GetLength(); |
| 2074 | klass->SetVirtualMethods(AllocObjectArray<Method>(num_virtual_methods)); |
| 2075 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2076 | SirtRef<Method> prototype(methods->Get(i)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2077 | klass->SetVirtualMethod(i, CreateProxyMethod(klass, prototype)); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2078 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2079 | |
| 2080 | klass->SetSuperClass(proxy_class); // The super class is java.lang.reflect.Proxy |
| 2081 | klass->SetStatus(Class::kStatusLoaded); // Class is now effectively in the loaded state |
| 2082 | DCHECK(!Thread::Current()->IsExceptionPending()); |
| 2083 | |
| 2084 | // Link the fields and virtual methods, creating vtable and iftables |
| 2085 | if (!LinkClass(klass, interfaces)) { |
Brian Carlstrom | 4d9716c | 2012-01-30 01:49:33 -0800 | [diff] [blame] | 2086 | klass->SetStatus(Class::kStatusError); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2087 | return NULL; |
| 2088 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2089 | sfield->SetObject(NULL, throws); // initialize throws field |
| 2090 | klass->SetStatus(Class::kStatusInitialized); |
| 2091 | |
| 2092 | // sanity checks |
| 2093 | #ifndef NDEBUG |
| 2094 | bool debug = true; |
| 2095 | #else |
| 2096 | bool debug = false; |
| 2097 | #endif |
| 2098 | if (debug) { |
| 2099 | CHECK(klass->GetIFields() == NULL); |
| 2100 | CheckProxyConstructor(klass->GetDirectMethod(0)); |
| 2101 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
| 2102 | SirtRef<Method> prototype(methods->Get(i)); |
| 2103 | CheckProxyMethod(klass->GetVirtualMethod(i), prototype); |
| 2104 | } |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 2105 | std::string throws_field_name("java.lang.Class[][] "); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2106 | throws_field_name += name->ToModifiedUtf8(); |
| 2107 | throws_field_name += ".throws"; |
| 2108 | CHECK(PrettyField(klass->GetStaticField(0)) == throws_field_name); |
| 2109 | |
| 2110 | SynthesizedProxyClass* synth_proxy_class = down_cast<SynthesizedProxyClass*>(klass.get()); |
| 2111 | CHECK_EQ(synth_proxy_class->GetThrows(), throws); |
| 2112 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2113 | return klass.get(); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2114 | } |
| 2115 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2116 | std::string ClassLinker::GetDescriptorForProxy(const Class* proxy_class) { |
| 2117 | DCHECK(proxy_class->IsProxyClass()); |
| 2118 | String* name = proxy_class->GetName(); |
| 2119 | DCHECK(name != NULL); |
| 2120 | return DotToDescriptor(name->ToModifiedUtf8().c_str()); |
| 2121 | } |
| 2122 | |
| 2123 | |
| 2124 | Method* ClassLinker::CreateProxyConstructor(SirtRef<Class>& klass, Class* proxy_class) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2125 | // Create constructor for Proxy that must initialize h |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2126 | ObjectArray<Method>* proxy_direct_methods = proxy_class->GetDirectMethods(); |
Jesse Wilson | ecbce8f | 2011-10-21 19:57:36 -0400 | [diff] [blame] | 2127 | CHECK_EQ(proxy_direct_methods->GetLength(), 15); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2128 | Method* proxy_constructor = proxy_direct_methods->Get(2); |
| 2129 | // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its |
| 2130 | // code_ too) |
| 2131 | Method* constructor = down_cast<Method*>(proxy_constructor->Clone()); |
| 2132 | // Make this constructor public and fix the class to be our Proxy version |
| 2133 | constructor->SetAccessFlags((constructor->GetAccessFlags() & ~kAccProtected) | kAccPublic); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2134 | constructor->SetDeclaringClass(klass.get()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2135 | return constructor; |
| 2136 | } |
| 2137 | |
| 2138 | static void CheckProxyConstructor(Method* constructor) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2139 | CHECK(constructor->IsConstructor()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2140 | MethodHelper mh(constructor); |
| 2141 | CHECK_STREQ(mh.GetName(), "<init>"); |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 2142 | CHECK_EQ(mh.GetSignature(), std::string("(Ljava/lang/reflect/InvocationHandler;)V")); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2143 | DCHECK(constructor->IsPublic()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2144 | } |
| 2145 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2146 | Method* ClassLinker::CreateProxyMethod(SirtRef<Class>& klass, SirtRef<Method>& prototype) { |
| 2147 | // Ensure prototype is in dex cache so that we can use the dex cache to look up the overridden |
| 2148 | // prototype method |
| 2149 | prototype->GetDexCacheResolvedMethods()->Set(prototype->GetDexMethodIndex(), prototype.get()); |
| 2150 | // We steal everything from the prototype (such as DexCache, invoke stub, etc.) then specialize |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2151 | // as necessary |
| 2152 | Method* method = down_cast<Method*>(prototype->Clone()); |
| 2153 | |
| 2154 | // Set class to be the concrete proxy class and clear the abstract flag, modify exceptions to |
| 2155 | // the intersection of throw exceptions as defined in Proxy |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2156 | method->SetDeclaringClass(klass.get()); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2157 | method->SetAccessFlags((method->GetAccessFlags() & ~kAccAbstract) | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2158 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2159 | // At runtime the method looks like a reference and argument saving method, clone the code |
| 2160 | // related parameters from this method. |
| 2161 | Method* refs_and_args = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs); |
| 2162 | method->SetCoreSpillMask(refs_and_args->GetCoreSpillMask()); |
| 2163 | method->SetFpSpillMask(refs_and_args->GetFpSpillMask()); |
| 2164 | method->SetFrameSizeInBytes(refs_and_args->GetFrameSizeInBytes()); |
| 2165 | method->SetCode(reinterpret_cast<void*>(art_proxy_invoke_handler)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2166 | return method; |
| 2167 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2168 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2169 | static void CheckProxyMethod(Method* method, SirtRef<Method>& prototype) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2170 | // Basic sanity |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2171 | CHECK(!prototype->IsFinal()); |
| 2172 | CHECK(method->IsFinal()); |
| 2173 | CHECK(!method->IsAbstract()); |
| 2174 | MethodHelper mh(method); |
| 2175 | const char* method_name = mh.GetName(); |
| 2176 | const char* method_shorty = mh.GetShorty(); |
| 2177 | Class* method_return = mh.GetReturnType(); |
| 2178 | |
| 2179 | mh.ChangeMethod(prototype.get()); |
| 2180 | |
| 2181 | CHECK_STREQ(mh.GetName(), method_name); |
| 2182 | CHECK_STREQ(mh.GetShorty(), method_shorty); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2183 | |
| 2184 | // More complex sanity - via dex cache |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2185 | CHECK_EQ(mh.GetReturnType(), method_return); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2186 | } |
| 2187 | |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 2188 | bool ClassLinker::InitializeClass(Class* klass, bool can_run_clinit) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 2189 | CHECK(klass->IsResolved() || klass->IsErroneous()) |
| 2190 | << PrettyClass(klass) << " is " << klass->GetStatus(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2191 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 2192 | Thread* self = Thread::Current(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2193 | |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 2194 | Method* clinit = NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2195 | { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 2196 | // 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] | 2197 | ObjectLock lock(klass); |
| 2198 | |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 2199 | if (klass->GetStatus() == Class::kStatusInitialized) { |
| 2200 | return true; |
| 2201 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2202 | |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 2203 | if (klass->IsErroneous()) { |
| 2204 | ThrowEarlierClassFailure(klass); |
| 2205 | return false; |
| 2206 | } |
| 2207 | |
| 2208 | if (klass->GetStatus() == Class::kStatusResolved) { |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 2209 | VerifyClass(klass); |
| 2210 | if (klass->GetStatus() != Class::kStatusVerified) { |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 2211 | CHECK(self->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2212 | return false; |
| 2213 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2214 | } |
| 2215 | |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 2216 | clinit = klass->FindDeclaredDirectMethod("<clinit>", "()V"); |
| 2217 | if (clinit != NULL && !can_run_clinit) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 2218 | // if the class has a <clinit> but we can't run it during compilation, |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 2219 | // don't bother going to kStatusInitializing. We return true to maintain |
| 2220 | // the invariant that a false result implies there is a pending exception. |
| 2221 | return true; |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 2222 | } |
| 2223 | |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 2224 | // If the class is kStatusInitializing, either this thread is |
| 2225 | // initializing higher up the stack or another thread has beat us |
| 2226 | // to initializing and we need to wait. Either way, this |
| 2227 | // invocation of InitializeClass will not be responsible for |
| 2228 | // running <clinit> and will return. |
| 2229 | if (klass->GetStatus() == Class::kStatusInitializing) { |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 2230 | // We caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 2231 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 2232 | // Yes. That's fine. Return so we can continue initializing. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2233 | return true; |
| 2234 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 2235 | // No. That's fine. Wait for another thread to finish initializing. |
| 2236 | return WaitForInitializeClass(klass, self, lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | if (!ValidateSuperClassDescriptors(klass)) { |
| 2240 | klass->SetStatus(Class::kStatusError); |
| 2241 | return false; |
| 2242 | } |
| 2243 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 2244 | DCHECK_EQ(klass->GetStatus(), Class::kStatusVerified) << PrettyClass(klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2245 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 2246 | klass->SetClinitThreadId(self->GetTid()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2247 | klass->SetStatus(Class::kStatusInitializing); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2248 | } |
| 2249 | |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 2250 | uint64_t t0 = NanoTime(); |
| 2251 | |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 2252 | if (!InitializeSuperClass(klass, can_run_clinit)) { |
Brian Carlstrom | 4d9716c | 2012-01-30 01:49:33 -0800 | [diff] [blame] | 2253 | CHECK(klass->IsErroneous()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2254 | return false; |
| 2255 | } |
| 2256 | |
| 2257 | InitializeStaticFields(klass); |
| 2258 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2259 | if (clinit != NULL) { |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 2260 | clinit->Invoke(self, NULL, NULL, NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2261 | } |
| 2262 | |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 2263 | uint64_t t1 = NanoTime(); |
| 2264 | |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 2265 | bool success = true; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2266 | { |
| 2267 | ObjectLock lock(klass); |
| 2268 | |
| 2269 | if (self->IsExceptionPending()) { |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 2270 | WrapExceptionInInitializer(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2271 | klass->SetStatus(Class::kStatusError); |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 2272 | success = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2273 | } else { |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 2274 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 2275 | RuntimeStats* thread_stats = self->GetStats(); |
| 2276 | ++global_stats->class_init_count; |
| 2277 | ++thread_stats->class_init_count; |
| 2278 | global_stats->class_init_time_ns += (t1 - t0); |
| 2279 | thread_stats->class_init_time_ns += (t1 - t0); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2280 | klass->SetStatus(Class::kStatusInitialized); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2281 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2282 | ClassHelper kh(klass); |
| 2283 | LOG(INFO) << "Initialized class " << kh.GetDescriptor() << " from " << kh.GetLocation(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 2284 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2285 | } |
| 2286 | lock.NotifyAll(); |
| 2287 | } |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 2288 | return success; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2289 | } |
| 2290 | |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 2291 | bool ClassLinker::WaitForInitializeClass(Class* klass, Thread* self, ObjectLock& lock) { |
| 2292 | while (true) { |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 2293 | CHECK(!self->IsExceptionPending()) << PrettyTypeOf(self->GetException()); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 2294 | lock.Wait(); |
| 2295 | |
| 2296 | // When we wake up, repeat the test for init-in-progress. If |
| 2297 | // there's an exception pending (only possible if |
| 2298 | // "interruptShouldThrow" was set), bail out. |
| 2299 | if (self->IsExceptionPending()) { |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 2300 | WrapExceptionInInitializer(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 2301 | klass->SetStatus(Class::kStatusError); |
| 2302 | return false; |
| 2303 | } |
| 2304 | // Spurious wakeup? Go back to waiting. |
| 2305 | if (klass->GetStatus() == Class::kStatusInitializing) { |
| 2306 | continue; |
| 2307 | } |
| 2308 | if (klass->IsErroneous()) { |
| 2309 | // The caller wants an exception, but it was thrown in a |
| 2310 | // different thread. Synthesize one here. |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 2311 | ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2312 | PrettyDescriptor(klass).c_str()); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 2313 | return false; |
| 2314 | } |
| 2315 | if (klass->IsInitialized()) { |
| 2316 | return true; |
| 2317 | } |
| 2318 | LOG(FATAL) << "Unexpected class status. " << PrettyClass(klass) << " is " << klass->GetStatus(); |
| 2319 | } |
| 2320 | LOG(FATAL) << "Not Reached" << PrettyClass(klass); |
| 2321 | } |
| 2322 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2323 | bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) { |
| 2324 | if (klass->IsInterface()) { |
| 2325 | return true; |
| 2326 | } |
| 2327 | // begin with the methods local to the superclass |
| 2328 | if (klass->HasSuperClass() && |
| 2329 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
| 2330 | const Class* super = klass->GetSuperClass(); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 2331 | for (int i = super->GetVTable()->GetLength() - 1; i >= 0; --i) { |
| 2332 | const Method* method = klass->GetVTable()->Get(i); |
| 2333 | if (method != super->GetVTable()->Get(i) && |
| 2334 | !IsSameMethodSignatureInDifferentClassContexts(method, super, klass)) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2335 | ThrowLinkageError("Class %s method %s resolves differently in superclass %s", |
| 2336 | PrettyDescriptor(klass).c_str(), PrettyMethod(method).c_str(), |
| 2337 | PrettyDescriptor(super).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2338 | return false; |
| 2339 | } |
| 2340 | } |
| 2341 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 2342 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 2343 | InterfaceEntry* interface_entry = klass->GetIfTable()->Get(i); |
| 2344 | Class* interface = interface_entry->GetInterface(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2345 | if (klass->GetClassLoader() != interface->GetClassLoader()) { |
| 2346 | for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) { |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 2347 | const Method* method = interface_entry->GetMethodArray()->Get(j); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 2348 | if (!IsSameMethodSignatureInDifferentClassContexts(method, interface, |
| 2349 | method->GetDeclaringClass())) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2350 | ThrowLinkageError("Class %s method %s resolves differently in interface %s", |
| 2351 | PrettyDescriptor(method->GetDeclaringClass()).c_str(), |
| 2352 | PrettyMethod(method).c_str(), |
| 2353 | PrettyDescriptor(interface).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2354 | return false; |
| 2355 | } |
| 2356 | } |
| 2357 | } |
| 2358 | } |
| 2359 | return true; |
| 2360 | } |
| 2361 | |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 2362 | // Returns true if classes referenced by the signature of the method are the |
| 2363 | // same classes in klass1 as they are in klass2. |
| 2364 | bool ClassLinker::IsSameMethodSignatureInDifferentClassContexts(const Method* method, |
| 2365 | const Class* klass1, |
| 2366 | const Class* klass2) { |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 2367 | if (klass1 == klass2) { |
| 2368 | return true; |
Brian Carlstrom | e10b697 | 2011-09-26 13:49:03 -0700 | [diff] [blame] | 2369 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2370 | const DexFile& dex_file = FindDexFile(method->GetDeclaringClass()->GetDexCache()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2371 | const DexFile::ProtoId& proto_id = |
| 2372 | dex_file.GetMethodPrototype(dex_file.GetMethodId(method->GetDexMethodIndex())); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2373 | for (DexFileParameterIterator it(dex_file, proto_id); it.HasNext(); it.Next()) { |
| 2374 | const char* descriptor = it.GetDescriptor(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2375 | if (descriptor == NULL) { |
| 2376 | break; |
| 2377 | } |
| 2378 | if (descriptor[0] == 'L' || descriptor[0] == '[') { |
| 2379 | // Found a non-primitive type. |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 2380 | if (!IsSameDescriptorInDifferentClassContexts(descriptor, klass1, klass2)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2381 | return false; |
| 2382 | } |
| 2383 | } |
| 2384 | } |
| 2385 | // Check the return type |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2386 | const char* descriptor = dex_file.GetReturnTypeDescriptor(proto_id); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2387 | if (descriptor[0] == 'L' || descriptor[0] == '[') { |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 2388 | if (!IsSameDescriptorInDifferentClassContexts(descriptor, klass1, klass2)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2389 | return false; |
| 2390 | } |
| 2391 | } |
| 2392 | return true; |
| 2393 | } |
| 2394 | |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 2395 | // Returns true if the descriptor resolves to the same class in the context of klass1 and klass2. |
| 2396 | bool ClassLinker::IsSameDescriptorInDifferentClassContexts(const char* descriptor, |
| 2397 | const Class* klass1, |
| 2398 | const Class* klass2) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2399 | CHECK(descriptor != NULL); |
| 2400 | CHECK(klass1 != NULL); |
| 2401 | CHECK(klass2 != NULL); |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 2402 | if (klass1 == klass2) { |
| 2403 | return true; |
| 2404 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2405 | Class* found1 = FindClass(descriptor, klass1->GetClassLoader()); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 2406 | if (found1 == NULL) { |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 2407 | Thread::Current()->ClearException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2408 | } |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 2409 | Class* found2 = FindClass(descriptor, klass2->GetClassLoader()); |
| 2410 | if (found2 == NULL) { |
| 2411 | Thread::Current()->ClearException(); |
| 2412 | } |
| 2413 | return found1 == found2; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2414 | } |
| 2415 | |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 2416 | bool ClassLinker::InitializeSuperClass(Class* klass, bool can_run_clinit) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2417 | CHECK(klass != NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2418 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
| 2419 | Class* super_class = klass->GetSuperClass(); |
| 2420 | if (super_class->GetStatus() != Class::kStatusInitialized) { |
| 2421 | CHECK(!super_class->IsInterface()); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 2422 | Thread* self = Thread::Current(); |
| 2423 | klass->MonitorEnter(self); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 2424 | bool super_initialized = InitializeClass(super_class, can_run_clinit); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 2425 | klass->MonitorExit(self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2426 | // TODO: check for a pending exception |
| 2427 | if (!super_initialized) { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 2428 | if (!can_run_clinit) { |
Brian Carlstrom | 4d9716c | 2012-01-30 01:49:33 -0800 | [diff] [blame] | 2429 | // Don't set status to error when we can't run <clinit>. |
| 2430 | CHECK_EQ(klass->GetStatus(), Class::kStatusInitializing) << PrettyClass(klass); |
| 2431 | klass->SetStatus(Class::kStatusVerified); |
| 2432 | return false; |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 2433 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2434 | klass->SetStatus(Class::kStatusError); |
| 2435 | klass->NotifyAll(); |
| 2436 | return false; |
| 2437 | } |
| 2438 | } |
| 2439 | } |
| 2440 | return true; |
| 2441 | } |
| 2442 | |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 2443 | bool ClassLinker::EnsureInitialized(Class* c, bool can_run_clinit) { |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 2444 | CHECK(c != NULL); |
| 2445 | if (c->IsInitialized()) { |
| 2446 | return true; |
| 2447 | } |
| 2448 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 2449 | Thread* self = Thread::Current(); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 2450 | ScopedThreadStateChange tsc(self, Thread::kRunnable); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 2451 | bool success = InitializeClass(c, can_run_clinit); |
| 2452 | if (!success) { |
Brian Carlstrom | 4d9716c | 2012-01-30 01:49:33 -0800 | [diff] [blame] | 2453 | CHECK(self->IsExceptionPending()) << PrettyClass(c); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 2454 | } |
| 2455 | return success; |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 2456 | } |
| 2457 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 2458 | void ClassLinker::ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def, |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2459 | Class* c, std::map<uint32_t, Field*>& field_map) { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 2460 | const ClassLoader* cl = c->GetClassLoader(); |
| 2461 | const byte* class_data = dex_file.GetClassData(dex_class_def); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2462 | ClassDataItemIterator it(dex_file, class_data); |
| 2463 | for (size_t i = 0; it.HasNextStaticField(); i++, it.Next()) { |
| 2464 | field_map[i] = ResolveField(dex_file, it.GetMemberIndex(), c->GetDexCache(), cl, true); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 2465 | } |
| 2466 | } |
| 2467 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2468 | void ClassLinker::InitializeStaticFields(Class* klass) { |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 2469 | size_t num_static_fields = klass->NumStaticFields(); |
| 2470 | if (num_static_fields == 0) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2471 | return; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 2472 | } |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 2473 | DexCache* dex_cache = klass->GetDexCache(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2474 | // 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] | 2475 | if (dex_cache == NULL) { |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 2476 | return; |
| 2477 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2478 | ClassHelper kh(klass); |
| 2479 | const DexFile::ClassDef* dex_class_def = kh.GetClassDef(); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 2480 | CHECK(dex_class_def != NULL); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2481 | const DexFile& dex_file = kh.GetDexFile(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2482 | EncodedStaticFieldValueIterator it(dex_file, dex_cache, this, *dex_class_def); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 2483 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2484 | if (it.HasNext()) { |
| 2485 | // We reordered the fields, so we need to be able to map the field indexes to the right fields. |
| 2486 | std::map<uint32_t, Field*> field_map; |
| 2487 | ConstructFieldMap(dex_file, *dex_class_def, klass, field_map); |
| 2488 | for (size_t i = 0; it.HasNext(); i++, it.Next()) { |
| 2489 | it.ReadValueToField(field_map[i]); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 2490 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2491 | } |
| 2492 | } |
| 2493 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2494 | bool ClassLinker::LinkClass(SirtRef<Class>& klass, ObjectArray<Class>* interfaces) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2495 | CHECK_EQ(Class::kStatusLoaded, klass->GetStatus()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2496 | if (!LinkSuperClass(klass)) { |
| 2497 | return false; |
| 2498 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2499 | if (!LinkMethods(klass, interfaces)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2500 | return false; |
| 2501 | } |
| 2502 | if (!LinkInstanceFields(klass)) { |
| 2503 | return false; |
| 2504 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2505 | if (!LinkStaticFields(klass)) { |
| 2506 | return false; |
| 2507 | } |
| 2508 | CreateReferenceInstanceOffsets(klass); |
| 2509 | CreateReferenceStaticOffsets(klass); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2510 | CHECK_EQ(Class::kStatusLoaded, klass->GetStatus()); |
| 2511 | klass->SetStatus(Class::kStatusResolved); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2512 | return true; |
| 2513 | } |
| 2514 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2515 | bool ClassLinker::LoadSuperAndInterfaces(SirtRef<Class>& klass, const DexFile& dex_file) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2516 | CHECK_EQ(Class::kStatusIdx, klass->GetStatus()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2517 | StringPiece descriptor(dex_file.StringByTypeIdx(klass->GetDexTypeIndex())); |
| 2518 | const DexFile::ClassDef* class_def = dex_file.FindClassDef(descriptor); |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 2519 | CHECK(class_def != NULL); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2520 | uint16_t super_class_idx = class_def->superclass_idx_; |
| 2521 | if (super_class_idx != DexFile::kDexNoIndex16) { |
| 2522 | Class* super_class = ResolveType(dex_file, super_class_idx, klass.get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2523 | if (super_class == NULL) { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 2524 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2525 | return false; |
| 2526 | } |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 2527 | // Verify |
| 2528 | if (!klass->CanAccess(super_class)) { |
| 2529 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;", |
| 2530 | "Class %s extended by class %s is inaccessible", |
| 2531 | PrettyDescriptor(super_class).c_str(), |
| 2532 | PrettyDescriptor(klass.get()).c_str()); |
| 2533 | return false; |
| 2534 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2535 | klass->SetSuperClass(super_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2536 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2537 | const DexFile::TypeList* interfaces = dex_file.GetInterfacesList(*class_def); |
| 2538 | if (interfaces != NULL) { |
| 2539 | for (size_t i = 0; i < interfaces->Size(); i++) { |
| 2540 | uint16_t idx = interfaces->GetTypeItem(i).type_idx_; |
| 2541 | Class* interface = ResolveType(dex_file, idx, klass.get()); |
| 2542 | if (interface == NULL) { |
| 2543 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 2544 | return false; |
| 2545 | } |
| 2546 | // Verify |
| 2547 | if (!klass->CanAccess(interface)) { |
| 2548 | // TODO: the RI seemed to ignore this in my testing. |
| 2549 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;", |
| 2550 | "Interface %s implemented by class %s is inaccessible", |
| 2551 | PrettyDescriptor(interface).c_str(), |
| 2552 | PrettyDescriptor(klass.get()).c_str()); |
| 2553 | return false; |
| 2554 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2555 | } |
| 2556 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2557 | // Mark the class as loaded. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2558 | klass->SetStatus(Class::kStatusLoaded); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2559 | return true; |
| 2560 | } |
| 2561 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2562 | bool ClassLinker::LinkSuperClass(SirtRef<Class>& klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2563 | CHECK(!klass->IsPrimitive()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2564 | Class* super = klass->GetSuperClass(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2565 | if (klass.get() == GetClassRoot(kJavaLangObject)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2566 | if (super != NULL) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 2567 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassFormatError;", |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2568 | "java.lang.Object must not have a superclass"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2569 | return false; |
| 2570 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2571 | return true; |
| 2572 | } |
| 2573 | if (super == NULL) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2574 | ThrowLinkageError("No superclass defined for class %s", PrettyDescriptor(klass.get()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2575 | return false; |
| 2576 | } |
| 2577 | // Verify |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2578 | if (super->IsFinal() || super->IsInterface()) { |
Brian Carlstrom | 4d9716c | 2012-01-30 01:49:33 -0800 | [diff] [blame] | 2579 | Thread* self = Thread::Current(); |
| 2580 | self->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;", |
Elliott Hughes | e555dc0 | 2011-09-25 10:46:35 -0700 | [diff] [blame] | 2581 | "Superclass %s of %s is %s", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2582 | PrettyDescriptor(super).c_str(), |
| 2583 | PrettyDescriptor(klass.get()).c_str(), |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2584 | super->IsFinal() ? "declared final" : "an interface"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2585 | return false; |
| 2586 | } |
| 2587 | if (!klass->CanAccess(super)) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 2588 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;", |
Elliott Hughes | e555dc0 | 2011-09-25 10:46:35 -0700 | [diff] [blame] | 2589 | "Superclass %s is inaccessible by %s", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2590 | PrettyDescriptor(super).c_str(), |
| 2591 | PrettyDescriptor(klass.get()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2592 | return false; |
| 2593 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 2594 | |
| 2595 | // Inherit kAccClassIsFinalizable from the superclass in case this class doesn't override finalize. |
| 2596 | if (super->IsFinalizable()) { |
| 2597 | klass->SetFinalizable(); |
| 2598 | } |
| 2599 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 2600 | // Inherit reference flags (if any) from the superclass. |
| 2601 | int reference_flags = (super->GetAccessFlags() & kAccReferenceFlagsMask); |
| 2602 | if (reference_flags != 0) { |
| 2603 | klass->SetAccessFlags(klass->GetAccessFlags() | reference_flags); |
| 2604 | } |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 2605 | // Disallow custom direct subclasses of java.lang.ref.Reference. |
Elliott Hughes | bf61ba3 | 2011-10-11 10:53:09 -0700 | [diff] [blame] | 2606 | if (init_done_ && super == GetClassRoot(kJavaLangRefReference)) { |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 2607 | ThrowLinkageError("Class %s attempts to subclass java.lang.ref.Reference, which is not allowed", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2608 | PrettyDescriptor(klass.get()).c_str()); |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 2609 | return false; |
| 2610 | } |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 2611 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2612 | #ifndef NDEBUG |
| 2613 | // Ensure super classes are fully resolved prior to resolving fields.. |
| 2614 | while (super != NULL) { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 2615 | CHECK(super->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2616 | super = super->GetSuperClass(); |
| 2617 | } |
| 2618 | #endif |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2619 | return true; |
| 2620 | } |
| 2621 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2622 | // Populate the class vtable and itable. Compute return type indices. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2623 | bool ClassLinker::LinkMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2624 | if (klass->IsInterface()) { |
| 2625 | // No vtable. |
| 2626 | size_t count = klass->NumVirtualMethods(); |
| 2627 | if (!IsUint(16, count)) { |
Elliott Hughes | 92cb498 | 2011-12-16 16:57:28 -0800 | [diff] [blame] | 2628 | ThrowClassFormatError("Too many methods on interface: %zd", count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2629 | return false; |
| 2630 | } |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 2631 | for (size_t i = 0; i < count; ++i) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2632 | klass->GetVirtualMethodDuringLinking(i)->SetMethodIndex(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2633 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 2634 | // Link interface method tables |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2635 | return LinkInterfaceMethods(klass, interfaces); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2636 | } else { |
Elliott Hughes | bc258fa | 2011-10-06 14:45:21 -0700 | [diff] [blame] | 2637 | // Link virtual and interface method tables |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2638 | return LinkVirtualMethods(klass) && LinkInterfaceMethods(klass, interfaces); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2639 | } |
| 2640 | return true; |
| 2641 | } |
| 2642 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2643 | bool ClassLinker::LinkVirtualMethods(SirtRef<Class>& klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2644 | if (klass->HasSuperClass()) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2645 | uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->GetVTable()->GetLength(); |
| 2646 | size_t actual_count = klass->GetSuperClass()->GetVTable()->GetLength(); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 2647 | CHECK_LE(actual_count, max_count); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 2648 | // TODO: do not assign to the vtable field until it is fully constructed. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2649 | SirtRef<ObjectArray<Method> > vtable(klass->GetSuperClass()->GetVTable()->CopyOf(max_count)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2650 | // See if any of our virtual methods override the superclass. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2651 | MethodHelper local_mh(NULL, this); |
| 2652 | MethodHelper super_mh(NULL, this); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2653 | for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2654 | Method* local_method = klass->GetVirtualMethodDuringLinking(i); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2655 | local_mh.ChangeMethod(local_method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2656 | size_t j = 0; |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 2657 | for (; j < actual_count; ++j) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2658 | Method* super_method = vtable->Get(j); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2659 | super_mh.ChangeMethod(super_method); |
| 2660 | if (local_mh.HasSameNameAndSignature(&super_mh)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2661 | // Verify |
| 2662 | if (super_method->IsFinal()) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2663 | MethodHelper mh(local_method); |
Elliott Hughes | e555dc0 | 2011-09-25 10:46:35 -0700 | [diff] [blame] | 2664 | ThrowLinkageError("Method %s.%s overrides final method in class %s", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2665 | PrettyDescriptor(klass.get()).c_str(), |
| 2666 | mh.GetName(), mh.GetDeclaringClassDescriptor()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2667 | return false; |
| 2668 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2669 | vtable->Set(j, local_method); |
| 2670 | local_method->SetMethodIndex(j); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2671 | break; |
| 2672 | } |
| 2673 | } |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 2674 | if (j == actual_count) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2675 | // Not overriding, append. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2676 | vtable->Set(actual_count, local_method); |
| 2677 | local_method->SetMethodIndex(actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2678 | actual_count += 1; |
| 2679 | } |
| 2680 | } |
| 2681 | if (!IsUint(16, actual_count)) { |
Elliott Hughes | 92cb498 | 2011-12-16 16:57:28 -0800 | [diff] [blame] | 2682 | ThrowClassFormatError("Too many methods defined on class: %zd", actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2683 | return false; |
| 2684 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2685 | // Shrink vtable if possible |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2686 | CHECK_LE(actual_count, max_count); |
| 2687 | if (actual_count < max_count) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2688 | vtable.reset(vtable->CopyOf(actual_count)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2689 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2690 | klass->SetVTable(vtable.get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2691 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2692 | CHECK(klass.get() == GetClassRoot(kJavaLangObject)); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 2693 | uint32_t num_virtual_methods = klass->NumVirtualMethods(); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 2694 | if (!IsUint(16, num_virtual_methods)) { |
Elliott Hughes | e555dc0 | 2011-09-25 10:46:35 -0700 | [diff] [blame] | 2695 | ThrowClassFormatError("Too many methods: %d", num_virtual_methods); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2696 | return false; |
| 2697 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2698 | SirtRef<ObjectArray<Method> > vtable(AllocObjectArray<Method>(num_virtual_methods)); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 2699 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2700 | Method* virtual_method = klass->GetVirtualMethodDuringLinking(i); |
| 2701 | vtable->Set(i, virtual_method); |
| 2702 | virtual_method->SetMethodIndex(i & 0xFFFF); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2703 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2704 | klass->SetVTable(vtable.get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2705 | } |
| 2706 | return true; |
| 2707 | } |
| 2708 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2709 | bool ClassLinker::LinkInterfaceMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2710 | size_t super_ifcount; |
| 2711 | if (klass->HasSuperClass()) { |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 2712 | super_ifcount = klass->GetSuperClass()->GetIfTableCount(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2713 | } else { |
| 2714 | super_ifcount = 0; |
| 2715 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 2716 | size_t ifcount = super_ifcount; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2717 | ClassHelper kh(klass.get(), this); |
| 2718 | uint32_t num_interfaces = interfaces == NULL ? kh.NumInterfaces() : interfaces->GetLength(); |
| 2719 | ifcount += num_interfaces; |
| 2720 | for (size_t i = 0; i < num_interfaces; i++) { |
| 2721 | Class* interface = interfaces == NULL ? kh.GetInterface(i) : interfaces->Get(i); |
| 2722 | ifcount += interface->GetIfTableCount(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2723 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 2724 | if (ifcount == 0) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2725 | // TODO: enable these asserts with klass status validation |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 2726 | // DCHECK_EQ(klass->GetIfTableCount(), 0); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 2727 | // DCHECK(klass->GetIfTable() == NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2728 | return true; |
| 2729 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2730 | SirtRef<ObjectArray<InterfaceEntry> > iftable(AllocObjectArray<InterfaceEntry>(ifcount)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2731 | if (super_ifcount != 0) { |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 2732 | ObjectArray<InterfaceEntry>* super_iftable = klass->GetSuperClass()->GetIfTable(); |
| 2733 | for (size_t i = 0; i < super_ifcount; i++) { |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 2734 | Class* super_interface = super_iftable->Get(i)->GetInterface(); |
| 2735 | iftable->Set(i, AllocInterfaceEntry(super_interface)); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 2736 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2737 | } |
| 2738 | // Flatten the interface inheritance hierarchy. |
| 2739 | size_t idx = super_ifcount; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2740 | for (size_t i = 0; i < num_interfaces; i++) { |
| 2741 | Class* interface = interfaces == NULL ? kh.GetInterface(i) : interfaces->Get(i); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 2742 | DCHECK(interface != NULL); |
| 2743 | if (!interface->IsInterface()) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2744 | ClassHelper ih(interface); |
Brian Carlstrom | 4d9716c | 2012-01-30 01:49:33 -0800 | [diff] [blame] | 2745 | Thread* self = Thread::Current(); |
| 2746 | self->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;", |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2747 | "Class %s implements non-interface class %s", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2748 | PrettyDescriptor(klass.get()).c_str(), |
| 2749 | PrettyDescriptor(ih.GetDescriptor()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2750 | return false; |
| 2751 | } |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 2752 | // Check if interface is already in iftable |
| 2753 | bool duplicate = false; |
| 2754 | for (size_t j = 0; j < idx; j++) { |
| 2755 | Class* existing_interface = iftable->Get(j)->GetInterface(); |
| 2756 | if (existing_interface == interface) { |
| 2757 | duplicate = true; |
| 2758 | break; |
| 2759 | } |
| 2760 | } |
| 2761 | if (!duplicate) { |
| 2762 | // Add this non-duplicate interface. |
| 2763 | iftable->Set(idx++, AllocInterfaceEntry(interface)); |
| 2764 | // Add this interface's non-duplicate super-interfaces. |
| 2765 | for (int32_t j = 0; j < interface->GetIfTableCount(); j++) { |
| 2766 | Class* super_interface = interface->GetIfTable()->Get(j)->GetInterface(); |
| 2767 | bool super_duplicate = false; |
| 2768 | for (size_t k = 0; k < idx; k++) { |
| 2769 | Class* existing_interface = iftable->Get(k)->GetInterface(); |
| 2770 | if (existing_interface == super_interface) { |
| 2771 | super_duplicate = true; |
| 2772 | break; |
| 2773 | } |
| 2774 | } |
| 2775 | if (!super_duplicate) { |
| 2776 | iftable->Set(idx++, AllocInterfaceEntry(super_interface)); |
| 2777 | } |
| 2778 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2779 | } |
| 2780 | } |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 2781 | // Shrink iftable in case duplicates were found |
| 2782 | if (idx < ifcount) { |
| 2783 | iftable.reset(iftable->CopyOf(idx)); |
| 2784 | ifcount = idx; |
| 2785 | } else { |
| 2786 | CHECK_EQ(idx, ifcount); |
| 2787 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2788 | klass->SetIfTable(iftable.get()); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 2789 | |
| 2790 | // If we're an interface, we don't need the vtable pointers, so we're done. |
| 2791 | if (klass->IsInterface() /*|| super_ifcount == ifcount*/) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2792 | return true; |
| 2793 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2794 | std::vector<Method*> miranda_list; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2795 | MethodHelper vtable_mh(NULL, this); |
| 2796 | MethodHelper interface_mh(NULL, this); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 2797 | for (size_t i = 0; i < ifcount; ++i) { |
| 2798 | InterfaceEntry* interface_entry = iftable->Get(i); |
| 2799 | Class* interface = interface_entry->GetInterface(); |
| 2800 | ObjectArray<Method>* method_array = AllocObjectArray<Method>(interface->NumVirtualMethods()); |
| 2801 | interface_entry->SetMethodArray(method_array); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2802 | ObjectArray<Method>* vtable = klass->GetVTableDuringLinking(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2803 | for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) { |
| 2804 | Method* interface_method = interface->GetVirtualMethod(j); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2805 | interface_mh.ChangeMethod(interface_method); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 2806 | int32_t k; |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 2807 | // For each method listed in the interface's method list, find the |
| 2808 | // matching method in our class's method list. We want to favor the |
| 2809 | // subclass over the superclass, which just requires walking |
| 2810 | // back from the end of the vtable. (This only matters if the |
| 2811 | // superclass defines a private method and this class redefines |
| 2812 | // it -- otherwise it would use the same vtable slot. In .dex files |
| 2813 | // those don't end up in the virtual method table, so it shouldn't |
| 2814 | // matter which direction we go. We walk it backward anyway.) |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2815 | for (k = vtable->GetLength() - 1; k >= 0; --k) { |
| 2816 | Method* vtable_method = vtable->Get(k); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2817 | vtable_mh.ChangeMethod(vtable_method); |
| 2818 | if (interface_mh.HasSameNameAndSignature(&vtable_mh)) { |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 2819 | if (!vtable_method->IsPublic()) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 2820 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;", |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2821 | "Implementation not public: %s", PrettyMethod(vtable_method).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2822 | return false; |
| 2823 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 2824 | method_array->Set(j, vtable_method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2825 | break; |
| 2826 | } |
| 2827 | } |
| 2828 | if (k < 0) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2829 | SirtRef<Method> miranda_method(NULL); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 2830 | for (size_t mir = 0; mir < miranda_list.size(); mir++) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2831 | Method* mir_method = miranda_list[mir]; |
| 2832 | vtable_mh.ChangeMethod(mir_method); |
| 2833 | if (interface_mh.HasSameNameAndSignature(&vtable_mh)) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2834 | miranda_method.reset(miranda_list[mir]); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2835 | break; |
| 2836 | } |
| 2837 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2838 | if (miranda_method.get() == NULL) { |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 2839 | // point the interface table at a phantom slot |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2840 | miranda_method.reset(AllocMethod()); |
| 2841 | memcpy(miranda_method.get(), interface_method, sizeof(Method)); |
| 2842 | miranda_list.push_back(miranda_method.get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2843 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2844 | method_array->Set(j, miranda_method.get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2845 | } |
| 2846 | } |
| 2847 | } |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 2848 | if (!miranda_list.empty()) { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 2849 | int old_method_count = klass->NumVirtualMethods(); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 2850 | int new_method_count = old_method_count + miranda_list.size(); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 2851 | klass->SetVirtualMethods((old_method_count == 0) |
| 2852 | ? AllocObjectArray<Method>(new_method_count) |
| 2853 | : klass->GetVirtualMethods()->CopyOf(new_method_count)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2854 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2855 | SirtRef<ObjectArray<Method> > vtable(klass->GetVTableDuringLinking()); |
| 2856 | CHECK(vtable.get() != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2857 | int old_vtable_count = vtable->GetLength(); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 2858 | int new_vtable_count = old_vtable_count + miranda_list.size(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2859 | vtable.reset(vtable->CopyOf(new_vtable_count)); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 2860 | for (size_t i = 0; i < miranda_list.size(); ++i) { |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2861 | Method* method = miranda_list[i]; |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 2862 | // Leave the declaring class alone as type indices are relative to it |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2863 | method->SetAccessFlags(method->GetAccessFlags() | kAccMiranda); |
| 2864 | method->SetMethodIndex(0xFFFF & (old_vtable_count + i)); |
| 2865 | klass->SetVirtualMethod(old_method_count + i, method); |
| 2866 | vtable->Set(old_vtable_count + i, method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2867 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2868 | // TODO: do not assign to the vtable field until it is fully constructed. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 2869 | klass->SetVTable(vtable.get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2870 | } |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 2871 | |
| 2872 | ObjectArray<Method>* vtable = klass->GetVTableDuringLinking(); |
| 2873 | for (int i = 0; i < vtable->GetLength(); ++i) { |
| 2874 | CHECK(vtable->Get(i) != NULL); |
| 2875 | } |
| 2876 | |
| 2877 | // klass->DumpClass(std::cerr, Class::kDumpClassFullDetail); |
| 2878 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2879 | return true; |
| 2880 | } |
| 2881 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2882 | bool ClassLinker::LinkInstanceFields(SirtRef<Class>& klass) { |
| 2883 | CHECK(klass.get() != NULL); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 2884 | return LinkFields(klass, false); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2885 | } |
| 2886 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2887 | bool ClassLinker::LinkStaticFields(SirtRef<Class>& klass) { |
| 2888 | CHECK(klass.get() != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2889 | size_t allocated_class_size = klass->GetClassSize(); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 2890 | bool success = LinkFields(klass, true); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2891 | CHECK_EQ(allocated_class_size, klass->GetClassSize()); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2892 | return success; |
| 2893 | } |
| 2894 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2895 | struct LinkFieldsComparator { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 2896 | explicit LinkFieldsComparator(FieldHelper* fh) : fh_(fh) {} |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 2897 | bool operator()(const Field* field1, const Field* field2) { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2898 | // First come reference fields, then 64-bit, and finally 32-bit |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2899 | fh_->ChangeField(field1); |
| 2900 | Primitive::Type type1 = fh_->GetTypeAsPrimitiveType(); |
| 2901 | fh_->ChangeField(field2); |
| 2902 | Primitive::Type type2 = fh_->GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2903 | bool isPrimitive1 = type1 != Primitive::kPrimNot; |
| 2904 | bool isPrimitive2 = type2 != Primitive::kPrimNot; |
| 2905 | bool is64bit1 = isPrimitive1 && (type1 == Primitive::kPrimLong || type1 == Primitive::kPrimDouble); |
| 2906 | bool is64bit2 = isPrimitive2 && (type2 == Primitive::kPrimLong || type2 == Primitive::kPrimDouble); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2907 | int order1 = (!isPrimitive1 ? 0 : (is64bit1 ? 1 : 2)); |
| 2908 | int order2 = (!isPrimitive2 ? 0 : (is64bit2 ? 1 : 2)); |
| 2909 | if (order1 != order2) { |
| 2910 | return order1 < order2; |
| 2911 | } |
| 2912 | |
| 2913 | // same basic group? then sort by string. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2914 | fh_->ChangeField(field1); |
| 2915 | StringPiece name1(fh_->GetName()); |
| 2916 | fh_->ChangeField(field2); |
| 2917 | StringPiece name2(fh_->GetName()); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2918 | return name1 < name2; |
| 2919 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2920 | |
| 2921 | FieldHelper* fh_; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2922 | }; |
| 2923 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2924 | bool ClassLinker::LinkFields(SirtRef<Class>& klass, bool is_static) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2925 | size_t num_fields = |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 2926 | is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2927 | |
| 2928 | ObjectArray<Field>* fields = |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 2929 | is_static ? klass->GetSFields() : klass->GetIFields(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2930 | |
| 2931 | // Initialize size and field_offset |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 2932 | size_t size; |
| 2933 | MemberOffset field_offset(0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 2934 | if (is_static) { |
| 2935 | size = klass->GetClassSize(); |
| 2936 | field_offset = Class::FieldsOffset(); |
| 2937 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2938 | Class* super_class = klass->GetSuperClass(); |
| 2939 | if (super_class != NULL) { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 2940 | CHECK(super_class->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2941 | field_offset = MemberOffset(super_class->GetObjectSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2942 | } |
| 2943 | size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2944 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2945 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2946 | CHECK_EQ(num_fields == 0, fields == NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2947 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2948 | // we want a relatively stable order so that adding new fields |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 2949 | // minimizes disruption of C++ version such as Class and Method. |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2950 | std::deque<Field*> grouped_and_sorted_fields; |
| 2951 | for (size_t i = 0; i < num_fields; i++) { |
| 2952 | grouped_and_sorted_fields.push_back(fields->Get(i)); |
| 2953 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2954 | FieldHelper fh(NULL, this); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2955 | std::sort(grouped_and_sorted_fields.begin(), |
| 2956 | grouped_and_sorted_fields.end(), |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2957 | LinkFieldsComparator(&fh)); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2958 | |
| 2959 | // References should be at the front. |
| 2960 | size_t current_field = 0; |
| 2961 | size_t num_reference_fields = 0; |
| 2962 | for (; current_field < num_fields; current_field++) { |
| 2963 | Field* field = grouped_and_sorted_fields.front(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2964 | fh.ChangeField(field); |
| 2965 | Primitive::Type type = fh.GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2966 | bool isPrimitive = type != Primitive::kPrimNot; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2967 | if (isPrimitive) { |
| 2968 | break; // past last reference, move on to the next phase |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2969 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2970 | grouped_and_sorted_fields.pop_front(); |
| 2971 | num_reference_fields++; |
| 2972 | fields->Set(current_field, field); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2973 | field->SetOffset(field_offset); |
| 2974 | field_offset = MemberOffset(field_offset.Uint32Value() + sizeof(uint32_t)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2975 | } |
| 2976 | |
| 2977 | // Now we want to pack all of the double-wide fields together. If |
| 2978 | // we're not aligned, though, we want to shuffle one 32-bit field |
| 2979 | // 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] | 2980 | if (current_field != num_fields && !IsAligned<8>(field_offset.Uint32Value())) { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2981 | for (size_t i = 0; i < grouped_and_sorted_fields.size(); i++) { |
| 2982 | Field* field = grouped_and_sorted_fields[i]; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2983 | fh.ChangeField(field); |
| 2984 | Primitive::Type type = fh.GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2985 | CHECK(type != Primitive::kPrimNot); // should only be working on primitive types |
| 2986 | if (type == Primitive::kPrimLong || type == Primitive::kPrimDouble) { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2987 | continue; |
| 2988 | } |
| 2989 | fields->Set(current_field++, field); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2990 | field->SetOffset(field_offset); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2991 | // drop the consumed field |
| 2992 | grouped_and_sorted_fields.erase(grouped_and_sorted_fields.begin() + i); |
| 2993 | break; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2994 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 2995 | // whether we found a 32-bit field for padding or not, we advance |
| 2996 | field_offset = MemberOffset(field_offset.Uint32Value() + sizeof(uint32_t)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | // Alignment is good, shuffle any double-wide fields forward, and |
| 3000 | // finish assigning field offsets to all fields. |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 3001 | DCHECK(current_field == num_fields || IsAligned<8>(field_offset.Uint32Value())); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3002 | while (!grouped_and_sorted_fields.empty()) { |
| 3003 | Field* field = grouped_and_sorted_fields.front(); |
| 3004 | grouped_and_sorted_fields.pop_front(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3005 | fh.ChangeField(field); |
| 3006 | Primitive::Type type = fh.GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3007 | CHECK(type != Primitive::kPrimNot); // should only be working on primitive types |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3008 | fields->Set(current_field, field); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3009 | field->SetOffset(field_offset); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3010 | field_offset = MemberOffset(field_offset.Uint32Value() + |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3011 | ((type == Primitive::kPrimLong || type == Primitive::kPrimDouble) |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3012 | ? sizeof(uint64_t) |
| 3013 | : sizeof(uint32_t))); |
| 3014 | current_field++; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3015 | } |
| 3016 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 3017 | // We lie to the GC about the java.lang.ref.Reference.referent field, so it doesn't scan it. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3018 | std::string descriptor(ClassHelper(klass.get(), this).GetDescriptor()); |
| 3019 | if (!is_static && descriptor == "Ljava/lang/ref/Reference;") { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 3020 | // We know there are no non-reference fields in the Reference classes, and we know |
| 3021 | // that 'referent' is alphabetically last, so this is easy... |
| 3022 | CHECK_EQ(num_reference_fields, num_fields); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3023 | fh.ChangeField(fields->Get(num_fields - 1)); |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 3024 | CHECK_STREQ(fh.GetName(), "referent"); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 3025 | --num_reference_fields; |
| 3026 | } |
| 3027 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3028 | #ifndef NDEBUG |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3029 | // Make sure that all reference fields appear before |
| 3030 | // non-reference fields, and all double-wide fields are aligned. |
| 3031 | bool seen_non_ref = false; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3032 | for (size_t i = 0; i < num_fields; i++) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3033 | Field* field = fields->Get(i); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3034 | if (false) { // enable to debug field layout |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 3035 | LOG(INFO) << "LinkFields: " << (is_static ? "static" : "instance") |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 3036 | << " class=" << PrettyClass(klass.get()) |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 3037 | << " field=" << PrettyField(field) |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3038 | << " offset=" << field->GetField32(MemberOffset(Field::OffsetOffset()), false); |
| 3039 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3040 | fh.ChangeField(field); |
| 3041 | Primitive::Type type = fh.GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3042 | bool is_primitive = type != Primitive::kPrimNot; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3043 | if (descriptor == "Ljava/lang/ref/Reference;" && StringPiece(fh.GetName()) == "referent") { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 3044 | is_primitive = true; // We lied above, so we have to expect a lie here. |
| 3045 | } |
| 3046 | if (is_primitive) { |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3047 | if (!seen_non_ref) { |
| 3048 | seen_non_ref = true; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3049 | DCHECK_EQ(num_reference_fields, i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3050 | } |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3051 | } else { |
| 3052 | DCHECK(!seen_non_ref); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3053 | } |
| 3054 | } |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3055 | if (!seen_non_ref) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3056 | DCHECK_EQ(num_fields, num_reference_fields); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3057 | } |
| 3058 | #endif |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3059 | size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3060 | // Update klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 3061 | if (is_static) { |
| 3062 | klass->SetNumReferenceStaticFields(num_reference_fields); |
| 3063 | klass->SetClassSize(size); |
| 3064 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3065 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3066 | if (!klass->IsVariableSize()) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3067 | klass->SetObjectSize(size); |
| 3068 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3069 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3070 | return true; |
| 3071 | } |
| 3072 | |
| 3073 | // Set the bitmap of reference offsets, refOffsets, from the ifields |
| 3074 | // list. |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 3075 | void ClassLinker::CreateReferenceInstanceOffsets(SirtRef<Class>& klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3076 | uint32_t reference_offsets = 0; |
| 3077 | Class* super_class = klass->GetSuperClass(); |
| 3078 | if (super_class != NULL) { |
| 3079 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3080 | // If our superclass overflowed, we don't stand a chance. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3081 | if (reference_offsets == CLASS_WALK_SUPER) { |
| 3082 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3083 | return; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3084 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3085 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 3086 | CreateReferenceOffsets(klass, false, reference_offsets); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3087 | } |
| 3088 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 3089 | void ClassLinker::CreateReferenceStaticOffsets(SirtRef<Class>& klass) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 3090 | CreateReferenceOffsets(klass, true, 0); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3091 | } |
| 3092 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 3093 | void ClassLinker::CreateReferenceOffsets(SirtRef<Class>& klass, bool is_static, |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3094 | uint32_t reference_offsets) { |
| 3095 | size_t num_reference_fields = |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 3096 | is_static ? klass->NumReferenceStaticFieldsDuringLinking() |
| 3097 | : klass->NumReferenceInstanceFieldsDuringLinking(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3098 | const ObjectArray<Field>* fields = |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 3099 | is_static ? klass->GetSFields() : klass->GetIFields(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3100 | // All of the fields that contain object references are guaranteed |
| 3101 | // to be at the beginning of the fields list. |
| 3102 | for (size_t i = 0; i < num_reference_fields; ++i) { |
| 3103 | // Note that byte_offset is the offset from the beginning of |
| 3104 | // object, not the offset into instance data |
| 3105 | const Field* field = fields->Get(i); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3106 | MemberOffset byte_offset = field->GetOffsetDuringLinking(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3107 | CHECK_EQ(byte_offset.Uint32Value() & (CLASS_OFFSET_ALIGNMENT - 1), 0U); |
| 3108 | if (CLASS_CAN_ENCODE_OFFSET(byte_offset.Uint32Value())) { |
| 3109 | uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset.Uint32Value()); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3110 | CHECK_NE(new_bit, 0U); |
| 3111 | reference_offsets |= new_bit; |
| 3112 | } else { |
| 3113 | reference_offsets = CLASS_WALK_SUPER; |
| 3114 | break; |
| 3115 | } |
| 3116 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3117 | // Update fields in klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 3118 | if (is_static) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3119 | klass->SetReferenceStaticOffsets(reference_offsets); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 3120 | } else { |
| 3121 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3122 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3123 | } |
| 3124 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3125 | String* ClassLinker::ResolveString(const DexFile& dex_file, |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 3126 | uint32_t string_idx, DexCache* dex_cache) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3127 | String* resolved = dex_cache->GetResolvedString(string_idx); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3128 | if (resolved != NULL) { |
| 3129 | return resolved; |
| 3130 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 3131 | const DexFile::StringId& string_id = dex_file.GetStringId(string_idx); |
| 3132 | int32_t utf16_length = dex_file.GetStringLength(string_id); |
| 3133 | const char* utf8_data = dex_file.GetStringData(string_id); |
Brian Carlstrom | 928bf02 | 2011-10-11 02:48:14 -0700 | [diff] [blame] | 3134 | String* string = intern_table_->InternStrong(utf16_length, utf8_data); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 3135 | dex_cache->SetResolvedString(string_idx, string); |
| 3136 | return string; |
| 3137 | } |
| 3138 | |
| 3139 | Class* ClassLinker::ResolveType(const DexFile& dex_file, |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3140 | uint16_t type_idx, |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 3141 | DexCache* dex_cache, |
| 3142 | const ClassLoader* class_loader) { |
| 3143 | Class* resolved = dex_cache->GetResolvedType(type_idx); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3144 | if (resolved == NULL) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3145 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3146 | resolved = FindClass(descriptor, class_loader); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3147 | if (resolved != NULL) { |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 3148 | // TODO: we used to throw here if resolved's class loader was not the |
| 3149 | // boot class loader. This was to permit different classes with the |
| 3150 | // same name to be loaded simultaneously by different loaders |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3151 | dex_cache->SetResolvedType(type_idx, resolved); |
| 3152 | } else { |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 3153 | CHECK(Thread::Current()->IsExceptionPending()) |
| 3154 | << "Expected pending exception for failed resolution of: " << descriptor; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3155 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3156 | } |
| 3157 | return resolved; |
| 3158 | } |
| 3159 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 3160 | Method* ClassLinker::ResolveMethod(const DexFile& dex_file, |
| 3161 | uint32_t method_idx, |
| 3162 | DexCache* dex_cache, |
| 3163 | const ClassLoader* class_loader, |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 3164 | bool is_direct) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 3165 | Method* resolved = dex_cache->GetResolvedMethod(method_idx); |
| 3166 | if (resolved != NULL) { |
| 3167 | return resolved; |
| 3168 | } |
| 3169 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
| 3170 | Class* klass = ResolveType(dex_file, method_id.class_idx_, dex_cache, class_loader); |
| 3171 | if (klass == NULL) { |
Elliott Hughes | cc5f9a9 | 2011-09-28 19:17:29 -0700 | [diff] [blame] | 3172 | DCHECK(Thread::Current()->IsExceptionPending()); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 3173 | return NULL; |
| 3174 | } |
| 3175 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3176 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 3177 | std::string signature(dex_file.CreateMethodSignature(method_id.proto_idx_, NULL)); |
Brian Carlstrom | 7540ff4 | 2011-09-04 16:38:46 -0700 | [diff] [blame] | 3178 | if (is_direct) { |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 3179 | resolved = klass->FindDirectMethod(name, signature); |
Brian Carlstrom | 7540ff4 | 2011-09-04 16:38:46 -0700 | [diff] [blame] | 3180 | } else if (klass->IsInterface()) { |
| 3181 | resolved = klass->FindInterfaceMethod(name, signature); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 3182 | } else { |
| 3183 | resolved = klass->FindVirtualMethod(name, signature); |
| 3184 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 3185 | if (resolved != NULL) { |
| 3186 | dex_cache->SetResolvedMethod(method_idx, resolved); |
| 3187 | } else { |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 3188 | ThrowNoSuchMethodError(is_direct, klass, name, signature); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 3189 | } |
| 3190 | return resolved; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3191 | } |
| 3192 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 3193 | Field* ClassLinker::ResolveField(const DexFile& dex_file, |
| 3194 | uint32_t field_idx, |
| 3195 | DexCache* dex_cache, |
| 3196 | const ClassLoader* class_loader, |
| 3197 | bool is_static) { |
| 3198 | Field* resolved = dex_cache->GetResolvedField(field_idx); |
| 3199 | if (resolved != NULL) { |
| 3200 | return resolved; |
| 3201 | } |
| 3202 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
| 3203 | Class* klass = ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader); |
| 3204 | if (klass == NULL) { |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 3205 | DCHECK(Thread::Current()->IsExceptionPending()); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 3206 | return NULL; |
| 3207 | } |
| 3208 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3209 | const char* name = dex_file.GetFieldName(field_id); |
| 3210 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 3211 | if (is_static) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3212 | resolved = klass->FindStaticField(name, type); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 3213 | } else { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3214 | resolved = klass->FindInstanceField(name, type); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 3215 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 3216 | if (resolved != NULL) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 3217 | dex_cache->SetResolvedField(field_idx, resolved); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 3218 | } else { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 3219 | ThrowNoSuchFieldError(is_static ? "static " : "instance ", klass, type, name); |
| 3220 | } |
| 3221 | return resolved; |
| 3222 | } |
| 3223 | |
| 3224 | Field* ClassLinker::ResolveFieldJLS(const DexFile& dex_file, |
| 3225 | uint32_t field_idx, |
| 3226 | DexCache* dex_cache, |
| 3227 | const ClassLoader* class_loader) { |
| 3228 | Field* resolved = dex_cache->GetResolvedField(field_idx); |
| 3229 | if (resolved != NULL) { |
| 3230 | return resolved; |
| 3231 | } |
| 3232 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
| 3233 | Class* klass = ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader); |
| 3234 | if (klass == NULL) { |
| 3235 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 3236 | return NULL; |
| 3237 | } |
| 3238 | |
| 3239 | const char* name = dex_file.GetFieldName(field_id); |
| 3240 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 3241 | resolved = klass->FindField(name, type); |
| 3242 | if (resolved != NULL) { |
| 3243 | dex_cache->SetResolvedField(field_idx, resolved); |
| 3244 | } else { |
| 3245 | ThrowNoSuchFieldError("", klass, type, name); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 3246 | } |
| 3247 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 3248 | } |
| 3249 | |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 3250 | const char* ClassLinker::MethodShorty(uint32_t method_idx, Method* referrer) { |
| 3251 | Class* declaring_class = referrer->GetDeclaringClass(); |
| 3252 | DexCache* dex_cache = declaring_class->GetDexCache(); |
| 3253 | const DexFile& dex_file = FindDexFile(dex_cache); |
| 3254 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
| 3255 | return dex_file.GetShorty(method_id.proto_idx_); |
| 3256 | } |
| 3257 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 3258 | void ClassLinker::DumpAllClasses(int flags) const { |
| 3259 | // TODO: at the time this was written, it wasn't safe to call PrettyField with the ClassLinker |
| 3260 | // lock held, because it might need to resolve a field's type, which would try to take the lock. |
| 3261 | std::vector<Class*> all_classes; |
| 3262 | { |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3263 | MutexLock mu(classes_lock_); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 3264 | typedef Table::const_iterator It; // TODO: C++0x auto |
| 3265 | for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) { |
| 3266 | all_classes.push_back(it->second); |
| 3267 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 3268 | for (It it = image_classes_.begin(), end = image_classes_.end(); it != end; ++it) { |
| 3269 | all_classes.push_back(it->second); |
| 3270 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 3271 | } |
| 3272 | |
| 3273 | for (size_t i = 0; i < all_classes.size(); ++i) { |
| 3274 | all_classes[i]->DumpClass(std::cerr, flags); |
| 3275 | } |
| 3276 | } |
| 3277 | |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 3278 | void ClassLinker::DumpForSigQuit(std::ostream& os) const { |
| 3279 | MutexLock mu(classes_lock_); |
| 3280 | os << "Loaded classes: " << image_classes_.size() << " image classes; " |
| 3281 | << classes_.size() << " allocated classes\n"; |
| 3282 | } |
| 3283 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 3284 | size_t ClassLinker::NumLoadedClasses() const { |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3285 | MutexLock mu(classes_lock_); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 3286 | return classes_.size() + image_classes_.size(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 3287 | } |
| 3288 | |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3289 | pid_t ClassLinker::GetClassesLockOwner() { |
| 3290 | return classes_lock_.GetOwner(); |
| 3291 | } |
| 3292 | |
| 3293 | pid_t ClassLinker::GetDexLockOwner() { |
| 3294 | return dex_lock_.GetOwner(); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 3295 | } |
| 3296 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3297 | void ClassLinker::SetClassRoot(ClassRoot class_root, Class* klass) { |
| 3298 | DCHECK(!init_done_); |
| 3299 | |
| 3300 | DCHECK(klass != NULL); |
| 3301 | DCHECK(klass->GetClassLoader() == NULL); |
| 3302 | |
| 3303 | DCHECK(class_roots_ != NULL); |
| 3304 | DCHECK(class_roots_->Get(class_root) == NULL); |
| 3305 | class_roots_->Set(class_root, klass); |
| 3306 | } |
| 3307 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3308 | } // namespace art |