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