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