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