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