blob: ca8a53287a77475d007e0f898a442aac2e2bc1a0 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "class_linker.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Brian Carlstromd601af82012-01-06 10:15:19 -080019#include <fcntl.h>
20#include <sys/file.h>
21#include <sys/stat.h>
Brian Carlstromdbf05b72011-12-15 00:55:24 -080022#include <sys/types.h>
23#include <sys/wait.h>
24
Brian Carlstromdbc05252011-09-09 01:59:59 -070025#include <deque>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070026#include <string>
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070027#include <utility>
Elliott Hughes90a33692011-08-30 13:27:07 -070028#include <vector>
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070029
Elliott Hughes1aa246d2012-12-13 09:29:36 -080030#include "base/casts.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080031#include "base/logging.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080032#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080033#include "base/unix_file/fd_file.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070034#include "class_loader.h"
Elliott Hughes4740cdf2011-12-07 14:07:12 -080035#include "debugger.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070036#include "dex_cache.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070037#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070038#include "heap.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070039#include "intern_table.h"
Ian Rogers64b6d142012-10-29 16:34:15 -070040#include "interpreter/interpreter.h"
Ian Rogers0571d352011-11-03 19:51:38 -070041#include "leb128.h"
Brian Carlstrom58ae9412011-10-04 00:56:06 -070042#include "oat_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070043#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080044#include "object_utils.h"
Brian Carlstrom5b332c82012-02-01 15:02:31 -080045#include "os.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070046#include "runtime.h"
Ian Rogers466bb252011-10-14 03:29:56 -070047#include "runtime_support.h"
TDYa1275bb86012012-04-11 05:57:28 -070048#if defined(ART_USE_LLVM_COMPILER)
49#include "compiler_llvm/runtime_support_llvm.h"
50#endif
Elliott Hughes4d0207c2011-10-03 19:14:34 -070051#include "ScopedLocalRef.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070052#include "scoped_thread_state_change.h"
Ian Rogers1f539342012-10-03 21:09:42 -070053#include "sirt_ref.h"
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070054#include "gc/space.h"
55#include "gc/space_bitmap.h"
Brian Carlstrom40381fb2011-10-19 14:13:40 -070056#include "stack_indirect_reference_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070057#include "thread.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070058#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070059#include "utils.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070060#include "well_known_classes.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070061
62namespace art {
63
Ian Rogers00f7d0e2012-07-19 15:28:27 -070064static void ThrowNoClassDefFoundError(const char* fmt, ...)
65 __attribute__((__format__(__printf__, 1, 2)))
Ian Rogersb726dcb2012-09-05 08:57:23 -070066 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes0512f022012-03-15 22:10:52 -070067static void ThrowNoClassDefFoundError(const char* fmt, ...) {
Elliott Hughes4a2b4172011-09-20 17:08:25 -070068 va_list args;
69 va_start(args, fmt);
70 Thread::Current()->ThrowNewExceptionV("Ljava/lang/NoClassDefFoundError;", fmt, args);
71 va_end(args);
72}
73
Ian Rogers00f7d0e2012-07-19 15:28:27 -070074static void ThrowClassFormatError(const char* fmt, ...)
75 __attribute__((__format__(__printf__, 1, 2)))
Ian Rogersb726dcb2012-09-05 08:57:23 -070076 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes0512f022012-03-15 22:10:52 -070077static void ThrowClassFormatError(const char* fmt, ...) {
Elliott Hughes4a2b4172011-09-20 17:08:25 -070078 va_list args;
79 va_start(args, fmt);
Elliott Hughese555dc02011-09-25 10:46:35 -070080 Thread::Current()->ThrowNewExceptionV("Ljava/lang/ClassFormatError;", fmt, args);
Elliott Hughes4a2b4172011-09-20 17:08:25 -070081 va_end(args);
82}
83
Ian Rogers00f7d0e2012-07-19 15:28:27 -070084static void ThrowLinkageError(const char* fmt, ...)
85 __attribute__((__format__(__printf__, 1, 2)))
Ian Rogersb726dcb2012-09-05 08:57:23 -070086 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes0512f022012-03-15 22:10:52 -070087static void ThrowLinkageError(const char* fmt, ...) {
Elliott Hughes4a2b4172011-09-20 17:08:25 -070088 va_list args;
89 va_start(args, fmt);
90 Thread::Current()->ThrowNewExceptionV("Ljava/lang/LinkageError;", fmt, args);
91 va_end(args);
92}
93
Elliott Hughes0512f022012-03-15 22:10:52 -070094static void ThrowNoSuchFieldError(const StringPiece& scope, Class* c, const StringPiece& type,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070095 const StringPiece& name)
Ian Rogersb726dcb2012-09-05 08:57:23 -070096 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers9f1ab122011-12-12 08:52:43 -080097 ClassHelper kh(c);
98 std::ostringstream msg;
Ian Rogers08f753d2012-08-24 14:35:25 -070099 msg << "No " << scope << "field " << name << " of type " << type
Ian Rogers9f1ab122011-12-12 08:52:43 -0800100 << " in class " << kh.GetDescriptor() << " or its superclasses";
101 std::string location(kh.GetLocation());
102 if (!location.empty()) {
103 msg << " (defined in " << location << ")";
104 }
105 Thread::Current()->ThrowNewException("Ljava/lang/NoSuchFieldError;", msg.str().c_str());
106}
107
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700108static void ThrowNullPointerException(const char* fmt, ...)
109 __attribute__((__format__(__printf__, 1, 2)))
Ian Rogersb726dcb2012-09-05 08:57:23 -0700110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes0512f022012-03-15 22:10:52 -0700111static void ThrowNullPointerException(const char* fmt, ...) {
Ian Rogerscab01012012-01-10 17:35:46 -0800112 va_list args;
113 va_start(args, fmt);
114 Thread::Current()->ThrowNewExceptionV("Ljava/lang/NullPointerException;", fmt, args);
115 va_end(args);
116}
117
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700118static void ThrowEarlierClassFailure(Class* c)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700119 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes5c599942012-06-13 16:45:05 -0700120 // The class failed to initialize on a previous attempt, so we want to throw
121 // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we
122 // failed in verification, in which case v2 5.4.1 says we need to re-throw
123 // the previous error.
Ian Rogers87e552d2012-08-31 15:54:48 -0700124 if (!Runtime::Current()->IsCompiler()) { // Give info if this occurs at runtime.
125 LOG(INFO) << "Rejecting re-init on previously-failed class " << PrettyClass(c);
126 }
Elliott Hughes4a2b4172011-09-20 17:08:25 -0700127
Elliott Hughes5c599942012-06-13 16:45:05 -0700128 CHECK(c->IsErroneous()) << PrettyClass(c) << " " << c->GetStatus();
Elliott Hughes4a2b4172011-09-20 17:08:25 -0700129 if (c->GetVerifyErrorClass() != NULL) {
130 // TODO: change the verifier to store an _instance_, with a useful detail message?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800131 ClassHelper ve_ch(c->GetVerifyErrorClass());
132 std::string error_descriptor(ve_ch.GetDescriptor());
133 Thread::Current()->ThrowNewException(error_descriptor.c_str(), PrettyDescriptor(c).c_str());
Elliott Hughes4a2b4172011-09-20 17:08:25 -0700134 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800135 ThrowNoClassDefFoundError("%s", PrettyDescriptor(c).c_str());
Elliott Hughes4a2b4172011-09-20 17:08:25 -0700136 }
137}
138
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700139static void WrapExceptionInInitializer()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700140 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughesa4f94742012-05-29 16:28:38 -0700141 Thread* self = Thread::Current();
142 JNIEnv* env = self->GetJniEnv();
Elliott Hughes4d0207c2011-10-03 19:14:34 -0700143
144 ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred());
145 CHECK(cause.get() != NULL);
146
147 env->ExceptionClear();
Elliott Hughesa4f94742012-05-29 16:28:38 -0700148 bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error);
149 env->Throw(cause.get());
Elliott Hughes4d0207c2011-10-03 19:14:34 -0700150
Elliott Hughesa4f94742012-05-29 16:28:38 -0700151 // We only wrap non-Error exceptions; an Error can just be used as-is.
152 if (!is_error) {
153 self->ThrowNewWrappedException("Ljava/lang/ExceptionInInitializerError;", NULL);
Elliott Hughes4d0207c2011-10-03 19:14:34 -0700154 }
Elliott Hughes4d0207c2011-10-03 19:14:34 -0700155}
156
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800157static size_t Hash(const char* s) {
158 // This is the java.lang.String hashcode for convenience, not interoperability.
159 size_t hash = 0;
160 for (; *s != '\0'; ++s) {
161 hash = hash * 31 + *s;
162 }
163 return hash;
164}
165
Elliott Hughes418d20f2011-09-22 14:00:39 -0700166const char* ClassLinker::class_roots_descriptors_[] = {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700167 "Ljava/lang/Class;",
168 "Ljava/lang/Object;",
Elliott Hughes418d20f2011-09-22 14:00:39 -0700169 "[Ljava/lang/Class;",
Brian Carlstroma663ea52011-08-19 23:33:41 -0700170 "[Ljava/lang/Object;",
171 "Ljava/lang/String;",
Mathieu Chartier66f19252012-09-18 08:57:04 -0700172 "Ljava/lang/DexCache;",
Elliott Hughesbf61ba32011-10-11 10:53:09 -0700173 "Ljava/lang/ref/Reference;",
Elliott Hughes80609252011-09-23 17:24:51 -0700174 "Ljava/lang/reflect/Constructor;",
Brian Carlstroma663ea52011-08-19 23:33:41 -0700175 "Ljava/lang/reflect/Field;",
Mathieu Chartier66f19252012-09-18 08:57:04 -0700176 "Ljava/lang/reflect/AbstractMethod;",
Brian Carlstroma663ea52011-08-19 23:33:41 -0700177 "Ljava/lang/reflect/Method;",
Ian Rogers466bb252011-10-14 03:29:56 -0700178 "Ljava/lang/reflect/Proxy;",
Mathieu Chartier66f19252012-09-18 08:57:04 -0700179 "[Ljava/lang/String;",
Mathieu Chartier66f19252012-09-18 08:57:04 -0700180 "[Ljava/lang/reflect/AbstractMethod;",
Ian Rogers4445a7e2012-10-05 17:19:13 -0700181 "[Ljava/lang/reflect/Field;",
182 "[Ljava/lang/reflect/Method;",
Brian Carlstroma663ea52011-08-19 23:33:41 -0700183 "Ljava/lang/ClassLoader;",
Ian Rogers5167c972012-02-03 10:41:20 -0800184 "Ljava/lang/Throwable;",
jeffhao8cd6dda2012-02-22 10:15:34 -0800185 "Ljava/lang/ClassNotFoundException;",
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700186 "Ljava/lang/StackTraceElement;",
Brian Carlstroma663ea52011-08-19 23:33:41 -0700187 "Z",
188 "B",
189 "C",
190 "D",
191 "F",
192 "I",
193 "J",
194 "S",
195 "V",
196 "[Z",
197 "[B",
198 "[C",
199 "[D",
200 "[F",
201 "[I",
202 "[J",
203 "[S",
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700204 "[Ljava/lang/StackTraceElement;",
Brian Carlstroma663ea52011-08-19 23:33:41 -0700205};
206
Brian Carlstroma004aa92012-02-08 18:05:09 -0800207ClassLinker* ClassLinker::CreateFromCompiler(const std::vector<const DexFile*>& boot_class_path,
208 InternTable* intern_table) {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700209 CHECK_NE(boot_class_path.size(), 0U);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800210 UniquePtr<ClassLinker> class_linker(new ClassLinker(intern_table));
Brian Carlstroma004aa92012-02-08 18:05:09 -0800211 class_linker->InitFromCompiler(boot_class_path);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700212 return class_linker.release();
213}
214
Brian Carlstroma004aa92012-02-08 18:05:09 -0800215ClassLinker* ClassLinker::CreateFromImage(InternTable* intern_table) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800216 UniquePtr<ClassLinker> class_linker(new ClassLinker(intern_table));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700217 class_linker->InitFromImage();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700218 return class_linker.release();
219}
220
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800221ClassLinker::ClassLinker(InternTable* intern_table)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700222 // dex_lock_ is recursive as it may be used in stack dumping.
223 : dex_lock_("ClassLinker dex lock", kDefaultMutexLevel, true),
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700224 class_roots_(NULL),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700225 array_iftable_(NULL),
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700226 init_done_(false),
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -0700227 is_dirty_(false),
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700228 intern_table_(intern_table) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700229 CHECK_EQ(arraysize(class_roots_descriptors_), size_t(kClassRootsMax));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700230}
Brian Carlstroma663ea52011-08-19 23:33:41 -0700231
Brian Carlstroma004aa92012-02-08 18:05:09 -0800232void ClassLinker::InitFromCompiler(const std::vector<const DexFile*>& boot_class_path) {
233 VLOG(startup) << "ClassLinker::Init";
234 CHECK(Runtime::Current()->IsCompiler());
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700235
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700236 CHECK(!init_done_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700237
Elliott Hughes30646832011-10-13 16:59:46 -0700238 // java_lang_Class comes first, it's needed for AllocClass
Ian Rogers1f539342012-10-03 21:09:42 -0700239 Thread* self = Thread::Current();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800240 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -0700241 SirtRef<Class>
242 java_lang_Class(self, down_cast<Class*>(heap->AllocObject(self, NULL, sizeof(ClassClass))));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700243 CHECK(java_lang_Class.get() != NULL);
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700244 Class::SetClassClass(java_lang_Class.get());
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700245 java_lang_Class->SetClass(java_lang_Class.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700246 java_lang_Class->SetClassSize(sizeof(ClassClass));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700247 // AllocClass(Class*) can now be used
Brian Carlstroma0808032011-07-18 00:39:23 -0700248
Elliott Hughes418d20f2011-09-22 14:00:39 -0700249 // Class[] is used for reflection support.
Ian Rogers50b35e22012-10-04 10:09:15 -0700250 SirtRef<Class> class_array_class(self, AllocClass(self, java_lang_Class.get(), sizeof(Class)));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700251 class_array_class->SetComponentType(java_lang_Class.get());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700252
Ian Rogers23435d02012-09-24 11:23:12 -0700253 // java_lang_Object comes next so that object_array_class can be created.
Ian Rogers50b35e22012-10-04 10:09:15 -0700254 SirtRef<Class> java_lang_Object(self, AllocClass(self, java_lang_Class.get(), sizeof(Class)));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700255 CHECK(java_lang_Object.get() != NULL);
Ian Rogers23435d02012-09-24 11:23:12 -0700256 // backfill Object as the super class of Class.
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700257 java_lang_Class->SetSuperClass(java_lang_Object.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700258 java_lang_Object->SetStatus(Class::kStatusLoaded);
Brian Carlstroma0808032011-07-18 00:39:23 -0700259
Ian Rogers23435d02012-09-24 11:23:12 -0700260 // Object[] next to hold class roots.
Ian Rogers50b35e22012-10-04 10:09:15 -0700261 SirtRef<Class> object_array_class(self, AllocClass(self, java_lang_Class.get(), sizeof(Class)));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700262 object_array_class->SetComponentType(java_lang_Object.get());
Brian Carlstroma0808032011-07-18 00:39:23 -0700263
Ian Rogers23435d02012-09-24 11:23:12 -0700264 // Setup the char class to be used for char[].
Ian Rogers50b35e22012-10-04 10:09:15 -0700265 SirtRef<Class> char_class(self, AllocClass(self, java_lang_Class.get(), sizeof(Class)));
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700266
Ian Rogers23435d02012-09-24 11:23:12 -0700267 // Setup the char[] class to be used for String.
Ian Rogers50b35e22012-10-04 10:09:15 -0700268 SirtRef<Class> char_array_class(self, AllocClass(self, java_lang_Class.get(), sizeof(Class)));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700269 char_array_class->SetComponentType(char_class.get());
270 CharArray::SetArrayClass(char_array_class.get());
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700271
Ian Rogers23435d02012-09-24 11:23:12 -0700272 // Setup String.
Ian Rogers50b35e22012-10-04 10:09:15 -0700273 SirtRef<Class> java_lang_String(self, AllocClass(self, java_lang_Class.get(), sizeof(StringClass)));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700274 String::SetClass(java_lang_String.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700275 java_lang_String->SetObjectSize(sizeof(String));
276 java_lang_String->SetStatus(Class::kStatusResolved);
Jesse Wilson14150742011-07-29 19:04:44 -0400277
Ian Rogers23435d02012-09-24 11:23:12 -0700278 // Create storage for root classes, save away our work so far (requires descriptors).
Ian Rogers50b35e22012-10-04 10:09:15 -0700279 class_roots_ = ObjectArray<Class>::Alloc(self, object_array_class.get(), kClassRootsMax);
Elliott Hughes30646832011-10-13 16:59:46 -0700280 CHECK(class_roots_ != NULL);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700281 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 Rogers0cfe1fb2011-08-26 03:29:44 -0700287
288 // Setup the primitive type classes.
Ian Rogers50b35e22012-10-04 10:09:15 -0700289 SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass(self, Primitive::kPrimBoolean));
290 SetClassRoot(kPrimitiveByte, CreatePrimitiveClass(self, Primitive::kPrimByte));
291 SetClassRoot(kPrimitiveShort, CreatePrimitiveClass(self, Primitive::kPrimShort));
292 SetClassRoot(kPrimitiveInt, CreatePrimitiveClass(self, Primitive::kPrimInt));
293 SetClassRoot(kPrimitiveLong, CreatePrimitiveClass(self, Primitive::kPrimLong));
294 SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass(self, Primitive::kPrimFloat));
295 SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass(self, Primitive::kPrimDouble));
296 SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass(self, Primitive::kPrimVoid));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700297
Ian Rogers23435d02012-09-24 11:23:12 -0700298 // Create array interface entries to populate once we can load system classes.
Ian Rogers50b35e22012-10-04 10:09:15 -0700299 array_iftable_ = AllocIfTable(self, 2);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700300
Ian Rogers23435d02012-09-24 11:23:12 -0700301 // Create int array type for AllocDexCache (done in AppendToBootClassPath).
Ian Rogers50b35e22012-10-04 10:09:15 -0700302 SirtRef<Class> int_array_class(self, AllocClass(self, java_lang_Class.get(), sizeof(Class)));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700303 int_array_class->SetComponentType(GetClassRoot(kPrimitiveInt));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700304 IntArray::SetArrayClass(int_array_class.get());
305 SetClassRoot(kIntArrayClass, int_array_class.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700306
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700307 // now that these are registered, we can use AllocClass() and AllocObjectArray
Brian Carlstroma0808032011-07-18 00:39:23 -0700308
Ian Rogers52813c92012-10-11 11:50:38 -0700309 // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache.
Ian Rogers50b35e22012-10-04 10:09:15 -0700310 SirtRef<Class>
311 java_lang_DexCache(self, AllocClass(self, java_lang_Class.get(), sizeof(DexCacheClass)));
Mathieu Chartier66f19252012-09-18 08:57:04 -0700312 SetClassRoot(kJavaLangDexCache, java_lang_DexCache.get());
313 java_lang_DexCache->SetObjectSize(sizeof(DexCacheClass));
314 java_lang_DexCache->SetStatus(Class::kStatusResolved);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700315
Mathieu Chartier66f19252012-09-18 08:57:04 -0700316 // Constructor, Field, Method, and AbstractMethod are necessary so that FindClass can link members.
Ian Rogers50b35e22012-10-04 10:09:15 -0700317 SirtRef<Class> java_lang_reflect_Field(self, AllocClass(self, java_lang_Class.get(),
318 sizeof(FieldClass)));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700319 CHECK(java_lang_reflect_Field.get() != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700320 java_lang_reflect_Field->SetObjectSize(sizeof(Field));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700321 SetClassRoot(kJavaLangReflectField, java_lang_reflect_Field.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700322 java_lang_reflect_Field->SetStatus(Class::kStatusResolved);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700323 Field::SetClass(java_lang_reflect_Field.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700324
Ian Rogers50b35e22012-10-04 10:09:15 -0700325 SirtRef<Class> java_lang_reflect_AbstractMethod(self, AllocClass(self, java_lang_Class.get(),
326 sizeof(MethodClass)));
Mathieu Chartier66f19252012-09-18 08:57:04 -0700327 CHECK(java_lang_reflect_AbstractMethod.get() != NULL);
328 java_lang_reflect_AbstractMethod->SetObjectSize(sizeof(AbstractMethod));
329 SetClassRoot(kJavaLangReflectAbstractMethod, java_lang_reflect_AbstractMethod.get());
330 java_lang_reflect_AbstractMethod->SetStatus(Class::kStatusResolved);
Ian Rogers4445a7e2012-10-05 17:19:13 -0700331
332 SirtRef<Class> java_lang_reflect_Constructor(self, AllocClass(self, java_lang_Class.get(),
333 sizeof(MethodClass)));
334 CHECK(java_lang_reflect_Constructor.get() != NULL);
335 java_lang_reflect_Constructor->SetObjectSize(sizeof(Constructor));
336 java_lang_reflect_Constructor->SetSuperClass(java_lang_reflect_AbstractMethod.get());
337 SetClassRoot(kJavaLangReflectConstructor, java_lang_reflect_Constructor.get());
338 java_lang_reflect_Constructor->SetStatus(Class::kStatusResolved);
339
340 SirtRef<Class> java_lang_reflect_Method(self, AllocClass(self, java_lang_Class.get(),
341 sizeof(MethodClass)));
342 CHECK(java_lang_reflect_Method.get() != NULL);
343 java_lang_reflect_Method->SetObjectSize(sizeof(Method));
344 java_lang_reflect_Method->SetSuperClass(java_lang_reflect_AbstractMethod.get());
345 SetClassRoot(kJavaLangReflectMethod, java_lang_reflect_Method.get());
346 java_lang_reflect_Method->SetStatus(Class::kStatusResolved);
347
Mathieu Chartier66f19252012-09-18 08:57:04 -0700348 AbstractMethod::SetClasses(java_lang_reflect_Constructor.get(), java_lang_reflect_Method.get());
349
350 // Set up array classes for string, field, method
Ian Rogers50b35e22012-10-04 10:09:15 -0700351 SirtRef<Class> object_array_string(self, AllocClass(self, java_lang_Class.get(), sizeof(Class)));
Mathieu Chartier66f19252012-09-18 08:57:04 -0700352 object_array_string->SetComponentType(java_lang_String.get());
353 SetClassRoot(kJavaLangStringArrayClass, object_array_string.get());
354
Ian Rogers4445a7e2012-10-05 17:19:13 -0700355 SirtRef<Class> object_array_abstract_method(self, AllocClass(self, java_lang_Class.get(), sizeof(Class)));
356 object_array_abstract_method->SetComponentType(java_lang_reflect_AbstractMethod.get());
357 SetClassRoot(kJavaLangReflectAbstractMethodArrayClass, object_array_abstract_method.get());
358
Ian Rogers50b35e22012-10-04 10:09:15 -0700359 SirtRef<Class> object_array_field(self, AllocClass(self, java_lang_Class.get(), sizeof(Class)));
Mathieu Chartier66f19252012-09-18 08:57:04 -0700360 object_array_field->SetComponentType(java_lang_reflect_Field.get());
361 SetClassRoot(kJavaLangReflectFieldArrayClass, object_array_field.get());
362
Ian Rogers4445a7e2012-10-05 17:19:13 -0700363 SirtRef<Class> object_array_method(self, AllocClass(self, java_lang_Class.get(), sizeof(Class)));
364 object_array_method->SetComponentType(java_lang_reflect_Method.get());
365 SetClassRoot(kJavaLangReflectMethodArrayClass, object_array_method.get());
Mathieu Chartier66f19252012-09-18 08:57:04 -0700366
Ian Rogers23435d02012-09-24 11:23:12 -0700367 // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create
368 // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses
369 // these roots.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700370 CHECK_NE(0U, boot_class_path.size());
371 for (size_t i = 0; i != boot_class_path.size(); ++i) {
372 const DexFile* dex_file = boot_class_path[i];
373 CHECK(dex_file != NULL);
374 AppendToBootClassPath(*dex_file);
375 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700376
377 // now we can use FindSystemClass
378
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700379 // run char class through InitializePrimitiveClass to finish init
Ian Rogersc8982582012-09-07 16:53:25 -0700380 InitializePrimitiveClass(char_class.get(), Primitive::kPrimChar);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700381 SetClassRoot(kPrimitiveChar, char_class.get()); // needs descriptor
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700382
Mathieu Chartier66f19252012-09-18 08:57:04 -0700383 // Object, String and DexCache need to be rerun through FindSystemClass to finish init
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700384 java_lang_Object->SetStatus(Class::kStatusNotReady);
385 Class* Object_class = FindSystemClass("Ljava/lang/Object;");
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700386 CHECK_EQ(java_lang_Object.get(), Object_class);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700387 CHECK_EQ(java_lang_Object->GetObjectSize(), sizeof(Object));
388 java_lang_String->SetStatus(Class::kStatusNotReady);
389 Class* String_class = FindSystemClass("Ljava/lang/String;");
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700390 CHECK_EQ(java_lang_String.get(), String_class);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700391 CHECK_EQ(java_lang_String->GetObjectSize(), sizeof(String));
Mathieu Chartier66f19252012-09-18 08:57:04 -0700392 java_lang_DexCache->SetStatus(Class::kStatusNotReady);
393 Class* DexCache_class = FindSystemClass("Ljava/lang/DexCache;");
394 CHECK_EQ(java_lang_String.get(), String_class);
395 CHECK_EQ(java_lang_DexCache.get(), DexCache_class);
396 CHECK_EQ(java_lang_DexCache->GetObjectSize(), sizeof(DexCache));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700397
Ian Rogers23435d02012-09-24 11:23:12 -0700398 // Setup the primitive array type classes - can't be done until Object has a vtable.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700399 SetClassRoot(kBooleanArrayClass, FindSystemClass("[Z"));
400 BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass));
401
402 SetClassRoot(kByteArrayClass, FindSystemClass("[B"));
403 ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass));
404
405 Class* found_char_array_class = FindSystemClass("[C");
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700406 CHECK_EQ(char_array_class.get(), found_char_array_class);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700407
408 SetClassRoot(kShortArrayClass, FindSystemClass("[S"));
409 ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass));
410
411 Class* found_int_array_class = FindSystemClass("[I");
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700412 CHECK_EQ(int_array_class.get(), found_int_array_class);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700413
414 SetClassRoot(kLongArrayClass, FindSystemClass("[J"));
415 LongArray::SetArrayClass(GetClassRoot(kLongArrayClass));
416
417 SetClassRoot(kFloatArrayClass, FindSystemClass("[F"));
418 FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass));
419
420 SetClassRoot(kDoubleArrayClass, FindSystemClass("[D"));
421 DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass));
422
Elliott Hughes418d20f2011-09-22 14:00:39 -0700423 Class* found_class_array_class = FindSystemClass("[Ljava/lang/Class;");
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700424 CHECK_EQ(class_array_class.get(), found_class_array_class);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700425
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700426 Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;");
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700427 CHECK_EQ(object_array_class.get(), found_object_array_class);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700428
Ian Rogers23435d02012-09-24 11:23:12 -0700429 // Setup the single, global copy of "iftable".
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700430 Class* java_lang_Cloneable = FindSystemClass("Ljava/lang/Cloneable;");
431 CHECK(java_lang_Cloneable != NULL);
432 Class* java_io_Serializable = FindSystemClass("Ljava/io/Serializable;");
433 CHECK(java_io_Serializable != NULL);
Ian Rogers23435d02012-09-24 11:23:12 -0700434 // We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to
435 // crawl up and explicitly list all of the supers as well.
Ian Rogers9bc81912012-10-11 21:43:36 -0700436 array_iftable_->SetInterface(0, java_lang_Cloneable);
437 array_iftable_->SetInterface(1, java_io_Serializable);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700438
Ian Rogers23435d02012-09-24 11:23:12 -0700439 // Sanity check Class[] and Object[]'s interfaces.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800440 ClassHelper kh(class_array_class.get(), this);
Ian Rogersd24e2642012-06-06 21:21:43 -0700441 CHECK_EQ(java_lang_Cloneable, kh.GetDirectInterface(0));
442 CHECK_EQ(java_io_Serializable, kh.GetDirectInterface(1));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800443 kh.ChangeClass(object_array_class.get());
Ian Rogersd24e2642012-06-06 21:21:43 -0700444 CHECK_EQ(java_lang_Cloneable, kh.GetDirectInterface(0));
445 CHECK_EQ(java_io_Serializable, kh.GetDirectInterface(1));
Ian Rogers23435d02012-09-24 11:23:12 -0700446 // Run Class, Constructor, Field, and Method through FindSystemClass. This initializes their
447 // dex_cache_ fields and register them in classes_.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700448 Class* Class_class = FindSystemClass("Ljava/lang/Class;");
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700449 CHECK_EQ(java_lang_Class.get(), Class_class);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700450
Mathieu Chartier66f19252012-09-18 08:57:04 -0700451 java_lang_reflect_AbstractMethod->SetStatus(Class::kStatusNotReady);
452 Class* Abstract_method_class = FindSystemClass("Ljava/lang/reflect/AbstractMethod;");
453 CHECK_EQ(java_lang_reflect_AbstractMethod.get(), Abstract_method_class);
454
455 // Method extends AbstractMethod so must reset after.
456 java_lang_reflect_Method->SetStatus(Class::kStatusNotReady);
457 Class* Method_class = FindSystemClass("Ljava/lang/reflect/Method;");
458 CHECK_EQ(java_lang_reflect_Method.get(), Method_class);
459
460 // Constructor extends AbstractMethod so must reset after.
Elliott Hughes80609252011-09-23 17:24:51 -0700461 java_lang_reflect_Constructor->SetStatus(Class::kStatusNotReady);
462 Class* Constructor_class = FindSystemClass("Ljava/lang/reflect/Constructor;");
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700463 CHECK_EQ(java_lang_reflect_Constructor.get(), Constructor_class);
Elliott Hughes80609252011-09-23 17:24:51 -0700464
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700465 java_lang_reflect_Field->SetStatus(Class::kStatusNotReady);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700466 Class* Field_class = FindSystemClass("Ljava/lang/reflect/Field;");
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700467 CHECK_EQ(java_lang_reflect_Field.get(), Field_class);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700468
Mathieu Chartier66f19252012-09-18 08:57:04 -0700469 Class* String_array_class = FindSystemClass(class_roots_descriptors_[kJavaLangStringArrayClass]);
470 CHECK_EQ(object_array_string.get(), String_array_class);
471
Mathieu Chartier66f19252012-09-18 08:57:04 -0700472 Class* Abstract_method_array_class =
473 FindSystemClass(class_roots_descriptors_[kJavaLangReflectAbstractMethodArrayClass]);
474 CHECK_EQ(object_array_abstract_method.get(), Abstract_method_array_class);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700475
Ian Rogers4445a7e2012-10-05 17:19:13 -0700476 Class* Field_array_class = FindSystemClass(class_roots_descriptors_[kJavaLangReflectFieldArrayClass]);
477 CHECK_EQ(object_array_field.get(), Field_array_class);
478
479 Class* Method_array_class =
480 FindSystemClass(class_roots_descriptors_[kJavaLangReflectMethodArrayClass]);
481 CHECK_EQ(object_array_method.get(), Method_array_class);
482
Ian Rogers23435d02012-09-24 11:23:12 -0700483 // End of special init trickery, subsequent classes may be loaded via FindSystemClass.
Ian Rogers466bb252011-10-14 03:29:56 -0700484
Ian Rogers23435d02012-09-24 11:23:12 -0700485 // Create java.lang.reflect.Proxy root.
Ian Rogers466bb252011-10-14 03:29:56 -0700486 Class* java_lang_reflect_Proxy = FindSystemClass("Ljava/lang/reflect/Proxy;");
487 SetClassRoot(kJavaLangReflectProxy, java_lang_reflect_Proxy);
488
Brian Carlstrom1f870082011-08-23 16:02:11 -0700489 // java.lang.ref classes need to be specially flagged, but otherwise are normal classes
Elliott Hughesbf61ba32011-10-11 10:53:09 -0700490 Class* java_lang_ref_Reference = FindSystemClass("Ljava/lang/ref/Reference;");
491 SetClassRoot(kJavaLangRefReference, java_lang_ref_Reference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700492 Class* java_lang_ref_FinalizerReference = FindSystemClass("Ljava/lang/ref/FinalizerReference;");
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700493 java_lang_ref_FinalizerReference->SetAccessFlags(
494 java_lang_ref_FinalizerReference->GetAccessFlags() |
495 kAccClassIsReference | kAccClassIsFinalizerReference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700496 Class* java_lang_ref_PhantomReference = FindSystemClass("Ljava/lang/ref/PhantomReference;");
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700497 java_lang_ref_PhantomReference->SetAccessFlags(
498 java_lang_ref_PhantomReference->GetAccessFlags() |
499 kAccClassIsReference | kAccClassIsPhantomReference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700500 Class* java_lang_ref_SoftReference = FindSystemClass("Ljava/lang/ref/SoftReference;");
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700501 java_lang_ref_SoftReference->SetAccessFlags(
502 java_lang_ref_SoftReference->GetAccessFlags() | kAccClassIsReference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700503 Class* java_lang_ref_WeakReference = FindSystemClass("Ljava/lang/ref/WeakReference;");
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700504 java_lang_ref_WeakReference->SetAccessFlags(
505 java_lang_ref_WeakReference->GetAccessFlags() |
506 kAccClassIsReference | kAccClassIsWeakReference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700507
Ian Rogers23435d02012-09-24 11:23:12 -0700508 // Setup the ClassLoader, verifying the object_size_.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700509 Class* java_lang_ClassLoader = FindSystemClass("Ljava/lang/ClassLoader;");
Brian Carlstromaded5f72011-10-07 17:15:04 -0700510 CHECK_EQ(java_lang_ClassLoader->GetObjectSize(), sizeof(ClassLoader));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700511 SetClassRoot(kJavaLangClassLoader, java_lang_ClassLoader);
512
jeffhao8cd6dda2012-02-22 10:15:34 -0800513 // Set up java.lang.Throwable, java.lang.ClassNotFoundException, and
Ian Rogers23435d02012-09-24 11:23:12 -0700514 // java.lang.StackTraceElement as a convenience.
Ian Rogers5167c972012-02-03 10:41:20 -0800515 SetClassRoot(kJavaLangThrowable, FindSystemClass("Ljava/lang/Throwable;"));
516 Throwable::SetClass(GetClassRoot(kJavaLangThrowable));
jeffhao8cd6dda2012-02-22 10:15:34 -0800517 SetClassRoot(kJavaLangClassNotFoundException, FindSystemClass("Ljava/lang/ClassNotFoundException;"));
Brian Carlstrom1f870082011-08-23 16:02:11 -0700518 SetClassRoot(kJavaLangStackTraceElement, FindSystemClass("Ljava/lang/StackTraceElement;"));
519 SetClassRoot(kJavaLangStackTraceElementArrayClass, FindSystemClass("[Ljava/lang/StackTraceElement;"));
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700520 StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700521
Brian Carlstroma663ea52011-08-19 23:33:41 -0700522 FinishInit();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700523
Brian Carlstroma004aa92012-02-08 18:05:09 -0800524 VLOG(startup) << "ClassLinker::InitFromCompiler exiting";
Brian Carlstroma663ea52011-08-19 23:33:41 -0700525}
526
527void ClassLinker::FinishInit() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800528 VLOG(startup) << "ClassLinker::FinishInit entering";
Brian Carlstrom16192862011-09-12 17:50:06 -0700529
530 // Let the heap know some key offsets into java.lang.ref instances
Elliott Hughes20cde902011-10-04 17:37:27 -0700531 // Note: we hard code the field indexes here rather than using FindInstanceField
Brian Carlstrom16192862011-09-12 17:50:06 -0700532 // as the types of the field can't be resolved prior to the runtime being
533 // fully initialized
Elliott Hughesbf61ba32011-10-11 10:53:09 -0700534 Class* java_lang_ref_Reference = GetClassRoot(kJavaLangRefReference);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700535 Class* java_lang_ref_ReferenceQueue = FindSystemClass("Ljava/lang/ref/ReferenceQueue;");
Brian Carlstrom16192862011-09-12 17:50:06 -0700536 Class* java_lang_ref_FinalizerReference = FindSystemClass("Ljava/lang/ref/FinalizerReference;");
537
Ian Rogers4445a7e2012-10-05 17:19:13 -0700538 const DexFile& java_lang_dex = *java_lang_ref_Reference->GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800539
Brian Carlstrom16192862011-09-12 17:50:06 -0700540 Field* pendingNext = java_lang_ref_Reference->GetInstanceField(0);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800541 FieldHelper fh(pendingNext, this);
542 CHECK_STREQ(fh.GetName(), "pendingNext");
543 CHECK_EQ(java_lang_dex.GetFieldId(pendingNext->GetDexFieldIndex()).type_idx_,
544 java_lang_ref_Reference->GetDexTypeIndex());
Brian Carlstrom16192862011-09-12 17:50:06 -0700545
546 Field* queue = java_lang_ref_Reference->GetInstanceField(1);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800547 fh.ChangeField(queue);
548 CHECK_STREQ(fh.GetName(), "queue");
549 CHECK_EQ(java_lang_dex.GetFieldId(queue->GetDexFieldIndex()).type_idx_,
550 java_lang_ref_ReferenceQueue->GetDexTypeIndex());
Brian Carlstrom16192862011-09-12 17:50:06 -0700551
552 Field* queueNext = java_lang_ref_Reference->GetInstanceField(2);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800553 fh.ChangeField(queueNext);
554 CHECK_STREQ(fh.GetName(), "queueNext");
555 CHECK_EQ(java_lang_dex.GetFieldId(queueNext->GetDexFieldIndex()).type_idx_,
556 java_lang_ref_Reference->GetDexTypeIndex());
Brian Carlstrom16192862011-09-12 17:50:06 -0700557
558 Field* referent = java_lang_ref_Reference->GetInstanceField(3);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800559 fh.ChangeField(referent);
560 CHECK_STREQ(fh.GetName(), "referent");
561 CHECK_EQ(java_lang_dex.GetFieldId(referent->GetDexFieldIndex()).type_idx_,
562 GetClassRoot(kJavaLangObject)->GetDexTypeIndex());
Brian Carlstrom16192862011-09-12 17:50:06 -0700563
564 Field* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800565 fh.ChangeField(zombie);
566 CHECK_STREQ(fh.GetName(), "zombie");
567 CHECK_EQ(java_lang_dex.GetFieldId(zombie->GetDexFieldIndex()).type_idx_,
568 GetClassRoot(kJavaLangObject)->GetDexTypeIndex());
Brian Carlstrom16192862011-09-12 17:50:06 -0700569
Elliott Hughesa4f94742012-05-29 16:28:38 -0700570 Heap* heap = Runtime::Current()->GetHeap();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800571 heap->SetReferenceOffsets(referent->GetOffset(),
Brian Carlstrom16192862011-09-12 17:50:06 -0700572 queue->GetOffset(),
573 queueNext->GetOffset(),
574 pendingNext->GetOffset(),
575 zombie->GetOffset());
576
Brian Carlstroma663ea52011-08-19 23:33:41 -0700577 // ensure all class_roots_ are initialized
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700578 for (size_t i = 0; i < kClassRootsMax; i++) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700579 ClassRoot class_root = static_cast<ClassRoot>(i);
580 Class* klass = GetClassRoot(class_root);
581 CHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700582 DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != NULL);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700583 // note SetClassRoot does additional validation.
584 // if possible add new checks there to catch errors early
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700585 }
586
Elliott Hughes92f14b22011-10-06 12:29:54 -0700587 CHECK(array_iftable_ != NULL);
Elliott Hughes92f14b22011-10-06 12:29:54 -0700588
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700589 // disable the slow paths in FindClass and CreatePrimitiveClass now
590 // that Object, Class, and Object[] are setup
591 init_done_ = true;
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700592
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800593 VLOG(startup) << "ClassLinker::FinishInit exiting";
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700594}
595
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700596void ClassLinker::RunRootClinits() {
597 Thread* self = Thread::Current();
598 for (size_t i = 0; i < ClassLinker::kClassRootsMax; ++i) {
599 Class* c = GetClassRoot(ClassRoot(i));
600 if (!c->IsArrayClass() && !c->IsPrimitive()) {
Ian Rogers0045a292012-03-31 21:08:41 -0700601 EnsureInitialized(GetClassRoot(ClassRoot(i)), true, true);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700602 self->AssertNoPendingException();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700603 }
604 }
605}
606
Brian Carlstromd601af82012-01-06 10:15:19 -0800607bool ClassLinker::GenerateOatFile(const std::string& dex_filename,
608 int oat_fd,
609 const std::string& oat_cache_filename) {
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800610 std::string dex2oat_string(GetAndroidRoot());
Elliott Hughes67d92002012-03-26 15:08:51 -0700611 dex2oat_string += (kIsDebugBuild ? "/bin/dex2oatd" : "/bin/dex2oat");
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800612 const char* dex2oat = dex2oat_string.c_str();
613
Brian Carlstroma004aa92012-02-08 18:05:09 -0800614 const char* class_path = Runtime::Current()->GetClassPathString().c_str();
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800615
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800616 Heap* heap = Runtime::Current()->GetHeap();
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800617 std::string boot_image_option_string("--boot-image=");
Brian Carlstromfddf6f62012-03-15 16:56:45 -0700618 boot_image_option_string += heap->GetImageSpace()->GetImageFilename();
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800619 const char* boot_image_option = boot_image_option_string.c_str();
620
621 std::string dex_file_option_string("--dex-file=");
Brian Carlstromd601af82012-01-06 10:15:19 -0800622 dex_file_option_string += dex_filename;
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800623 const char* dex_file_option = dex_file_option_string.c_str();
624
Brian Carlstromd601af82012-01-06 10:15:19 -0800625 std::string oat_fd_option_string("--oat-fd=");
Brian Carlstrom866c8622012-01-06 16:35:13 -0800626 StringAppendF(&oat_fd_option_string, "%d", oat_fd);
Brian Carlstromd601af82012-01-06 10:15:19 -0800627 const char* oat_fd_option = oat_fd_option_string.c_str();
628
Brian Carlstroma004aa92012-02-08 18:05:09 -0800629 std::string oat_location_option_string("--oat-location=");
630 oat_location_option_string += oat_cache_filename;
631 const char* oat_location_option = oat_location_option_string.c_str();
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800632
jeffhao262bf462011-10-20 18:36:32 -0700633 // fork and exec dex2oat
634 pid_t pid = fork();
635 if (pid == 0) {
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800636 // no allocation allowed between fork and exec
Ian Rogers725aee52012-01-11 11:56:56 -0800637
638 // change process groups, so we don't get reaped by ProcessManager
639 setpgid(0, 0);
640
jeffhao10037c82012-01-23 15:06:23 -0800641 VLOG(class_linker) << dex2oat
642 << " --runtime-arg -Xms64m"
643 << " --runtime-arg -Xmx64m"
644 << " --runtime-arg -classpath"
645 << " --runtime-arg " << class_path
646 << " " << boot_image_option
647 << " " << dex_file_option
648 << " " << oat_fd_option
Brian Carlstroma004aa92012-02-08 18:05:09 -0800649 << " " << oat_location_option;
jeffhao10037c82012-01-23 15:06:23 -0800650
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800651 execl(dex2oat, dex2oat,
jeffhao5d840402011-10-24 17:09:45 -0700652 "--runtime-arg", "-Xms64m",
653 "--runtime-arg", "-Xmx64m",
Jesse Wilson254db0f2011-11-16 16:44:11 -0500654 "--runtime-arg", "-classpath",
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800655 "--runtime-arg", class_path,
656 boot_image_option,
657 dex_file_option,
Brian Carlstromd601af82012-01-06 10:15:19 -0800658 oat_fd_option,
Brian Carlstroma004aa92012-02-08 18:05:09 -0800659 oat_location_option,
jeffhao262bf462011-10-20 18:36:32 -0700660 NULL);
661
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800662 PLOG(FATAL) << "execl(" << dex2oat << ") failed";
Brian Carlstromd601af82012-01-06 10:15:19 -0800663 return false;
jeffhao262bf462011-10-20 18:36:32 -0700664 } else {
665 // wait for dex2oat to finish
666 int status;
667 pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
668 if (got_pid != pid) {
669 PLOG(ERROR) << "waitpid failed: wanted " << pid << ", got " << got_pid;
Brian Carlstromd601af82012-01-06 10:15:19 -0800670 return false;
jeffhao262bf462011-10-20 18:36:32 -0700671 }
672 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Brian Carlstromd601af82012-01-06 10:15:19 -0800673 LOG(ERROR) << dex2oat << " failed with dex-file=" << dex_filename;
674 return false;
jeffhao262bf462011-10-20 18:36:32 -0700675 }
676 }
Brian Carlstromd601af82012-01-06 10:15:19 -0800677 return true;
jeffhao262bf462011-10-20 18:36:32 -0700678}
679
Brian Carlstrom866c8622012-01-06 16:35:13 -0800680void ClassLinker::RegisterOatFile(const OatFile& oat_file) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700681 MutexLock mu(Thread::Current(), dex_lock_);
Brian Carlstrom866c8622012-01-06 16:35:13 -0800682 RegisterOatFileLocked(oat_file);
683}
684
685void ClassLinker::RegisterOatFileLocked(const OatFile& oat_file) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700686 dex_lock_.AssertHeld(Thread::Current());
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800687#ifndef NDEBUG
688 for (size_t i = 0; i < oat_files_.size(); ++i) {
689 CHECK_NE(&oat_file, oat_files_[i]) << oat_file.GetLocation();
690 }
691#endif
Brian Carlstrom866c8622012-01-06 16:35:13 -0800692 oat_files_.push_back(&oat_file);
693}
694
Ian Rogers30fab402012-01-23 15:43:46 -0800695OatFile* ClassLinker::OpenOat(const ImageSpace* space) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700696 MutexLock mu(Thread::Current(), dex_lock_);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700697 const Runtime* runtime = Runtime::Current();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700698 const ImageHeader& image_header = space->GetImageHeader();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800699 // Grab location but don't use Object::AsString as we haven't yet initialized the roots to
700 // check the down cast
701 String* oat_location = down_cast<String*>(image_header.GetImageRoot(ImageHeader::kOatLocation));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700702 std::string oat_filename;
703 oat_filename += runtime->GetHostPrefix();
704 oat_filename += oat_location->ToModifiedUtf8();
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700705 runtime->GetHeap()->UnReserveOatFileAddressRange();
Brian Carlstrom1cac3432012-12-12 10:56:22 -0800706 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, image_header.GetOatBegin());
Ian Rogers30fab402012-01-23 15:43:46 -0800707 VLOG(startup) << "ClassLinker::OpenOat entering oat_filename=" << oat_filename;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700708 if (oat_file == NULL) {
Brian Carlstroma9f19782011-10-13 00:14:47 -0700709 LOG(ERROR) << "Failed to open oat file " << oat_filename << " referenced from image.";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700710 return NULL;
711 }
712 uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum();
713 uint32_t image_oat_checksum = image_header.GetOatChecksum();
714 if (oat_checksum != image_oat_checksum) {
Brian Carlstrom866c8622012-01-06 16:35:13 -0800715 LOG(ERROR) << "Failed to match oat file checksum " << std::hex << oat_checksum
Brian Carlstroma85b8372012-10-18 17:00:32 -0700716 << " to expected oat checksum " << std::hex << image_oat_checksum
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700717 << " in image";
718 return NULL;
719 }
Brian Carlstrom866c8622012-01-06 16:35:13 -0800720 RegisterOatFileLocked(*oat_file);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800721 VLOG(startup) << "ClassLinker::OpenOat exiting";
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700722 return oat_file;
723}
724
Brian Carlstromae826982011-11-09 01:33:42 -0800725const OatFile* ClassLinker::FindOpenedOatFileForDexFile(const DexFile& dex_file) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700726 MutexLock mu(Thread::Current(), dex_lock_);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800727 return FindOpenedOatFileFromDexLocation(dex_file.GetLocation());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800728}
729
Brian Carlstroma004aa92012-02-08 18:05:09 -0800730const OatFile* ClassLinker::FindOpenedOatFileFromDexLocation(const std::string& dex_location) {
Brian Carlstromae826982011-11-09 01:33:42 -0800731 for (size_t i = 0; i < oat_files_.size(); i++) {
732 const OatFile* oat_file = oat_files_[i];
733 DCHECK(oat_file != NULL);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800734 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, false);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800735 if (oat_dex_file != NULL) {
Brian Carlstromae826982011-11-09 01:33:42 -0800736 return oat_file;
737 }
738 }
739 return NULL;
740}
741
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800742static const DexFile* FindDexFileInOatLocation(const std::string& dex_location,
743 uint32_t dex_location_checksum,
744 const std::string& oat_location) {
Brian Carlstrom1cac3432012-12-12 10:56:22 -0800745 UniquePtr<OatFile> oat_file(OatFile::Open(oat_location, oat_location, NULL));
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800746 if (oat_file.get() == NULL) {
747 return NULL;
748 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700749 Runtime* runtime = Runtime::Current();
750 const ImageHeader& image_header = runtime->GetHeap()->GetImageSpace()->GetImageHeader();
Brian Carlstrom28db0122012-10-18 16:20:41 -0700751 if (oat_file->GetOatHeader().GetImageFileLocationOatChecksum() != image_header.GetOatChecksum()) {
752 return NULL;
753 }
754 if (oat_file->GetOatHeader().GetImageFileLocationOatBegin()
755 != reinterpret_cast<uint32_t>(image_header.GetOatBegin())) {
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700756 return NULL;
757 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800758 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location);
759 if (oat_dex_file == NULL) {
760 return NULL;
761 }
762 if (oat_dex_file->GetDexFileLocationChecksum() != dex_location_checksum) {
763 return NULL;
764 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700765 runtime->GetClassLinker()->RegisterOatFile(*oat_file.release());
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800766 return oat_dex_file->OpenDexFile();
767}
768
769const DexFile* ClassLinker::FindOrCreateOatFileForDexLocation(const std::string& dex_location,
770 const std::string& oat_location) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700771 MutexLock mu(Thread::Current(), dex_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700772 return FindOrCreateOatFileForDexLocationLocked(dex_location, oat_location);
773}
774
775const DexFile* ClassLinker::FindOrCreateOatFileForDexLocationLocked(const std::string& dex_location,
776 const std::string& oat_location) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800777 uint32_t dex_location_checksum;
778 if (!DexFile::GetChecksum(dex_location, dex_location_checksum)) {
779 LOG(ERROR) << "Failed to compute checksum '" << dex_location << "'";
780 return NULL;
781 }
782
783 // Check if we already have an up-to-date output file
784 const DexFile* dex_file = FindDexFileInOatLocation(dex_location,
785 dex_location_checksum,
786 oat_location);
787 if (dex_file != NULL) {
788 return dex_file;
789 }
790
791 // Generate the output oat file for the dex file
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800792 UniquePtr<File> file(OS::OpenFile(oat_location.c_str(), true));
793 if (file.get() == NULL) {
794 LOG(ERROR) << "Failed to create oat file: " << oat_location;
795 return NULL;
796 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700797 if (!GenerateOatFile(dex_location, file->Fd(), oat_location)) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800798 LOG(ERROR) << "Failed to generate oat file: " << oat_location;
799 return NULL;
800 }
801 // Open the oat from file descriptor we passed to GenerateOatFile
802 if (lseek(file->Fd(), 0, SEEK_SET) != 0) {
803 LOG(ERROR) << "Failed to seek to start of generated oat file: " << oat_location;
804 return NULL;
805 }
Brian Carlstrom1cac3432012-12-12 10:56:22 -0800806 const OatFile* oat_file = OatFile::Open(*file.get(), oat_location, NULL);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800807 if (oat_file == NULL) {
808 LOG(ERROR) << "Failed to open generated oat file: " << oat_location;
809 return NULL;
810 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700811 RegisterOatFileLocked(*oat_file);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800812 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location);
813 if (oat_dex_file == NULL) {
814 LOG(ERROR) << "Failed to find dex file in generated oat file: " << oat_location;
815 return NULL;
816 }
817 return oat_dex_file->OpenDexFile();
818}
819
Brian Carlstromafe25512012-06-27 17:02:58 -0700820bool ClassLinker::VerifyOatFileChecksums(const OatFile* oat_file,
821 const std::string& dex_location,
822 uint32_t dex_location_checksum) {
823 Runtime* runtime = Runtime::Current();
824 const ImageHeader& image_header = runtime->GetHeap()->GetImageSpace()->GetImageHeader();
Brian Carlstrom28db0122012-10-18 16:20:41 -0700825 uint32_t image_oat_checksum = image_header.GetOatChecksum();
826 uint32_t image_oat_begin = reinterpret_cast<uint32_t>(image_header.GetOatBegin());
827 bool image_check = ((oat_file->GetOatHeader().GetImageFileLocationOatChecksum() == image_oat_checksum)
828 && (oat_file->GetOatHeader().GetImageFileLocationOatBegin() == image_oat_begin));
Brian Carlstrom46b8a622012-06-19 23:13:22 -0700829
Brian Carlstromafe25512012-06-27 17:02:58 -0700830 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location);
831 if (oat_dex_file == NULL) {
832 LOG(ERROR) << ".oat file " << oat_file->GetLocation()
833 << " does not contain contents for " << dex_location;
834 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file->GetOatDexFiles();
835 for (size_t i = 0; i < oat_dex_files.size(); i++) {
836 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
837 LOG(ERROR) << ".oat file " << oat_file->GetLocation()
838 << " contains contents for " << oat_dex_file->GetDexFileLocation();
839 }
840 return false;
841 }
Brian Carlstrom28db0122012-10-18 16:20:41 -0700842 bool dex_check = dex_location_checksum == oat_dex_file->GetDexFileLocationChecksum();
Brian Carlstrom46b8a622012-06-19 23:13:22 -0700843
Brian Carlstromafe25512012-06-27 17:02:58 -0700844 if (image_check && dex_check) {
845 return true;
846 }
Brian Carlstrom46b8a622012-06-19 23:13:22 -0700847
Brian Carlstromafe25512012-06-27 17:02:58 -0700848 if (!image_check) {
849 std::string image_file(image_header.GetImageRoot(
850 ImageHeader::kOatLocation)->AsString()->ToModifiedUtf8());
851 LOG(WARNING) << ".oat file " << oat_file->GetLocation()
Brian Carlstrom28db0122012-10-18 16:20:41 -0700852 << " mismatch ( " << std::hex << oat_file->GetOatHeader().GetImageFileLocationOatChecksum()
853 << ", " << oat_file->GetOatHeader().GetImageFileLocationOatBegin()
854 << ") with " << image_file
855 << " (" << image_oat_checksum << ", " << std::hex << image_oat_begin << ")";
Brian Carlstromafe25512012-06-27 17:02:58 -0700856 }
857 if (!dex_check) {
858 LOG(WARNING) << ".oat file " << oat_file->GetLocation()
Brian Carlstrom28db0122012-10-18 16:20:41 -0700859 << " mismatch ( " << std::hex << oat_dex_file->GetDexFileLocationChecksum()
860 << ") with " << dex_location
Brian Carlstromafe25512012-06-27 17:02:58 -0700861 << " (" << std::hex << dex_location_checksum << ")";
862 }
863 return false;
864}
865
866const DexFile* ClassLinker::VerifyAndOpenDexFileFromOatFile(const OatFile* oat_file,
867 const std::string& dex_location,
868 uint32_t dex_location_checksum) {
869 bool verified = VerifyOatFileChecksums(oat_file, dex_location, dex_location_checksum);
870 if (!verified) {
871 return NULL;
872 }
873 RegisterOatFileLocked(*oat_file);
874 return oat_file->GetOatDexFile(dex_location)->OpenDexFile();
Brian Carlstrom46b8a622012-06-19 23:13:22 -0700875}
876
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800877const DexFile* ClassLinker::FindDexFileInOatFileFromDexLocation(const std::string& dex_location) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700878 MutexLock mu(Thread::Current(), dex_lock_);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800879
Brian Carlstroma004aa92012-02-08 18:05:09 -0800880 const OatFile* open_oat_file = FindOpenedOatFileFromDexLocation(dex_location);
Brian Carlstrom866c8622012-01-06 16:35:13 -0800881 if (open_oat_file != NULL) {
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800882 return open_oat_file->GetOatDexFile(dex_location)->OpenDexFile();
Brian Carlstromae826982011-11-09 01:33:42 -0800883 }
884
Brian Carlstrom46b8a622012-06-19 23:13:22 -0700885 // Look for an existing file next to dex. for example, for
886 // /foo/bar/baz.jar, look for /foo/bar/baz.jar.oat.
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800887 std::string oat_filename(OatFile::DexFilenameToOatFilename(dex_location));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700888 const OatFile* oat_file = FindOatFileFromOatLocationLocked(oat_filename);
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800889 if (oat_file != NULL) {
Brian Carlstrom46b8a622012-06-19 23:13:22 -0700890 uint32_t dex_location_checksum;
891 if (!DexFile::GetChecksum(dex_location, dex_location_checksum)) {
892 // If no classes.dex found in dex_location, it has been stripped, assume oat is up-to-date.
893 // This is the common case in user builds for jar's and apk's in the /system directory.
894 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location);
895 CHECK(oat_dex_file != NULL) << oat_filename << " " << dex_location;
896 RegisterOatFileLocked(*oat_file);
897 return oat_dex_file->OpenDexFile();
898 }
Brian Carlstromafe25512012-06-27 17:02:58 -0700899 const DexFile* dex_file = VerifyAndOpenDexFileFromOatFile(oat_file,
900 dex_location,
901 dex_location_checksum);
Brian Carlstrom46b8a622012-06-19 23:13:22 -0700902 if (dex_file != NULL) {
903 return dex_file;
904 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800905 }
906 // Look for an existing file in the art-cache, validating the result if found
907 // not found in /foo/bar/baz.oat? try /data/art-cache/foo@bar@baz.oat
908 std::string cache_location(GetArtCacheFilenameOrDie(oat_filename));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700909 oat_file = FindOatFileFromOatLocationLocked(cache_location);
Brian Carlstroma004aa92012-02-08 18:05:09 -0800910 if (oat_file != NULL) {
911 uint32_t dex_location_checksum;
912 if (!DexFile::GetChecksum(dex_location, dex_location_checksum)) {
913 LOG(WARNING) << "Failed to compute checksum: " << dex_location;
914 return NULL;
915 }
Brian Carlstromafe25512012-06-27 17:02:58 -0700916 const DexFile* dex_file = VerifyAndOpenDexFileFromOatFile(oat_file,
917 dex_location,
918 dex_location_checksum);
Brian Carlstrom46b8a622012-06-19 23:13:22 -0700919 if (dex_file != NULL) {
920 return dex_file;
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700921 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800922 if (TEMP_FAILURE_RETRY(unlink(oat_file->GetLocation().c_str())) != 0) {
Brian Carlstrom5ef74932012-03-23 17:56:02 -0700923 PLOG(FATAL) << "Failed to remove obsolete .oat file " << oat_file->GetLocation();
Brian Carlstromd601af82012-01-06 10:15:19 -0800924 }
jeffhao262bf462011-10-20 18:36:32 -0700925 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800926 LOG(INFO) << "Failed to open oat file from " << oat_filename << " or " << cache_location << ".";
927
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800928 // Try to generate oat file if it wasn't found or was obsolete.
929 std::string oat_cache_filename(GetArtCacheFilenameOrDie(oat_filename));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700930 return FindOrCreateOatFileForDexLocationLocked(dex_location, oat_cache_filename);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700931}
932
Brian Carlstromae826982011-11-09 01:33:42 -0800933const OatFile* ClassLinker::FindOpenedOatFileFromOatLocation(const std::string& oat_location) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700934 for (size_t i = 0; i < oat_files_.size(); i++) {
935 const OatFile* oat_file = oat_files_[i];
936 DCHECK(oat_file != NULL);
Brian Carlstromae826982011-11-09 01:33:42 -0800937 if (oat_file->GetLocation() == oat_location) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700938 return oat_file;
939 }
940 }
Brian Carlstromfad71432011-10-16 20:25:10 -0700941 return NULL;
942}
Brian Carlstromaded5f72011-10-07 17:15:04 -0700943
Brian Carlstromae826982011-11-09 01:33:42 -0800944const OatFile* ClassLinker::FindOatFileFromOatLocation(const std::string& oat_location) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700945 MutexLock mu(Thread::Current(), dex_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700946 return FindOatFileFromOatLocationLocked(oat_location);
947}
948
949const OatFile* ClassLinker::FindOatFileFromOatLocationLocked(const std::string& oat_location) {
jeffhaof6174e82012-01-31 16:14:17 -0800950 const OatFile* oat_file = FindOpenedOatFileFromOatLocation(oat_location);
951 if (oat_file != NULL) {
952 return oat_file;
953 }
954
Brian Carlstrom1cac3432012-12-12 10:56:22 -0800955 oat_file = OatFile::Open(oat_location, oat_location, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700956 if (oat_file == NULL) {
Brian Carlstroma004aa92012-02-08 18:05:09 -0800957 return NULL;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700958 }
959 return oat_file;
960}
961
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700962void ClassLinker::InitFromImage() {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800963 VLOG(startup) << "ClassLinker::InitFromImage entering";
Brian Carlstroma663ea52011-08-19 23:33:41 -0700964 CHECK(!init_done_);
965
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800966 Heap* heap = Runtime::Current()->GetHeap();
Brian Carlstromfddf6f62012-03-15 16:56:45 -0700967 ImageSpace* space = heap->GetImageSpace();
968 OatFile* oat_file = OpenOat(space);
969 CHECK(oat_file != NULL) << "Failed to open oat file for image";
Brian Carlstrom28db0122012-10-18 16:20:41 -0700970 CHECK_EQ(oat_file->GetOatHeader().GetImageFileLocationOatChecksum(), 0U);
971 CHECK_EQ(oat_file->GetOatHeader().GetImageFileLocationOatBegin(), 0U);
Elliott Hughes74847412012-06-20 18:10:21 -0700972 CHECK(oat_file->GetOatHeader().GetImageFileLocation().empty());
Brian Carlstromfddf6f62012-03-15 16:56:45 -0700973 Object* dex_caches_object = space->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches);
974 ObjectArray<DexCache>* dex_caches = dex_caches_object->AsObjectArray<DexCache>();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700975
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700976 ObjectArray<Class>* class_roots =
977 space->GetImageHeader().GetImageRoot(ImageHeader::kClassRoots)->AsObjectArray<Class>();
978
Brian Carlstromfddf6f62012-03-15 16:56:45 -0700979 // Special case of setting up the String class early so that we can test arbitrary objects
980 // as being Strings or not
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700981 String::SetClass(class_roots->Get(kJavaLangString));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800982
Brian Carlstromfddf6f62012-03-15 16:56:45 -0700983 CHECK_EQ(oat_file->GetOatHeader().GetDexFileCount(),
984 static_cast<uint32_t>(dex_caches->GetLength()));
Ian Rogers1f539342012-10-03 21:09:42 -0700985 Thread* self = Thread::Current();
Brian Carlstromfddf6f62012-03-15 16:56:45 -0700986 for (int i = 0; i < dex_caches->GetLength(); i++) {
Ian Rogers1f539342012-10-03 21:09:42 -0700987 SirtRef<DexCache> dex_cache(self, dex_caches->Get(i));
Brian Carlstromfddf6f62012-03-15 16:56:45 -0700988 const std::string& dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8());
989 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file_location);
990 const DexFile* dex_file = oat_dex_file->OpenDexFile();
991 if (dex_file == NULL) {
992 LOG(FATAL) << "Failed to open dex file " << dex_file_location
993 << " from within oat file " << oat_file->GetLocation();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700994 }
Brian Carlstromfddf6f62012-03-15 16:56:45 -0700995
996 CHECK_EQ(dex_file->GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum());
997
998 AppendToBootClassPath(*dex_file, dex_cache);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700999 }
1000
Brian Carlstroma663ea52011-08-19 23:33:41 -07001001 // reinit clases_ table
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001002 {
Ian Rogers50b35e22012-10-04 10:09:15 -07001003 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001004 heap->FlushAllocStack();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -07001005 heap->GetLiveBitmap()->Walk(InitFromImageCallback, this);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001006 }
Brian Carlstroma663ea52011-08-19 23:33:41 -07001007
1008 // reinit class_roots_
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001009 Class::SetClassClass(class_roots->Get(kJavaLangClass));
1010 class_roots_ = class_roots;
Brian Carlstroma663ea52011-08-19 23:33:41 -07001011
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001012 // reinit array_iftable_ from any array class instance, they should be ==
Elliott Hughes92f14b22011-10-06 12:29:54 -07001013 array_iftable_ = GetClassRoot(kObjectArrayClass)->GetIfTable();
1014 DCHECK(array_iftable_ == GetClassRoot(kBooleanArrayClass)->GetIfTable());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001015 // String class root was set above
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001016 Field::SetClass(GetClassRoot(kJavaLangReflectField));
Mathieu Chartier66f19252012-09-18 08:57:04 -07001017 AbstractMethod::SetClasses(GetClassRoot(kJavaLangReflectConstructor),
1018 GetClassRoot(kJavaLangReflectMethod));
Brian Carlstroma663ea52011-08-19 23:33:41 -07001019 BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass));
1020 ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass));
1021 CharArray::SetArrayClass(GetClassRoot(kCharArrayClass));
1022 DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass));
1023 FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass));
1024 IntArray::SetArrayClass(GetClassRoot(kIntArrayClass));
1025 LongArray::SetArrayClass(GetClassRoot(kLongArrayClass));
1026 ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass));
Ian Rogers5167c972012-02-03 10:41:20 -08001027 Throwable::SetClass(GetClassRoot(kJavaLangThrowable));
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001028 StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement));
Brian Carlstroma663ea52011-08-19 23:33:41 -07001029
1030 FinishInit();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -07001031
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001032 VLOG(startup) << "ClassLinker::InitFromImage exiting";
Brian Carlstroma663ea52011-08-19 23:33:41 -07001033}
1034
Brian Carlstrom78128a62011-09-15 17:21:19 -07001035void ClassLinker::InitFromImageCallback(Object* obj, void* arg) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001036 DCHECK(obj != NULL);
1037 DCHECK(arg != NULL);
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001038 ClassLinker* class_linker = reinterpret_cast<ClassLinker*>(arg);
Brian Carlstroma663ea52011-08-19 23:33:41 -07001039
Elliott Hughesdbb40792011-11-18 17:05:22 -08001040 if (obj->GetClass()->IsStringClass()) {
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001041 class_linker->intern_table_->RegisterStrong(obj->AsString());
Brian Carlstromc74255f2011-09-11 22:47:39 -07001042 return;
1043 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001044 if (obj->IsClass()) {
1045 // restore class to ClassLinker::classes_ table
1046 Class* klass = obj->AsClass();
Elliott Hughesc3b77c72011-12-15 20:56:48 -08001047 ClassHelper kh(klass, class_linker);
Brian Carlstrom07bb8552012-01-18 22:10:50 -08001048 Class* existing = class_linker->InsertClass(kh.GetDescriptor(), klass, true);
1049 DCHECK(existing == NULL) << kh.GetDescriptor();
Brian Carlstroma663ea52011-08-19 23:33:41 -07001050 return;
1051 }
Brian Carlstroma663ea52011-08-19 23:33:41 -07001052}
1053
1054// Keep in sync with InitCallback. Anything we visit, we need to
1055// reinit references to when reinitializing a ClassLinker from a
1056// mapped image.
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -07001057void ClassLinker::VisitRoots(Heap::RootVisitor* visitor, void* arg) {
Elliott Hughes410c0c82011-09-01 17:58:25 -07001058 visitor(class_roots_, arg);
Ian Rogers50b35e22012-10-04 10:09:15 -07001059 Thread* self = Thread::Current();
Elliott Hughesf8349362012-06-18 15:00:06 -07001060 {
Ian Rogers50b35e22012-10-04 10:09:15 -07001061 MutexLock mu(self, dex_lock_);
Elliott Hughesf8349362012-06-18 15:00:06 -07001062 for (size_t i = 0; i < dex_caches_.size(); i++) {
1063 visitor(dex_caches_[i], arg);
1064 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001065 }
1066
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001067 {
Ian Rogers50b35e22012-10-04 10:09:15 -07001068 MutexLock mu(self, *Locks::classlinker_classes_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001069 typedef Table::const_iterator It; // TODO: C++0x auto
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001070 for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
Elliott Hughes410c0c82011-09-01 17:58:25 -07001071 visitor(it->second, arg);
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001072 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -07001073
1074 // We deliberately ignore the class roots in the image since we
1075 // handle image roots by using the MS/CMS rescanning of dirty cards.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001076 }
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001077
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001078 visitor(array_iftable_, arg);
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -07001079 is_dirty_ = false;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001080}
1081
Elliott Hughesa2155262011-11-16 16:26:58 -08001082void ClassLinker::VisitClasses(ClassVisitor* visitor, void* arg) const {
Ian Rogers50b35e22012-10-04 10:09:15 -07001083 MutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
Elliott Hughesa2155262011-11-16 16:26:58 -08001084 typedef Table::const_iterator It; // TODO: C++0x auto
1085 for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
1086 if (!visitor(it->second, arg)) {
1087 return;
1088 }
1089 }
1090 for (It it = image_classes_.begin(), end = image_classes_.end(); it != end; ++it) {
1091 if (!visitor(it->second, arg)) {
1092 return;
1093 }
1094 }
1095}
1096
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001097static bool GetClassesVisitor(Class* c, void* arg) {
1098 std::set<Class*>* classes = reinterpret_cast<std::set<Class*>*>(arg);
1099 classes->insert(c);
1100 return true;
1101}
1102
1103void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor, void* arg) const {
1104 std::set<Class*> classes;
1105 VisitClasses(GetClassesVisitor, &classes);
1106 typedef std::set<Class*>::const_iterator It; // TODO: C++0x auto
1107 for (It it = classes.begin(), end = classes.end(); it != end; ++it) {
1108 if (!visitor(*it, arg)) {
1109 return;
1110 }
1111 }
1112}
1113
1114
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001115ClassLinker::~ClassLinker() {
Mathieu Chartier02b6a782012-10-26 13:51:26 -07001116 Class::ResetClass();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001117 String::ResetClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001118 Field::ResetClass();
Mathieu Chartier66f19252012-09-18 08:57:04 -07001119 AbstractMethod::ResetClasses();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001120 BooleanArray::ResetArrayClass();
1121 ByteArray::ResetArrayClass();
1122 CharArray::ResetArrayClass();
1123 DoubleArray::ResetArrayClass();
1124 FloatArray::ResetArrayClass();
1125 IntArray::ResetArrayClass();
1126 LongArray::ResetArrayClass();
1127 ShortArray::ResetArrayClass();
Ian Rogers5167c972012-02-03 10:41:20 -08001128 Throwable::ResetClass();
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001129 StackTraceElement::ResetClass();
Brian Carlstrom58ae9412011-10-04 00:56:06 -07001130 STLDeleteElements(&boot_class_path_);
1131 STLDeleteElements(&oat_files_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001132}
1133
Ian Rogers50b35e22012-10-04 10:09:15 -07001134DexCache* ClassLinker::AllocDexCache(Thread* self, const DexFile& dex_file) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001135 Heap* heap = Runtime::Current()->GetHeap();
1136 Class* dex_cache_class = GetClassRoot(kJavaLangDexCache);
Ian Rogers50b35e22012-10-04 10:09:15 -07001137 SirtRef<DexCache> dex_cache(self,
1138 down_cast<DexCache*>(heap->AllocObject(self, dex_cache_class,
1139 dex_cache_class->GetObjectSize())));
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001140 if (dex_cache.get() == NULL) {
Elliott Hughes30646832011-10-13 16:59:46 -07001141 return NULL;
1142 }
Ian Rogers1f539342012-10-03 21:09:42 -07001143 SirtRef<String> location(self, intern_table_->InternStrong(dex_file.GetLocation().c_str()));
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001144 if (location.get() == NULL) {
Elliott Hughes30646832011-10-13 16:59:46 -07001145 return NULL;
1146 }
Ian Rogers50b35e22012-10-04 10:09:15 -07001147 SirtRef<ObjectArray<String> > strings(self, AllocStringArray(self, dex_file.NumStringIds()));
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001148 if (strings.get() == NULL) {
1149 return NULL;
1150 }
Ian Rogers50b35e22012-10-04 10:09:15 -07001151 SirtRef<ObjectArray<Class> > types(self, AllocClassArray(self, dex_file.NumTypeIds()));
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001152 if (types.get() == NULL) {
1153 return NULL;
1154 }
Ian Rogers50b35e22012-10-04 10:09:15 -07001155 SirtRef<ObjectArray<AbstractMethod> >
Ian Rogers4445a7e2012-10-05 17:19:13 -07001156 methods(self, AllocAbstractMethodArray(self, dex_file.NumMethodIds()));
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001157 if (methods.get() == NULL) {
1158 return NULL;
1159 }
Ian Rogers50b35e22012-10-04 10:09:15 -07001160 SirtRef<ObjectArray<Field> > fields(self, AllocFieldArray(self, dex_file.NumFieldIds()));
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001161 if (fields.get() == NULL) {
1162 return NULL;
1163 }
Ian Rogers1f539342012-10-03 21:09:42 -07001164 SirtRef<ObjectArray<StaticStorageBase> >
Ian Rogers50b35e22012-10-04 10:09:15 -07001165 initialized_static_storage(self,
1166 AllocObjectArray<StaticStorageBase>(self, dex_file.NumTypeIds()));
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001167 if (initialized_static_storage.get() == NULL) {
1168 return NULL;
1169 }
1170
Mathieu Chartier66f19252012-09-18 08:57:04 -07001171 dex_cache->Init(&dex_file,
1172 location.get(),
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001173 strings.get(),
1174 types.get(),
1175 methods.get(),
1176 fields.get(),
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001177 initialized_static_storage.get());
1178 return dex_cache.get();
Brian Carlstroma0808032011-07-18 00:39:23 -07001179}
1180
Ian Rogers50b35e22012-10-04 10:09:15 -07001181Class* ClassLinker::AllocClass(Thread* self, Class* java_lang_Class, size_t class_size) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001182 DCHECK_GE(class_size, sizeof(Class));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001183 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001184 SirtRef<Class> klass(self,
1185 heap->AllocObject(self, java_lang_Class, class_size)->AsClass());
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001186 klass->SetPrimitiveType(Primitive::kPrimNot); // default to not being primitive
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001187 klass->SetClassSize(class_size);
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001188 return klass.get();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001189}
1190
Ian Rogers50b35e22012-10-04 10:09:15 -07001191Class* ClassLinker::AllocClass(Thread* self, size_t class_size) {
1192 return AllocClass(self, GetClassRoot(kJavaLangClass), class_size);
Brian Carlstroma0808032011-07-18 00:39:23 -07001193}
1194
Ian Rogers50b35e22012-10-04 10:09:15 -07001195Field* ClassLinker::AllocField(Thread* self) {
1196 return down_cast<Field*>(GetClassRoot(kJavaLangReflectField)->AllocObject(self));
Brian Carlstroma0808032011-07-18 00:39:23 -07001197}
1198
Ian Rogers50b35e22012-10-04 10:09:15 -07001199Method* ClassLinker::AllocMethod(Thread* self) {
1200 return down_cast<Method*>(GetClassRoot(kJavaLangReflectMethod)->AllocObject(self));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001201}
1202
Ian Rogers50b35e22012-10-04 10:09:15 -07001203Constructor* ClassLinker::AllocConstructor(Thread* self) {
1204 return down_cast<Constructor*>(GetClassRoot(kJavaLangReflectConstructor)->AllocObject(self));
Mathieu Chartier66f19252012-09-18 08:57:04 -07001205}
1206
Ian Rogers50b35e22012-10-04 10:09:15 -07001207ObjectArray<StackTraceElement>* ClassLinker::AllocStackTraceElementArray(Thread* self,
1208 size_t length) {
1209 return ObjectArray<StackTraceElement>::Alloc(self,
1210 GetClassRoot(kJavaLangStackTraceElementArrayClass),
1211 length);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001212}
1213
Ian Rogers50b35e22012-10-04 10:09:15 -07001214static Class* EnsureResolved(Thread* self, Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001215 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001216 DCHECK(klass != NULL);
1217 // Wait for the class if it has not already been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001218 if (!klass->IsResolved() && !klass->IsErroneous()) {
Ian Rogers1f539342012-10-03 21:09:42 -07001219 ObjectLock lock(self, klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001220 // Check for circular dependencies between classes.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001221 if (!klass->IsResolved() && klass->GetClinitThreadId() == self->GetTid()) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001222 self->ThrowNewException("Ljava/lang/ClassCircularityError;",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001223 PrettyDescriptor(klass).c_str());
Brian Carlstrom4d9716c2012-01-30 01:49:33 -08001224 klass->SetStatus(Class::kStatusError);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001225 return NULL;
1226 }
1227 // Wait for the pending initialization to complete.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001228 while (!klass->IsResolved() && !klass->IsErroneous()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001229 lock.Wait();
1230 }
1231 }
1232 if (klass->IsErroneous()) {
Elliott Hughes4a2b4172011-09-20 17:08:25 -07001233 ThrowEarlierClassFailure(klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001234 return NULL;
1235 }
1236 // Return the loaded class. No exceptions should be pending.
Brian Carlstromaded5f72011-10-07 17:15:04 -07001237 CHECK(klass->IsResolved()) << PrettyClass(klass);
1238 CHECK(!self->IsExceptionPending())
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001239 << PrettyClass(klass) << " " << PrettyTypeOf(self->GetException()) << "\n"
1240 << self->GetException()->Dump();
Brian Carlstromaded5f72011-10-07 17:15:04 -07001241 return klass;
1242}
1243
Elliott Hughesdb7d5e92011-12-16 18:47:37 -08001244Class* ClassLinker::FindSystemClass(const char* descriptor) {
1245 return FindClass(descriptor, NULL);
1246}
1247
Ian Rogers365c1022012-06-22 15:05:28 -07001248Class* ClassLinker::FindClass(const char* descriptor, ClassLoader* class_loader) {
Elliott Hughesba8eee12012-01-24 20:25:24 -08001249 DCHECK_NE(*descriptor, '\0') << "descriptor is empty string";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001250 Thread* self = Thread::Current();
1251 DCHECK(self != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001252 self->AssertNoPendingException();
Elliott Hughesc3b77c72011-12-15 20:56:48 -08001253 if (descriptor[1] == '\0') {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001254 // only the descriptors of primitive types should be 1 character long, also avoid class lookup
1255 // for primitive classes that aren't backed by dex files.
1256 return FindPrimitiveClass(descriptor[0]);
1257 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001258 // Find the class in the loaded classes table.
1259 Class* klass = LookupClass(descriptor, class_loader);
1260 if (klass != NULL) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001261 return EnsureResolved(self, klass);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001262 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001263 // Class is not yet loaded.
1264 if (descriptor[0] == '[') {
1265 return CreateArrayClass(descriptor, class_loader);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001266
Jesse Wilson47daf872011-11-23 11:42:45 -05001267 } else if (class_loader == NULL) {
1268 DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, boot_class_path_);
1269 if (pair.second != NULL) {
1270 return DefineClass(descriptor, NULL, *pair.first, *pair.second);
1271 }
1272
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001273 } else if (Runtime::Current()->UseCompileTimeClassPath()) {
Jesse Wilson47daf872011-11-23 11:42:45 -05001274 // first try the boot class path
1275 Class* system_class = FindSystemClass(descriptor);
1276 if (system_class != NULL) {
1277 return system_class;
1278 }
1279 CHECK(self->IsExceptionPending());
1280 self->ClearException();
1281
1282 // next try the compile time class path
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001283 const std::vector<const DexFile*>* class_path;
1284 {
1285 ScopedObjectAccessUnchecked soa(Thread::Current());
1286 ScopedLocalRef<jobject> jclass_loader(soa.Env(), soa.AddLocalReference<jobject>(class_loader));
1287 class_path = &Runtime::Current()->GetCompileTimeClassPath(jclass_loader.get());
1288 }
1289
1290 DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, *class_path);
Jesse Wilson47daf872011-11-23 11:42:45 -05001291 if (pair.second != NULL) {
1292 return DefineClass(descriptor, class_loader, *pair.first, *pair.second);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001293 }
Jesse Wilson47daf872011-11-23 11:42:45 -05001294
1295 } else {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001296 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1297 ScopedLocalRef<jobject> class_loader_object(soa.Env(),
1298 soa.AddLocalReference<jobject>(class_loader));
Elliott Hughes95572412011-12-13 18:14:20 -08001299 std::string class_name_string(DescriptorToDot(descriptor));
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001300 ScopedLocalRef<jobject> result(soa.Env(), NULL);
Ian Rogers365c1022012-06-22 15:05:28 -07001301 {
1302 ScopedThreadStateChange tsc(self, kNative);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001303 ScopedLocalRef<jobject> class_name_object(soa.Env(),
1304 soa.Env()->NewStringUTF(class_name_string.c_str()));
Ian Rogers365c1022012-06-22 15:05:28 -07001305 if (class_name_object.get() == NULL) {
1306 return NULL;
1307 }
1308 CHECK(class_loader_object.get() != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001309 result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(),
1310 WellKnownClasses::java_lang_ClassLoader_loadClass,
1311 class_name_object.get()));
Jesse Wilson47daf872011-11-23 11:42:45 -05001312 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001313 if (soa.Env()->ExceptionCheck()) {
Elliott Hughes748382f2012-01-26 18:07:38 -08001314 // If the ClassLoader threw, pass that exception up.
1315 return NULL;
Ian Rogers761bfa82012-01-11 10:14:05 -08001316 } else if (result.get() == NULL) {
Ian Rogerscab01012012-01-10 17:35:46 -08001317 // broken loader - throw NPE to be compatible with Dalvik
1318 ThrowNullPointerException("ClassLoader.loadClass returned null for %s",
1319 class_name_string.c_str());
1320 return NULL;
Ian Rogers761bfa82012-01-11 10:14:05 -08001321 } else {
Ian Rogerscab01012012-01-10 17:35:46 -08001322 // success, return Class*
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001323 return soa.Decode<Class*>(result.get());
Ian Rogers6b0870d2011-12-15 19:38:12 -08001324 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001325 }
1326
Elliott Hughes82914b62012-04-09 15:56:29 -07001327 ThrowNoClassDefFoundError("Class %s not found", PrintableString(descriptor).c_str());
Jesse Wilson47daf872011-11-23 11:42:45 -05001328 return NULL;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001329}
1330
Elliott Hughesc3b77c72011-12-15 20:56:48 -08001331Class* ClassLinker::DefineClass(const StringPiece& descriptor,
Ian Rogers365c1022012-06-22 15:05:28 -07001332 ClassLoader* class_loader,
Brian Carlstromaded5f72011-10-07 17:15:04 -07001333 const DexFile& dex_file,
1334 const DexFile::ClassDef& dex_class_def) {
Ian Rogers1f539342012-10-03 21:09:42 -07001335 Thread* self = Thread::Current();
1336 SirtRef<Class> klass(self, NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001337 // Load the class from the dex file.
1338 if (!init_done_) {
1339 // finish up init of hand crafted class_roots_
1340 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001341 klass.reset(GetClassRoot(kJavaLangObject));
Brian Carlstromaded5f72011-10-07 17:15:04 -07001342 } else if (descriptor == "Ljava/lang/Class;") {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001343 klass.reset(GetClassRoot(kJavaLangClass));
Brian Carlstromaded5f72011-10-07 17:15:04 -07001344 } else if (descriptor == "Ljava/lang/String;") {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001345 klass.reset(GetClassRoot(kJavaLangString));
Mathieu Chartier66f19252012-09-18 08:57:04 -07001346 } else if (descriptor == "Ljava/lang/DexCache;") {
1347 klass.reset(GetClassRoot(kJavaLangDexCache));
Brian Carlstromaded5f72011-10-07 17:15:04 -07001348 } else if (descriptor == "Ljava/lang/reflect/Field;") {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001349 klass.reset(GetClassRoot(kJavaLangReflectField));
Mathieu Chartier66f19252012-09-18 08:57:04 -07001350 } else if (descriptor == "Ljava/lang/reflect/AbstractMethod;") {
1351 klass.reset(GetClassRoot(kJavaLangReflectAbstractMethod));
1352 } else if (descriptor == "Ljava/lang/reflect/Constructor;") {
1353 klass.reset(GetClassRoot(kJavaLangReflectConstructor));
Brian Carlstromaded5f72011-10-07 17:15:04 -07001354 } else if (descriptor == "Ljava/lang/reflect/Method;") {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001355 klass.reset(GetClassRoot(kJavaLangReflectMethod));
Brian Carlstromaded5f72011-10-07 17:15:04 -07001356 } else {
Ian Rogers50b35e22012-10-04 10:09:15 -07001357 klass.reset(AllocClass(self, SizeOfClass(dex_file, dex_class_def)));
Brian Carlstromaded5f72011-10-07 17:15:04 -07001358 }
1359 } else {
Ian Rogers50b35e22012-10-04 10:09:15 -07001360 klass.reset(AllocClass(self, SizeOfClass(dex_file, dex_class_def)));
Brian Carlstromaded5f72011-10-07 17:15:04 -07001361 }
1362 klass->SetDexCache(FindDexCache(dex_file));
1363 LoadClass(dex_file, dex_class_def, klass, class_loader);
1364 // Check for a pending exception during load
Brian Carlstromaded5f72011-10-07 17:15:04 -07001365 if (self->IsExceptionPending()) {
Brian Carlstrom4d9716c2012-01-30 01:49:33 -08001366 klass->SetStatus(Class::kStatusError);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001367 return NULL;
1368 }
Ian Rogers1f539342012-10-03 21:09:42 -07001369 ObjectLock lock(self, klass.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -07001370 klass->SetClinitThreadId(self->GetTid());
1371 // Add the newly loaded class to the loaded classes table.
Ian Rogers1f539342012-10-03 21:09:42 -07001372 SirtRef<Class> existing(self, InsertClass(descriptor, klass.get(), false));
Brian Carlstrom01e076e2012-03-30 11:54:16 -07001373 if (existing.get() != NULL) {
Brian Carlstrom07bb8552012-01-18 22:10:50 -08001374 // We failed to insert because we raced with another thread.
Ian Rogers50b35e22012-10-04 10:09:15 -07001375 return EnsureResolved(self, existing.get());
Brian Carlstromaded5f72011-10-07 17:15:04 -07001376 }
1377 // Finish loading (if necessary) by finding parents
1378 CHECK(!klass->IsLoaded());
1379 if (!LoadSuperAndInterfaces(klass, dex_file)) {
1380 // Loading failed.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001381 klass->SetStatus(Class::kStatusError);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001382 lock.NotifyAll();
1383 return NULL;
1384 }
1385 CHECK(klass->IsLoaded());
1386 // Link the class (if necessary)
1387 CHECK(!klass->IsResolved());
Ian Rogersc2b44472011-12-14 21:17:17 -08001388 if (!LinkClass(klass, NULL)) {
Brian Carlstromaded5f72011-10-07 17:15:04 -07001389 // Linking failed.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001390 klass->SetStatus(Class::kStatusError);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001391 lock.NotifyAll();
1392 return NULL;
1393 }
1394 CHECK(klass->IsResolved());
Elliott Hughes4740cdf2011-12-07 14:07:12 -08001395
1396 /*
1397 * We send CLASS_PREPARE events to the debugger from here. The
1398 * definition of "preparation" is creating the static fields for a
1399 * class and initializing them to the standard default values, but not
1400 * executing any code (that comes later, during "initialization").
1401 *
1402 * We did the static preparation in LinkClass.
1403 *
1404 * The class has been prepared and resolved but possibly not yet verified
1405 * at this point.
1406 */
1407 Dbg::PostClassPrepare(klass.get());
1408
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001409 return klass.get();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001410}
1411
Brian Carlstrom4873d462011-08-21 15:23:39 -07001412// Precomputes size that will be needed for Class, matching LinkStaticFields
1413size_t ClassLinker::SizeOfClass(const DexFile& dex_file,
1414 const DexFile::ClassDef& dex_class_def) {
1415 const byte* class_data = dex_file.GetClassData(dex_class_def);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001416 size_t num_ref = 0;
1417 size_t num_32 = 0;
1418 size_t num_64 = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001419 if (class_data != NULL) {
1420 for (ClassDataItemIterator it(dex_file, class_data); it.HasNextStaticField(); it.Next()) {
1421 const DexFile::FieldId& field_id = dex_file.GetFieldId(it.GetMemberIndex());
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001422 const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001423 char c = descriptor[0];
1424 if (c == 'L' || c == '[') {
1425 num_ref++;
1426 } else if (c == 'J' || c == 'D') {
1427 num_64++;
1428 } else {
1429 num_32++;
1430 }
1431 }
1432 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001433 // start with generic class data
1434 size_t size = sizeof(Class);
1435 // follow with reference fields which must be contiguous at start
1436 size += (num_ref * sizeof(uint32_t));
1437 // if there are 64-bit fields to add, make sure they are aligned
1438 if (num_64 != 0 && size != RoundUp(size, 8)) { // for 64-bit alignment
1439 if (num_32 != 0) {
1440 // use an available 32-bit field for padding
1441 num_32--;
1442 }
1443 size += sizeof(uint32_t); // either way, we are adding a word
1444 DCHECK_EQ(size, RoundUp(size, 8));
1445 }
1446 // tack on any 64-bit fields now that alignment is assured
1447 size += (num_64 * sizeof(uint64_t));
1448 // tack on any remaining 32-bit fields
1449 size += (num_32 * sizeof(uint32_t));
1450 return size;
1451}
1452
Ian Rogers19846512012-02-24 11:42:47 -08001453const OatFile::OatClass* ClassLinker::GetOatClass(const DexFile& dex_file, const char* descriptor) {
1454 DCHECK(descriptor != NULL);
Ian Rogers19846512012-02-24 11:42:47 -08001455 const OatFile* oat_file = FindOpenedOatFileForDexFile(dex_file);
1456 CHECK(oat_file != NULL) << dex_file.GetLocation() << " " << descriptor;
1457 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation());
1458 CHECK(oat_dex_file != NULL) << dex_file.GetLocation() << " " << descriptor;
1459 uint32_t class_def_index;
1460 bool found = dex_file.FindClassDefIndex(descriptor, class_def_index);
1461 CHECK(found) << dex_file.GetLocation() << " " << descriptor;
1462 const OatFile::OatClass* oat_class = oat_dex_file->GetOatClass(class_def_index);
1463 CHECK(oat_class != NULL) << dex_file.GetLocation() << " " << descriptor;
1464 return oat_class;
1465}
1466
Mathieu Chartiere35517a2012-10-30 18:49:55 -07001467static uint32_t GetOatMethodIndexFromMethodIndex(const DexFile& dex_file, uint32_t method_idx) {
1468 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
1469 const DexFile::TypeId& type_id = dex_file.GetTypeId(method_id.class_idx_);
1470 const DexFile::ClassDef* class_def = dex_file.FindClassDef(dex_file.GetTypeDescriptor(type_id));
1471 CHECK(class_def != NULL);
1472 const byte* class_data = dex_file.GetClassData(*class_def);
1473 CHECK(class_data != NULL);
1474 ClassDataItemIterator it(dex_file, class_data);
1475 // Skip fields
1476 while (it.HasNextStaticField()) {
1477 it.Next();
1478 }
1479 while (it.HasNextInstanceField()) {
1480 it.Next();
1481 }
1482 // Process methods
1483 size_t class_def_method_index = 0;
1484 while (it.HasNextDirectMethod()) {
1485 if (it.GetMemberIndex() == method_idx) {
1486 return class_def_method_index;
1487 }
1488 class_def_method_index++;
1489 it.Next();
1490 }
1491 while (it.HasNextVirtualMethod()) {
1492 if (it.GetMemberIndex() == method_idx) {
1493 return class_def_method_index;
1494 }
1495 class_def_method_index++;
1496 it.Next();
1497 }
1498 DCHECK(!it.HasNext());
1499 LOG(FATAL) << "Failed to find method index " << method_idx << " in " << dex_file.GetLocation();
1500 return 0;
1501}
1502
Mathieu Chartier66f19252012-09-18 08:57:04 -07001503const OatFile::OatMethod ClassLinker::GetOatMethodFor(const AbstractMethod* method) {
Ian Rogers19846512012-02-24 11:42:47 -08001504 // Although we overwrite the trampoline of non-static methods, we may get here via the resolution
Ian Rogersfb6adba2012-03-04 21:51:51 -08001505 // method for direct methods (or virtual methods made direct).
1506 Class* declaring_class = method->GetDeclaringClass();
1507 size_t oat_method_index;
1508 if (method->IsStatic() || method->IsDirect()) {
1509 // Simple case where the oat method index was stashed at load time.
1510 oat_method_index = method->GetMethodIndex();
1511 } else {
1512 // We're invoking a virtual method directly (thanks to sharpening), compute the oat_method_index
1513 // by search for its position in the declared virtual methods.
1514 oat_method_index = declaring_class->NumDirectMethods();
1515 size_t end = declaring_class->NumVirtualMethods();
1516 bool found = false;
1517 for (size_t i = 0; i < end; i++) {
Ian Rogersfb6adba2012-03-04 21:51:51 -08001518 if (declaring_class->GetVirtualMethod(i) == method) {
1519 found = true;
1520 break;
1521 }
Ian Rogersf320b632012-03-13 18:47:47 -07001522 oat_method_index++;
Ian Rogersfb6adba2012-03-04 21:51:51 -08001523 }
1524 CHECK(found) << "Didn't find oat method index for virtual method: " << PrettyMethod(method);
1525 }
1526 ClassHelper kh(declaring_class);
Ian Rogers19846512012-02-24 11:42:47 -08001527 UniquePtr<const OatFile::OatClass> oat_class(GetOatClass(kh.GetDexFile(), kh.GetDescriptor()));
Brian Carlstromf5822582012-03-19 22:34:31 -07001528 CHECK(oat_class.get() != NULL);
Mathieu Chartiere35517a2012-10-30 18:49:55 -07001529 DCHECK_EQ(oat_method_index,
1530 GetOatMethodIndexFromMethodIndex(*declaring_class->GetDexCache()->GetDexFile(),
1531 method->GetDexMethodIndex()));
1532
TDYa12785321912012-04-01 15:24:56 -07001533 return oat_class->GetOatMethod(oat_method_index);
1534}
1535
1536// Special case to get oat code without overwriting a trampoline.
Mathieu Chartier66f19252012-09-18 08:57:04 -07001537const void* ClassLinker::GetOatCodeFor(const AbstractMethod* method) {
TDYa127ccffd9e2012-04-08 14:37:03 -07001538 CHECK(Runtime::Current()->IsCompiler() || method->GetDeclaringClass()->IsInitializing());
TDYa12785321912012-04-01 15:24:56 -07001539 return GetOatMethodFor(method).GetCode();
1540}
1541
Mathieu Chartiere35517a2012-10-30 18:49:55 -07001542const void* ClassLinker::GetOatCodeFor(const DexFile& dex_file, uint32_t method_idx) {
1543 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
1544 const char* descriptor = dex_file.GetTypeDescriptor(dex_file.GetTypeId(method_id.class_idx_));
1545 uint32_t oat_method_idx = GetOatMethodIndexFromMethodIndex(dex_file, method_idx);
1546 UniquePtr<const OatFile::OatClass> oat_class(GetOatClass(dex_file, descriptor));
1547 CHECK(oat_class.get() != NULL);
1548 return oat_class->GetOatMethod(oat_method_idx).GetCode();
1549}
1550
Ian Rogers19846512012-02-24 11:42:47 -08001551void ClassLinker::FixupStaticTrampolines(Class* klass) {
1552 ClassHelper kh(klass);
1553 const DexFile::ClassDef* dex_class_def = kh.GetClassDef();
1554 CHECK(dex_class_def != NULL);
1555 const DexFile& dex_file = kh.GetDexFile();
1556 const byte* class_data = dex_file.GetClassData(*dex_class_def);
1557 if (class_data == NULL) {
1558 return; // no fields or methods - for example a marker interface
1559 }
Brian Carlstromf5822582012-03-19 22:34:31 -07001560 if (!Runtime::Current()->IsStarted() || Runtime::Current()->UseCompileTimeClassPath()) {
Ian Rogers19846512012-02-24 11:42:47 -08001561 // OAT file unavailable
1562 return;
1563 }
Brian Carlstromf5822582012-03-19 22:34:31 -07001564 UniquePtr<const OatFile::OatClass> oat_class(GetOatClass(dex_file, kh.GetDescriptor()));
1565 CHECK(oat_class.get() != NULL);
Ian Rogers19846512012-02-24 11:42:47 -08001566 ClassDataItemIterator it(dex_file, class_data);
1567 // Skip fields
1568 while (it.HasNextStaticField()) {
1569 it.Next();
1570 }
1571 while (it.HasNextInstanceField()) {
1572 it.Next();
1573 }
1574 size_t method_index = 0;
1575 // Link the code of methods skipped by LinkCode
1576 const void* trampoline = Runtime::Current()->GetResolutionStubArray(Runtime::kStaticMethod)->GetData();
1577 for (size_t i = 0; it.HasNextDirectMethod(); i++, it.Next()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001578 AbstractMethod* method = klass->GetDirectMethod(i);
jeffhaob5e81852012-03-12 11:15:45 -07001579 if (Runtime::Current()->IsMethodTracingActive()) {
jeffhao725a9572012-11-13 18:20:12 -08001580 Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1581 if (instrumentation->GetSavedCodeFromMap(method) == trampoline) {
jeffhaob5e81852012-03-12 11:15:45 -07001582 const void* code = oat_class->GetOatMethod(method_index).GetCode();
jeffhao725a9572012-11-13 18:20:12 -08001583 instrumentation->ResetSavedCode(method);
jeffhaob5e81852012-03-12 11:15:45 -07001584 method->SetCode(code);
jeffhao725a9572012-11-13 18:20:12 -08001585 instrumentation->SaveAndUpdateCode(method);
jeffhaob5e81852012-03-12 11:15:45 -07001586 }
1587 } else if (method->GetCode() == trampoline) {
Ian Rogers19846512012-02-24 11:42:47 -08001588 const void* code = oat_class->GetOatMethod(method_index).GetCode();
Ian Rogers08f753d2012-08-24 14:35:25 -07001589 CHECK(code != NULL)
1590 << "Resolving a static trampoline but found no code for: " << PrettyMethod(method);
Ian Rogers19846512012-02-24 11:42:47 -08001591 method->SetCode(code);
1592 }
1593 method_index++;
1594 }
1595}
1596
Mathieu Chartier66f19252012-09-18 08:57:04 -07001597static void LinkCode(SirtRef<AbstractMethod>& method, const OatFile::OatClass* oat_class,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001598 uint32_t method_index)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001599 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom92827a52011-10-10 15:50:01 -07001600 // Every kind of method should at least get an invoke stub from the oat_method.
1601 // non-abstract methods also get their code pointers.
1602 const OatFile::OatMethod oat_method = oat_class->GetOatMethod(method_index);
Brian Carlstromae826982011-11-09 01:33:42 -08001603 oat_method.LinkMethodPointers(method.get());
Brian Carlstrom92827a52011-10-10 15:50:01 -07001604
Ian Rogers19846512012-02-24 11:42:47 -08001605 Runtime* runtime = Runtime::Current();
Brian Carlstrom92827a52011-10-10 15:50:01 -07001606 if (method->IsAbstract()) {
Ian Rogers19846512012-02-24 11:42:47 -08001607 method->SetCode(runtime->GetAbstractMethodErrorStubArray()->GetData());
Brian Carlstrom92827a52011-10-10 15:50:01 -07001608 return;
1609 }
Ian Rogers19846512012-02-24 11:42:47 -08001610
1611 if (method->IsStatic() && !method->IsConstructor()) {
1612 // For static methods excluding the class initializer, install the trampoline
1613 method->SetCode(runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData());
Ian Rogers0d6de042012-02-29 08:50:26 -08001614 }
1615 if (method->IsNative()) {
Brian Carlstrom92827a52011-10-10 15:50:01 -07001616 // unregistering restores the dlsym lookup stub
Ian Rogers19846512012-02-24 11:42:47 -08001617 method->UnregisterNative(Thread::Current());
jeffhao26c0a1a2012-01-17 16:28:33 -08001618 }
1619
1620 if (Runtime::Current()->IsMethodTracingActive()) {
jeffhao725a9572012-11-13 18:20:12 -08001621 Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1622 instrumentation->SaveAndUpdateCode(method.get());
Brian Carlstrom92827a52011-10-10 15:50:01 -07001623 }
1624}
1625
Brian Carlstromf615a612011-07-23 12:50:34 -07001626void ClassLinker::LoadClass(const DexFile& dex_file,
1627 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001628 SirtRef<Class>& klass,
Ian Rogers365c1022012-06-22 15:05:28 -07001629 ClassLoader* class_loader) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001630 CHECK(klass.get() != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001631 CHECK(klass->GetDexCache() != NULL);
1632 CHECK_EQ(Class::kStatusNotReady, klass->GetStatus());
Brian Carlstromf615a612011-07-23 12:50:34 -07001633 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -07001634 CHECK(descriptor != NULL);
1635
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001636 klass->SetClass(GetClassRoot(kJavaLangClass));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001637 uint32_t access_flags = dex_class_def.access_flags_;
Elliott Hughes582a7d12011-10-10 18:38:42 -07001638 // Make sure that none of our runtime-only flags are set.
mikaelpeltier6ffd0962012-10-25 15:37:45 +02001639 // TODO: JACK CLASS ACCESS (HACK TO BE REMOVED)
1640 CHECK_EQ(access_flags & ~(kAccJavaFlagsMask | kAccClassJack), 0U);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001641 klass->SetAccessFlags(access_flags);
1642 klass->SetClassLoader(class_loader);
Ian Rogersc2b44472011-12-14 21:17:17 -08001643 DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001644 klass->SetStatus(Class::kStatusIdx);
Brian Carlstrom934486c2011-07-12 23:42:50 -07001645
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001646 klass->SetDexTypeIndex(dex_class_def.class_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -07001647
Ian Rogers0571d352011-11-03 19:51:38 -07001648 // Load fields fields.
1649 const byte* class_data = dex_file.GetClassData(dex_class_def);
1650 if (class_data == NULL) {
1651 return; // no fields or methods - for example a marker interface
Brian Carlstrom934486c2011-07-12 23:42:50 -07001652 }
Ian Rogers0571d352011-11-03 19:51:38 -07001653 ClassDataItemIterator it(dex_file, class_data);
Ian Rogers50b35e22012-10-04 10:09:15 -07001654 Thread* self = Thread::Current();
Ian Rogers0571d352011-11-03 19:51:38 -07001655 if (it.NumStaticFields() != 0) {
Ian Rogers4445a7e2012-10-05 17:19:13 -07001656 klass->SetSFields(AllocFieldArray(self, it.NumStaticFields()));
Ian Rogers0571d352011-11-03 19:51:38 -07001657 }
1658 if (it.NumInstanceFields() != 0) {
Ian Rogers4445a7e2012-10-05 17:19:13 -07001659 klass->SetIFields(AllocFieldArray(self, it.NumInstanceFields()));
Ian Rogers0571d352011-11-03 19:51:38 -07001660 }
1661 for (size_t i = 0; it.HasNextStaticField(); i++, it.Next()) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001662 SirtRef<Field> sfield(self, AllocField(self));
Ian Rogers0571d352011-11-03 19:51:38 -07001663 klass->SetStaticField(i, sfield.get());
1664 LoadField(dex_file, it, klass, sfield);
1665 }
1666 for (size_t i = 0; it.HasNextInstanceField(); i++, it.Next()) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001667 SirtRef<Field> ifield(self, AllocField(self));
Ian Rogers0571d352011-11-03 19:51:38 -07001668 klass->SetInstanceField(i, ifield.get());
1669 LoadField(dex_file, it, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -07001670 }
1671
Brian Carlstromf5822582012-03-19 22:34:31 -07001672 UniquePtr<const OatFile::OatClass> oat_class;
1673 if (Runtime::Current()->IsStarted() && !Runtime::Current()->UseCompileTimeClassPath()) {
1674 oat_class.reset(GetOatClass(dex_file, descriptor));
1675 }
Ian Rogers19846512012-02-24 11:42:47 -08001676
Ian Rogers0571d352011-11-03 19:51:38 -07001677 // Load methods.
1678 if (it.NumDirectMethods() != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -07001679 // TODO: append direct methods to class object
Ian Rogers4445a7e2012-10-05 17:19:13 -07001680 klass->SetDirectMethods(AllocAbstractMethodArray(self, it.NumDirectMethods()));
Brian Carlstrom934486c2011-07-12 23:42:50 -07001681 }
Ian Rogers0571d352011-11-03 19:51:38 -07001682 if (it.NumVirtualMethods() != 0) {
1683 // TODO: append direct methods to class object
Ian Rogers4445a7e2012-10-05 17:19:13 -07001684 klass->SetVirtualMethods(AllocMethodArray(self, it.NumVirtualMethods()));
Brian Carlstrom934486c2011-07-12 23:42:50 -07001685 }
Ian Rogersfb6adba2012-03-04 21:51:51 -08001686 size_t class_def_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001687 for (size_t i = 0; it.HasNextDirectMethod(); i++, it.Next()) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001688 SirtRef<AbstractMethod> method(self, LoadMethod(self, dex_file, it, klass));
Ian Rogers0571d352011-11-03 19:51:38 -07001689 klass->SetDirectMethod(i, method.get());
Ian Rogers0571d352011-11-03 19:51:38 -07001690 if (oat_class.get() != NULL) {
Ian Rogersfb6adba2012-03-04 21:51:51 -08001691 LinkCode(method, oat_class.get(), class_def_method_index);
Ian Rogers0571d352011-11-03 19:51:38 -07001692 }
Ian Rogersfb6adba2012-03-04 21:51:51 -08001693 method->SetMethodIndex(class_def_method_index);
1694 class_def_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001695 }
1696 for (size_t i = 0; it.HasNextVirtualMethod(); i++, it.Next()) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001697 SirtRef<AbstractMethod> method(self, LoadMethod(self, dex_file, it, klass));
Ian Rogers0571d352011-11-03 19:51:38 -07001698 klass->SetVirtualMethod(i, method.get());
Ian Rogersfb6adba2012-03-04 21:51:51 -08001699 DCHECK_EQ(class_def_method_index, it.NumDirectMethods() + i);
Ian Rogers0571d352011-11-03 19:51:38 -07001700 if (oat_class.get() != NULL) {
Ian Rogersfb6adba2012-03-04 21:51:51 -08001701 LinkCode(method, oat_class.get(), class_def_method_index);
Ian Rogers0571d352011-11-03 19:51:38 -07001702 }
Ian Rogersfb6adba2012-03-04 21:51:51 -08001703 class_def_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -07001704 }
1705 DCHECK(!it.HasNext());
Brian Carlstrom934486c2011-07-12 23:42:50 -07001706}
1707
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001708void ClassLinker::LoadField(const DexFile& /*dex_file*/, const ClassDataItemIterator& it,
Ian Rogers0571d352011-11-03 19:51:38 -07001709 SirtRef<Class>& klass, SirtRef<Field>& dst) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001710 uint32_t field_idx = it.GetMemberIndex();
1711 dst->SetDexFieldIndex(field_idx);
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001712 dst->SetDeclaringClass(klass.get());
Ian Rogers0571d352011-11-03 19:51:38 -07001713 dst->SetAccessFlags(it.GetMemberAccessFlags());
Brian Carlstrom934486c2011-07-12 23:42:50 -07001714}
1715
Ian Rogers50b35e22012-10-04 10:09:15 -07001716AbstractMethod* ClassLinker::LoadMethod(Thread* self, const DexFile& dex_file,
1717 const ClassDataItemIterator& it,
1718 SirtRef<Class>& klass) {
Ian Rogers19846512012-02-24 11:42:47 -08001719 uint32_t dex_method_idx = it.GetMemberIndex();
Ian Rogers19846512012-02-24 11:42:47 -08001720 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001721 StringPiece method_name(dex_file.GetMethodName(method_id));
Mathieu Chartier66f19252012-09-18 08:57:04 -07001722
1723 AbstractMethod* dst = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001724 if (method_name == "<init>") {
Ian Rogers50b35e22012-10-04 10:09:15 -07001725 dst = AllocConstructor(self);
Mathieu Chartier66f19252012-09-18 08:57:04 -07001726 } else {
Ian Rogers50b35e22012-10-04 10:09:15 -07001727 dst = AllocMethod(self);
Elliott Hughes80609252011-09-23 17:24:51 -07001728 }
Mathieu Chartier66f19252012-09-18 08:57:04 -07001729 DCHECK(dst->IsMethod()) << PrettyDescriptor(dst->GetClass());
1730
Ian Rogers50b35e22012-10-04 10:09:15 -07001731 const char* old_cause = self->StartAssertNoThreadSuspension("LoadMethod");
Mathieu Chartier66f19252012-09-18 08:57:04 -07001732 dst->SetDexMethodIndex(dex_method_idx);
1733 dst->SetDeclaringClass(klass.get());
Elliott Hughes20cde902011-10-04 17:37:27 -07001734
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001735 if (method_name == "finalize") {
1736 // Create the prototype for a signature of "()V"
1737 const DexFile::StringId* void_string_id = dex_file.FindStringId("V");
1738 if (void_string_id != NULL) {
1739 const DexFile::TypeId* void_type_id =
1740 dex_file.FindTypeId(dex_file.GetIndexForStringId(*void_string_id));
1741 if (void_type_id != NULL) {
1742 std::vector<uint16_t> no_args;
1743 const DexFile::ProtoId* finalizer_proto =
1744 dex_file.FindProtoId(dex_file.GetIndexForTypeId(*void_type_id), no_args);
1745 if (finalizer_proto != NULL) {
1746 // We have the prototype in the dex file
1747 if (klass->GetClassLoader() != NULL) { // All non-boot finalizer methods are flagged
1748 klass->SetFinalizable();
1749 } else {
1750 StringPiece klass_descriptor(dex_file.StringByTypeIdx(klass->GetDexTypeIndex()));
1751 // The Enum class declares a "final" finalize() method to prevent subclasses from
1752 // introducing a finalizer. We don't want to set the finalizable flag for Enum or its
1753 // subclasses, so we exclude it here.
1754 // We also want to avoid setting the flag on Object, where we know that finalize() is
1755 // empty.
1756 if (klass_descriptor != "Ljava/lang/Object;" &&
1757 klass_descriptor != "Ljava/lang/Enum;") {
1758 klass->SetFinalizable();
1759 }
1760 }
1761 }
1762 }
Elliott Hughes20cde902011-10-04 17:37:27 -07001763 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001764 }
Ian Rogers0571d352011-11-03 19:51:38 -07001765 dst->SetCodeItemOffset(it.GetMethodCodeItemOffset());
Ian Rogers0571d352011-11-03 19:51:38 -07001766 dst->SetAccessFlags(it.GetMemberAccessFlags());
Brian Carlstrom934486c2011-07-12 23:42:50 -07001767
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001768 dst->SetDexCacheStrings(klass->GetDexCache()->GetStrings());
Ian Rogers19846512012-02-24 11:42:47 -08001769 dst->SetDexCacheResolvedMethods(klass->GetDexCache()->GetResolvedMethods());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001770 dst->SetDexCacheResolvedTypes(klass->GetDexCache()->GetResolvedTypes());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001771 dst->SetDexCacheInitializedStaticStorage(klass->GetDexCache()->GetInitializedStaticStorage());
Mathieu Chartier66f19252012-09-18 08:57:04 -07001772
1773 CHECK(dst->IsMethod());
1774
Ian Rogers50b35e22012-10-04 10:09:15 -07001775 self->EndAssertNoThreadSuspension(old_cause);
Mathieu Chartier66f19252012-09-18 08:57:04 -07001776 return dst;
Brian Carlstrom934486c2011-07-12 23:42:50 -07001777}
1778
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001779void ClassLinker::AppendToBootClassPath(const DexFile& dex_file) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001780 Thread* self = Thread::Current();
1781 SirtRef<DexCache> dex_cache(self, AllocDexCache(self, dex_file));
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001782 AppendToBootClassPath(dex_file, dex_cache);
Brian Carlstroma663ea52011-08-19 23:33:41 -07001783}
1784
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001785void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, SirtRef<DexCache>& dex_cache) {
1786 CHECK(dex_cache.get() != NULL) << dex_file.GetLocation();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001787 boot_class_path_.push_back(&dex_file);
Brian Carlstroma663ea52011-08-19 23:33:41 -07001788 RegisterDexFile(dex_file, dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001789}
1790
Brian Carlstromaded5f72011-10-07 17:15:04 -07001791bool ClassLinker::IsDexFileRegisteredLocked(const DexFile& dex_file) const {
Ian Rogers50b35e22012-10-04 10:09:15 -07001792 dex_lock_.AssertHeld(Thread::Current());
Mathieu Chartier66f19252012-09-18 08:57:04 -07001793 for (size_t i = 0; i != dex_caches_.size(); ++i) {
1794 if (dex_caches_[i]->GetDexFile() == &dex_file) {
Ian Rogers19846512012-02-24 11:42:47 -08001795 return true;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001796 }
1797 }
1798 return false;
Brian Carlstroma663ea52011-08-19 23:33:41 -07001799}
1800
Brian Carlstromaded5f72011-10-07 17:15:04 -07001801bool ClassLinker::IsDexFileRegistered(const DexFile& dex_file) const {
Ian Rogers50b35e22012-10-04 10:09:15 -07001802 MutexLock mu(Thread::Current(), dex_lock_);
Brian Carlstrom06918512011-10-16 23:39:12 -07001803 return IsDexFileRegisteredLocked(dex_file);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001804}
1805
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001806void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, SirtRef<DexCache>& dex_cache) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001807 dex_lock_.AssertHeld(Thread::Current());
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001808 CHECK(dex_cache.get() != NULL) << dex_file.GetLocation();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07001809 CHECK(dex_cache->GetLocation()->Equals(dex_file.GetLocation()));
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001810 dex_caches_.push_back(dex_cache.get());
Mathieu Chartier66f19252012-09-18 08:57:04 -07001811 dex_cache->SetDexFile(&dex_file);
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -07001812 Dirty();
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001813}
1814
Brian Carlstromaded5f72011-10-07 17:15:04 -07001815void ClassLinker::RegisterDexFile(const DexFile& dex_file) {
Ian Rogers1f539342012-10-03 21:09:42 -07001816 Thread* self = Thread::Current();
Brian Carlstrom47d237a2011-10-18 15:08:33 -07001817 {
Ian Rogers1f539342012-10-03 21:09:42 -07001818 MutexLock mu(self, dex_lock_);
Brian Carlstrom47d237a2011-10-18 15:08:33 -07001819 if (IsDexFileRegisteredLocked(dex_file)) {
1820 return;
1821 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001822 }
Brian Carlstrom47d237a2011-10-18 15:08:33 -07001823 // Don't alloc while holding the lock, since allocation may need to
1824 // suspend all threads and another thread may need the dex_lock_ to
1825 // get to a suspend point.
Ian Rogers50b35e22012-10-04 10:09:15 -07001826 SirtRef<DexCache> dex_cache(self, AllocDexCache(self, dex_file));
Brian Carlstrom47d237a2011-10-18 15:08:33 -07001827 {
Ian Rogers1f539342012-10-03 21:09:42 -07001828 MutexLock mu(self, dex_lock_);
Brian Carlstrom47d237a2011-10-18 15:08:33 -07001829 if (IsDexFileRegisteredLocked(dex_file)) {
1830 return;
1831 }
1832 RegisterDexFileLocked(dex_file, dex_cache);
1833 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001834}
1835
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001836void ClassLinker::RegisterDexFile(const DexFile& dex_file, SirtRef<DexCache>& dex_cache) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001837 MutexLock mu(Thread::Current(), dex_lock_);
Brian Carlstromaded5f72011-10-07 17:15:04 -07001838 RegisterDexFileLocked(dex_file, dex_cache);
1839}
1840
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001841DexCache* ClassLinker::FindDexCache(const DexFile& dex_file) const {
Ian Rogers50b35e22012-10-04 10:09:15 -07001842 MutexLock mu(Thread::Current(), dex_lock_);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001843 // Search assuming unique-ness of dex file.
Mathieu Chartier66f19252012-09-18 08:57:04 -07001844 for (size_t i = 0; i != dex_caches_.size(); ++i) {
1845 DexCache* dex_cache = dex_caches_[i];
1846 if (dex_cache->GetDexFile() == &dex_file) {
1847 return dex_cache;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001848 }
1849 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001850 // Search matching by location name.
1851 std::string location(dex_file.GetLocation());
1852 for (size_t i = 0; i != dex_caches_.size(); ++i) {
1853 DexCache* dex_cache = dex_caches_[i];
1854 if (dex_cache->GetDexFile()->GetLocation() == location) {
1855 return dex_cache;
1856 }
1857 }
1858 // Failure, dump diagnostic and abort.
1859 for (size_t i = 0; i != dex_caches_.size(); ++i) {
1860 DexCache* dex_cache = dex_caches_[i];
1861 LOG(ERROR) << "Registered dex file " << i << " = " << dex_cache->GetDexFile()->GetLocation();
1862 }
1863 LOG(FATAL) << "Failed to find DexCache for DexFile " << location;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001864 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001865}
1866
Mathieu Chartier66f19252012-09-18 08:57:04 -07001867void ClassLinker::FixupDexCaches(AbstractMethod* resolution_method) const {
Ian Rogers50b35e22012-10-04 10:09:15 -07001868 MutexLock mu(Thread::Current(), dex_lock_);
Ian Rogers19846512012-02-24 11:42:47 -08001869 for (size_t i = 0; i != dex_caches_.size(); ++i) {
1870 dex_caches_[i]->Fixup(resolution_method);
1871 }
1872}
1873
Ian Rogersc8982582012-09-07 16:53:25 -07001874Class* ClassLinker::InitializePrimitiveClass(Class* primitive_class, Primitive::Type type) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001875 CHECK(primitive_class != NULL);
Ian Rogers1f539342012-10-03 21:09:42 -07001876 // Must hold lock on object when initializing.
1877 ObjectLock lock(Thread::Current(), primitive_class);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001878 primitive_class->SetAccessFlags(kAccPublic | kAccFinal | kAccAbstract);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001879 primitive_class->SetPrimitiveType(type);
1880 primitive_class->SetStatus(Class::kStatusInitialized);
Ian Rogersc8982582012-09-07 16:53:25 -07001881 Class* existing = InsertClass(Primitive::Descriptor(type), primitive_class, false);
1882 CHECK(existing == NULL) << "InitPrimitiveClass(" << type << ") failed";
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001883 return primitive_class;
Carl Shapiro565f5072011-07-10 13:39:43 -07001884}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001885
Brian Carlstrombe977852011-07-19 14:54:54 -07001886// Create an array class (i.e. the class object for the array, not the
1887// array itself). "descriptor" looks like "[C" or "[[[[B" or
1888// "[Ljava/lang/String;".
1889//
1890// If "descriptor" refers to an array of primitives, look up the
1891// primitive type's internally-generated class object.
1892//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001893// "class_loader" is the class loader of the class that's referring to
1894// us. It's used to ensure that we're looking for the element type in
1895// the right context. It does NOT become the class loader for the
1896// array class; that always comes from the base element class.
Brian Carlstrombe977852011-07-19 14:54:54 -07001897//
1898// Returns NULL with an exception raised on failure.
Ian Rogers365c1022012-06-22 15:05:28 -07001899Class* ClassLinker::CreateArrayClass(const std::string& descriptor, ClassLoader* class_loader) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001900 CHECK_EQ('[', descriptor[0]);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001901
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001902 // Identify the underlying component type
Elliott Hughesc3b77c72011-12-15 20:56:48 -08001903 Class* component_type = FindClass(descriptor.substr(1).c_str(), class_loader);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001904 if (component_type == NULL) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001905 DCHECK(Thread::Current()->IsExceptionPending());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001906 return NULL;
1907 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001908
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001909 // See if the component type is already loaded. Array classes are
1910 // always associated with the class loader of their underlying
1911 // element type -- an array of Strings goes with the loader for
1912 // java/lang/String -- so we need to look for it there. (The
1913 // caller should have checked for the existence of the class
1914 // before calling here, but they did so with *their* class loader,
1915 // not the component type's loader.)
1916 //
1917 // If we find it, the caller adds "loader" to the class' initiating
1918 // loader list, which should prevent us from going through this again.
1919 //
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001920 // This call is unnecessary if "loader" and "component_type->GetClassLoader()"
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001921 // are the same, because our caller (FindClass) just did the
1922 // lookup. (Even if we get this wrong we still have correct behavior,
1923 // because we effectively do this lookup again when we add the new
1924 // class to the hash table --- necessary because of possible races with
1925 // other threads.)
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001926 if (class_loader != component_type->GetClassLoader()) {
Elliott Hughesc3b77c72011-12-15 20:56:48 -08001927 Class* new_class = LookupClass(descriptor.c_str(), component_type->GetClassLoader());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001928 if (new_class != NULL) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001929 return new_class;
1930 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001931 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001932
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001933 // Fill out the fields in the Class.
1934 //
1935 // It is possible to execute some methods against arrays, because
1936 // all arrays are subclasses of java_lang_Object_, so we need to set
1937 // up a vtable. We can just point at the one in java_lang_Object_.
1938 //
1939 // Array classes are simple enough that we don't need to do a full
1940 // link step.
Ian Rogers1f539342012-10-03 21:09:42 -07001941 Thread* self = Thread::Current();
1942 SirtRef<Class> new_class(self, NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001943 if (!init_done_) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001944 // Classes that were hand created, ie not by FindSystemClass
Elliott Hughes418d20f2011-09-22 14:00:39 -07001945 if (descriptor == "[Ljava/lang/Class;") {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001946 new_class.reset(GetClassRoot(kClassArrayClass));
Elliott Hughes418d20f2011-09-22 14:00:39 -07001947 } else if (descriptor == "[Ljava/lang/Object;") {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001948 new_class.reset(GetClassRoot(kObjectArrayClass));
Mathieu Chartier66f19252012-09-18 08:57:04 -07001949 } else if (descriptor == class_roots_descriptors_[kJavaLangStringArrayClass]) {
1950 new_class.reset(GetClassRoot(kJavaLangStringArrayClass));
Mathieu Chartier66f19252012-09-18 08:57:04 -07001951 } else if (descriptor == class_roots_descriptors_[kJavaLangReflectAbstractMethodArrayClass]) {
1952 new_class.reset(GetClassRoot(kJavaLangReflectAbstractMethodArrayClass));
Ian Rogers4445a7e2012-10-05 17:19:13 -07001953 } else if (descriptor == class_roots_descriptors_[kJavaLangReflectFieldArrayClass]) {
1954 new_class.reset(GetClassRoot(kJavaLangReflectFieldArrayClass));
1955 } else if (descriptor == class_roots_descriptors_[kJavaLangReflectMethodArrayClass]) {
1956 new_class.reset(GetClassRoot(kJavaLangReflectMethodArrayClass));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001957 } else if (descriptor == "[C") {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001958 new_class.reset(GetClassRoot(kCharArrayClass));
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001959 } else if (descriptor == "[I") {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001960 new_class.reset(GetClassRoot(kIntArrayClass));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001961 }
1962 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001963 if (new_class.get() == NULL) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001964 new_class.reset(AllocClass(self, sizeof(Class)));
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001965 if (new_class.get() == NULL) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001966 return NULL;
1967 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001968 new_class->SetComponentType(component_type);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001969 }
Ian Rogers1f539342012-10-03 21:09:42 -07001970 ObjectLock lock(self, new_class.get()); // Must hold lock on object when initializing.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001971 DCHECK(new_class->GetComponentType() != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001972 Class* java_lang_Object = GetClassRoot(kJavaLangObject);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001973 new_class->SetSuperClass(java_lang_Object);
1974 new_class->SetVTable(java_lang_Object->GetVTable());
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001975 new_class->SetPrimitiveType(Primitive::kPrimNot);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001976 new_class->SetClassLoader(component_type->GetClassLoader());
1977 new_class->SetStatus(Class::kStatusInitialized);
1978 // don't need to set new_class->SetObjectSize(..)
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001979 // because Object::SizeOf delegates to Array::SizeOf
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001980
1981
1982 // All arrays have java/lang/Cloneable and java/io/Serializable as
1983 // interfaces. We need to set that up here, so that stuff like
1984 // "instanceof" works right.
1985 //
1986 // Note: The GC could run during the call to FindSystemClass,
1987 // so we need to make sure the class object is GC-valid while we're in
1988 // there. Do this by clearing the interface list so the GC will just
1989 // think that the entries are null.
1990
1991
1992 // Use the single, global copies of "interfaces" and "iftable"
1993 // (remember not to free them for arrays).
Elliott Hughes92f14b22011-10-06 12:29:54 -07001994 CHECK(array_iftable_ != NULL);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001995 new_class->SetIfTable(array_iftable_);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001996
1997 // Inherit access flags from the component type. Arrays can't be
1998 // used as a superclass or interface, so we want to add "final"
1999 // and remove "interface".
2000 //
2001 // Don't inherit any non-standard flags (e.g., kAccFinal)
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002002 // from component_type. We assume that the array class does not
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07002003 // override finalize().
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002004 new_class->SetAccessFlags(((new_class->GetComponentType()->GetAccessFlags() &
2005 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07002006
Brian Carlstrom07bb8552012-01-18 22:10:50 -08002007 Class* existing = InsertClass(descriptor, new_class.get(), false);
2008 if (existing == NULL) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07002009 return new_class.get();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07002010 }
2011 // Another thread must have loaded the class after we
2012 // started but before we finished. Abandon what we've
2013 // done.
2014 //
2015 // (Yes, this happens.)
2016
Brian Carlstrom07bb8552012-01-18 22:10:50 -08002017 return existing;
Brian Carlstroma331b3c2011-07-18 17:47:56 -07002018}
2019
2020Class* ClassLinker::FindPrimitiveClass(char type) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002021 switch (Primitive::GetType(type)) {
2022 case Primitive::kPrimByte:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07002023 return GetClassRoot(kPrimitiveByte);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002024 case Primitive::kPrimChar:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07002025 return GetClassRoot(kPrimitiveChar);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002026 case Primitive::kPrimDouble:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07002027 return GetClassRoot(kPrimitiveDouble);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002028 case Primitive::kPrimFloat:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07002029 return GetClassRoot(kPrimitiveFloat);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002030 case Primitive::kPrimInt:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07002031 return GetClassRoot(kPrimitiveInt);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002032 case Primitive::kPrimLong:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07002033 return GetClassRoot(kPrimitiveLong);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002034 case Primitive::kPrimShort:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07002035 return GetClassRoot(kPrimitiveShort);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002036 case Primitive::kPrimBoolean:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07002037 return GetClassRoot(kPrimitiveBoolean);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002038 case Primitive::kPrimVoid:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07002039 return GetClassRoot(kPrimitiveVoid);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002040 case Primitive::kPrimNot:
2041 break;
Carl Shapiro744ad052011-08-06 15:53:36 -07002042 }
Elliott Hughesbd935992011-08-22 11:59:34 -07002043 std::string printable_type(PrintableChar(type));
Elliott Hughes4a2b4172011-09-20 17:08:25 -07002044 ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str());
Elliott Hughesbd935992011-08-22 11:59:34 -07002045 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002046}
2047
Brian Carlstrom07bb8552012-01-18 22:10:50 -08002048Class* ClassLinker::InsertClass(const StringPiece& descriptor, Class* klass, bool image_class) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002049 if (VLOG_IS_ON(class_linker)) {
Brian Carlstromae826982011-11-09 01:33:42 -08002050 DexCache* dex_cache = klass->GetDexCache();
2051 std::string source;
2052 if (dex_cache != NULL) {
2053 source += " from ";
2054 source += dex_cache->GetLocation()->ToModifiedUtf8();
2055 }
2056 LOG(INFO) << "Loaded class " << descriptor << source;
2057 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07002058 size_t hash = StringPieceHash()(descriptor);
Ian Rogers50b35e22012-10-04 10:09:15 -07002059 MutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
Brian Carlstrom07bb8552012-01-18 22:10:50 -08002060 Table& classes = image_class ? image_classes_ : classes_;
Elliott Hughesf8349362012-06-18 15:00:06 -07002061 Class* existing = LookupClassLocked(descriptor.data(), klass->GetClassLoader(), hash, classes);
Brian Carlstrom07bb8552012-01-18 22:10:50 -08002062#ifndef NDEBUG
2063 // Check we don't have the class in the other table in error
2064 Table& other_classes = image_class ? classes_ : image_classes_;
Elliott Hughesf8349362012-06-18 15:00:06 -07002065 CHECK(LookupClassLocked(descriptor.data(), klass->GetClassLoader(), hash, other_classes) == NULL);
Brian Carlstrom07bb8552012-01-18 22:10:50 -08002066#endif
2067 if (existing != NULL) {
2068 return existing;
Ian Rogers5d76c432011-10-31 21:42:49 -07002069 }
Brian Carlstrom07bb8552012-01-18 22:10:50 -08002070 classes.insert(std::make_pair(hash, klass));
Mathieu Chartier9ebae1f2012-10-15 17:38:16 -07002071 Dirty();
Brian Carlstrom07bb8552012-01-18 22:10:50 -08002072 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002073}
2074
Elliott Hughesc3b77c72011-12-15 20:56:48 -08002075bool ClassLinker::RemoveClass(const char* descriptor, const ClassLoader* class_loader) {
2076 size_t hash = Hash(descriptor);
Ian Rogers50b35e22012-10-04 10:09:15 -07002077 MutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
Elliott Hughese5448b52012-01-18 16:44:06 -08002078 typedef Table::iterator It; // TODO: C++0x auto
Brian Carlstromae826982011-11-09 01:33:42 -08002079 // TODO: determine if its better to search classes_ or image_classes_ first
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002080 ClassHelper kh;
Brian Carlstrom07bb8552012-01-18 22:10:50 -08002081 for (It it = classes_.lower_bound(hash), end = classes_.end(); it != end && it->first == hash; ++it) {
Brian Carlstromae826982011-11-09 01:33:42 -08002082 Class* klass = it->second;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002083 kh.ChangeClass(klass);
Elliott Hughesc3b77c72011-12-15 20:56:48 -08002084 if (strcmp(kh.GetDescriptor(), descriptor) == 0 && klass->GetClassLoader() == class_loader) {
Brian Carlstromae826982011-11-09 01:33:42 -08002085 classes_.erase(it);
2086 return true;
2087 }
2088 }
Brian Carlstrom07bb8552012-01-18 22:10:50 -08002089 for (It it = image_classes_.lower_bound(hash), end = classes_.end(); it != end && it->first == hash; ++it) {
Brian Carlstromae826982011-11-09 01:33:42 -08002090 Class* klass = it->second;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002091 kh.ChangeClass(klass);
Elliott Hughesc3b77c72011-12-15 20:56:48 -08002092 if (strcmp(kh.GetDescriptor(), descriptor) == 0 && klass->GetClassLoader() == class_loader) {
Brian Carlstromae826982011-11-09 01:33:42 -08002093 image_classes_.erase(it);
2094 return true;
2095 }
2096 }
2097 return false;
2098}
2099
Elliott Hughesc3b77c72011-12-15 20:56:48 -08002100Class* ClassLinker::LookupClass(const char* descriptor, const ClassLoader* class_loader) {
2101 size_t hash = Hash(descriptor);
Ian Rogers50b35e22012-10-04 10:09:15 -07002102 MutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -07002103 // TODO: determine if its better to search classes_ or image_classes_ first
Elliott Hughesf8349362012-06-18 15:00:06 -07002104 Class* klass = LookupClassLocked(descriptor, class_loader, hash, classes_);
Brian Carlstrom07bb8552012-01-18 22:10:50 -08002105 if (klass != NULL) {
2106 return klass;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002107 }
Elliott Hughesf8349362012-06-18 15:00:06 -07002108 return LookupClassLocked(descriptor, class_loader, hash, image_classes_);
Brian Carlstrom07bb8552012-01-18 22:10:50 -08002109}
2110
Elliott Hughesf8349362012-06-18 15:00:06 -07002111Class* ClassLinker::LookupClassLocked(const char* descriptor, const ClassLoader* class_loader,
2112 size_t hash, const Table& classes) {
Brian Carlstrom07bb8552012-01-18 22:10:50 -08002113 ClassHelper kh(NULL, this);
2114 typedef Table::const_iterator It; // TODO: C++0x auto
2115 for (It it = classes.lower_bound(hash), end = classes_.end(); it != end && it->first == hash; ++it) {
Ian Rogers5d76c432011-10-31 21:42:49 -07002116 Class* klass = it->second;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002117 kh.ChangeClass(klass);
Elliott Hughesc3b77c72011-12-15 20:56:48 -08002118 if (strcmp(descriptor, kh.GetDescriptor()) == 0 && klass->GetClassLoader() == class_loader) {
Brian Carlstrom07bb8552012-01-18 22:10:50 -08002119#ifndef NDEBUG
2120 for (++it; it != end && it->first == hash; ++it) {
Ian Rogersd85016c2012-02-03 18:27:34 -08002121 Class* klass2 = it->second;
2122 kh.ChangeClass(klass2);
2123 CHECK(!(strcmp(descriptor, kh.GetDescriptor()) == 0 && klass2->GetClassLoader() == class_loader))
Brian Carlstrom07bb8552012-01-18 22:10:50 -08002124 << PrettyClass(klass) << " " << klass << " " << klass->GetClassLoader() << " "
Ian Rogersd85016c2012-02-03 18:27:34 -08002125 << PrettyClass(klass2) << " " << klass2 << " " << klass2->GetClassLoader();
Brian Carlstrom07bb8552012-01-18 22:10:50 -08002126 }
2127#endif
Ian Rogers5d76c432011-10-31 21:42:49 -07002128 return klass;
2129 }
2130 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07002131 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002132}
2133
Elliott Hughesc3b77c72011-12-15 20:56:48 -08002134void ClassLinker::LookupClasses(const char* descriptor, std::vector<Class*>& classes) {
Elliott Hughes6fa602d2011-12-02 17:54:25 -08002135 classes.clear();
Elliott Hughesc3b77c72011-12-15 20:56:48 -08002136 size_t hash = Hash(descriptor);
Ian Rogers50b35e22012-10-04 10:09:15 -07002137 MutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
Elliott Hughes6fa602d2011-12-02 17:54:25 -08002138 typedef Table::const_iterator It; // TODO: C++0x auto
2139 // TODO: determine if its better to search classes_ or image_classes_ first
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002140 ClassHelper kh(NULL, this);
Brian Carlstrom07bb8552012-01-18 22:10:50 -08002141 for (It it = classes_.lower_bound(hash), end = classes_.end(); it != end && it->first == hash; ++it) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002142 Class* klass = it->second;
2143 kh.ChangeClass(klass);
Elliott Hughesc3b77c72011-12-15 20:56:48 -08002144 if (strcmp(descriptor, kh.GetDescriptor()) == 0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002145 classes.push_back(klass);
Elliott Hughes6fa602d2011-12-02 17:54:25 -08002146 }
2147 }
Brian Carlstrom07bb8552012-01-18 22:10:50 -08002148 for (It it = image_classes_.lower_bound(hash), end = classes_.end(); it != end && it->first == hash; ++it) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002149 Class* klass = it->second;
2150 kh.ChangeClass(klass);
Elliott Hughesc3b77c72011-12-15 20:56:48 -08002151 if (strcmp(descriptor, kh.GetDescriptor()) == 0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002152 classes.push_back(klass);
Elliott Hughes6fa602d2011-12-02 17:54:25 -08002153 }
2154 }
2155}
2156
jeffhao98eacac2011-09-14 16:11:53 -07002157void ClassLinker::VerifyClass(Class* klass) {
Brian Carlstrom9b5ee882012-02-28 09:48:54 -08002158 // TODO: assert that the monitor on the Class is held
Ian Rogers1f539342012-10-03 21:09:42 -07002159 Thread* self = Thread::Current();
2160 ObjectLock lock(self, klass);
Elliott Hughesd9c67be2012-02-02 19:54:06 -08002161
Ian Rogers9ffb0392012-09-10 11:56:50 -07002162 // Don't attempt to re-verify if already sufficiently verified.
2163 if (klass->IsVerified() ||
2164 (klass->IsCompileTimeVerified() && Runtime::Current()->IsCompiler())) {
jeffhao98eacac2011-09-14 16:11:53 -07002165 return;
2166 }
2167
Ian Rogers9ffb0392012-09-10 11:56:50 -07002168 // The class might already be erroneous, for example at compile time if we attempted to verify
2169 // this class as a parent to another.
Brian Carlstrom9b5ee882012-02-28 09:48:54 -08002170 if (klass->IsErroneous()) {
2171 ThrowEarlierClassFailure(klass);
2172 return;
2173 }
2174
Ian Rogers9ffb0392012-09-10 11:56:50 -07002175 if (klass->GetStatus() == Class::kStatusResolved) {
2176 klass->SetStatus(Class::kStatusVerifying);
2177 } else {
2178 CHECK_EQ(klass->GetStatus(), Class::kStatusRetryVerificationAtRuntime) << PrettyClass(klass);
2179 CHECK(!Runtime::Current()->IsCompiler());
2180 klass->SetStatus(Class::kStatusVerifyingAtRuntime);
2181 }
jeffhao98eacac2011-09-14 16:11:53 -07002182
Ian Rogers9ffb0392012-09-10 11:56:50 -07002183 // Verify super class.
Ian Rogers1c5eb702012-02-01 09:18:34 -08002184 Class* super = klass->GetSuperClass();
2185 std::string error_msg;
2186 if (super != NULL) {
Ian Rogers9ffb0392012-09-10 11:56:50 -07002187 // Acquire lock to prevent races on verifying the super class.
Ian Rogers1f539342012-10-03 21:09:42 -07002188 ObjectLock lock(self, super);
Ian Rogers1c5eb702012-02-01 09:18:34 -08002189
2190 if (!super->IsVerified() && !super->IsErroneous()) {
2191 Runtime::Current()->GetClassLinker()->VerifyClass(super);
2192 }
jeffhaof1e6b7c2012-06-05 18:33:30 -07002193 if (!super->IsCompileTimeVerified()) {
Ian Rogers1c5eb702012-02-01 09:18:34 -08002194 error_msg = "Rejecting class ";
2195 error_msg += PrettyDescriptor(klass);
2196 error_msg += " that attempts to sub-class erroneous class ";
2197 error_msg += PrettyDescriptor(super);
2198 LOG(ERROR) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8();
Ian Rogers1f539342012-10-03 21:09:42 -07002199 SirtRef<Throwable> cause(self, self->GetException());
Ian Rogers1c5eb702012-02-01 09:18:34 -08002200 if (cause.get() != NULL) {
2201 self->ClearException();
2202 }
2203 self->ThrowNewException("Ljava/lang/VerifyError;", error_msg.c_str());
2204 if (cause.get() != NULL) {
2205 self->GetException()->SetCause(cause.get());
2206 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08002207 klass->SetStatus(Class::kStatusError);
2208 return;
2209 }
2210 }
2211
Elliott Hughes634eb2e2012-03-22 16:06:28 -07002212 // Try to use verification information from the oat file, otherwise do runtime verification.
Ian Rogers4445a7e2012-10-05 17:19:13 -07002213 const DexFile& dex_file = *klass->GetDexCache()->GetDexFile();
Elliott Hughes634eb2e2012-03-22 16:06:28 -07002214 Class::Status oat_file_class_status(Class::kStatusNotReady);
2215 bool preverified = VerifyClassUsingOatFile(dex_file, klass, oat_file_class_status);
jeffhaof1e6b7c2012-06-05 18:33:30 -07002216 verifier::MethodVerifier::FailureKind verifier_failure = verifier::MethodVerifier::kNoFailure;
jeffhaoec014232012-09-05 10:42:25 -07002217 if (oat_file_class_status == Class::kStatusError) {
2218 LOG(WARNING) << "Skipping runtime verification of erroneous class " << PrettyDescriptor(klass)
2219 << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8();
2220 error_msg = "Rejecting class ";
2221 error_msg += PrettyDescriptor(klass);
2222 error_msg += " because it failed compile-time verification";
2223 Thread::Current()->ThrowNewException("Ljava/lang/VerifyError;", error_msg.c_str());
2224 klass->SetStatus(Class::kStatusError);
2225 return;
2226 }
jeffhaof1e6b7c2012-06-05 18:33:30 -07002227 if (!preverified) {
2228 verifier_failure = verifier::MethodVerifier::VerifyClass(klass, error_msg);
2229 }
2230 if (preverified || verifier_failure != verifier::MethodVerifier::kHardFailure) {
Ian Rogers529781d2012-07-23 17:24:29 -07002231 if (!preverified && verifier_failure != verifier::MethodVerifier::kNoFailure) {
2232 LOG(WARNING) << "Soft verification failure in class " << PrettyDescriptor(klass)
2233 << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8()
2234 << " because: " << error_msg;
2235 }
Ian Rogers1f539342012-10-03 21:09:42 -07002236 self->AssertNoPendingException();
jeffhaoe4f0b2a2012-08-30 11:18:57 -07002237 // Make sure all classes referenced by catch blocks are resolved.
Brian Carlstrome7d856b2012-01-11 18:10:55 -08002238 ResolveClassExceptionHandlerTypes(dex_file, klass);
jeffhaoe4f0b2a2012-08-30 11:18:57 -07002239 if (verifier_failure == verifier::MethodVerifier::kNoFailure) {
2240 klass->SetStatus(Class::kStatusVerified);
2241 } else {
2242 CHECK_EQ(verifier_failure, verifier::MethodVerifier::kSoftFailure);
2243 // Soft failures at compile time should be retried at runtime. Soft
2244 // failures at runtime will be handled by slow paths in the generated
2245 // code. Set status accordingly.
2246 if (Runtime::Current()->IsCompiler()) {
2247 klass->SetStatus(Class::kStatusRetryVerificationAtRuntime);
2248 } else {
2249 klass->SetStatus(Class::kStatusVerified);
2250 }
2251 }
jeffhao5cfd6fb2011-09-27 13:54:29 -07002252 } else {
Ian Rogers09f6b562012-01-31 21:58:52 -08002253 LOG(ERROR) << "Verification failed on class " << PrettyDescriptor(klass)
Ian Rogers1c5eb702012-02-01 09:18:34 -08002254 << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8()
2255 << " because: " << error_msg;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002256 self->AssertNoPendingException();
Ian Rogers1c5eb702012-02-01 09:18:34 -08002257 self->ThrowNewException("Ljava/lang/VerifyError;", error_msg.c_str());
Elliott Hughesd9cdfe92011-10-06 16:09:04 -07002258 klass->SetStatus(Class::kStatusError);
jeffhao5cfd6fb2011-09-27 13:54:29 -07002259 }
jeffhao98eacac2011-09-14 16:11:53 -07002260}
2261
Elliott Hughes634eb2e2012-03-22 16:06:28 -07002262bool ClassLinker::VerifyClassUsingOatFile(const DexFile& dex_file, Class* klass,
2263 Class::Status& oat_file_class_status) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08002264 if (!Runtime::Current()->IsStarted()) {
2265 return false;
2266 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002267 if (Runtime::Current()->UseCompileTimeClassPath()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08002268 return false;
2269 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -08002270 const OatFile* oat_file = FindOpenedOatFileForDexFile(dex_file);
2271 CHECK(oat_file != NULL) << dex_file.GetLocation() << " " << PrettyClass(klass);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08002272 const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation());
Brian Carlstrom5b332c82012-02-01 15:02:31 -08002273 CHECK(oat_dex_file != NULL) << dex_file.GetLocation() << " " << PrettyClass(klass);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08002274 const char* descriptor = ClassHelper(klass).GetDescriptor();
2275 uint32_t class_def_index;
2276 bool found = dex_file.FindClassDefIndex(descriptor, class_def_index);
Brian Carlstrom5b332c82012-02-01 15:02:31 -08002277 CHECK(found) << dex_file.GetLocation() << " " << PrettyClass(klass) << " " << descriptor;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08002278 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index));
Brian Carlstrom5b332c82012-02-01 15:02:31 -08002279 CHECK(oat_class.get() != NULL)
2280 << dex_file.GetLocation() << " " << PrettyClass(klass) << " " << descriptor;
Elliott Hughes634eb2e2012-03-22 16:06:28 -07002281 oat_file_class_status = oat_class->GetStatus();
Ian Rogers9ffb0392012-09-10 11:56:50 -07002282 if (oat_file_class_status == Class::kStatusVerified ||
2283 oat_file_class_status == Class::kStatusInitialized) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08002284 return true;
2285 }
jeffhaof1e6b7c2012-06-05 18:33:30 -07002286 if (oat_file_class_status == Class::kStatusRetryVerificationAtRuntime) {
jeffhao1ac29442012-03-26 11:37:32 -07002287 // Compile time verification failed with a soft error. Compile time verification can fail
2288 // because we have incomplete type information. Consider the following:
Ian Rogersc4762272012-02-01 15:55:55 -08002289 // class ... {
2290 // Foo x;
2291 // .... () {
2292 // if (...) {
2293 // v1 gets assigned a type of resolved class Foo
2294 // } else {
2295 // v1 gets assigned a type of unresolved class Bar
2296 // }
2297 // iput x = v1
2298 // } }
2299 // when we merge v1 following the if-the-else it results in Conflict
2300 // (see verifier::RegType::Merge) as we can't know the type of Bar and we could possibly be
2301 // allowing an unsafe assignment to the field x in the iput (javac may have compiled this as
2302 // it knew Bar was a sub-class of Foo, but for us this may have been moved into a separate apk
2303 // at compile time).
2304 return false;
2305 }
jeffhao1ac29442012-03-26 11:37:32 -07002306 if (oat_file_class_status == Class::kStatusError) {
2307 // Compile time verification failed with a hard error. This is caused by invalid instructions
2308 // in the class. These errors are unrecoverable.
2309 return false;
2310 }
Elliott Hughes634eb2e2012-03-22 16:06:28 -07002311 if (oat_file_class_status == Class::kStatusNotReady) {
Ian Rogersc4762272012-02-01 15:55:55 -08002312 // Status is uninitialized if we couldn't determine the status at compile time, for example,
2313 // not loading the class.
2314 // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy
2315 // isn't a problem and this case shouldn't occur
Brian Carlstrome7d856b2012-01-11 18:10:55 -08002316 return false;
2317 }
Elliott Hughes634eb2e2012-03-22 16:06:28 -07002318 LOG(FATAL) << "Unexpected class status: " << oat_file_class_status
Brian Carlstrom5b332c82012-02-01 15:02:31 -08002319 << " " << dex_file.GetLocation() << " " << PrettyClass(klass) << " " << descriptor;
2320
Brian Carlstrome7d856b2012-01-11 18:10:55 -08002321 return false;
2322}
2323
2324void ClassLinker::ResolveClassExceptionHandlerTypes(const DexFile& dex_file, Class* klass) {
2325 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
2326 ResolveMethodExceptionHandlerTypes(dex_file, klass->GetDirectMethod(i));
2327 }
2328 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
2329 ResolveMethodExceptionHandlerTypes(dex_file, klass->GetVirtualMethod(i));
2330 }
2331}
2332
Mathieu Chartier66f19252012-09-18 08:57:04 -07002333void ClassLinker::ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, AbstractMethod* method) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08002334 // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod.
2335 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset());
2336 if (code_item == NULL) {
2337 return; // native or abstract method
2338 }
2339 if (code_item->tries_size_ == 0) {
2340 return; // nothing to process
2341 }
2342 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item, 0);
2343 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2344 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2345 for (uint32_t idx = 0; idx < handlers_size; idx++) {
2346 CatchHandlerIterator iterator(handlers_ptr);
2347 for (; iterator.HasNext(); iterator.Next()) {
2348 // Ensure exception types are resolved so that they don't need resolution to be delivered,
2349 // unresolved exception types will be ignored by exception delivery
2350 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
2351 Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method);
2352 if (exception_type == NULL) {
2353 DCHECK(Thread::Current()->IsExceptionPending());
2354 Thread::Current()->ClearException();
2355 }
2356 }
2357 }
2358 handlers_ptr = iterator.EndDataPointer();
2359 }
2360}
2361
Mathieu Chartier66f19252012-09-18 08:57:04 -07002362static void CheckProxyConstructor(AbstractMethod* constructor);
2363static void CheckProxyMethod(AbstractMethod* method, SirtRef<AbstractMethod>& prototype);
Ian Rogersc2b44472011-12-14 21:17:17 -08002364
Jesse Wilson95caa792011-10-12 18:14:17 -04002365Class* ClassLinker::CreateProxyClass(String* name, ObjectArray<Class>* interfaces,
Mathieu Chartier66f19252012-09-18 08:57:04 -07002366 ClassLoader* loader, ObjectArray<AbstractMethod>* methods,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002367 ObjectArray<ObjectArray<Class> >* throws) {
Ian Rogers1f539342012-10-03 21:09:42 -07002368 Thread* self = Thread::Current();
Ian Rogers50b35e22012-10-04 10:09:15 -07002369 SirtRef<Class> klass(self, AllocClass(self, GetClassRoot(kJavaLangClass),
2370 sizeof(SynthesizedProxyClass)));
Brian Carlstrom40381fb2011-10-19 14:13:40 -07002371 CHECK(klass.get() != NULL);
Ian Rogersc2b44472011-12-14 21:17:17 -08002372 DCHECK(klass->GetClass() != NULL);
Jesse Wilson95caa792011-10-12 18:14:17 -04002373 klass->SetObjectSize(sizeof(Proxy));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002374 klass->SetAccessFlags(kAccClassIsProxy | kAccPublic | kAccFinal);
Jesse Wilson95caa792011-10-12 18:14:17 -04002375 klass->SetClassLoader(loader);
Ian Rogersc2b44472011-12-14 21:17:17 -08002376 DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002377 klass->SetName(name);
Ian Rogers466bb252011-10-14 03:29:56 -07002378 Class* proxy_class = GetClassRoot(kJavaLangReflectProxy);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002379 klass->SetDexCache(proxy_class->GetDexCache());
Ian Rogersc2b44472011-12-14 21:17:17 -08002380
2381 klass->SetStatus(Class::kStatusIdx);
2382
2383 klass->SetDexTypeIndex(DexFile::kDexNoIndex16);
2384
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002385 // Instance fields are inherited, but we add a couple of static fields...
Ian Rogers4445a7e2012-10-05 17:19:13 -07002386 klass->SetSFields(AllocFieldArray(self, 2));
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002387 // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by
2388 // our proxy, so Class.getInterfaces doesn't return the flattened set.
Ian Rogers50b35e22012-10-04 10:09:15 -07002389 SirtRef<Field> interfaces_sfield(self, AllocField(self));
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002390 klass->SetStaticField(0, interfaces_sfield.get());
2391 interfaces_sfield->SetDexFieldIndex(0);
2392 interfaces_sfield->SetDeclaringClass(klass.get());
2393 interfaces_sfield->SetAccessFlags(kAccStatic | kAccPublic | kAccFinal);
2394 // 2. Create a static field 'throws' that holds exceptions thrown by our methods.
Ian Rogers50b35e22012-10-04 10:09:15 -07002395 SirtRef<Field> throws_sfield(self, AllocField(self));
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002396 klass->SetStaticField(1, throws_sfield.get());
2397 throws_sfield->SetDexFieldIndex(1);
2398 throws_sfield->SetDeclaringClass(klass.get());
2399 throws_sfield->SetAccessFlags(kAccStatic | kAccPublic | kAccFinal);
Jesse Wilson95caa792011-10-12 18:14:17 -04002400
Ian Rogers466bb252011-10-14 03:29:56 -07002401 // Proxies have 1 direct method, the constructor
Ian Rogers4445a7e2012-10-05 17:19:13 -07002402 klass->SetDirectMethods(AllocAbstractMethodArray(self, 1));
Ian Rogers50b35e22012-10-04 10:09:15 -07002403 klass->SetDirectMethod(0, CreateProxyConstructor(self, klass, proxy_class));
Jesse Wilson95caa792011-10-12 18:14:17 -04002404
Ian Rogers466bb252011-10-14 03:29:56 -07002405 // Create virtual method using specified prototypes
Jesse Wilson95caa792011-10-12 18:14:17 -04002406 size_t num_virtual_methods = methods->GetLength();
Ian Rogers4445a7e2012-10-05 17:19:13 -07002407 klass->SetVirtualMethods(AllocMethodArray(self, num_virtual_methods));
Jesse Wilson95caa792011-10-12 18:14:17 -04002408 for (size_t i = 0; i < num_virtual_methods; ++i) {
Ian Rogers1f539342012-10-03 21:09:42 -07002409 SirtRef<AbstractMethod> prototype(self, methods->Get(i));
Ian Rogers50b35e22012-10-04 10:09:15 -07002410 klass->SetVirtualMethod(i, CreateProxyMethod(self, klass, prototype));
Jesse Wilson95caa792011-10-12 18:14:17 -04002411 }
Ian Rogersc2b44472011-12-14 21:17:17 -08002412
2413 klass->SetSuperClass(proxy_class); // The super class is java.lang.reflect.Proxy
2414 klass->SetStatus(Class::kStatusLoaded); // Class is now effectively in the loaded state
2415 DCHECK(!Thread::Current()->IsExceptionPending());
2416
2417 // Link the fields and virtual methods, creating vtable and iftables
2418 if (!LinkClass(klass, interfaces)) {
Brian Carlstrom4d9716c2012-01-30 01:49:33 -08002419 klass->SetStatus(Class::kStatusError);
Jesse Wilson95caa792011-10-12 18:14:17 -04002420 return NULL;
2421 }
Ian Rogersc8982582012-09-07 16:53:25 -07002422 {
Ian Rogers1f539342012-10-03 21:09:42 -07002423 ObjectLock lock(self, klass.get()); // Must hold lock on object when initializing.
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002424 interfaces_sfield->SetObject(klass.get(), interfaces);
2425 throws_sfield->SetObject(klass.get(), throws);
Ian Rogersc8982582012-09-07 16:53:25 -07002426 klass->SetStatus(Class::kStatusInitialized);
2427 }
Ian Rogersc2b44472011-12-14 21:17:17 -08002428
2429 // sanity checks
Elliott Hughes67d92002012-03-26 15:08:51 -07002430 if (kIsDebugBuild) {
Ian Rogersc2b44472011-12-14 21:17:17 -08002431 CHECK(klass->GetIFields() == NULL);
2432 CheckProxyConstructor(klass->GetDirectMethod(0));
2433 for (size_t i = 0; i < num_virtual_methods; ++i) {
Ian Rogers1f539342012-10-03 21:09:42 -07002434 SirtRef<AbstractMethod> prototype(self, methods->Get(i));
Ian Rogersc2b44472011-12-14 21:17:17 -08002435 CheckProxyMethod(klass->GetVirtualMethod(i), prototype);
2436 }
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002437
2438 std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces",
2439 name->ToModifiedUtf8().c_str()));
2440 CHECK_EQ(PrettyField(klass->GetStaticField(0)), interfaces_field_name);
2441
2442 std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws",
2443 name->ToModifiedUtf8().c_str()));
2444 CHECK_EQ(PrettyField(klass->GetStaticField(1)), throws_field_name);
Ian Rogersc2b44472011-12-14 21:17:17 -08002445
2446 SynthesizedProxyClass* synth_proxy_class = down_cast<SynthesizedProxyClass*>(klass.get());
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002447 CHECK_EQ(synth_proxy_class->GetInterfaces(), interfaces);
Ian Rogersc2b44472011-12-14 21:17:17 -08002448 CHECK_EQ(synth_proxy_class->GetThrows(), throws);
2449 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07002450 return klass.get();
Jesse Wilson95caa792011-10-12 18:14:17 -04002451}
2452
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002453std::string ClassLinker::GetDescriptorForProxy(const Class* proxy_class) {
2454 DCHECK(proxy_class->IsProxyClass());
2455 String* name = proxy_class->GetName();
2456 DCHECK(name != NULL);
2457 return DotToDescriptor(name->ToModifiedUtf8().c_str());
2458}
2459
Mathieu Chartier66f19252012-09-18 08:57:04 -07002460AbstractMethod* ClassLinker::FindMethodForProxy(const Class* proxy_class, const AbstractMethod* proxy_method) {
Ian Rogers16f93672012-02-14 12:29:06 -08002461 DCHECK(proxy_class->IsProxyClass());
2462 DCHECK(proxy_method->IsProxyMethod());
2463 // Locate the dex cache of the original interface/Object
2464 DexCache* dex_cache = NULL;
2465 {
2466 ObjectArray<Class>* resolved_types = proxy_method->GetDexCacheResolvedTypes();
Ian Rogers50b35e22012-10-04 10:09:15 -07002467 MutexLock mu(Thread::Current(), dex_lock_);
Ian Rogers16f93672012-02-14 12:29:06 -08002468 for (size_t i = 0; i != dex_caches_.size(); ++i) {
2469 if (dex_caches_[i]->GetResolvedTypes() == resolved_types) {
2470 dex_cache = dex_caches_[i];
2471 break;
2472 }
2473 }
2474 }
2475 CHECK(dex_cache != NULL);
2476 uint32_t method_idx = proxy_method->GetDexMethodIndex();
Mathieu Chartier66f19252012-09-18 08:57:04 -07002477 AbstractMethod* resolved_method = dex_cache->GetResolvedMethod(method_idx);
Ian Rogers16f93672012-02-14 12:29:06 -08002478 CHECK(resolved_method != NULL);
2479 return resolved_method;
2480}
2481
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002482
Ian Rogers50b35e22012-10-04 10:09:15 -07002483AbstractMethod* ClassLinker::CreateProxyConstructor(Thread* self, SirtRef<Class>& klass, Class* proxy_class) {
Ian Rogers466bb252011-10-14 03:29:56 -07002484 // Create constructor for Proxy that must initialize h
Mathieu Chartier66f19252012-09-18 08:57:04 -07002485 ObjectArray<AbstractMethod>* proxy_direct_methods = proxy_class->GetDirectMethods();
Jesse Wilsonecbce8f2011-10-21 19:57:36 -04002486 CHECK_EQ(proxy_direct_methods->GetLength(), 15);
Mathieu Chartier66f19252012-09-18 08:57:04 -07002487 AbstractMethod* proxy_constructor = proxy_direct_methods->Get(2);
Ian Rogers466bb252011-10-14 03:29:56 -07002488 // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its
2489 // code_ too)
Ian Rogers50b35e22012-10-04 10:09:15 -07002490 AbstractMethod* constructor = down_cast<AbstractMethod*>(proxy_constructor->Clone(self));
Ian Rogers466bb252011-10-14 03:29:56 -07002491 // Make this constructor public and fix the class to be our Proxy version
2492 constructor->SetAccessFlags((constructor->GetAccessFlags() & ~kAccProtected) | kAccPublic);
Brian Carlstrom40381fb2011-10-19 14:13:40 -07002493 constructor->SetDeclaringClass(klass.get());
Ian Rogersc2b44472011-12-14 21:17:17 -08002494 return constructor;
2495}
2496
Mathieu Chartier66f19252012-09-18 08:57:04 -07002497static void CheckProxyConstructor(AbstractMethod* constructor)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002498 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers466bb252011-10-14 03:29:56 -07002499 CHECK(constructor->IsConstructor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002500 MethodHelper mh(constructor);
2501 CHECK_STREQ(mh.GetName(), "<init>");
Elliott Hughesba8eee12012-01-24 20:25:24 -08002502 CHECK_EQ(mh.GetSignature(), std::string("(Ljava/lang/reflect/InvocationHandler;)V"));
Ian Rogers466bb252011-10-14 03:29:56 -07002503 DCHECK(constructor->IsPublic());
Jesse Wilson95caa792011-10-12 18:14:17 -04002504}
2505
Ian Rogers50b35e22012-10-04 10:09:15 -07002506AbstractMethod* ClassLinker::CreateProxyMethod(Thread* self, SirtRef<Class>& klass,
2507 SirtRef<AbstractMethod>& prototype) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002508 // Ensure prototype is in dex cache so that we can use the dex cache to look up the overridden
2509 // prototype method
Ian Rogers16f93672012-02-14 12:29:06 -08002510 prototype->GetDeclaringClass()->GetDexCache()->SetResolvedMethod(prototype->GetDexMethodIndex(),
2511 prototype.get());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002512 // We steal everything from the prototype (such as DexCache, invoke stub, etc.) then specialize
Ian Rogers466bb252011-10-14 03:29:56 -07002513 // as necessary
Ian Rogers50b35e22012-10-04 10:09:15 -07002514 AbstractMethod* method = down_cast<AbstractMethod*>(prototype->Clone(self));
Ian Rogers466bb252011-10-14 03:29:56 -07002515
2516 // Set class to be the concrete proxy class and clear the abstract flag, modify exceptions to
2517 // the intersection of throw exceptions as defined in Proxy
Brian Carlstrom40381fb2011-10-19 14:13:40 -07002518 method->SetDeclaringClass(klass.get());
Ian Rogers466bb252011-10-14 03:29:56 -07002519 method->SetAccessFlags((method->GetAccessFlags() & ~kAccAbstract) | kAccFinal);
Jesse Wilson95caa792011-10-12 18:14:17 -04002520
Ian Rogers466bb252011-10-14 03:29:56 -07002521 // At runtime the method looks like a reference and argument saving method, clone the code
2522 // related parameters from this method.
Mathieu Chartier66f19252012-09-18 08:57:04 -07002523 AbstractMethod* refs_and_args = Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs);
Ian Rogers466bb252011-10-14 03:29:56 -07002524 method->SetCoreSpillMask(refs_and_args->GetCoreSpillMask());
2525 method->SetFpSpillMask(refs_and_args->GetFpSpillMask());
2526 method->SetFrameSizeInBytes(refs_and_args->GetFrameSizeInBytes());
TDYa1275bb86012012-04-11 05:57:28 -07002527#if !defined(ART_USE_LLVM_COMPILER)
Ian Rogers466bb252011-10-14 03:29:56 -07002528 method->SetCode(reinterpret_cast<void*>(art_proxy_invoke_handler));
TDYa1275bb86012012-04-11 05:57:28 -07002529#else
Logan Chien7a2a23a2012-06-06 11:01:00 +08002530 OatFile::OatMethod oat_method = GetOatMethodFor(prototype.get());
2531 method->SetCode(oat_method.GetProxyStub());
TDYa1275bb86012012-04-11 05:57:28 -07002532#endif
Ian Rogers16f93672012-02-14 12:29:06 -08002533
Ian Rogersc2b44472011-12-14 21:17:17 -08002534 return method;
2535}
Jesse Wilson95caa792011-10-12 18:14:17 -04002536
Mathieu Chartier66f19252012-09-18 08:57:04 -07002537static void CheckProxyMethod(AbstractMethod* method, SirtRef<AbstractMethod>& prototype)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002538 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers466bb252011-10-14 03:29:56 -07002539 // Basic sanity
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002540 CHECK(!prototype->IsFinal());
2541 CHECK(method->IsFinal());
2542 CHECK(!method->IsAbstract());
Ian Rogers19846512012-02-24 11:42:47 -08002543
2544 // The proxy method doesn't have its own dex cache or dex file and so it steals those of its
2545 // interface prototype. The exception to this are Constructors and the Class of the Proxy itself.
2546 CHECK_EQ(prototype->GetDexCacheStrings(), method->GetDexCacheStrings());
2547 CHECK_EQ(prototype->GetDexCacheResolvedMethods(), method->GetDexCacheResolvedMethods());
2548 CHECK_EQ(prototype->GetDexCacheResolvedTypes(), method->GetDexCacheResolvedTypes());
2549 CHECK_EQ(prototype->GetDexCacheInitializedStaticStorage(),
2550 method->GetDexCacheInitializedStaticStorage());
2551 CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex());
2552
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002553 MethodHelper mh(method);
Ian Rogers19846512012-02-24 11:42:47 -08002554 MethodHelper mh2(prototype.get());
2555 CHECK_STREQ(mh.GetName(), mh2.GetName());
2556 CHECK_STREQ(mh.GetShorty(), mh2.GetShorty());
Ian Rogers466bb252011-10-14 03:29:56 -07002557 // More complex sanity - via dex cache
Ian Rogers19846512012-02-24 11:42:47 -08002558 CHECK_EQ(mh.GetReturnType(), mh2.GetReturnType());
Jesse Wilson95caa792011-10-12 18:14:17 -04002559}
2560
Ian Rogers0045a292012-03-31 21:08:41 -07002561bool ClassLinker::InitializeClass(Class* klass, bool can_run_clinit, bool can_init_statics) {
Brian Carlstromd1422f82011-09-28 11:37:09 -07002562 CHECK(klass->IsResolved() || klass->IsErroneous())
Ian Rogers9ffb0392012-09-10 11:56:50 -07002563 << PrettyClass(klass) << ": state=" << klass->GetStatus();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002564
Carl Shapirob5573532011-07-12 18:22:59 -07002565 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002566
Mathieu Chartier66f19252012-09-18 08:57:04 -07002567 AbstractMethod* clinit = NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002568 {
Brian Carlstromd1422f82011-09-28 11:37:09 -07002569 // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol
Ian Rogers1f539342012-10-03 21:09:42 -07002570 ObjectLock lock(self, klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002571
Brian Carlstromd1422f82011-09-28 11:37:09 -07002572 if (klass->GetStatus() == Class::kStatusInitialized) {
2573 return true;
2574 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002575
Brian Carlstromd1422f82011-09-28 11:37:09 -07002576 if (klass->IsErroneous()) {
2577 ThrowEarlierClassFailure(klass);
2578 return false;
2579 }
2580
jeffhaoebe2e0f2012-06-06 15:19:40 -07002581 if (klass->GetStatus() == Class::kStatusResolved ||
2582 klass->GetStatus() == Class::kStatusRetryVerificationAtRuntime) {
jeffhao98eacac2011-09-14 16:11:53 -07002583 VerifyClass(klass);
2584 if (klass->GetStatus() != Class::kStatusVerified) {
jeffhaoa9b3bf42012-06-06 17:18:39 -07002585 if (klass->GetStatus() == Class::kStatusError) {
2586 CHECK(self->IsExceptionPending());
2587 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002588 return false;
2589 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002590 }
2591
Brian Carlstrom25c33252011-09-18 15:58:35 -07002592 clinit = klass->FindDeclaredDirectMethod("<clinit>", "()V");
2593 if (clinit != NULL && !can_run_clinit) {
Brian Carlstromd1422f82011-09-28 11:37:09 -07002594 // if the class has a <clinit> but we can't run it during compilation,
Ian Rogers1bddec32012-02-04 12:27:34 -08002595 // don't bother going to kStatusInitializing. We return false so that
2596 // sub-classes don't believe this class is initialized.
Ian Rogers19846512012-02-24 11:42:47 -08002597 // Opportunistically link non-static methods, TODO: don't initialize and dirty pages
2598 // in second pass.
Ian Rogers1bddec32012-02-04 12:27:34 -08002599 return false;
Brian Carlstrom25c33252011-09-18 15:58:35 -07002600 }
2601
Brian Carlstromd1422f82011-09-28 11:37:09 -07002602 // If the class is kStatusInitializing, either this thread is
2603 // initializing higher up the stack or another thread has beat us
2604 // to initializing and we need to wait. Either way, this
2605 // invocation of InitializeClass will not be responsible for
2606 // running <clinit> and will return.
2607 if (klass->GetStatus() == Class::kStatusInitializing) {
Elliott Hughes005ab2e2011-09-11 17:15:31 -07002608 // We caught somebody else in the act; was it us?
Elliott Hughesdcc24742011-09-07 14:02:44 -07002609 if (klass->GetClinitThreadId() == self->GetTid()) {
Brian Carlstromd1422f82011-09-28 11:37:09 -07002610 // Yes. That's fine. Return so we can continue initializing.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002611 return true;
2612 }
Brian Carlstromd1422f82011-09-28 11:37:09 -07002613 // No. That's fine. Wait for another thread to finish initializing.
2614 return WaitForInitializeClass(klass, self, lock);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002615 }
2616
2617 if (!ValidateSuperClassDescriptors(klass)) {
2618 klass->SetStatus(Class::kStatusError);
Ian Rogers3d1548d2012-09-24 14:08:03 -07002619 lock.NotifyAll();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002620 return false;
2621 }
2622
Brian Carlstrome7d856b2012-01-11 18:10:55 -08002623 DCHECK_EQ(klass->GetStatus(), Class::kStatusVerified) << PrettyClass(klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002624
Elliott Hughesdcc24742011-09-07 14:02:44 -07002625 klass->SetClinitThreadId(self->GetTid());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002626 klass->SetStatus(Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002627 }
2628
Elliott Hughes83df2ac2011-10-11 16:37:54 -07002629 uint64_t t0 = NanoTime();
2630
Ian Rogers0045a292012-03-31 21:08:41 -07002631 if (!InitializeSuperClass(klass, can_run_clinit, can_init_statics)) {
Ian Rogers1bddec32012-02-04 12:27:34 -08002632 // Super class initialization failed, this can be because we can't run
2633 // super-class class initializers in which case we'll be verified.
2634 // Otherwise this class is erroneous.
2635 if (!can_run_clinit) {
2636 CHECK(klass->IsVerified());
2637 } else {
2638 CHECK(klass->IsErroneous());
2639 }
Ian Rogers3d1548d2012-09-24 14:08:03 -07002640 // Signal to any waiting threads that saw this class as initializing.
Ian Rogers1f539342012-10-03 21:09:42 -07002641 ObjectLock lock(self, klass);
Ian Rogers3d1548d2012-09-24 14:08:03 -07002642 lock.NotifyAll();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002643 return false;
2644 }
2645
Ian Rogers0045a292012-03-31 21:08:41 -07002646 bool has_static_field_initializers = InitializeStaticFields(klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002647
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002648 if (clinit != NULL) {
Ian Rogers64b6d142012-10-29 16:34:15 -07002649 if (Runtime::Current()->IsStarted()) {
2650 clinit->Invoke(self, NULL, NULL, NULL);
2651 } else {
2652 art::interpreter::EnterInterpreterFromInvoke(self, clinit, NULL, NULL, NULL);
2653 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002654 }
2655
Ian Rogers19846512012-02-24 11:42:47 -08002656 FixupStaticTrampolines(klass);
2657
Elliott Hughes83df2ac2011-10-11 16:37:54 -07002658 uint64_t t1 = NanoTime();
2659
Ian Rogersbdfb1a52012-01-12 14:05:22 -08002660 bool success = true;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002661 {
Ian Rogers1f539342012-10-03 21:09:42 -07002662 ObjectLock lock(self, klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002663
2664 if (self->IsExceptionPending()) {
Elliott Hughes4d0207c2011-10-03 19:14:34 -07002665 WrapExceptionInInitializer();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002666 klass->SetStatus(Class::kStatusError);
Ian Rogersbdfb1a52012-01-12 14:05:22 -08002667 success = false;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002668 } else {
Elliott Hughes83df2ac2011-10-11 16:37:54 -07002669 RuntimeStats* global_stats = Runtime::Current()->GetStats();
2670 RuntimeStats* thread_stats = self->GetStats();
2671 ++global_stats->class_init_count;
2672 ++thread_stats->class_init_count;
2673 global_stats->class_init_time_ns += (t1 - t0);
2674 thread_stats->class_init_time_ns += (t1 - t0);
Ian Rogers0045a292012-03-31 21:08:41 -07002675 // Set the class as initialized except if we can't initialize static fields and static field
2676 // initialization is necessary.
2677 if (!can_init_statics && has_static_field_initializers) {
2678 klass->SetStatus(Class::kStatusVerified); // Don't leave class in initializing state.
2679 success = false;
2680 } else {
2681 klass->SetStatus(Class::kStatusInitialized);
2682 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08002683 if (VLOG_IS_ON(class_linker)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002684 ClassHelper kh(klass);
2685 LOG(INFO) << "Initialized class " << kh.GetDescriptor() << " from " << kh.GetLocation();
Brian Carlstromae826982011-11-09 01:33:42 -08002686 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002687 }
2688 lock.NotifyAll();
2689 }
Ian Rogersbdfb1a52012-01-12 14:05:22 -08002690 return success;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002691}
2692
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002693bool ClassLinker::WaitForInitializeClass(Class* klass, Thread* self, ObjectLock& lock)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002694 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd1422f82011-09-28 11:37:09 -07002695 while (true) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002696 self->AssertNoPendingException();
Brian Carlstromd1422f82011-09-28 11:37:09 -07002697 lock.Wait();
2698
2699 // When we wake up, repeat the test for init-in-progress. If
2700 // there's an exception pending (only possible if
2701 // "interruptShouldThrow" was set), bail out.
2702 if (self->IsExceptionPending()) {
Elliott Hughes4d0207c2011-10-03 19:14:34 -07002703 WrapExceptionInInitializer();
Brian Carlstromd1422f82011-09-28 11:37:09 -07002704 klass->SetStatus(Class::kStatusError);
2705 return false;
2706 }
2707 // Spurious wakeup? Go back to waiting.
2708 if (klass->GetStatus() == Class::kStatusInitializing) {
2709 continue;
2710 }
Ian Rogers3d1548d2012-09-24 14:08:03 -07002711 if (klass->GetStatus() == Class::kStatusVerified && Runtime::Current()->IsCompiler()) {
2712 // Compile time initialization failed.
2713 return false;
2714 }
Brian Carlstromd1422f82011-09-28 11:37:09 -07002715 if (klass->IsErroneous()) {
2716 // The caller wants an exception, but it was thrown in a
2717 // different thread. Synthesize one here.
Brian Carlstromdf143242011-10-10 18:05:34 -07002718 ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002719 PrettyDescriptor(klass).c_str());
Brian Carlstromd1422f82011-09-28 11:37:09 -07002720 return false;
2721 }
2722 if (klass->IsInitialized()) {
2723 return true;
2724 }
2725 LOG(FATAL) << "Unexpected class status. " << PrettyClass(klass) << " is " << klass->GetStatus();
2726 }
2727 LOG(FATAL) << "Not Reached" << PrettyClass(klass);
2728}
2729
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002730bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
2731 if (klass->IsInterface()) {
2732 return true;
2733 }
2734 // begin with the methods local to the superclass
2735 if (klass->HasSuperClass() &&
2736 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
2737 const Class* super = klass->GetSuperClass();
Ian Rogers595799e2012-01-11 17:32:51 -08002738 for (int i = super->GetVTable()->GetLength() - 1; i >= 0; --i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07002739 const AbstractMethod* method = klass->GetVTable()->Get(i);
Ian Rogers595799e2012-01-11 17:32:51 -08002740 if (method != super->GetVTable()->Get(i) &&
2741 !IsSameMethodSignatureInDifferentClassContexts(method, super, klass)) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002742 ThrowLinkageError("Class %s method %s resolves differently in superclass %s",
2743 PrettyDescriptor(klass).c_str(), PrettyMethod(method).c_str(),
2744 PrettyDescriptor(super).c_str());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002745 return false;
2746 }
2747 }
2748 }
Ian Rogers9bc81912012-10-11 21:43:36 -07002749 IfTable* iftable = klass->GetIfTable();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002750 for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) {
Ian Rogers9bc81912012-10-11 21:43:36 -07002751 Class* interface = iftable->GetInterface(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002752 if (klass->GetClassLoader() != interface->GetClassLoader()) {
2753 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
Ian Rogers9bc81912012-10-11 21:43:36 -07002754 const AbstractMethod* method = iftable->GetMethodArray(i)->Get(j);
Ian Rogers595799e2012-01-11 17:32:51 -08002755 if (!IsSameMethodSignatureInDifferentClassContexts(method, interface,
2756 method->GetDeclaringClass())) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002757 ThrowLinkageError("Class %s method %s resolves differently in interface %s",
2758 PrettyDescriptor(method->GetDeclaringClass()).c_str(),
2759 PrettyMethod(method).c_str(),
2760 PrettyDescriptor(interface).c_str());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002761 return false;
2762 }
2763 }
2764 }
2765 }
2766 return true;
2767}
2768
Ian Rogers595799e2012-01-11 17:32:51 -08002769// Returns true if classes referenced by the signature of the method are the
2770// same classes in klass1 as they are in klass2.
Mathieu Chartier66f19252012-09-18 08:57:04 -07002771bool ClassLinker::IsSameMethodSignatureInDifferentClassContexts(const AbstractMethod* method,
Ian Rogers595799e2012-01-11 17:32:51 -08002772 const Class* klass1,
2773 const Class* klass2) {
Ian Rogers9074b992011-10-26 17:41:55 -07002774 if (klass1 == klass2) {
2775 return true;
Brian Carlstrome10b6972011-09-26 13:49:03 -07002776 }
Ian Rogers4445a7e2012-10-05 17:19:13 -07002777 const DexFile& dex_file = *method->GetDeclaringClass()->GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002778 const DexFile::ProtoId& proto_id =
2779 dex_file.GetMethodPrototype(dex_file.GetMethodId(method->GetDexMethodIndex()));
Ian Rogers0571d352011-11-03 19:51:38 -07002780 for (DexFileParameterIterator it(dex_file, proto_id); it.HasNext(); it.Next()) {
2781 const char* descriptor = it.GetDescriptor();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002782 if (descriptor == NULL) {
2783 break;
2784 }
2785 if (descriptor[0] == 'L' || descriptor[0] == '[') {
2786 // Found a non-primitive type.
Ian Rogers595799e2012-01-11 17:32:51 -08002787 if (!IsSameDescriptorInDifferentClassContexts(descriptor, klass1, klass2)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002788 return false;
2789 }
2790 }
2791 }
2792 // Check the return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07002793 const char* descriptor = dex_file.GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002794 if (descriptor[0] == 'L' || descriptor[0] == '[') {
Ian Rogers595799e2012-01-11 17:32:51 -08002795 if (!IsSameDescriptorInDifferentClassContexts(descriptor, klass1, klass2)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002796 return false;
2797 }
2798 }
2799 return true;
2800}
2801
Ian Rogers595799e2012-01-11 17:32:51 -08002802// Returns true if the descriptor resolves to the same class in the context of klass1 and klass2.
2803bool ClassLinker::IsSameDescriptorInDifferentClassContexts(const char* descriptor,
2804 const Class* klass1,
2805 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002806 CHECK(descriptor != NULL);
2807 CHECK(klass1 != NULL);
2808 CHECK(klass2 != NULL);
Ian Rogers9074b992011-10-26 17:41:55 -07002809 if (klass1 == klass2) {
2810 return true;
2811 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07002812 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Ian Rogers595799e2012-01-11 17:32:51 -08002813 if (found1 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -07002814 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002815 }
Ian Rogers595799e2012-01-11 17:32:51 -08002816 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
2817 if (found2 == NULL) {
2818 Thread::Current()->ClearException();
2819 }
2820 return found1 == found2;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002821}
2822
Ian Rogers0045a292012-03-31 21:08:41 -07002823bool ClassLinker::InitializeSuperClass(Class* klass, bool can_run_clinit, bool can_init_fields) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002824 CHECK(klass != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002825 if (!klass->IsInterface() && klass->HasSuperClass()) {
2826 Class* super_class = klass->GetSuperClass();
Ian Rogers9ffb0392012-09-10 11:56:50 -07002827 if (!super_class->IsInitialized()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002828 CHECK(!super_class->IsInterface());
Ian Rogers1f539342012-10-03 21:09:42 -07002829 // Must hold lock on object when initializing and setting status.
2830 ObjectLock lock(Thread::Current(), klass);
Ian Rogers0045a292012-03-31 21:08:41 -07002831 bool super_initialized = InitializeClass(super_class, can_run_clinit, can_init_fields);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002832 // TODO: check for a pending exception
2833 if (!super_initialized) {
Brian Carlstrom25c33252011-09-18 15:58:35 -07002834 if (!can_run_clinit) {
Brian Carlstrom4d9716c2012-01-30 01:49:33 -08002835 // Don't set status to error when we can't run <clinit>.
2836 CHECK_EQ(klass->GetStatus(), Class::kStatusInitializing) << PrettyClass(klass);
2837 klass->SetStatus(Class::kStatusVerified);
2838 return false;
Brian Carlstrom25c33252011-09-18 15:58:35 -07002839 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002840 klass->SetStatus(Class::kStatusError);
2841 klass->NotifyAll();
2842 return false;
2843 }
2844 }
2845 }
2846 return true;
2847}
2848
Ian Rogers0045a292012-03-31 21:08:41 -07002849bool ClassLinker::EnsureInitialized(Class* c, bool can_run_clinit, bool can_init_fields) {
Mathieu Chartier155dfe92012-10-09 14:24:49 -07002850 DCHECK(c != NULL);
Elliott Hughesf4c21c92011-08-19 17:31:31 -07002851 if (c->IsInitialized()) {
2852 return true;
2853 }
2854
Elliott Hughes5f791332011-09-15 17:45:30 -07002855 Thread* self = Thread::Current();
Elliott Hughes34e06962012-04-09 13:55:55 -07002856 ScopedThreadStateChange tsc(self, kRunnable);
Ian Rogers0045a292012-03-31 21:08:41 -07002857 bool success = InitializeClass(c, can_run_clinit, can_init_fields);
Ian Rogers595799e2012-01-11 17:32:51 -08002858 if (!success) {
Ian Rogers1bddec32012-02-04 12:27:34 -08002859 CHECK(self->IsExceptionPending() || !can_run_clinit) << PrettyClass(c);
Ian Rogers595799e2012-01-11 17:32:51 -08002860 }
2861 return success;
Elliott Hughesf4c21c92011-08-19 17:31:31 -07002862}
2863
Elliott Hughes5fe594f2011-09-08 12:33:17 -07002864void ClassLinker::ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def,
Elliott Hughesa0e18062012-04-13 15:59:59 -07002865 Class* c, SafeMap<uint32_t, Field*>& field_map) {
Ian Rogers365c1022012-06-22 15:05:28 -07002866 ClassLoader* cl = c->GetClassLoader();
Elliott Hughes5fe594f2011-09-08 12:33:17 -07002867 const byte* class_data = dex_file.GetClassData(dex_class_def);
Ian Rogers0571d352011-11-03 19:51:38 -07002868 ClassDataItemIterator it(dex_file, class_data);
2869 for (size_t i = 0; it.HasNextStaticField(); i++, it.Next()) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002870 field_map.Put(i, ResolveField(dex_file, it.GetMemberIndex(), c->GetDexCache(), cl, true));
Elliott Hughes5fe594f2011-09-08 12:33:17 -07002871 }
2872}
2873
Ian Rogers0045a292012-03-31 21:08:41 -07002874bool ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002875 size_t num_static_fields = klass->NumStaticFields();
2876 if (num_static_fields == 0) {
Ian Rogers0045a292012-03-31 21:08:41 -07002877 return false;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002878 }
Brian Carlstromf615a612011-07-23 12:50:34 -07002879 DexCache* dex_cache = klass->GetDexCache();
Brian Carlstrom4873d462011-08-21 15:23:39 -07002880 // TODO: this seems like the wrong check. do we really want !IsPrimitive && !IsArray?
Brian Carlstromf615a612011-07-23 12:50:34 -07002881 if (dex_cache == NULL) {
Ian Rogers0045a292012-03-31 21:08:41 -07002882 return false;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002883 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002884 ClassHelper kh(klass);
2885 const DexFile::ClassDef* dex_class_def = kh.GetClassDef();
Brian Carlstromf615a612011-07-23 12:50:34 -07002886 CHECK(dex_class_def != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002887 const DexFile& dex_file = kh.GetDexFile();
Brian Carlstrom88f36542012-10-16 23:24:21 -07002888 EncodedStaticFieldValueIterator it(dex_file, dex_cache, klass->GetClassLoader(),
2889 this, *dex_class_def);
Elliott Hughes5fe594f2011-09-08 12:33:17 -07002890
Ian Rogers0571d352011-11-03 19:51:38 -07002891 if (it.HasNext()) {
2892 // We reordered the fields, so we need to be able to map the field indexes to the right fields.
Elliott Hughesa0e18062012-04-13 15:59:59 -07002893 SafeMap<uint32_t, Field*> field_map;
Ian Rogers0571d352011-11-03 19:51:38 -07002894 ConstructFieldMap(dex_file, *dex_class_def, klass, field_map);
2895 for (size_t i = 0; it.HasNext(); i++, it.Next()) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002896 it.ReadValueToField(field_map.Get(i));
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002897 }
Ian Rogers0045a292012-03-31 21:08:41 -07002898 return true;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002899 }
Ian Rogers0045a292012-03-31 21:08:41 -07002900 return false;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002901}
2902
Ian Rogersc2b44472011-12-14 21:17:17 -08002903bool ClassLinker::LinkClass(SirtRef<Class>& klass, ObjectArray<Class>* interfaces) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002904 CHECK_EQ(Class::kStatusLoaded, klass->GetStatus());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002905 if (!LinkSuperClass(klass)) {
2906 return false;
2907 }
Ian Rogersc2b44472011-12-14 21:17:17 -08002908 if (!LinkMethods(klass, interfaces)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002909 return false;
2910 }
2911 if (!LinkInstanceFields(klass)) {
2912 return false;
2913 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07002914 if (!LinkStaticFields(klass)) {
2915 return false;
2916 }
2917 CreateReferenceInstanceOffsets(klass);
2918 CreateReferenceStaticOffsets(klass);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002919 CHECK_EQ(Class::kStatusLoaded, klass->GetStatus());
2920 klass->SetStatus(Class::kStatusResolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002921 return true;
2922}
2923
Brian Carlstrom40381fb2011-10-19 14:13:40 -07002924bool ClassLinker::LoadSuperAndInterfaces(SirtRef<Class>& klass, const DexFile& dex_file) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002925 CHECK_EQ(Class::kStatusIdx, klass->GetStatus());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002926 StringPiece descriptor(dex_file.StringByTypeIdx(klass->GetDexTypeIndex()));
2927 const DexFile::ClassDef* class_def = dex_file.FindClassDef(descriptor);
Ian Rogerscab01012012-01-10 17:35:46 -08002928 CHECK(class_def != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002929 uint16_t super_class_idx = class_def->superclass_idx_;
2930 if (super_class_idx != DexFile::kDexNoIndex16) {
2931 Class* super_class = ResolveType(dex_file, super_class_idx, klass.get());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002932 if (super_class == NULL) {
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002933 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002934 return false;
2935 }
Ian Rogersbe125a92012-01-11 15:19:49 -08002936 // Verify
2937 if (!klass->CanAccess(super_class)) {
2938 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
2939 "Class %s extended by class %s is inaccessible",
2940 PrettyDescriptor(super_class).c_str(),
2941 PrettyDescriptor(klass.get()).c_str());
2942 return false;
2943 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002944 klass->SetSuperClass(super_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002945 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002946 const DexFile::TypeList* interfaces = dex_file.GetInterfacesList(*class_def);
2947 if (interfaces != NULL) {
2948 for (size_t i = 0; i < interfaces->Size(); i++) {
2949 uint16_t idx = interfaces->GetTypeItem(i).type_idx_;
2950 Class* interface = ResolveType(dex_file, idx, klass.get());
2951 if (interface == NULL) {
2952 DCHECK(Thread::Current()->IsExceptionPending());
2953 return false;
2954 }
2955 // Verify
2956 if (!klass->CanAccess(interface)) {
2957 // TODO: the RI seemed to ignore this in my testing.
2958 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
2959 "Interface %s implemented by class %s is inaccessible",
2960 PrettyDescriptor(interface).c_str(),
2961 PrettyDescriptor(klass.get()).c_str());
2962 return false;
2963 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002964 }
2965 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07002966 // Mark the class as loaded.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002967 klass->SetStatus(Class::kStatusLoaded);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002968 return true;
2969}
2970
Brian Carlstrom40381fb2011-10-19 14:13:40 -07002971bool ClassLinker::LinkSuperClass(SirtRef<Class>& klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002972 CHECK(!klass->IsPrimitive());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002973 Class* super = klass->GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002974 if (klass.get() == GetClassRoot(kJavaLangObject)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002975 if (super != NULL) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002976 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassFormatError;",
Elliott Hughes4a2b4172011-09-20 17:08:25 -07002977 "java.lang.Object must not have a superclass");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002978 return false;
2979 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002980 return true;
2981 }
2982 if (super == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002983 ThrowLinkageError("No superclass defined for class %s", PrettyDescriptor(klass.get()).c_str());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002984 return false;
2985 }
2986 // Verify
Elliott Hughes4a2b4172011-09-20 17:08:25 -07002987 if (super->IsFinal() || super->IsInterface()) {
Brian Carlstrom4d9716c2012-01-30 01:49:33 -08002988 Thread* self = Thread::Current();
2989 self->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
Elliott Hughese555dc02011-09-25 10:46:35 -07002990 "Superclass %s of %s is %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002991 PrettyDescriptor(super).c_str(),
2992 PrettyDescriptor(klass.get()).c_str(),
Elliott Hughes4a2b4172011-09-20 17:08:25 -07002993 super->IsFinal() ? "declared final" : "an interface");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002994 return false;
2995 }
2996 if (!klass->CanAccess(super)) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07002997 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
Elliott Hughese555dc02011-09-25 10:46:35 -07002998 "Superclass %s is inaccessible by %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002999 PrettyDescriptor(super).c_str(),
3000 PrettyDescriptor(klass.get()).c_str());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003001 return false;
3002 }
Elliott Hughes20cde902011-10-04 17:37:27 -07003003
3004 // Inherit kAccClassIsFinalizable from the superclass in case this class doesn't override finalize.
3005 if (super->IsFinalizable()) {
3006 klass->SetFinalizable();
3007 }
3008
Elliott Hughes2da50362011-10-10 16:57:08 -07003009 // Inherit reference flags (if any) from the superclass.
3010 int reference_flags = (super->GetAccessFlags() & kAccReferenceFlagsMask);
3011 if (reference_flags != 0) {
3012 klass->SetAccessFlags(klass->GetAccessFlags() | reference_flags);
3013 }
Elliott Hughes72ee0ae2011-10-10 17:31:28 -07003014 // Disallow custom direct subclasses of java.lang.ref.Reference.
Elliott Hughesbf61ba32011-10-11 10:53:09 -07003015 if (init_done_ && super == GetClassRoot(kJavaLangRefReference)) {
Elliott Hughes72ee0ae2011-10-10 17:31:28 -07003016 ThrowLinkageError("Class %s attempts to subclass java.lang.ref.Reference, which is not allowed",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003017 PrettyDescriptor(klass.get()).c_str());
Elliott Hughes72ee0ae2011-10-10 17:31:28 -07003018 return false;
3019 }
Elliott Hughes2da50362011-10-10 16:57:08 -07003020
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003021#ifndef NDEBUG
3022 // Ensure super classes are fully resolved prior to resolving fields..
3023 while (super != NULL) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07003024 CHECK(super->IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003025 super = super->GetSuperClass();
3026 }
3027#endif
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003028 return true;
3029}
3030
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003031// Populate the class vtable and itable. Compute return type indices.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003032bool ClassLinker::LinkMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003033 if (klass->IsInterface()) {
3034 // No vtable.
3035 size_t count = klass->NumVirtualMethods();
3036 if (!IsUint(16, count)) {
Elliott Hughes92cb4982011-12-16 16:57:28 -08003037 ThrowClassFormatError("Too many methods on interface: %zd", count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003038 return false;
3039 }
Carl Shapiro565f5072011-07-10 13:39:43 -07003040 for (size_t i = 0; i < count; ++i) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003041 klass->GetVirtualMethodDuringLinking(i)->SetMethodIndex(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003042 }
jeffhaobdb76512011-09-07 11:43:16 -07003043 // Link interface method tables
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003044 return LinkInterfaceMethods(klass, interfaces);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003045 } else {
Elliott Hughesbc258fa2011-10-06 14:45:21 -07003046 // Link virtual and interface method tables
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003047 return LinkVirtualMethods(klass) && LinkInterfaceMethods(klass, interfaces);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003048 }
3049 return true;
3050}
3051
Brian Carlstrom40381fb2011-10-19 14:13:40 -07003052bool ClassLinker::LinkVirtualMethods(SirtRef<Class>& klass) {
Ian Rogers1f539342012-10-03 21:09:42 -07003053 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003054 if (klass->HasSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003055 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->GetVTable()->GetLength();
3056 size_t actual_count = klass->GetSuperClass()->GetVTable()->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07003057 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07003058 // TODO: do not assign to the vtable field until it is fully constructed.
Ian Rogers1f539342012-10-03 21:09:42 -07003059 SirtRef<ObjectArray<AbstractMethod> >
Ian Rogers50b35e22012-10-04 10:09:15 -07003060 vtable(self, klass->GetSuperClass()->GetVTable()->CopyOf(self, max_count));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003061 // See if any of our virtual methods override the superclass.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003062 MethodHelper local_mh(NULL, this);
3063 MethodHelper super_mh(NULL, this);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003064 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07003065 AbstractMethod* local_method = klass->GetVirtualMethodDuringLinking(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003066 local_mh.ChangeMethod(local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003067 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07003068 for (; j < actual_count; ++j) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07003069 AbstractMethod* super_method = vtable->Get(j);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003070 super_mh.ChangeMethod(super_method);
Elliott Hughes39717372012-07-13 16:21:23 -07003071 if (local_mh.HasSameNameAndSignature(&super_mh)) {
3072 if (klass->CanAccessMember(super_method->GetDeclaringClass(), super_method->GetAccessFlags())) {
3073 if (super_method->IsFinal()) {
3074 ThrowLinkageError("Method %s overrides final method in class %s",
3075 PrettyMethod(local_method).c_str(),
3076 super_mh.GetDeclaringClassDescriptor());
3077 return false;
3078 }
3079 vtable->Set(j, local_method);
3080 local_method->SetMethodIndex(j);
3081 break;
3082 } else {
3083 LOG(WARNING) << "Before Android 4.1, method " << PrettyMethod(local_method)
3084 << " would have incorrectly overridden the package-private method in "
3085 << PrettyDescriptor(super_mh.GetDeclaringClassDescriptor());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003086 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003087 }
3088 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07003089 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003090 // Not overriding, append.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003091 vtable->Set(actual_count, local_method);
3092 local_method->SetMethodIndex(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003093 actual_count += 1;
3094 }
3095 }
3096 if (!IsUint(16, actual_count)) {
Elliott Hughes92cb4982011-12-16 16:57:28 -08003097 ThrowClassFormatError("Too many methods defined on class: %zd", actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003098 return false;
3099 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003100 // Shrink vtable if possible
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003101 CHECK_LE(actual_count, max_count);
3102 if (actual_count < max_count) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003103 vtable.reset(vtable->CopyOf(self, actual_count));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003104 }
Ian Rogers30fab402012-01-23 15:43:46 -08003105 klass->SetVTable(vtable.get());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003106 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003107 CHECK(klass.get() == GetClassRoot(kJavaLangObject));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07003108 uint32_t num_virtual_methods = klass->NumVirtualMethods();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07003109 if (!IsUint(16, num_virtual_methods)) {
Elliott Hughese555dc02011-09-25 10:46:35 -07003110 ThrowClassFormatError("Too many methods: %d", num_virtual_methods);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003111 return false;
3112 }
Ian Rogers1f539342012-10-03 21:09:42 -07003113 SirtRef<ObjectArray<AbstractMethod> >
Ian Rogers4445a7e2012-10-05 17:19:13 -07003114 vtable(self, AllocMethodArray(self, num_virtual_methods));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07003115 for (size_t i = 0; i < num_virtual_methods; ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07003116 AbstractMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003117 vtable->Set(i, virtual_method);
3118 virtual_method->SetMethodIndex(i & 0xFFFF);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003119 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07003120 klass->SetVTable(vtable.get());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003121 }
3122 return true;
3123}
3124
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003125bool ClassLinker::LinkInterfaceMethods(SirtRef<Class>& klass, ObjectArray<Class>* interfaces) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003126 size_t super_ifcount;
3127 if (klass->HasSuperClass()) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07003128 super_ifcount = klass->GetSuperClass()->GetIfTableCount();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003129 } else {
3130 super_ifcount = 0;
3131 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07003132 size_t ifcount = super_ifcount;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003133 ClassHelper kh(klass.get(), this);
Ian Rogersd24e2642012-06-06 21:21:43 -07003134 uint32_t num_interfaces = interfaces == NULL ? kh.NumDirectInterfaces() : interfaces->GetLength();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003135 ifcount += num_interfaces;
3136 for (size_t i = 0; i < num_interfaces; i++) {
Ian Rogersd24e2642012-06-06 21:21:43 -07003137 Class* interface = interfaces == NULL ? kh.GetDirectInterface(i) : interfaces->Get(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003138 ifcount += interface->GetIfTableCount();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003139 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07003140 if (ifcount == 0) {
Ian Rogers9bc81912012-10-11 21:43:36 -07003141 // Class implements no interfaces.
3142 DCHECK_EQ(klass->GetIfTableCount(), 0);
3143 DCHECK(klass->GetIfTable() == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003144 return true;
3145 }
Ian Rogers9bc81912012-10-11 21:43:36 -07003146 if (ifcount == super_ifcount) {
3147 // Class implements same interfaces as parent, are any of these not marker interfaces?
3148 bool has_non_marker_interface = false;
3149 IfTable* super_iftable = klass->GetSuperClass()->GetIfTable();
3150 for (size_t i = 0; i < ifcount; ++i) {
3151 if (super_iftable->GetMethodArrayCount(i) > 0) {
3152 has_non_marker_interface = true;
3153 break;
3154 }
3155 }
3156 if (!has_non_marker_interface) {
3157 // Class just inherits marker interfaces from parent so recycle parent's iftable.
3158 klass->SetIfTable(super_iftable);
3159 return true;
3160 }
3161 }
Ian Rogers1f539342012-10-03 21:09:42 -07003162 Thread* self = Thread::Current();
Ian Rogers9bc81912012-10-11 21:43:36 -07003163 SirtRef<IfTable> iftable(self, AllocIfTable(self, ifcount));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003164 if (super_ifcount != 0) {
Ian Rogers9bc81912012-10-11 21:43:36 -07003165 IfTable* super_iftable = klass->GetSuperClass()->GetIfTable();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07003166 for (size_t i = 0; i < super_ifcount; i++) {
Ian Rogers9bc81912012-10-11 21:43:36 -07003167 Class* super_interface = super_iftable->GetInterface(i);
3168 iftable->SetInterface(i, super_interface);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07003169 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003170 }
3171 // Flatten the interface inheritance hierarchy.
3172 size_t idx = super_ifcount;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003173 for (size_t i = 0; i < num_interfaces; i++) {
Ian Rogersd24e2642012-06-06 21:21:43 -07003174 Class* interface = interfaces == NULL ? kh.GetDirectInterface(i) : interfaces->Get(i);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07003175 DCHECK(interface != NULL);
3176 if (!interface->IsInterface()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003177 ClassHelper ih(interface);
Brian Carlstrom4d9716c2012-01-30 01:49:33 -08003178 self->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
Elliott Hughes4a2b4172011-09-20 17:08:25 -07003179 "Class %s implements non-interface class %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003180 PrettyDescriptor(klass.get()).c_str(),
3181 PrettyDescriptor(ih.GetDescriptor()).c_str());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003182 return false;
3183 }
Ian Rogersb52b01a2012-01-12 17:01:38 -08003184 // Check if interface is already in iftable
3185 bool duplicate = false;
3186 for (size_t j = 0; j < idx; j++) {
Ian Rogers9bc81912012-10-11 21:43:36 -07003187 Class* existing_interface = iftable->GetInterface(j);
Ian Rogersb52b01a2012-01-12 17:01:38 -08003188 if (existing_interface == interface) {
3189 duplicate = true;
3190 break;
3191 }
3192 }
3193 if (!duplicate) {
3194 // Add this non-duplicate interface.
Ian Rogers9bc81912012-10-11 21:43:36 -07003195 iftable->SetInterface(idx++, interface);
Ian Rogersb52b01a2012-01-12 17:01:38 -08003196 // Add this interface's non-duplicate super-interfaces.
3197 for (int32_t j = 0; j < interface->GetIfTableCount(); j++) {
Ian Rogers9bc81912012-10-11 21:43:36 -07003198 Class* super_interface = interface->GetIfTable()->GetInterface(j);
Ian Rogersb52b01a2012-01-12 17:01:38 -08003199 bool super_duplicate = false;
3200 for (size_t k = 0; k < idx; k++) {
Ian Rogers9bc81912012-10-11 21:43:36 -07003201 Class* existing_interface = iftable->GetInterface(k);
Ian Rogersb52b01a2012-01-12 17:01:38 -08003202 if (existing_interface == super_interface) {
3203 super_duplicate = true;
3204 break;
3205 }
3206 }
3207 if (!super_duplicate) {
Ian Rogers9bc81912012-10-11 21:43:36 -07003208 iftable->SetInterface(idx++, super_interface);
Ian Rogersb52b01a2012-01-12 17:01:38 -08003209 }
3210 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003211 }
3212 }
Ian Rogersb52b01a2012-01-12 17:01:38 -08003213 // Shrink iftable in case duplicates were found
3214 if (idx < ifcount) {
Ian Rogers9bc81912012-10-11 21:43:36 -07003215 iftable.reset(down_cast<IfTable*>(iftable->CopyOf(self, idx * IfTable::kMax)));
Ian Rogersb52b01a2012-01-12 17:01:38 -08003216 ifcount = idx;
3217 } else {
3218 CHECK_EQ(idx, ifcount);
3219 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07003220 klass->SetIfTable(iftable.get());
Elliott Hughes4681c802011-09-25 18:04:37 -07003221
3222 // If we're an interface, we don't need the vtable pointers, so we're done.
Ian Rogers9bc81912012-10-11 21:43:36 -07003223 if (klass->IsInterface()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003224 return true;
3225 }
Mathieu Chartier66f19252012-09-18 08:57:04 -07003226 std::vector<AbstractMethod*> miranda_list;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003227 MethodHelper vtable_mh(NULL, this);
3228 MethodHelper interface_mh(NULL, this);
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07003229 for (size_t i = 0; i < ifcount; ++i) {
Ian Rogers9bc81912012-10-11 21:43:36 -07003230 Class* interface = iftable->GetInterface(i);
3231 size_t num_methods = interface->NumVirtualMethods();
3232 if (num_methods > 0) {
3233 ObjectArray<AbstractMethod>* method_array = AllocMethodArray(self, num_methods);
3234 iftable->SetMethodArray(i, method_array);
3235 ObjectArray<AbstractMethod>* vtable = klass->GetVTableDuringLinking();
3236 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
3237 AbstractMethod* interface_method = interface->GetVirtualMethod(j);
3238 interface_mh.ChangeMethod(interface_method);
3239 int32_t k;
3240 // For each method listed in the interface's method list, find the
3241 // matching method in our class's method list. We want to favor the
3242 // subclass over the superclass, which just requires walking
3243 // back from the end of the vtable. (This only matters if the
3244 // superclass defines a private method and this class redefines
3245 // it -- otherwise it would use the same vtable slot. In .dex files
3246 // those don't end up in the virtual method table, so it shouldn't
3247 // matter which direction we go. We walk it backward anyway.)
3248 for (k = vtable->GetLength() - 1; k >= 0; --k) {
3249 AbstractMethod* vtable_method = vtable->Get(k);
3250 vtable_mh.ChangeMethod(vtable_method);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003251 if (interface_mh.HasSameNameAndSignature(&vtable_mh)) {
Ian Rogers9bc81912012-10-11 21:43:36 -07003252 if (!vtable_method->IsPublic()) {
3253 self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
3254 "Implementation not public: %s",
3255 PrettyMethod(vtable_method).c_str());
3256 return false;
3257 }
3258 method_array->Set(j, vtable_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003259 break;
3260 }
3261 }
Ian Rogers9bc81912012-10-11 21:43:36 -07003262 if (k < 0) {
3263 SirtRef<AbstractMethod> miranda_method(self, NULL);
3264 for (size_t mir = 0; mir < miranda_list.size(); mir++) {
3265 AbstractMethod* mir_method = miranda_list[mir];
3266 vtable_mh.ChangeMethod(mir_method);
3267 if (interface_mh.HasSameNameAndSignature(&vtable_mh)) {
3268 miranda_method.reset(miranda_list[mir]);
3269 break;
3270 }
3271 }
3272 if (miranda_method.get() == NULL) {
3273 // point the interface table at a phantom slot
3274 miranda_method.reset(down_cast<AbstractMethod*>(interface_method->Clone(self)));
3275 miranda_list.push_back(miranda_method.get());
3276 }
3277 method_array->Set(j, miranda_method.get());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003278 }
3279 }
3280 }
3281 }
Elliott Hughes4681c802011-09-25 18:04:37 -07003282 if (!miranda_list.empty()) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07003283 int old_method_count = klass->NumVirtualMethods();
Elliott Hughes4681c802011-09-25 18:04:37 -07003284 int new_method_count = old_method_count + miranda_list.size();
Brian Carlstrom27ec9612011-09-19 20:20:38 -07003285 klass->SetVirtualMethods((old_method_count == 0)
Ian Rogers4445a7e2012-10-05 17:19:13 -07003286 ? AllocMethodArray(self, new_method_count)
Ian Rogers50b35e22012-10-04 10:09:15 -07003287 : klass->GetVirtualMethods()->CopyOf(self, new_method_count));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003288
Ian Rogers1f539342012-10-03 21:09:42 -07003289 SirtRef<ObjectArray<AbstractMethod> > vtable(self, klass->GetVTableDuringLinking());
Ian Rogers30fab402012-01-23 15:43:46 -08003290 CHECK(vtable.get() != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003291 int old_vtable_count = vtable->GetLength();
Elliott Hughes4681c802011-09-25 18:04:37 -07003292 int new_vtable_count = old_vtable_count + miranda_list.size();
Ian Rogers50b35e22012-10-04 10:09:15 -07003293 vtable.reset(vtable->CopyOf(self, new_vtable_count));
Elliott Hughes4681c802011-09-25 18:04:37 -07003294 for (size_t i = 0; i < miranda_list.size(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07003295 AbstractMethod* method = miranda_list[i];
Ian Rogers9074b992011-10-26 17:41:55 -07003296 // Leave the declaring class alone as type indices are relative to it
Brian Carlstrom92827a52011-10-10 15:50:01 -07003297 method->SetAccessFlags(method->GetAccessFlags() | kAccMiranda);
3298 method->SetMethodIndex(0xFFFF & (old_vtable_count + i));
3299 klass->SetVirtualMethod(old_method_count + i, method);
3300 vtable->Set(old_vtable_count + i, method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003301 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003302 // TODO: do not assign to the vtable field until it is fully constructed.
Ian Rogers30fab402012-01-23 15:43:46 -08003303 klass->SetVTable(vtable.get());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003304 }
Elliott Hughes4681c802011-09-25 18:04:37 -07003305
Mathieu Chartier66f19252012-09-18 08:57:04 -07003306 ObjectArray<AbstractMethod>* vtable = klass->GetVTableDuringLinking();
Elliott Hughes4681c802011-09-25 18:04:37 -07003307 for (int i = 0; i < vtable->GetLength(); ++i) {
3308 CHECK(vtable->Get(i) != NULL);
3309 }
3310
3311// klass->DumpClass(std::cerr, Class::kDumpClassFullDetail);
3312
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003313 return true;
3314}
3315
Brian Carlstrom40381fb2011-10-19 14:13:40 -07003316bool ClassLinker::LinkInstanceFields(SirtRef<Class>& klass) {
3317 CHECK(klass.get() != NULL);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07003318 return LinkFields(klass, false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07003319}
3320
Brian Carlstrom40381fb2011-10-19 14:13:40 -07003321bool ClassLinker::LinkStaticFields(SirtRef<Class>& klass) {
3322 CHECK(klass.get() != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003323 size_t allocated_class_size = klass->GetClassSize();
Brian Carlstrom3320cf42011-10-04 14:58:28 -07003324 bool success = LinkFields(klass, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003325 CHECK_EQ(allocated_class_size, klass->GetClassSize());
Brian Carlstrom4873d462011-08-21 15:23:39 -07003326 return success;
3327}
3328
Brian Carlstromdbc05252011-09-09 01:59:59 -07003329struct LinkFieldsComparator {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003330 explicit LinkFieldsComparator(FieldHelper* fh)
Ian Rogersb726dcb2012-09-05 08:57:23 -07003331 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003332 : fh_(fh) {}
3333 // No thread safety analysis as will be called from STL. Checked lock held in constructor.
3334 bool operator()(const Field* field1, const Field* field2) NO_THREAD_SAFETY_ANALYSIS {
Brian Carlstromdbc05252011-09-09 01:59:59 -07003335 // First come reference fields, then 64-bit, and finally 32-bit
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003336 fh_->ChangeField(field1);
3337 Primitive::Type type1 = fh_->GetTypeAsPrimitiveType();
3338 fh_->ChangeField(field2);
3339 Primitive::Type type2 = fh_->GetTypeAsPrimitiveType();
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003340 bool isPrimitive1 = type1 != Primitive::kPrimNot;
3341 bool isPrimitive2 = type2 != Primitive::kPrimNot;
3342 bool is64bit1 = isPrimitive1 && (type1 == Primitive::kPrimLong || type1 == Primitive::kPrimDouble);
3343 bool is64bit2 = isPrimitive2 && (type2 == Primitive::kPrimLong || type2 == Primitive::kPrimDouble);
Brian Carlstromdbc05252011-09-09 01:59:59 -07003344 int order1 = (!isPrimitive1 ? 0 : (is64bit1 ? 1 : 2));
3345 int order2 = (!isPrimitive2 ? 0 : (is64bit2 ? 1 : 2));
3346 if (order1 != order2) {
3347 return order1 < order2;
3348 }
3349
3350 // same basic group? then sort by string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003351 fh_->ChangeField(field1);
3352 StringPiece name1(fh_->GetName());
3353 fh_->ChangeField(field2);
3354 StringPiece name2(fh_->GetName());
Brian Carlstromdbc05252011-09-09 01:59:59 -07003355 return name1 < name2;
3356 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003357
3358 FieldHelper* fh_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07003359};
3360
Brian Carlstrom40381fb2011-10-19 14:13:40 -07003361bool ClassLinker::LinkFields(SirtRef<Class>& klass, bool is_static) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003362 size_t num_fields =
Brian Carlstrom3320cf42011-10-04 14:58:28 -07003363 is_static ? klass->NumStaticFields() : klass->NumInstanceFields();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003364
3365 ObjectArray<Field>* fields =
Brian Carlstrom3320cf42011-10-04 14:58:28 -07003366 is_static ? klass->GetSFields() : klass->GetIFields();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003367
3368 // Initialize size and field_offset
Brian Carlstrom693267a2011-09-06 09:25:34 -07003369 size_t size;
3370 MemberOffset field_offset(0);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07003371 if (is_static) {
3372 size = klass->GetClassSize();
3373 field_offset = Class::FieldsOffset();
3374 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003375 Class* super_class = klass->GetSuperClass();
3376 if (super_class != NULL) {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07003377 CHECK(super_class->IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003378 field_offset = MemberOffset(super_class->GetObjectSize());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003379 }
3380 size = field_offset.Uint32Value();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003381 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003382
Brian Carlstromdbc05252011-09-09 01:59:59 -07003383 CHECK_EQ(num_fields == 0, fields == NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003384
Brian Carlstromdbc05252011-09-09 01:59:59 -07003385 // we want a relatively stable order so that adding new fields
Elliott Hughesadb460d2011-10-05 17:02:34 -07003386 // minimizes disruption of C++ version such as Class and Method.
Brian Carlstromdbc05252011-09-09 01:59:59 -07003387 std::deque<Field*> grouped_and_sorted_fields;
3388 for (size_t i = 0; i < num_fields; i++) {
3389 grouped_and_sorted_fields.push_back(fields->Get(i));
3390 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003391 FieldHelper fh(NULL, this);
Brian Carlstromdbc05252011-09-09 01:59:59 -07003392 std::sort(grouped_and_sorted_fields.begin(),
3393 grouped_and_sorted_fields.end(),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003394 LinkFieldsComparator(&fh));
Brian Carlstromdbc05252011-09-09 01:59:59 -07003395
3396 // References should be at the front.
3397 size_t current_field = 0;
3398 size_t num_reference_fields = 0;
3399 for (; current_field < num_fields; current_field++) {
3400 Field* field = grouped_and_sorted_fields.front();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003401 fh.ChangeField(field);
3402 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003403 bool isPrimitive = type != Primitive::kPrimNot;
Brian Carlstromdbc05252011-09-09 01:59:59 -07003404 if (isPrimitive) {
3405 break; // past last reference, move on to the next phase
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003406 }
Brian Carlstromdbc05252011-09-09 01:59:59 -07003407 grouped_and_sorted_fields.pop_front();
3408 num_reference_fields++;
3409 fields->Set(current_field, field);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003410 field->SetOffset(field_offset);
3411 field_offset = MemberOffset(field_offset.Uint32Value() + sizeof(uint32_t));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003412 }
3413
3414 // Now we want to pack all of the double-wide fields together. If
3415 // we're not aligned, though, we want to shuffle one 32-bit field
3416 // into place. If we can't find one, we'll have to pad it.
Elliott Hughes06b37d92011-10-16 11:51:29 -07003417 if (current_field != num_fields && !IsAligned<8>(field_offset.Uint32Value())) {
Brian Carlstromdbc05252011-09-09 01:59:59 -07003418 for (size_t i = 0; i < grouped_and_sorted_fields.size(); i++) {
3419 Field* field = grouped_and_sorted_fields[i];
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003420 fh.ChangeField(field);
3421 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003422 CHECK(type != Primitive::kPrimNot); // should only be working on primitive types
3423 if (type == Primitive::kPrimLong || type == Primitive::kPrimDouble) {
Brian Carlstromdbc05252011-09-09 01:59:59 -07003424 continue;
3425 }
3426 fields->Set(current_field++, field);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003427 field->SetOffset(field_offset);
Brian Carlstromdbc05252011-09-09 01:59:59 -07003428 // drop the consumed field
3429 grouped_and_sorted_fields.erase(grouped_and_sorted_fields.begin() + i);
3430 break;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003431 }
Brian Carlstromdbc05252011-09-09 01:59:59 -07003432 // whether we found a 32-bit field for padding or not, we advance
3433 field_offset = MemberOffset(field_offset.Uint32Value() + sizeof(uint32_t));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003434 }
3435
3436 // Alignment is good, shuffle any double-wide fields forward, and
3437 // finish assigning field offsets to all fields.
Elliott Hughes06b37d92011-10-16 11:51:29 -07003438 DCHECK(current_field == num_fields || IsAligned<8>(field_offset.Uint32Value()));
Brian Carlstromdbc05252011-09-09 01:59:59 -07003439 while (!grouped_and_sorted_fields.empty()) {
3440 Field* field = grouped_and_sorted_fields.front();
3441 grouped_and_sorted_fields.pop_front();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003442 fh.ChangeField(field);
3443 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003444 CHECK(type != Primitive::kPrimNot); // should only be working on primitive types
Brian Carlstromdbc05252011-09-09 01:59:59 -07003445 fields->Set(current_field, field);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003446 field->SetOffset(field_offset);
Brian Carlstromdbc05252011-09-09 01:59:59 -07003447 field_offset = MemberOffset(field_offset.Uint32Value() +
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003448 ((type == Primitive::kPrimLong || type == Primitive::kPrimDouble)
Brian Carlstromdbc05252011-09-09 01:59:59 -07003449 ? sizeof(uint64_t)
3450 : sizeof(uint32_t)));
3451 current_field++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003452 }
3453
Elliott Hughesadb460d2011-10-05 17:02:34 -07003454 // We lie to the GC about the java.lang.ref.Reference.referent field, so it doesn't scan it.
Ian Rogers64b6d142012-10-29 16:34:15 -07003455 if (!is_static &&
3456 StringPiece(ClassHelper(klass.get(), this).GetDescriptor()) == "Ljava/lang/ref/Reference;") {
Elliott Hughesadb460d2011-10-05 17:02:34 -07003457 // We know there are no non-reference fields in the Reference classes, and we know
3458 // that 'referent' is alphabetically last, so this is easy...
3459 CHECK_EQ(num_reference_fields, num_fields);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003460 fh.ChangeField(fields->Get(num_fields - 1));
Elliott Hughesba8eee12012-01-24 20:25:24 -08003461 CHECK_STREQ(fh.GetName(), "referent");
Elliott Hughesadb460d2011-10-05 17:02:34 -07003462 --num_reference_fields;
3463 }
3464
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003465#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07003466 // Make sure that all reference fields appear before
3467 // non-reference fields, and all double-wide fields are aligned.
3468 bool seen_non_ref = false;
Brian Carlstromdbc05252011-09-09 01:59:59 -07003469 for (size_t i = 0; i < num_fields; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003470 Field* field = fields->Get(i);
Brian Carlstromdbc05252011-09-09 01:59:59 -07003471 if (false) { // enable to debug field layout
Brian Carlstrom3320cf42011-10-04 14:58:28 -07003472 LOG(INFO) << "LinkFields: " << (is_static ? "static" : "instance")
Brian Carlstrom40381fb2011-10-19 14:13:40 -07003473 << " class=" << PrettyClass(klass.get())
Brian Carlstrom65ca0772011-09-24 16:03:08 -07003474 << " field=" << PrettyField(field)
Brian Carlstromdbc05252011-09-09 01:59:59 -07003475 << " offset=" << field->GetField32(MemberOffset(Field::OffsetOffset()), false);
3476 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003477 fh.ChangeField(field);
3478 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003479 bool is_primitive = type != Primitive::kPrimNot;
Ian Rogers64b6d142012-10-29 16:34:15 -07003480 if (StringPiece(ClassHelper(klass.get(), this).GetDescriptor()) == "Ljava/lang/ref/Reference;" &&
3481 StringPiece(fh.GetName()) == "referent") {
Elliott Hughesadb460d2011-10-05 17:02:34 -07003482 is_primitive = true; // We lied above, so we have to expect a lie here.
3483 }
3484 if (is_primitive) {
Brian Carlstrombe977852011-07-19 14:54:54 -07003485 if (!seen_non_ref) {
3486 seen_non_ref = true;
Brian Carlstrom4873d462011-08-21 15:23:39 -07003487 DCHECK_EQ(num_reference_fields, i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003488 }
Brian Carlstrombe977852011-07-19 14:54:54 -07003489 } else {
3490 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003491 }
3492 }
Brian Carlstrombe977852011-07-19 14:54:54 -07003493 if (!seen_non_ref) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07003494 DCHECK_EQ(num_fields, num_reference_fields);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003495 }
3496#endif
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003497 size = field_offset.Uint32Value();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003498 // Update klass
Brian Carlstrom3320cf42011-10-04 14:58:28 -07003499 if (is_static) {
3500 klass->SetNumReferenceStaticFields(num_reference_fields);
3501 klass->SetClassSize(size);
3502 } else {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003503 klass->SetNumReferenceInstanceFields(num_reference_fields);
Brian Carlstromdbc05252011-09-09 01:59:59 -07003504 if (!klass->IsVariableSize()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003505 klass->SetObjectSize(size);
3506 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003507 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003508 return true;
3509}
3510
3511// Set the bitmap of reference offsets, refOffsets, from the ifields
3512// list.
Brian Carlstrom40381fb2011-10-19 14:13:40 -07003513void ClassLinker::CreateReferenceInstanceOffsets(SirtRef<Class>& klass) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003514 uint32_t reference_offsets = 0;
3515 Class* super_class = klass->GetSuperClass();
3516 if (super_class != NULL) {
3517 reference_offsets = super_class->GetReferenceInstanceOffsets();
Brian Carlstrom4873d462011-08-21 15:23:39 -07003518 // If our superclass overflowed, we don't stand a chance.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003519 if (reference_offsets == CLASS_WALK_SUPER) {
3520 klass->SetReferenceInstanceOffsets(reference_offsets);
Brian Carlstrom4873d462011-08-21 15:23:39 -07003521 return;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003522 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003523 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -07003524 CreateReferenceOffsets(klass, false, reference_offsets);
Brian Carlstrom4873d462011-08-21 15:23:39 -07003525}
3526
Brian Carlstrom40381fb2011-10-19 14:13:40 -07003527void ClassLinker::CreateReferenceStaticOffsets(SirtRef<Class>& klass) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -07003528 CreateReferenceOffsets(klass, true, 0);
Brian Carlstrom4873d462011-08-21 15:23:39 -07003529}
3530
Brian Carlstrom40381fb2011-10-19 14:13:40 -07003531void ClassLinker::CreateReferenceOffsets(SirtRef<Class>& klass, bool is_static,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003532 uint32_t reference_offsets) {
3533 size_t num_reference_fields =
Brian Carlstrom3320cf42011-10-04 14:58:28 -07003534 is_static ? klass->NumReferenceStaticFieldsDuringLinking()
3535 : klass->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003536 const ObjectArray<Field>* fields =
Brian Carlstrom3320cf42011-10-04 14:58:28 -07003537 is_static ? klass->GetSFields() : klass->GetIFields();
Brian Carlstrom4873d462011-08-21 15:23:39 -07003538 // All of the fields that contain object references are guaranteed
3539 // to be at the beginning of the fields list.
3540 for (size_t i = 0; i < num_reference_fields; ++i) {
3541 // Note that byte_offset is the offset from the beginning of
3542 // object, not the offset into instance data
3543 const Field* field = fields->Get(i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003544 MemberOffset byte_offset = field->GetOffsetDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003545 CHECK_EQ(byte_offset.Uint32Value() & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
3546 if (CLASS_CAN_ENCODE_OFFSET(byte_offset.Uint32Value())) {
3547 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset.Uint32Value());
Brian Carlstrom4873d462011-08-21 15:23:39 -07003548 CHECK_NE(new_bit, 0U);
3549 reference_offsets |= new_bit;
3550 } else {
3551 reference_offsets = CLASS_WALK_SUPER;
3552 break;
3553 }
3554 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003555 // Update fields in klass
Brian Carlstrom3320cf42011-10-04 14:58:28 -07003556 if (is_static) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003557 klass->SetReferenceStaticOffsets(reference_offsets);
Brian Carlstrom3320cf42011-10-04 14:58:28 -07003558 } else {
3559 klass->SetReferenceInstanceOffsets(reference_offsets);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003560 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003561}
3562
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003563String* ClassLinker::ResolveString(const DexFile& dex_file,
Elliott Hughescf4c6c42011-09-01 15:16:42 -07003564 uint32_t string_idx, DexCache* dex_cache) {
Brian Carlstrom7d776242012-03-06 23:05:49 -08003565 DCHECK(dex_cache != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003566 String* resolved = dex_cache->GetResolvedString(string_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003567 if (resolved != NULL) {
3568 return resolved;
3569 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07003570 const DexFile::StringId& string_id = dex_file.GetStringId(string_idx);
3571 int32_t utf16_length = dex_file.GetStringLength(string_id);
3572 const char* utf8_data = dex_file.GetStringData(string_id);
Brian Carlstrom928bf022011-10-11 02:48:14 -07003573 String* string = intern_table_->InternStrong(utf16_length, utf8_data);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07003574 dex_cache->SetResolvedString(string_idx, string);
3575 return string;
3576}
3577
3578Class* ClassLinker::ResolveType(const DexFile& dex_file,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003579 uint16_t type_idx,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07003580 DexCache* dex_cache,
Ian Rogers365c1022012-06-22 15:05:28 -07003581 ClassLoader* class_loader) {
Brian Carlstrom7d776242012-03-06 23:05:49 -08003582 DCHECK(dex_cache != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07003583 Class* resolved = dex_cache->GetResolvedType(type_idx);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003584 if (resolved == NULL) {
Ian Rogers0571d352011-11-03 19:51:38 -07003585 const char* descriptor = dex_file.StringByTypeIdx(type_idx);
Brian Carlstromaded5f72011-10-07 17:15:04 -07003586 resolved = FindClass(descriptor, class_loader);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003587 if (resolved != NULL) {
Jesse Wilson254db0f2011-11-16 16:44:11 -05003588 // TODO: we used to throw here if resolved's class loader was not the
3589 // boot class loader. This was to permit different classes with the
3590 // same name to be loaded simultaneously by different loaders
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003591 dex_cache->SetResolvedType(type_idx, resolved);
3592 } else {
Ian Rogerscab01012012-01-10 17:35:46 -08003593 CHECK(Thread::Current()->IsExceptionPending())
3594 << "Expected pending exception for failed resolution of: " << descriptor;
jeffhao8cd6dda2012-02-22 10:15:34 -08003595 // Convert a ClassNotFoundException to a NoClassDefFoundError
3596 if (Thread::Current()->GetException()->InstanceOf(GetClassRoot(kJavaLangClassNotFoundException))) {
3597 Thread::Current()->ClearException();
3598 ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor);
3599 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003600 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003601 }
3602 return resolved;
3603}
3604
Mathieu Chartier66f19252012-09-18 08:57:04 -07003605AbstractMethod* ClassLinker::ResolveMethod(const DexFile& dex_file,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07003606 uint32_t method_idx,
3607 DexCache* dex_cache,
Ian Rogers365c1022012-06-22 15:05:28 -07003608 ClassLoader* class_loader,
Mathieu Chartier66f19252012-09-18 08:57:04 -07003609 const AbstractMethod* referrer,
Ian Rogers08f753d2012-08-24 14:35:25 -07003610 InvokeType type) {
Brian Carlstrom7d776242012-03-06 23:05:49 -08003611 DCHECK(dex_cache != NULL);
Ian Rogers08f753d2012-08-24 14:35:25 -07003612 // Check for hit in the dex cache.
Mathieu Chartier66f19252012-09-18 08:57:04 -07003613 AbstractMethod* resolved = dex_cache->GetResolvedMethod(method_idx);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07003614 if (resolved != NULL) {
3615 return resolved;
3616 }
Ian Rogers08f753d2012-08-24 14:35:25 -07003617 // Fail, get the declaring class.
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07003618 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
3619 Class* klass = ResolveType(dex_file, method_id.class_idx_, dex_cache, class_loader);
3620 if (klass == NULL) {
Elliott Hughescc5f9a92011-09-28 19:17:29 -07003621 DCHECK(Thread::Current()->IsExceptionPending());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07003622 return NULL;
3623 }
Ian Rogers08f753d2012-08-24 14:35:25 -07003624 // Scan using method_idx, this saves string compares but will only hit for matching dex
3625 // caches/files.
3626 switch (type) {
3627 case kDirect: // Fall-through.
3628 case kStatic:
3629 resolved = klass->FindDirectMethod(dex_cache, method_idx);
3630 break;
3631 case kInterface:
3632 resolved = klass->FindInterfaceMethod(dex_cache, method_idx);
3633 break;
3634 case kSuper: // Fall-through.
3635 case kVirtual:
3636 resolved = klass->FindVirtualMethod(dex_cache, method_idx);
3637 break;
3638 default:
3639 LOG(FATAL) << "Unreachable - invocation type: " << type;
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07003640 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003641 if (resolved == NULL) {
Ian Rogers08f753d2012-08-24 14:35:25 -07003642 // Search by name, which works across dex files.
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003643 const char* name = dex_file.StringDataByIdx(method_id.name_idx_);
3644 std::string signature(dex_file.CreateMethodSignature(method_id.proto_idx_, NULL));
Ian Rogers08f753d2012-08-24 14:35:25 -07003645 switch (type) {
3646 case kDirect: // Fall-through.
3647 case kStatic:
jeffhao8cd6dda2012-02-22 10:15:34 -08003648 resolved = klass->FindDirectMethod(name, signature);
Ian Rogers08f753d2012-08-24 14:35:25 -07003649 break;
3650 case kInterface:
3651 resolved = klass->FindInterfaceMethod(name, signature);
3652 break;
3653 case kSuper: // Fall-through.
3654 case kVirtual:
3655 resolved = klass->FindVirtualMethod(name, signature);
3656 break;
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003657 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07003658 }
Ian Rogers08f753d2012-08-24 14:35:25 -07003659 if (resolved != NULL) {
3660 // We found a method, check for incompatible class changes.
Ian Rogers87e552d2012-08-31 15:54:48 -07003661 if (resolved->CheckIncompatibleClassChange(type)) {
3662 resolved = NULL;
Ian Rogers08f753d2012-08-24 14:35:25 -07003663 }
3664 }
3665 if (resolved != NULL) {
3666 // Be a good citizen and update the dex cache to speed subsequent calls.
3667 dex_cache->SetResolvedMethod(method_idx, resolved);
3668 return resolved;
3669 } else {
jeffhaoc0228b82012-08-29 18:15:05 -07003670 // We failed to find the method which means either an access error, an incompatible class
3671 // change, or no such method. First try to find the method among direct and virtual methods.
Ian Rogers08f753d2012-08-24 14:35:25 -07003672 const char* name = dex_file.StringDataByIdx(method_id.name_idx_);
3673 std::string signature(dex_file.CreateMethodSignature(method_id.proto_idx_, NULL));
3674 switch (type) {
3675 case kDirect:
3676 case kStatic:
3677 resolved = klass->FindVirtualMethod(name, signature);
jeffhaoc0228b82012-08-29 18:15:05 -07003678 break;
3679 case kInterface:
3680 case kVirtual:
3681 case kSuper:
3682 resolved = klass->FindDirectMethod(name, signature);
3683 break;
3684 }
3685
3686 // If we found something, check that it can be accessed by the referrer.
3687 if (resolved != NULL && referrer != NULL) {
3688 Class* methods_class = resolved->GetDeclaringClass();
3689 Class* referring_class = referrer->GetDeclaringClass();
3690 if (!referring_class->CanAccess(methods_class)) {
Ian Rogers87e552d2012-08-31 15:54:48 -07003691 ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, methods_class,
3692 referrer, resolved, type);
jeffhaoc0228b82012-08-29 18:15:05 -07003693 return NULL;
3694 } else if (!referring_class->CanAccessMember(methods_class,
3695 resolved->GetAccessFlags())) {
Ian Rogers87e552d2012-08-31 15:54:48 -07003696 ThrowIllegalAccessErrorMethod(referring_class, resolved);
jeffhaoc0228b82012-08-29 18:15:05 -07003697 return NULL;
3698 }
3699 }
3700
3701 // Otherwise, throw an IncompatibleClassChangeError if we found something, and check interface
3702 // methods and throw if we find the method there. If we find nothing, throw a NoSuchMethodError.
3703 switch (type) {
3704 case kDirect:
3705 case kStatic:
Ian Rogers08f753d2012-08-24 14:35:25 -07003706 if (resolved != NULL) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003707 ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -07003708 } else {
3709 resolved = klass->FindInterfaceMethod(name, signature);
3710 if (resolved != NULL) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003711 ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -07003712 } else {
Ian Rogers2fc14272012-08-30 10:56:57 -07003713 ThrowNoSuchMethodError(type, klass, name, signature, referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -07003714 }
3715 }
3716 break;
3717 case kInterface:
Ian Rogers08f753d2012-08-24 14:35:25 -07003718 if (resolved != NULL) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003719 ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -07003720 } else {
3721 resolved = klass->FindVirtualMethod(name, signature);
3722 if (resolved != NULL) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003723 ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -07003724 } else {
Ian Rogers2fc14272012-08-30 10:56:57 -07003725 ThrowNoSuchMethodError(type, klass, name, signature, referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -07003726 }
3727 }
3728 break;
3729 case kSuper:
Ian Rogers2fc14272012-08-30 10:56:57 -07003730 ThrowNoSuchMethodError(type, klass, name, signature, referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -07003731 break;
3732 case kVirtual:
Ian Rogers08f753d2012-08-24 14:35:25 -07003733 if (resolved != NULL) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003734 ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -07003735 } else {
3736 resolved = klass->FindInterfaceMethod(name, signature);
3737 if (resolved != NULL) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003738 ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -07003739 } else {
Ian Rogers2fc14272012-08-30 10:56:57 -07003740 ThrowNoSuchMethodError(type, klass, name, signature, referrer);
Ian Rogers08f753d2012-08-24 14:35:25 -07003741 }
3742 }
3743 break;
3744 }
3745 DCHECK(Thread::Current()->IsExceptionPending());
3746 return NULL;
3747 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003748}
3749
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07003750Field* ClassLinker::ResolveField(const DexFile& dex_file,
3751 uint32_t field_idx,
3752 DexCache* dex_cache,
Ian Rogers365c1022012-06-22 15:05:28 -07003753 ClassLoader* class_loader,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07003754 bool is_static) {
Brian Carlstrom7d776242012-03-06 23:05:49 -08003755 DCHECK(dex_cache != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07003756 Field* resolved = dex_cache->GetResolvedField(field_idx);
3757 if (resolved != NULL) {
3758 return resolved;
3759 }
3760 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx);
3761 Class* klass = ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader);
3762 if (klass == NULL) {
Ian Rogers9f1ab122011-12-12 08:52:43 -08003763 DCHECK(Thread::Current()->IsExceptionPending());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07003764 return NULL;
3765 }
3766
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07003767 if (is_static) {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003768 resolved = klass->FindStaticField(dex_cache, field_idx);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07003769 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003770 resolved = klass->FindInstanceField(dex_cache, field_idx);
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07003771 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003772
3773 if (resolved == NULL) {
3774 const char* name = dex_file.GetFieldName(field_id);
3775 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
3776 if (is_static) {
3777 resolved = klass->FindStaticField(name, type);
3778 } else {
3779 resolved = klass->FindInstanceField(name, type);
3780 }
3781 if (resolved == NULL) {
3782 ThrowNoSuchFieldError(is_static ? "static " : "instance ", klass, type, name);
3783 return NULL;
3784 }
Ian Rogersb067ac22011-12-13 18:05:09 -08003785 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003786 dex_cache->SetResolvedField(field_idx, resolved);
Ian Rogersb067ac22011-12-13 18:05:09 -08003787 return resolved;
3788}
3789
3790Field* ClassLinker::ResolveFieldJLS(const DexFile& dex_file,
3791 uint32_t field_idx,
3792 DexCache* dex_cache,
Ian Rogers365c1022012-06-22 15:05:28 -07003793 ClassLoader* class_loader) {
Brian Carlstrom7d776242012-03-06 23:05:49 -08003794 DCHECK(dex_cache != NULL);
Ian Rogersb067ac22011-12-13 18:05:09 -08003795 Field* resolved = dex_cache->GetResolvedField(field_idx);
3796 if (resolved != NULL) {
3797 return resolved;
3798 }
3799 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx);
3800 Class* klass = ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader);
3801 if (klass == NULL) {
3802 DCHECK(Thread::Current()->IsExceptionPending());
3803 return NULL;
3804 }
3805
3806 const char* name = dex_file.GetFieldName(field_id);
3807 const char* type = dex_file.GetFieldTypeDescriptor(field_id);
3808 resolved = klass->FindField(name, type);
3809 if (resolved != NULL) {
3810 dex_cache->SetResolvedField(field_idx, resolved);
3811 } else {
3812 ThrowNoSuchFieldError("", klass, type, name);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07003813 }
3814 return resolved;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07003815}
3816
Mathieu Chartier66f19252012-09-18 08:57:04 -07003817const char* ClassLinker::MethodShorty(uint32_t method_idx, AbstractMethod* referrer, uint32_t* length) {
Ian Rogersad25ac52011-10-04 19:13:33 -07003818 Class* declaring_class = referrer->GetDeclaringClass();
3819 DexCache* dex_cache = declaring_class->GetDexCache();
Ian Rogers4445a7e2012-10-05 17:19:13 -07003820 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogersad25ac52011-10-04 19:13:33 -07003821 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
Ian Rogers19846512012-02-24 11:42:47 -08003822 return dex_file.GetMethodShorty(method_id, length);
Ian Rogersad25ac52011-10-04 19:13:33 -07003823}
3824
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003825void ClassLinker::DumpAllClasses(int flags) const {
3826 // TODO: at the time this was written, it wasn't safe to call PrettyField with the ClassLinker
3827 // lock held, because it might need to resolve a field's type, which would try to take the lock.
3828 std::vector<Class*> all_classes;
3829 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003830 MutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003831 typedef Table::const_iterator It; // TODO: C++0x auto
3832 for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
3833 all_classes.push_back(it->second);
3834 }
Ian Rogers5d76c432011-10-31 21:42:49 -07003835 for (It it = image_classes_.begin(), end = image_classes_.end(); it != end; ++it) {
3836 all_classes.push_back(it->second);
3837 }
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07003838 }
3839
3840 for (size_t i = 0; i < all_classes.size(); ++i) {
3841 all_classes[i]->DumpClass(std::cerr, flags);
3842 }
3843}
3844
Elliott Hughescac6cc72011-11-03 20:31:21 -07003845void ClassLinker::DumpForSigQuit(std::ostream& os) const {
Ian Rogers50b35e22012-10-04 10:09:15 -07003846 MutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
Elliott Hughescac6cc72011-11-03 20:31:21 -07003847 os << "Loaded classes: " << image_classes_.size() << " image classes; "
3848 << classes_.size() << " allocated classes\n";
3849}
3850
Elliott Hughese27955c2011-08-26 15:21:24 -07003851size_t ClassLinker::NumLoadedClasses() const {
Ian Rogers50b35e22012-10-04 10:09:15 -07003852 MutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -07003853 return classes_.size() + image_classes_.size();
Elliott Hughese27955c2011-08-26 15:21:24 -07003854}
3855
Brian Carlstrom47d237a2011-10-18 15:08:33 -07003856pid_t ClassLinker::GetClassesLockOwner() {
Ian Rogersb726dcb2012-09-05 08:57:23 -07003857 return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid();
Brian Carlstrom47d237a2011-10-18 15:08:33 -07003858}
3859
3860pid_t ClassLinker::GetDexLockOwner() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003861 return dex_lock_.GetExclusiveOwnerTid();
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -07003862}
3863
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08003864void ClassLinker::SetClassRoot(ClassRoot class_root, Class* klass) {
3865 DCHECK(!init_done_);
3866
3867 DCHECK(klass != NULL);
3868 DCHECK(klass->GetClassLoader() == NULL);
3869
3870 DCHECK(class_roots_ != NULL);
3871 DCHECK(class_roots_->Get(class_root) == NULL);
3872 class_roots_->Set(class_root, klass);
3873}
3874
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003875} // namespace art