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