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