blob: 3c72dad6ab0af207a10649883423b1f59145c1fe [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "class_linker.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004
5#include <vector>
6#include <utility>
7
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07008#include "casts.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07009#include "dex_cache.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "dex_verifier.h"
11#include "heap.h"
12#include "logging.h"
13#include "monitor.h"
14#include "object.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070015#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070016#include "scoped_ptr.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070017#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070018#include "thread.h"
19#include "utils.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070020
21namespace art {
22
Brian Carlstroma663ea52011-08-19 23:33:41 -070023const char* ClassLinker::class_roots_descriptors_[kClassRootsMax] = {
24 "Ljava/lang/Class;",
25 "Ljava/lang/Object;",
26 "[Ljava/lang/Object;",
27 "Ljava/lang/String;",
28 "Ljava/lang/reflect/Field;",
29 "Ljava/lang/reflect/Method;",
30 "Ljava/lang/ClassLoader;",
31 "Ldalvik/system/BaseDexClassLoader;",
32 "Ldalvik/system/PathClassLoader;",
Shih-wei Liao55df06b2011-08-26 14:39:27 -070033 "Ljava/lang/StackTraceElement;",
Brian Carlstroma663ea52011-08-19 23:33:41 -070034 "Z",
35 "B",
36 "C",
37 "D",
38 "F",
39 "I",
40 "J",
41 "S",
42 "V",
43 "[Z",
44 "[B",
45 "[C",
46 "[D",
47 "[F",
48 "[I",
49 "[J",
50 "[S",
Shih-wei Liao55df06b2011-08-26 14:39:27 -070051 "[Ljava/lang/StackTraceElement;",
Brian Carlstroma663ea52011-08-19 23:33:41 -070052};
53
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070054ClassLinker* ClassLinker::Create(const std::vector<const DexFile*>& boot_class_path, Space* space) {
Carl Shapiro61e019d2011-07-14 16:53:09 -070055 scoped_ptr<ClassLinker> class_linker(new ClassLinker);
Brian Carlstroma663ea52011-08-19 23:33:41 -070056 if (space == NULL) {
57 class_linker->Init(boot_class_path);
58 } else {
59 class_linker->Init(boot_class_path, space);
60 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070061 // TODO: check for failure during initialization
62 return class_linker.release();
63}
64
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070065ClassLinker::ClassLinker()
66 : classes_lock_(Mutex::Create("ClassLinker::Lock")),
67 class_roots_(NULL),
68 init_done_(false) {
69}
Brian Carlstroma663ea52011-08-19 23:33:41 -070070
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070071void ClassLinker::Init(const std::vector<const DexFile*>& boot_class_path) {
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070072 CHECK(!init_done_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070073
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070074 // java_lang_Class comes first, its needed for AllocClass
Brian Carlstrom4873d462011-08-21 15:23:39 -070075 Class* java_lang_Class = down_cast<Class*>(Heap::AllocObject(NULL, sizeof(ClassClass)));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070076 CHECK(java_lang_Class != NULL);
Brian Carlstrom4873d462011-08-21 15:23:39 -070077 java_lang_Class->class_size_ = sizeof(ClassClass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070078 java_lang_Class->klass_ = java_lang_Class;
79 // AllocClass(Class*) can now be used
Brian Carlstroma0808032011-07-18 00:39:23 -070080
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070081 // java_lang_Object comes next so that object_array_class can be created
Brian Carlstrom4873d462011-08-21 15:23:39 -070082 Class* java_lang_Object = AllocClass(java_lang_Class, sizeof(Class));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070083 CHECK(java_lang_Object != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070084 // backfill Object as the super class of Class
85 java_lang_Class->super_class_ = java_lang_Object;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070086 // mark as non-primitive for object_array_class
87 java_lang_Object->primitive_type_ = Class::kPrimNot;
Brian Carlstroma0808032011-07-18 00:39:23 -070088
Elliott Hughesc1674ed2011-08-25 18:09:09 -070089 // Object[] is for DexCache and int[] is for various Class members.
Brian Carlstrom4873d462011-08-21 15:23:39 -070090 Class* object_array_class = AllocClass(java_lang_Class, sizeof(Class));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070091 CHECK(object_array_class != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070092 object_array_class->component_type_ = java_lang_Object;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070093 Class* int_array_class = AllocClass(java_lang_Class, sizeof(Class));
94 CHECK(int_array_class != NULL);
95 IntArray::SetArrayClass(int_array_class);
Brian Carlstroma0808032011-07-18 00:39:23 -070096
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070097 // String and char[] are necessary so that FindClass can assign names to members
Brian Carlstrom4873d462011-08-21 15:23:39 -070098 Class* java_lang_String = AllocClass(java_lang_Class, sizeof(StringClass));
Jesse Wilson14150742011-07-29 19:04:44 -040099 CHECK(java_lang_String != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700100 CHECK_LT(java_lang_String->object_size_, sizeof(String));
Jesse Wilson14150742011-07-29 19:04:44 -0400101 java_lang_String->object_size_ = sizeof(String);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700102 String::SetClass(java_lang_String);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700103 Class* char_array_class = AllocClass(java_lang_Class, sizeof(Class));
Jesse Wilson8989d992011-08-02 13:39:42 -0700104 CHECK(char_array_class != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700105 CharArray::SetArrayClass(char_array_class);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700106 // Now String::Alloc* can be used
107
108 // backfill Class descriptors missing until this point
109 java_lang_Class->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/Class;");
110 java_lang_Object->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/Object;");
111 object_array_class->descriptor_ = String::AllocFromModifiedUtf8("[Ljava/lang/Object;");
112 java_lang_String->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/String;");
113 char_array_class->descriptor_ = String::AllocFromModifiedUtf8("[C");
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700114 int_array_class->descriptor_ = String::AllocFromModifiedUtf8("[I");
Jesse Wilson14150742011-07-29 19:04:44 -0400115
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700116 // Field and Method are necessary so that FindClass can link members
Brian Carlstrom4873d462011-08-21 15:23:39 -0700117 Class* java_lang_reflect_Field = AllocClass(java_lang_Class, sizeof(FieldClass));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700118 CHECK(java_lang_reflect_Field != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700119 java_lang_reflect_Field->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/reflect/Field;");
Jesse Wilson35baaab2011-08-10 16:18:03 -0400120 CHECK_LT(java_lang_reflect_Field->object_size_, sizeof(Field));
121 java_lang_reflect_Field->object_size_ = sizeof(Field);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700122 Class* java_lang_reflect_Method = AllocClass(java_lang_Class, sizeof(MethodClass));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700123 java_lang_reflect_Method->descriptor_ = String::AllocFromModifiedUtf8("Ljava/lang/reflect/Method;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700124 CHECK(java_lang_reflect_Method != NULL);
125 CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method));
126 java_lang_reflect_Method->object_size_ = sizeof(Method);
127
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700128 // create storage for root classes, save away our work so far
129 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700130 SetClassRoot(kJavaLangClass, java_lang_Class);
131 SetClassRoot(kJavaLangObject, java_lang_Object);
132 SetClassRoot(kObjectArrayClass, object_array_class);
133 SetClassRoot(kJavaLangString, java_lang_String);
134 SetClassRoot(kCharArrayClass, char_array_class);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700135 SetClassRoot(kIntArrayClass, int_array_class);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700136 SetClassRoot(kJavaLangReflectField, java_lang_reflect_Field);
137 SetClassRoot(kJavaLangReflectMethod, java_lang_reflect_Method);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700138 // now that these are registered, we can use AllocClass() and AllocObjectArray
Brian Carlstroma0808032011-07-18 00:39:23 -0700139
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700140 // setup boot_class_path_ now that we can use AllocObjectArray to
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700141 // create DexCache instances
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700142 for (size_t i = 0; i != boot_class_path.size(); ++i) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700143 const DexFile* dex_file = boot_class_path[i];
144 CHECK(dex_file != NULL);
145 AppendToBootClassPath(*dex_file);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700146 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700147 // now we can use FindSystemClass, at least for non-arrays classes.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700148
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700149 // run Class, Field, and Method through FindSystemClass.
150 // this initializes their dex_cache_ fields and register them in classes_.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700151 // we also override their object_size_ values to accommodate the extra C++ fields.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700152 Class* Class_class = FindSystemClass("Ljava/lang/Class;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700153 CHECK_EQ(java_lang_Class, Class_class);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700154 CHECK_LT(java_lang_Class->object_size_, sizeof(Class));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700155 java_lang_Class->object_size_ = sizeof(Class);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700156 Class* Field_class = FindSystemClass("Ljava/lang/reflect/Field;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700157 CHECK_EQ(java_lang_reflect_Field, Field_class);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400158 CHECK_LT(java_lang_reflect_Field->object_size_, sizeof(Field));
159 java_lang_reflect_Field->object_size_ = sizeof(Field);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700160 Class* Method_class = FindSystemClass("Ljava/lang/reflect/Method;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700161 CHECK_EQ(java_lang_reflect_Method, Method_class);
162 CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700163 java_lang_reflect_Method->object_size_ = sizeof(Method);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700164
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700165 // Object and String just need more minimal setup, since they do not have extra C++ fields.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700166 Class* Object_class = FindSystemClass("Ljava/lang/Object;");
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700167 CHECK_EQ(java_lang_Object, Object_class);
168 CHECK_EQ(java_lang_Object->object_size_, sizeof(Object));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700169 Class* String_class = FindSystemClass("Ljava/lang/String;");
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700170 CHECK_EQ(java_lang_String, String_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700171 CHECK_EQ(java_lang_String->object_size_, sizeof(String));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700172
173 // Setup the ClassLoaders, adjusting the object_size_ as necessary
174 Class* java_lang_ClassLoader = FindSystemClass("Ljava/lang/ClassLoader;");
175 CHECK(java_lang_ClassLoader != NULL);
176 CHECK_LT(java_lang_ClassLoader->object_size_, sizeof(ClassLoader));
177 java_lang_ClassLoader->object_size_ = sizeof(ClassLoader);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700178 SetClassRoot(kJavaLangClassLoader, java_lang_ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700179 Class* dalvik_system_BaseDexClassLoader = FindSystemClass("Ldalvik/system/BaseDexClassLoader;");
180 CHECK(dalvik_system_BaseDexClassLoader != NULL);
181 CHECK_EQ(dalvik_system_BaseDexClassLoader->object_size_, sizeof(BaseDexClassLoader));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700182 SetClassRoot(kDalvikSystemBaseDexClassLoader, dalvik_system_BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700183 Class* dalvik_system_PathClassLoader = FindSystemClass("Ldalvik/system/PathClassLoader;");
184 CHECK(dalvik_system_PathClassLoader != NULL);
185 CHECK_EQ(dalvik_system_PathClassLoader->object_size_, sizeof(PathClassLoader));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700186 SetClassRoot(kDalvikSystemPathClassLoader, dalvik_system_PathClassLoader);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700187 PathClassLoader::SetClass(dalvik_system_PathClassLoader);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700188
189 // Setup a single, global copy of "interfaces" and "iftable" for
190 // reuse across array classes
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700191 Class* java_lang_Cloneable = FindSystemClass("Ljava/lang/Cloneable;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700192 CHECK(java_lang_Cloneable != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700193 Class* java_io_Serializable = FindSystemClass("Ljava/io/Serializable;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700194 CHECK(java_io_Serializable != NULL);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700195
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700196 array_interfaces_ = AllocObjectArray<Class>(2);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700197 CHECK(array_interfaces_ != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700198 array_interfaces_->Set(0, java_lang_Cloneable);
199 array_interfaces_->Set(1, java_io_Serializable);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700200
201 // We assume that Cloneable/Serializable don't have superinterfaces --
202 // normally we'd have to crawl up and explicitly list all of the
203 // supers as well. These interfaces don't have any methods, so we
204 // don't have to worry about the ifviPool either.
205 array_iftable_ = new InterfaceEntry[2];
Brian Carlstrom30b94452011-08-25 21:35:26 -0700206 array_iftable_[0].SetInterface(array_interfaces_->Get(0));
207 array_iftable_[1].SetInterface(array_interfaces_->Get(1));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700208 // now FindClass can be used for non-primitive array classes
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700209
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700210 // run Object[] through FindClass to complete initialization
Jesse Wilson8989d992011-08-02 13:39:42 -0700211 Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;");
212 CHECK_EQ(object_array_class, found_object_array_class);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700213 CHECK_EQ(java_lang_Cloneable, object_array_class->GetInterface(0));
214 CHECK_EQ(java_io_Serializable, object_array_class->GetInterface(1));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700215
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700216 // Setup the primitive type classes.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700217 SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass("Z"));
218 SetClassRoot(kPrimitiveByte, CreatePrimitiveClass("B"));
219 SetClassRoot(kPrimitiveChar, CreatePrimitiveClass("C"));
220 SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass("D"));
221 SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass("F"));
222 SetClassRoot(kPrimitiveInt, CreatePrimitiveClass("I"));
223 SetClassRoot(kPrimitiveLong, CreatePrimitiveClass("J"));
224 SetClassRoot(kPrimitiveShort, CreatePrimitiveClass("S"));
225 SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass("V"));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700226 // now we can use FindSystemClass for anything, including for "[C"
227
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700228 // run char[] and int[] through FindClass to complete initialization
Jesse Wilson8989d992011-08-02 13:39:42 -0700229 Class* found_char_array_class = FindSystemClass("[C");
230 CHECK_EQ(char_array_class, found_char_array_class);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700231 Class* found_int_array_class = FindSystemClass("[I");
232 CHECK_EQ(int_array_class, found_int_array_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700233
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700234 // Initialize all the other primitive array types for PrimitiveArray::Alloc.
235 // These are easy because everything we need has already been set up.
Brian Carlstroma663ea52011-08-19 23:33:41 -0700236 SetClassRoot(kBooleanArrayClass, FindSystemClass("[Z"));
237 SetClassRoot(kByteArrayClass, FindSystemClass("[B"));
238 SetClassRoot(kDoubleArrayClass, FindSystemClass("[D"));
239 SetClassRoot(kFloatArrayClass, FindSystemClass("[F"));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700240 SetClassRoot(kLongArrayClass, FindSystemClass("[J"));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700241 SetClassRoot(kShortArrayClass, FindSystemClass("[S"));
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700242 SetClassRoot(kJavaLangStackTraceElement, FindSystemClass("Ljava/lang/StackTraceElement;"));
243 SetClassRoot(kJavaLangStackTraceElementArrayClass, FindSystemClass("[Ljava/lang/StackTraceElement;"));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700244 BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass));
245 ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass));
246 DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass));
247 FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass));
Brian Carlstrom4873d462011-08-21 15:23:39 -0700248 LongArray::SetArrayClass(GetClassRoot(kLongArrayClass));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700249 ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass));
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700250 StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700251
Brian Carlstroma663ea52011-08-19 23:33:41 -0700252 FinishInit();
253}
254
255void ClassLinker::FinishInit() {
256 // ensure all class_roots_ are initialized
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700257 for (size_t i = 0; i < kClassRootsMax; i++) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700258 ClassRoot class_root = static_cast<ClassRoot>(i);
259 Class* klass = GetClassRoot(class_root);
260 CHECK(klass != NULL);
261 DCHECK(klass->IsArray() || klass->IsPrimitive() || klass->dex_cache_ != NULL);
262 // note SetClassRoot does additional validation.
263 // if possible add new checks there to catch errors early
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700264 }
265
266 // disable the slow paths in FindClass and CreatePrimitiveClass now
267 // that Object, Class, and Object[] are setup
268 init_done_ = true;
269}
270
Brian Carlstroma663ea52011-08-19 23:33:41 -0700271struct ClassLinker::InitCallbackState {
272 ClassLinker* class_linker;
273
274 Class* class_roots[kClassRootsMax];
275
276 typedef std::tr1::unordered_map<std::string, ClassRoot> Table;
277 Table descriptor_to_class_root;
278
279 struct DexCacheHash {
280 size_t operator()(art::DexCache* const& obj) const {
281 return reinterpret_cast<size_t>(&obj);
282 }
283 };
284 typedef std::tr1::unordered_set<DexCache*, DexCacheHash> Set;
285 Set dex_caches;
286};
287
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700288void ClassLinker::Init(const std::vector<const DexFile*>& boot_class_path, Space* space) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700289 CHECK(!init_done_);
290
291 HeapBitmap* heap_bitmap = Heap::GetLiveBits();
292 DCHECK(heap_bitmap != NULL);
293
294 InitCallbackState state;
295 state.class_linker = this;
296 for (size_t i = 0; i < kClassRootsMax; i++) {
297 ClassRoot class_root = static_cast<ClassRoot>(i);
298 state.descriptor_to_class_root[GetClassRootDescriptor(class_root)] = class_root;
299 }
300
301 // reinit clases_ table
302 heap_bitmap->Walk(InitCallback, &state);
303
304 // reinit class_roots_
305 Class* object_array_class = state.class_roots[kObjectArrayClass];
306 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
307 for (size_t i = 0; i < kClassRootsMax; i++) {
308 ClassRoot class_root = static_cast<ClassRoot>(i);
309 SetClassRoot(class_root, state.class_roots[class_root]);
310 }
311
312 // reinit intern_table_
313 ObjectArray<Object>* interned_array = space->GetImageHeader().GetInternedArray();
314 for (int32_t i = 0; i < interned_array->GetLength(); i++) {
315 String* string = interned_array->Get(i)->AsString();
316 intern_table_.Register(string);
317 }
318
319 // reinit array_interfaces_ from any array class instance, they should all be ==
320 array_interfaces_ = GetClassRoot(kObjectArrayClass)->interfaces_;
321 DCHECK(array_interfaces_ == GetClassRoot(kBooleanArrayClass)->interfaces_);
322
323 // build a map from location to DexCache to match up with DexFile::GetLocation
324 std::tr1::unordered_map<std::string, DexCache*> location_to_dex_cache;
325 typedef InitCallbackState::Set::const_iterator It; // TODO: C++0x auto
326 for (It it = state.dex_caches.begin(), end = state.dex_caches.end(); it != end; ++it) {
327 DexCache* dex_cache = *it;
328 std::string location = dex_cache->GetLocation()->ToModifiedUtf8();
329 location_to_dex_cache[location] = dex_cache;
330 }
331
332 // reinit boot_class_path with DexFile arguments and found DexCaches
333 for (size_t i = 0; i != boot_class_path.size(); ++i) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700334 const DexFile* dex_file = boot_class_path[i];
335 CHECK(dex_file != NULL);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700336 DexCache* dex_cache = location_to_dex_cache[dex_file->GetLocation()];
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700337 AppendToBootClassPath(*dex_file, dex_cache);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700338 }
339
340 String::SetClass(GetClassRoot(kJavaLangString));
341 BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass));
342 ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass));
343 CharArray::SetArrayClass(GetClassRoot(kCharArrayClass));
344 DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass));
345 FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass));
346 IntArray::SetArrayClass(GetClassRoot(kIntArrayClass));
347 LongArray::SetArrayClass(GetClassRoot(kLongArrayClass));
348 ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700349 PathClassLoader::SetClass(GetClassRoot(kDalvikSystemPathClassLoader));
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700350 StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700351
352 FinishInit();
353}
354
Brian Carlstrom4873d462011-08-21 15:23:39 -0700355void ClassLinker::InitCallback(Object* obj, void *arg) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700356 DCHECK(obj != NULL);
357 DCHECK(arg != NULL);
358 InitCallbackState* state = reinterpret_cast<InitCallbackState*>(arg);
359
360 if (!obj->IsClass()) {
361 return;
362 }
363 Class* klass = obj->AsClass();
364 CHECK(klass->class_loader_ == NULL);
365
366 std::string descriptor = klass->GetDescriptor()->ToModifiedUtf8();
367
368 // restore class to ClassLinker::classes_ table
369 state->class_linker->InsertClass(descriptor, klass);
370
371 // note DexCache to match with DexFile later
372 DexCache* dex_cache = klass->GetDexCache();
373 if (dex_cache != NULL) {
374 state->dex_caches.insert(dex_cache);
375 } else {
376 DCHECK(klass->IsArray() || klass->IsPrimitive());
377 }
378
379 // check if this is a root, if so, register it
380 typedef InitCallbackState::Table::const_iterator It; // TODO: C++0x auto
381 It it = state->descriptor_to_class_root.find(descriptor);
382 if (it != state->descriptor_to_class_root.end()) {
383 ClassRoot class_root = it->second;
384 state->class_roots[class_root] = klass;
385 }
386}
387
388// Keep in sync with InitCallback. Anything we visit, we need to
389// reinit references to when reinitializing a ClassLinker from a
390// mapped image.
391void ClassLinker::VisitRoots(Heap::RootVistor* root_visitor, void* arg) const {
392
Brian Carlstromb88e9442011-07-28 15:15:51 -0700393 root_visitor(class_roots_, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700394
395 for (size_t i = 0; i < dex_caches_.size(); i++) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700396 root_visitor(dex_caches_[i], arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700397 }
398
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700399 {
400 MutexLock mu(classes_lock_);
401 typedef Table::const_iterator It; // TODO: C++0x auto
402 for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
403 root_visitor(it->second, arg);
404 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700405 }
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700406
407 intern_table_.VisitRoots(root_visitor, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700408
Brian Carlstromb88e9442011-07-28 15:15:51 -0700409 root_visitor(array_interfaces_, arg);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700410}
411
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700412ClassLinker::~ClassLinker() {
413 delete classes_lock_;
414 String::ResetClass();
415 BooleanArray::ResetArrayClass();
416 ByteArray::ResetArrayClass();
417 CharArray::ResetArrayClass();
418 DoubleArray::ResetArrayClass();
419 FloatArray::ResetArrayClass();
420 IntArray::ResetArrayClass();
421 LongArray::ResetArrayClass();
422 ShortArray::ResetArrayClass();
423 PathClassLoader::ResetClass();
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700424 StackTraceElement::ResetClass();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700425}
426
427DexCache* ClassLinker::AllocDexCache(const DexFile& dex_file) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700428 DexCache* dex_cache = down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::kMax));
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700429 dex_cache->Init(String::AllocFromModifiedUtf8(dex_file.GetLocation().c_str()),
430 AllocObjectArray<String>(dex_file.NumStringIds()),
431 AllocObjectArray<Class>(dex_file.NumTypeIds()),
432 AllocObjectArray<Method>(dex_file.NumMethodIds()),
433 AllocObjectArray<Field>(dex_file.NumFieldIds()));
Brian Carlstroma663ea52011-08-19 23:33:41 -0700434 return dex_cache;
Brian Carlstroma0808032011-07-18 00:39:23 -0700435}
436
Brian Carlstrom4873d462011-08-21 15:23:39 -0700437Class* ClassLinker::AllocClass(Class* java_lang_Class, size_t class_size) {
438 DCHECK_GE(class_size, sizeof(Class));
439 Class* klass = Heap::AllocObject(java_lang_Class, class_size)->AsClass();
440 klass->class_size_ = class_size;
441 return klass;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700442}
443
Brian Carlstrom4873d462011-08-21 15:23:39 -0700444Class* ClassLinker::AllocClass(size_t class_size) {
445 return AllocClass(GetClassRoot(kJavaLangClass), class_size);
Brian Carlstroma0808032011-07-18 00:39:23 -0700446}
447
Jesse Wilson35baaab2011-08-10 16:18:03 -0400448Field* ClassLinker::AllocField() {
449 return down_cast<Field*>(GetClassRoot(kJavaLangReflectField)->NewInstance());
Brian Carlstroma0808032011-07-18 00:39:23 -0700450}
451
452Method* ClassLinker::AllocMethod() {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700453 return down_cast<Method*>(GetClassRoot(kJavaLangReflectMethod)->NewInstance());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700454}
455
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700456ObjectArray<StackTraceElement>* ClassLinker::AllocStackTraceElementArray(size_t length) {
457 return ObjectArray<StackTraceElement>::Alloc(
458 GetClassRoot(kJavaLangStackTraceElementArrayClass),
459 length);
460}
461
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700462Class* ClassLinker::FindClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700463 const ClassLoader* class_loader) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700464 // TODO: remove this contrived parent class loader check when we have a real ClassLoader.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700465 if (class_loader != NULL) {
466 Class* klass = FindClass(descriptor, NULL);
467 if (klass != NULL) {
468 return klass;
469 }
Elliott Hughesbd935992011-08-22 11:59:34 -0700470 Thread::Current()->ClearException();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700471 }
472
Carl Shapirob5573532011-07-12 18:22:59 -0700473 Thread* self = Thread::Current();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700474 DCHECK(self != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700475 CHECK(!self->IsExceptionPending());
476 // Find the class in the loaded classes table.
477 Class* klass = LookupClass(descriptor, class_loader);
478 if (klass == NULL) {
479 // Class is not yet loaded.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700480 if (descriptor[0] == '[') {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700481 return CreateArrayClass(descriptor, class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700482 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700483 const DexFile::ClassPath& class_path = ((class_loader != NULL) ? class_loader->GetClassPath() : boot_class_path_);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700484 DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, class_path);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700485 if (pair.second == NULL) {
Elliott Hughesbd935992011-08-22 11:59:34 -0700486 std::string name(PrintableString(descriptor));
487 self->ThrowNewException("Ljava/lang/NoClassDefFoundError;",
488 "Class %s not found in class loader %p", name.c_str(), class_loader);
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700489 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700490 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700491 const DexFile& dex_file = *pair.first;
492 const DexFile::ClassDef& dex_class_def = *pair.second;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700493 DexCache* dex_cache = FindDexCache(dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700494 // Load the class from the dex file.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700495 if (!init_done_) {
496 // finish up init of hand crafted class_roots_
497 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700498 klass = GetClassRoot(kJavaLangObject);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700499 } else if (descriptor == "Ljava/lang/Class;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700500 klass = GetClassRoot(kJavaLangClass);
Jesse Wilson14150742011-07-29 19:04:44 -0400501 } else if (descriptor == "Ljava/lang/String;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700502 klass = GetClassRoot(kJavaLangString);
503 } else if (descriptor == "Ljava/lang/reflect/Field;") {
504 klass = GetClassRoot(kJavaLangReflectField);
505 } else if (descriptor == "Ljava/lang/reflect/Method;") {
506 klass = GetClassRoot(kJavaLangReflectMethod);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700507 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700508 klass = AllocClass(SizeOfClass(dex_file, dex_class_def));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700509 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700510 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700511 klass = AllocClass(SizeOfClass(dex_file, dex_class_def));
Carl Shapiro565f5072011-07-10 13:39:43 -0700512 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700513 klass->dex_cache_ = dex_cache;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700514 LoadClass(dex_file, dex_class_def, klass, class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700515 // Check for a pending exception during load
516 if (self->IsExceptionPending()) {
517 // TODO: free native allocations in klass
518 return NULL;
519 }
520 {
521 ObjectLock lock(klass);
Carl Shapirob5573532011-07-12 18:22:59 -0700522 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700523 // Add the newly loaded class to the loaded classes table.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700524 bool success = InsertClass(descriptor, klass); // TODO: just return collision
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700525 if (!success) {
526 // We may fail to insert if we raced with another thread.
527 klass->clinit_thread_id_ = 0;
528 // TODO: free native allocations in klass
529 klass = LookupClass(descriptor, class_loader);
530 CHECK(klass != NULL);
531 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700532 // Finish loading (if necessary) by finding parents
533 if (!klass->IsLoaded() && !LoadSuperAndInterfaces(klass, dex_file)) {
534 // Loading failed.
535 // TODO: CHECK(self->IsExceptionPending());
536 lock.NotifyAll();
537 return NULL;
538 }
539 CHECK(klass->IsLoaded());
540 // Link the class (if necessary)
541 if (!klass->IsLinked() && !LinkClass(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700542 // Linking failed.
543 // TODO: CHECK(self->IsExceptionPending());
544 lock.NotifyAll();
545 return NULL;
546 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700547 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700548 }
549 }
550 }
551 // Link the class if it has not already been linked.
552 if (!klass->IsLinked() && !klass->IsErroneous()) {
553 ObjectLock lock(klass);
554 // Check for circular dependencies between classes.
Carl Shapirob5573532011-07-12 18:22:59 -0700555 if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) {
Elliott Hughesbd935992011-08-22 11:59:34 -0700556 self->ThrowNewException("Ljava/lang/ClassCircularityError;", NULL); // TODO: detail
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700557 return NULL;
558 }
559 // Wait for the pending initialization to complete.
560 while (!klass->IsLinked() && !klass->IsErroneous()) {
561 lock.Wait();
562 }
563 }
564 if (klass->IsErroneous()) {
565 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
566 return NULL;
567 }
568 // Return the loaded class. No exceptions should be pending.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700569 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700570 CHECK(!self->IsExceptionPending());
571 return klass;
572}
573
Brian Carlstrom4873d462011-08-21 15:23:39 -0700574// Precomputes size that will be needed for Class, matching LinkStaticFields
575size_t ClassLinker::SizeOfClass(const DexFile& dex_file,
576 const DexFile::ClassDef& dex_class_def) {
577 const byte* class_data = dex_file.GetClassData(dex_class_def);
578 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
579 size_t num_static_fields = header.static_fields_size_;
580 size_t num_ref = 0;
581 size_t num_32 = 0;
582 size_t num_64 = 0;
583 if (num_static_fields != 0) {
584 uint32_t last_idx = 0;
585 for (size_t i = 0; i < num_static_fields; ++i) {
586 DexFile::Field dex_field;
587 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
588 const DexFile::FieldId& field_id = dex_file.GetFieldId(dex_field.field_idx_);
589 const char* descriptor = dex_file.dexStringByTypeIdx(field_id.type_idx_);
590 char c = descriptor[0];
591 if (c == 'L' || c == '[') {
592 num_ref++;
593 } else if (c == 'J' || c == 'D') {
594 num_64++;
595 } else {
596 num_32++;
597 }
598 }
599 }
600
601 // start with generic class data
602 size_t size = sizeof(Class);
603 // follow with reference fields which must be contiguous at start
604 size += (num_ref * sizeof(uint32_t));
605 // if there are 64-bit fields to add, make sure they are aligned
606 if (num_64 != 0 && size != RoundUp(size, 8)) { // for 64-bit alignment
607 if (num_32 != 0) {
608 // use an available 32-bit field for padding
609 num_32--;
610 }
611 size += sizeof(uint32_t); // either way, we are adding a word
612 DCHECK_EQ(size, RoundUp(size, 8));
613 }
614 // tack on any 64-bit fields now that alignment is assured
615 size += (num_64 * sizeof(uint64_t));
616 // tack on any remaining 32-bit fields
617 size += (num_32 * sizeof(uint32_t));
618 return size;
619}
620
Brian Carlstromf615a612011-07-23 12:50:34 -0700621void ClassLinker::LoadClass(const DexFile& dex_file,
622 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700623 Class* klass,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700624 const ClassLoader* class_loader) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700625 CHECK(klass != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700626 CHECK(klass->dex_cache_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700627 CHECK_EQ(Class::kStatusNotReady, klass->status_);
Brian Carlstromf615a612011-07-23 12:50:34 -0700628 const byte* class_data = dex_file.GetClassData(dex_class_def);
629 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700630
Brian Carlstromf615a612011-07-23 12:50:34 -0700631 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700632 CHECK(descriptor != NULL);
633
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700634 klass->klass_ = GetClassRoot(kJavaLangClass);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700635 klass->descriptor_ = String::AllocFromModifiedUtf8(descriptor);
Brian Carlstromf615a612011-07-23 12:50:34 -0700636 klass->access_flags_ = dex_class_def.access_flags_;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700637 klass->class_loader_ = class_loader;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700638 klass->primitive_type_ = Class::kPrimNot;
639 klass->status_ = Class::kStatusIdx;
640
641 klass->super_class_ = NULL;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700642 klass->super_class_type_idx_ = dex_class_def.superclass_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700643
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700644 size_t num_static_fields = header.static_fields_size_;
645 size_t num_instance_fields = header.instance_fields_size_;
646 size_t num_direct_methods = header.direct_methods_size_;
647 size_t num_virtual_methods = header.virtual_methods_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700648
Brian Carlstromf615a612011-07-23 12:50:34 -0700649 klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700650
651 // Load class interfaces.
Brian Carlstromf615a612011-07-23 12:50:34 -0700652 LoadInterfaces(dex_file, dex_class_def, klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700653
654 // Load static fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700655 DCHECK(klass->sfields_ == NULL);
656 if (num_static_fields != 0) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400657 klass->sfields_ = AllocObjectArray<Field>(num_static_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700658 uint32_t last_idx = 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700659 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700660 DexFile::Field dex_field;
661 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400662 Field* sfield = AllocField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700663 klass->SetStaticField(i, sfield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700664 LoadField(dex_file, dex_field, klass, sfield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700665 }
666 }
667
668 // Load instance fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700669 DCHECK(klass->ifields_ == NULL);
670 if (num_instance_fields != 0) {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400671 klass->ifields_ = AllocObjectArray<Field>(num_instance_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700672 uint32_t last_idx = 0;
673 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700674 DexFile::Field dex_field;
675 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400676 Field* ifield = AllocField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700677 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700678 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700679 }
680 }
681
682 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700683 DCHECK(klass->direct_methods_ == NULL);
684 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700685 // TODO: append direct methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700686 klass->direct_methods_ = AllocObjectArray<Method>(num_direct_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700687 uint32_t last_idx = 0;
688 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700689 DexFile::Method dex_method;
690 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700691 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700692 klass->SetDirectMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700693 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700694 // TODO: register maps
695 }
696 }
697
698 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700699 DCHECK(klass->virtual_methods_ == NULL);
700 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700701 // TODO: append virtual methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700702 klass->virtual_methods_ = AllocObjectArray<Method>(num_virtual_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700703 uint32_t last_idx = 0;
704 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700705 DexFile::Method dex_method;
706 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700707 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700708 klass->SetVirtualMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700709 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700710 // TODO: register maps
711 }
712 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700713}
714
Brian Carlstromf615a612011-07-23 12:50:34 -0700715void ClassLinker::LoadInterfaces(const DexFile& dex_file,
716 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700717 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700718 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700719 if (list != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700720 DCHECK(klass->interfaces_ == NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700721 klass->interfaces_ = AllocObjectArray<Class>(list->Size());
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700722 DCHECK(klass->interfaces_type_idx_ == NULL);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700723 klass->interfaces_type_idx_ = IntArray::Alloc(list->Size());
Brian Carlstrom934486c2011-07-12 23:42:50 -0700724 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700725 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700726 klass->interfaces_type_idx_->Set(i, type_item.type_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700727 }
728 }
729}
730
Brian Carlstromf615a612011-07-23 12:50:34 -0700731void ClassLinker::LoadField(const DexFile& dex_file,
732 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700733 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700734 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700735 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400736 dst->declaring_class_ = klass;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700737 dst->name_ = ResolveString(dex_file, field_id.name_idx_, klass->GetDexCache());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700738 dst->descriptor_.set(dex_file.dexStringByTypeIdx(field_id.type_idx_));
Jesse Wilson35baaab2011-08-10 16:18:03 -0400739 // TODO: Assign dst->type_.
Brian Carlstrom934486c2011-07-12 23:42:50 -0700740 dst->access_flags_ = src.access_flags_;
741}
742
Brian Carlstromf615a612011-07-23 12:50:34 -0700743void ClassLinker::LoadMethod(const DexFile& dex_file,
744 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700745 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700746 Method* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700747 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400748 dst->declaring_class_ = klass;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700749 dst->name_ = ResolveString(dex_file, method_id.name_idx_, klass->GetDexCache());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700750 {
751 int32_t utf16_length;
Elliott Hughes0c424cb2011-08-26 10:16:25 -0700752 std::string utf8(dex_file.CreateMethodDescriptor(method_id.proto_idx_, &utf16_length));
753 dst->signature_ = String::AllocFromModifiedUtf8(utf16_length, utf8.c_str());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700754 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700755 dst->proto_idx_ = method_id.proto_idx_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700756 dst->code_off_ = src.code_off_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700757 dst->shorty_ = dex_file.GetShorty(method_id.proto_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700758 dst->access_flags_ = src.access_flags_;
759
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700760 dst->dex_cache_strings_ = klass->dex_cache_->GetStrings();
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700761 dst->dex_cache_types_ = klass->dex_cache_->GetTypes();
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700762 dst->dex_cache_methods_ = klass->dex_cache_->GetMethods();
763 dst->dex_cache_fields_ = klass->dex_cache_->GetFields();
764
Brian Carlstrom934486c2011-07-12 23:42:50 -0700765 // TODO: check for finalize method
766
Brian Carlstromf615a612011-07-23 12:50:34 -0700767 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700768 if (code_item != NULL) {
769 dst->num_registers_ = code_item->registers_size_;
770 dst->num_ins_ = code_item->ins_size_;
771 dst->num_outs_ = code_item->outs_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700772 } else {
773 uint16_t num_args = dst->NumArgRegisters();
774 if (!dst->IsStatic()) {
775 ++num_args;
776 }
777 dst->num_registers_ = dst->num_ins_ + num_args;
778 // TODO: native methods
779 }
780}
781
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700782void ClassLinker::AppendToBootClassPath(const DexFile& dex_file) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700783 AppendToBootClassPath(dex_file, AllocDexCache(dex_file));
784}
785
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700786void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, DexCache* dex_cache) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700787 CHECK(dex_cache != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700788 boot_class_path_.push_back(&dex_file);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700789 RegisterDexFile(dex_file, dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700790}
791
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700792void ClassLinker::RegisterDexFile(const DexFile& dex_file) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700793 RegisterDexFile(dex_file, AllocDexCache(dex_file));
794}
795
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700796void ClassLinker::RegisterDexFile(const DexFile& dex_file, DexCache* dex_cache) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700797 CHECK(dex_cache != NULL);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700798 dex_files_.push_back(&dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700799 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700800}
801
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700802const DexFile& ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700803 for (size_t i = 0; i != dex_caches_.size(); ++i) {
804 if (dex_caches_[i] == dex_cache) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700805 return *dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700806 }
807 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700808 CHECK(false) << "Could not find DexFile";
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700809 return *dex_files_[-1];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700810}
811
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700812DexCache* ClassLinker::FindDexCache(const DexFile& dex_file) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700813 for (size_t i = 0; i != dex_files_.size(); ++i) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700814 if (dex_files_[i] == &dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700815 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700816 }
817 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700818 CHECK(false) << "Could not find DexCache";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700819 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700820}
821
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700822Class* ClassLinker::CreatePrimitiveClass(const char* descriptor) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700823 Class* klass = AllocClass(sizeof(Class));
Carl Shapiro565f5072011-07-10 13:39:43 -0700824 CHECK(klass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700825 klass->super_class_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700826 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700827 klass->descriptor_ = String::AllocFromModifiedUtf8(descriptor);
Carl Shapiro565f5072011-07-10 13:39:43 -0700828 klass->status_ = Class::kStatusInitialized;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700829 bool success = InsertClass(descriptor, klass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700830 CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed";
Carl Shapiro565f5072011-07-10 13:39:43 -0700831 return klass;
832}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700833
Brian Carlstrombe977852011-07-19 14:54:54 -0700834// Create an array class (i.e. the class object for the array, not the
835// array itself). "descriptor" looks like "[C" or "[[[[B" or
836// "[Ljava/lang/String;".
837//
838// If "descriptor" refers to an array of primitives, look up the
839// primitive type's internally-generated class object.
840//
841// "loader" is the class loader of the class that's referring to us. It's
842// used to ensure that we're looking for the element type in the right
843// context. It does NOT become the class loader for the array class; that
844// always comes from the base element class.
845//
846// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700847Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700848 const ClassLoader* class_loader)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700849{
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700850 CHECK(descriptor[0] == '[');
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700851
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700852 // Identify the underlying element class and the array dimension depth.
853 Class* component_type_ = NULL;
854 int array_rank;
855 if (descriptor[1] == '[') {
856 // array of arrays; keep descriptor and grab stuff from parent
857 Class* outer = FindClass(descriptor.substr(1), class_loader);
858 if (outer != NULL) {
859 // want the base class, not "outer", in our component_type_
860 component_type_ = outer->component_type_;
861 array_rank = outer->array_rank_ + 1;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700862 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700863 DCHECK(component_type_ == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700864 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700865 } else {
866 array_rank = 1;
867 if (descriptor[1] == 'L') {
868 // array of objects; strip off "[" and look up descriptor.
869 const StringPiece subDescriptor = descriptor.substr(1);
870 component_type_ = FindClass(subDescriptor, class_loader);
871 } else {
872 // array of a primitive type
873 component_type_ = FindPrimitiveClass(descriptor[1]);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700874 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700875 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700876
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700877 if (component_type_ == NULL) {
878 // failed
879 // DCHECK(Thread::Current()->IsExceptionPending()); // TODO
880 return NULL;
881 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700882
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700883 // See if the component type is already loaded. Array classes are
884 // always associated with the class loader of their underlying
885 // element type -- an array of Strings goes with the loader for
886 // java/lang/String -- so we need to look for it there. (The
887 // caller should have checked for the existence of the class
888 // before calling here, but they did so with *their* class loader,
889 // not the component type's loader.)
890 //
891 // If we find it, the caller adds "loader" to the class' initiating
892 // loader list, which should prevent us from going through this again.
893 //
894 // This call is unnecessary if "loader" and "component_type_->class_loader_"
895 // are the same, because our caller (FindClass) just did the
896 // lookup. (Even if we get this wrong we still have correct behavior,
897 // because we effectively do this lookup again when we add the new
898 // class to the hash table --- necessary because of possible races with
899 // other threads.)
900 if (class_loader != component_type_->class_loader_) {
901 Class* new_class = LookupClass(descriptor, component_type_->class_loader_);
902 if (new_class != NULL) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700903 return new_class;
904 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700905 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700906
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700907 // Fill out the fields in the Class.
908 //
909 // It is possible to execute some methods against arrays, because
910 // all arrays are subclasses of java_lang_Object_, so we need to set
911 // up a vtable. We can just point at the one in java_lang_Object_.
912 //
913 // Array classes are simple enough that we don't need to do a full
914 // link step.
915
916 Class* new_class = NULL;
917 if (!init_done_) {
918 if (descriptor == "[Ljava/lang/Object;") {
919 new_class = GetClassRoot(kObjectArrayClass);
920 } else if (descriptor == "[C") {
921 new_class = GetClassRoot(kCharArrayClass);
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700922 } else if (descriptor == "[I") {
923 new_class = GetClassRoot(kIntArrayClass);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700924 }
925 }
926 if (new_class == NULL) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700927 new_class = AllocClass(sizeof(Class));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700928 if (new_class == NULL) {
929 return NULL;
930 }
931 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700932 new_class->descriptor_ = String::AllocFromModifiedUtf8(descriptor.ToString().c_str());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700933 Class* java_lang_Object = GetClassRoot(kJavaLangObject);
934 new_class->super_class_ = java_lang_Object;
935 new_class->vtable_ = java_lang_Object->vtable_;
936 new_class->primitive_type_ = Class::kPrimNot;
937 new_class->component_type_ = component_type_;
938 new_class->class_loader_ = component_type_->class_loader_;
939 new_class->array_rank_ = array_rank;
940 new_class->status_ = Class::kStatusInitialized;
941 // don't need to set new_class->object_size_
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700942 // because Object::SizeOf delegates to Array::SizeOf
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700943
944
945 // All arrays have java/lang/Cloneable and java/io/Serializable as
946 // interfaces. We need to set that up here, so that stuff like
947 // "instanceof" works right.
948 //
949 // Note: The GC could run during the call to FindSystemClass,
950 // so we need to make sure the class object is GC-valid while we're in
951 // there. Do this by clearing the interface list so the GC will just
952 // think that the entries are null.
953
954
955 // Use the single, global copies of "interfaces" and "iftable"
956 // (remember not to free them for arrays).
957 DCHECK(array_interfaces_ != NULL);
958 new_class->interfaces_ = array_interfaces_;
959 new_class->iftable_count_ = 2;
960 DCHECK(array_iftable_ != NULL);
961 new_class->iftable_ = array_iftable_;
962
963 // Inherit access flags from the component type. Arrays can't be
964 // used as a superclass or interface, so we want to add "final"
965 // and remove "interface".
966 //
967 // Don't inherit any non-standard flags (e.g., kAccFinal)
968 // from component_type_. We assume that the array class does not
969 // override finalize().
970 new_class->access_flags_ = ((new_class->component_type_->access_flags_ &
971 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask;
972
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700973 if (InsertClass(descriptor, new_class)) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700974 return new_class;
975 }
976 // Another thread must have loaded the class after we
977 // started but before we finished. Abandon what we've
978 // done.
979 //
980 // (Yes, this happens.)
981
982 // Grab the winning class.
983 Class* other_class = LookupClass(descriptor, component_type_->class_loader_);
984 DCHECK(other_class != NULL);
985 return other_class;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700986}
987
988Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700989 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700990 case 'B':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700991 return GetClassRoot(kPrimitiveByte);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700992 case 'C':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700993 return GetClassRoot(kPrimitiveChar);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700994 case 'D':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700995 return GetClassRoot(kPrimitiveDouble);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700996 case 'F':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700997 return GetClassRoot(kPrimitiveFloat);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700998 case 'I':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700999 return GetClassRoot(kPrimitiveInt);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001000 case 'J':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001001 return GetClassRoot(kPrimitiveLong);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001002 case 'S':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001003 return GetClassRoot(kPrimitiveShort);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001004 case 'Z':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001005 return GetClassRoot(kPrimitiveBoolean);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001006 case 'V':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001007 return GetClassRoot(kPrimitiveVoid);
Carl Shapiro744ad052011-08-06 15:53:36 -07001008 }
Elliott Hughesbd935992011-08-22 11:59:34 -07001009 std::string printable_type(PrintableChar(type));
1010 Thread::Current()->ThrowNewException("Ljava/lang/NoClassDefFoundError;",
1011 "Not a primitive type: %s", printable_type.c_str());
1012 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001013}
1014
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001015bool ClassLinker::InsertClass(const StringPiece& descriptor, Class* klass) {
1016 size_t hash = StringPieceHash()(descriptor);
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001017 MutexLock mu(classes_lock_);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001018 Table::iterator it = classes_.insert(std::make_pair(hash, klass));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001019 return ((*it).second == klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001020}
1021
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001022Class* ClassLinker::LookupClass(const StringPiece& descriptor, const ClassLoader* class_loader) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001023 size_t hash = StringPieceHash()(descriptor);
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001024 MutexLock mu(classes_lock_);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001025 typedef Table::const_iterator It; // TODO: C++0x auto
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001026 for (It it = classes_.find(hash), end = classes_.end(); it != end; ++it) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001027 Class* klass = it->second;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001028 if (klass->descriptor_->Equals(descriptor) && klass->class_loader_ == class_loader) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001029 return klass;
1030 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001031 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001032 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001033}
1034
1035bool ClassLinker::InitializeClass(Class* klass) {
1036 CHECK(klass->GetStatus() == Class::kStatusResolved ||
1037 klass->GetStatus() == Class::kStatusError);
1038
Carl Shapirob5573532011-07-12 18:22:59 -07001039 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001040
1041 {
1042 ObjectLock lock(klass);
1043
1044 if (klass->GetStatus() < Class::kStatusVerified) {
1045 if (klass->IsErroneous()) {
1046 LG << "re-initializing failed class"; // TODO: throw
1047 return false;
1048 }
1049
1050 CHECK(klass->GetStatus() == Class::kStatusResolved);
1051
1052 klass->status_ = Class::kStatusVerifying;
1053 if (!DexVerify::VerifyClass(klass)) {
1054 LG << "Verification failed"; // TODO: ThrowVerifyError
1055 Object* exception = self->GetException();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001056 klass->SetVerifyErrorClass(exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001057 klass->SetStatus(Class::kStatusError);
1058 return false;
1059 }
1060
1061 klass->SetStatus(Class::kStatusVerified);
1062 }
1063
1064 if (klass->status_ == Class::kStatusInitialized) {
1065 return true;
1066 }
1067
1068 while (klass->status_ == Class::kStatusInitializing) {
1069 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -07001070 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001071 LG << "recursive <clinit>";
1072 return true;
1073 }
1074
1075 CHECK(!self->IsExceptionPending());
1076
1077 lock.Wait(); // TODO: check for interruption
1078
1079 // When we wake up, repeat the test for init-in-progress. If
1080 // there's an exception pending (only possible if
1081 // "interruptShouldThrow" was set), bail out.
1082 if (self->IsExceptionPending()) {
1083 CHECK(false);
1084 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
1085 klass->SetStatus(Class::kStatusError);
1086 return false;
1087 }
1088 if (klass->GetStatus() == Class::kStatusInitializing) {
1089 continue;
1090 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001091 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001092 klass->GetStatus() == Class::kStatusError);
1093 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001094 // The caller wants an exception, but it was thrown in a
1095 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001096 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
1097 return false;
1098 }
1099 return true; // otherwise, initialized
1100 }
1101
1102 // see if we failed previously
1103 if (klass->IsErroneous()) {
1104 // might be wise to unlock before throwing; depends on which class
1105 // it is that we have locked
1106
1107 // TODO: throwEarlierClassFailure(klass);
1108 return false;
1109 }
1110
1111 if (!ValidateSuperClassDescriptors(klass)) {
1112 klass->SetStatus(Class::kStatusError);
1113 return false;
1114 }
1115
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001116 DCHECK(klass->status_ < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001117
Carl Shapirob5573532011-07-12 18:22:59 -07001118 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001119 klass->status_ = Class::kStatusInitializing;
1120 }
1121
1122 if (!InitializeSuperClass(klass)) {
1123 return false;
1124 }
1125
1126 InitializeStaticFields(klass);
1127
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001128 Method* clinit = klass->FindDeclaredDirectMethod("<clinit>", "()V");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001129 if (clinit != NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001130 // JValue unused;
1131 // TODO: dvmCallMethod(self, method, NULL, &unused);
Elliott Hughes53b61312011-08-12 18:28:20 -07001132 // UNIMPLEMENTED(FATAL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001133 }
1134
1135 {
1136 ObjectLock lock(klass);
1137
1138 if (self->IsExceptionPending()) {
1139 klass->SetStatus(Class::kStatusError);
1140 } else {
1141 klass->SetStatus(Class::kStatusInitialized);
1142 }
1143 lock.NotifyAll();
1144 }
1145
1146 return true;
1147}
1148
1149bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
1150 if (klass->IsInterface()) {
1151 return true;
1152 }
1153 // begin with the methods local to the superclass
1154 if (klass->HasSuperClass() &&
1155 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
1156 const Class* super = klass->GetSuperClass();
1157 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001158 const Method* method = super->GetVirtualMethod(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001159 if (method != super->GetVirtualMethod(i) &&
1160 !HasSameMethodDescriptorClasses(method, super, klass)) {
1161 LG << "Classes resolve differently in superclass";
1162 return false;
1163 }
1164 }
1165 }
1166 for (size_t i = 0; i < klass->iftable_count_; ++i) {
1167 const InterfaceEntry* iftable = &klass->iftable_[i];
Brian Carlstrom30b94452011-08-25 21:35:26 -07001168 Class* interface = iftable->GetInterface();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001169 if (klass->GetClassLoader() != interface->GetClassLoader()) {
1170 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1171 uint32_t vtable_index = iftable->method_index_array_[j];
1172 const Method* method = klass->GetVirtualMethod(vtable_index);
1173 if (!HasSameMethodDescriptorClasses(method, interface,
1174 method->GetClass())) {
1175 LG << "Classes resolve differently in interface"; // TODO: LinkageError
1176 return false;
1177 }
1178 }
1179 }
1180 }
1181 return true;
1182}
1183
1184bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -07001185 const Class* klass1,
1186 const Class* klass2) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001187 const DexFile& dex_file = FindDexFile(method->GetClass()->GetDexCache());
1188 const DexFile::ProtoId& proto_id = dex_file.GetProtoId(method->proto_idx_);
Brian Carlstromf615a612011-07-23 12:50:34 -07001189 DexFile::ParameterIterator *it;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001190 for (it = dex_file.GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001191 const char* descriptor = it->GetDescriptor();
1192 if (descriptor == NULL) {
1193 break;
1194 }
1195 if (descriptor[0] == 'L' || descriptor[0] == '[') {
1196 // Found a non-primitive type.
1197 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
1198 return false;
1199 }
1200 }
1201 }
1202 // Check the return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001203 const char* descriptor = dex_file.GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001204 if (descriptor[0] == 'L' || descriptor[0] == '[') {
1205 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
1206 return false;
1207 }
1208 }
1209 return true;
1210}
1211
1212// Returns true if classes referenced by the descriptor are the
1213// same classes in klass1 as they are in klass2.
1214bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -07001215 const Class* klass1,
1216 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001217 CHECK(descriptor != NULL);
1218 CHECK(klass1 != NULL);
1219 CHECK(klass2 != NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001220 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001221 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001222 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001223 // TODO: found2 == NULL
1224 // TODO: lookup found1 in initiating loader list
1225 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -07001226 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001227 if (found1 == found2) {
1228 return true;
1229 } else {
1230 return false;
1231 }
1232 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001233 return true;
1234}
1235
1236bool ClassLinker::InitializeSuperClass(Class* klass) {
1237 CHECK(klass != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001238 if (!klass->IsInterface() && klass->HasSuperClass()) {
1239 Class* super_class = klass->GetSuperClass();
1240 if (super_class->GetStatus() != Class::kStatusInitialized) {
1241 CHECK(!super_class->IsInterface());
1242 klass->MonitorExit();
1243 bool super_initialized = InitializeClass(super_class);
1244 klass->MonitorEnter();
1245 // TODO: check for a pending exception
1246 if (!super_initialized) {
1247 klass->SetStatus(Class::kStatusError);
1248 klass->NotifyAll();
1249 return false;
1250 }
1251 }
1252 }
1253 return true;
1254}
1255
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001256bool ClassLinker::EnsureInitialized(Class* c) {
1257 CHECK(c != NULL);
1258 if (c->IsInitialized()) {
1259 return true;
1260 }
1261
1262 c->MonitorExit();
1263 InitializeClass(c);
1264 c->MonitorEnter();
1265 return !Thread::Current()->IsExceptionPending();
1266}
1267
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001268void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001269 size_t num_static_fields = klass->NumStaticFields();
1270 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001271 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001272 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001273 DexCache* dex_cache = klass->GetDexCache();
Brian Carlstrom4873d462011-08-21 15:23:39 -07001274 // TODO: this seems like the wrong check. do we really want !IsPrimitive && !IsArray?
Brian Carlstromf615a612011-07-23 12:50:34 -07001275 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001276 return;
1277 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001278 const std::string descriptor(klass->GetDescriptor()->ToModifiedUtf8());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001279 const DexFile& dex_file = FindDexFile(dex_cache);
1280 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Brian Carlstromf615a612011-07-23 12:50:34 -07001281 CHECK(dex_class_def != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001282 const byte* addr = dex_file.GetEncodedArray(*dex_class_def);
Elliott Hughesf4c21c92011-08-19 17:31:31 -07001283 if (addr == NULL) {
1284 // All this class' static fields have default values.
1285 return;
1286 }
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001287 size_t array_size = DecodeUnsignedLeb128(&addr);
1288 for (size_t i = 0; i < array_size; ++i) {
Jesse Wilson35baaab2011-08-10 16:18:03 -04001289 Field* field = klass->GetStaticField(i);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001290 JValue value;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001291 DexFile::ValueType type = dex_file.ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001292 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001293 case DexFile::kByte:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001294 field->SetByte(NULL, value.b);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001295 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001296 case DexFile::kShort:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001297 field->SetShort(NULL, value.s);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001298 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001299 case DexFile::kChar:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001300 field->SetChar(NULL, value.c);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001301 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001302 case DexFile::kInt:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001303 field->SetInt(NULL, value.i);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001304 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001305 case DexFile::kLong:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001306 field->SetLong(NULL, value.j);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001307 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001308 case DexFile::kFloat:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001309 field->SetFloat(NULL, value.f);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001310 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001311 case DexFile::kDouble:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001312 field->SetDouble(NULL, value.d);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001313 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001314 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001315 uint32_t string_idx = value.i;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001316 String* resolved = ResolveString(dex_file, string_idx, klass->GetDexCache());
Brian Carlstrom4873d462011-08-21 15:23:39 -07001317 field->SetObject(NULL, resolved);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001318 break;
1319 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001320 case DexFile::kBoolean:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001321 field->SetBoolean(NULL, value.z);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001322 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001323 case DexFile::kNull:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001324 field->SetObject(NULL, value.l);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001325 break;
1326 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001327 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001328 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001329 }
1330}
1331
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001332bool ClassLinker::LinkClass(Class* klass, const DexFile& dex_file) {
1333 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001334 if (!LinkSuperClass(klass)) {
1335 return false;
1336 }
1337 if (!LinkMethods(klass)) {
1338 return false;
1339 }
1340 if (!LinkInstanceFields(klass)) {
1341 return false;
1342 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001343 if (!LinkStaticFields(klass)) {
1344 return false;
1345 }
1346 CreateReferenceInstanceOffsets(klass);
1347 CreateReferenceStaticOffsets(klass);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001348 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001349 klass->status_ = Class::kStatusResolved;
1350 return true;
1351}
1352
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001353bool ClassLinker::LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file) {
1354 CHECK_EQ(Class::kStatusIdx, klass->status_);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001355 if (klass->super_class_type_idx_ != DexFile::kDexNoIndex) {
1356 Class* super_class = ResolveType(dex_file, klass->super_class_type_idx_, klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001357 if (super_class == NULL) {
1358 LG << "Failed to resolve superclass";
1359 return false;
1360 }
1361 klass->super_class_ = super_class; // TODO: write barrier
1362 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001363 if (klass->NumInterfaces() > 0) {
1364 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001365 uint32_t type_idx = klass->interfaces_type_idx_->Get(i);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001366 klass->SetInterface(i, ResolveType(dex_file, type_idx, klass));
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001367 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001368 LG << "Failed to resolve interface";
1369 return false;
1370 }
1371 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001372 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001373 LG << "Inaccessible interface";
1374 return false;
1375 }
1376 }
1377 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001378 // Mark the class as loaded.
1379 klass->status_ = Class::kStatusLoaded;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001380 return true;
1381}
1382
1383bool ClassLinker::LinkSuperClass(Class* klass) {
1384 CHECK(!klass->IsPrimitive());
1385 const Class* super = klass->GetSuperClass();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001386 if (klass->GetDescriptor()->Equals("Ljava/lang/Object;")) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001387 if (super != NULL) {
1388 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1389 return false;
1390 }
1391 // TODO: clear finalize attribute
1392 return true;
1393 }
1394 if (super == NULL) {
1395 LG << "No superclass defined"; // TODO: LinkageError
1396 return false;
1397 }
1398 // Verify
1399 if (super->IsFinal()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001400 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is declared final"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001401 return false;
1402 }
1403 if (super->IsInterface()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001404 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is an interface"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001405 return false;
1406 }
1407 if (!klass->CanAccess(super)) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001408 LG << "Superclass " << super->descriptor_->ToModifiedUtf8() << " is inaccessible"; // TODO: IllegalAccessError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001409 return false;
1410 }
1411 return true;
1412}
1413
1414// Populate the class vtable and itable.
1415bool ClassLinker::LinkMethods(Class* klass) {
1416 if (klass->IsInterface()) {
1417 // No vtable.
1418 size_t count = klass->NumVirtualMethods();
1419 if (!IsUint(16, count)) {
1420 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1421 return false;
1422 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001423 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001424 klass->GetVirtualMethod(i)->method_index_ = i;
1425 }
1426 } else {
1427 // Link virtual method tables
1428 LinkVirtualMethods(klass);
1429
1430 // Link interface method tables
1431 LinkInterfaceMethods(klass);
1432
1433 // Insert stubs.
1434 LinkAbstractMethods(klass);
1435 }
1436 return true;
1437}
1438
1439bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001440 if (klass->HasSuperClass()) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001441 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001442 size_t actual_count = klass->GetSuperClass()->vtable_->GetLength();
1443 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001444 // TODO: do not assign to the vtable field until it is fully constructed.
1445 klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001446 // See if any of our virtual methods override the superclass.
1447 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1448 Method* local_method = klass->GetVirtualMethod(i);
1449 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001450 for (; j < actual_count; ++j) {
1451 Method* super_method = klass->vtable_->Get(j);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001452 if (local_method->HasSameNameAndDescriptor(super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001453 // Verify
1454 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001455 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001456 return false;
1457 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001458 klass->vtable_->Set(j, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001459 local_method->method_index_ = j;
1460 break;
1461 }
1462 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001463 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001464 // Not overriding, append.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001465 klass->vtable_->Set(actual_count, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001466 local_method->method_index_ = actual_count;
1467 actual_count += 1;
1468 }
1469 }
1470 if (!IsUint(16, actual_count)) {
1471 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1472 return false;
1473 }
1474 CHECK_LE(actual_count, max_count);
1475 if (actual_count < max_count) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001476 // TODO: do not assign to the vtable field until it is fully constructed.
1477 klass->vtable_ = klass->vtable_->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001478 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001479 } else {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001480 CHECK(klass->GetDescriptor()->Equals("Ljava/lang/Object;"));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001481 uint32_t num_virtual_methods = klass->NumVirtualMethods();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001482 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001483 LG << "Too many methods"; // TODO: VirtualMachineError
1484 return false;
1485 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001486 // TODO: do not assign to the vtable field until it is fully constructed.
1487 klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods);
1488 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001489 klass->vtable_->Set(i, klass->GetVirtualMethod(i));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001490 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1491 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001492 }
1493 return true;
1494}
1495
1496bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1497 int pool_offset = 0;
1498 int pool_size = 0;
1499 int miranda_count = 0;
1500 int miranda_alloc = 0;
1501 size_t super_ifcount;
1502 if (klass->HasSuperClass()) {
1503 super_ifcount = klass->GetSuperClass()->iftable_count_;
1504 } else {
1505 super_ifcount = 0;
1506 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001507 size_t ifcount = super_ifcount;
1508 ifcount += klass->NumInterfaces();
1509 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1510 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001511 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001512 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001513 DCHECK(klass->iftable_count_ == 0);
1514 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001515 return true;
1516 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001517 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1518 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001519 if (super_ifcount != 0) {
1520 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1521 sizeof(InterfaceEntry) * super_ifcount);
1522 }
1523 // Flatten the interface inheritance hierarchy.
1524 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001525 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1526 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001527 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001528 if (!interf->IsInterface()) {
1529 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1530 return false;
1531 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001532 klass->iftable_[idx++].SetInterface(interf);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001533 for (size_t j = 0; j < interf->iftable_count_; j++) {
Brian Carlstrom30b94452011-08-25 21:35:26 -07001534 klass->iftable_[idx++].SetInterface(interf->iftable_[j].GetInterface());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001535 }
1536 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001537 CHECK_EQ(idx, ifcount);
1538 klass->iftable_count_ = ifcount;
1539 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001540 return true;
1541 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001542 for (size_t i = super_ifcount; i < ifcount; i++) {
Brian Carlstrom30b94452011-08-25 21:35:26 -07001543 pool_size += klass->iftable_[i].GetInterface()->NumVirtualMethods();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001544 }
1545 if (pool_size == 0) {
1546 return true;
1547 }
1548 klass->ifvi_pool_count_ = pool_size;
1549 klass->ifvi_pool_ = new uint32_t[pool_size];
1550 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001551 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001552 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
Brian Carlstrom30b94452011-08-25 21:35:26 -07001553 Class* interface = klass->iftable_[i].GetInterface();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001554 pool_offset += interface->NumVirtualMethods(); // end here
1555 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1556 Method* interface_method = interface->GetVirtualMethod(j);
1557 int k; // must be signed
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001558 for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001559 Method* vtable_method = klass->vtable_->Get(k);
1560 if (interface_method->HasSameNameAndDescriptor(vtable_method)) {
1561 if (!vtable_method->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001562 LG << "Implementation not public";
1563 return false;
1564 }
1565 klass->iftable_[i].method_index_array_[j] = k;
1566 break;
1567 }
1568 }
1569 if (k < 0) {
1570 if (miranda_count == miranda_alloc) {
1571 miranda_alloc += 8;
1572 if (miranda_list.empty()) {
1573 miranda_list.resize(miranda_alloc);
1574 } else {
1575 miranda_list.resize(miranda_alloc);
1576 }
1577 }
1578 int mir;
1579 for (mir = 0; mir < miranda_count; mir++) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001580 Method* miranda_method = miranda_list[mir];
1581 if (miranda_method->HasSameNameAndDescriptor(interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001582 break;
1583 }
1584 }
1585 // point the interface table at a phantom slot index
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001586 klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001587 if (mir == miranda_count) {
1588 miranda_list[miranda_count++] = interface_method;
1589 }
1590 }
1591 }
1592 }
1593 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001594 int old_method_count = klass->NumVirtualMethods();
1595 int new_method_count = old_method_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001596 klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001597
1598 CHECK(klass->vtable_ != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001599 int old_vtable_count = klass->vtable_->GetLength();
1600 int new_vtable_count = old_vtable_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001601 // TODO: do not assign to the vtable field until it is fully constructed.
1602 klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001603
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001604 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001605 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001606 memcpy(meth, miranda_list[i], sizeof(Method));
1607 meth->klass_ = klass;
1608 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001609 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1610 klass->SetVirtualMethod(old_method_count + i, meth);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001611 klass->vtable_->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001612 }
1613 }
1614 return true;
1615}
1616
1617void ClassLinker::LinkAbstractMethods(Class* klass) {
1618 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1619 Method* method = klass->GetVirtualMethod(i);
1620 if (method->IsAbstract()) {
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001621 LG << "AbstractMethodError";
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001622 // TODO: throw AbstractMethodError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001623 }
1624 }
1625}
1626
1627bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001628 CHECK(klass != NULL);
1629 size_t field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001630 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001631 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001632 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001633 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001634 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001635 return LinkFields(field_offset,
1636 klass->num_reference_instance_fields_,
1637 klass->NumInstanceFields(),
1638 klass->ifields_,
1639 klass->object_size_);
1640}
1641
1642bool ClassLinker::LinkStaticFields(Class* klass) {
1643 CHECK(klass != NULL);
1644 size_t allocated_class_size = klass->class_size_;
1645 size_t field_offset = OFFSETOF_MEMBER(Class, fields_);
1646 bool success = LinkFields(field_offset,
1647 klass->num_reference_static_fields_,
1648 klass->NumStaticFields(),
1649 klass->sfields_,
1650 klass->class_size_);
1651 CHECK_EQ(allocated_class_size, klass->class_size_);
1652 return success;
1653}
1654
1655bool ClassLinker::LinkFields(size_t field_offset,
1656 size_t& num_reference_fields,
1657 size_t num_fields,
1658 ObjectArray<Field>* fields,
1659 size_t& size) {
1660 CHECK((num_fields == 0) == (fields == NULL));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001661 // Move references to the front.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001662 num_reference_fields = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001663 size_t i = 0;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001664 for ( ; i < num_fields; i++) {
1665 Field* pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001666 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001667 if (c != '[' && c != 'L') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001668 for (size_t j = num_fields - 1; j > i; j--) {
1669 Field* refField = fields->Get(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001670 char rc = refField->GetType();
1671 if (rc == '[' || rc == 'L') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001672 fields->Set(i, refField);
1673 fields->Set(j, pField);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001674 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001675 c = rc;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001676 num_reference_fields++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001677 break;
1678 }
1679 }
1680 } else {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001681 num_reference_fields++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001682 }
1683 if (c != '[' && c != 'L') {
1684 break;
1685 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001686 pField->SetOffset(field_offset);
1687 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001688 }
1689
1690 // Now we want to pack all of the double-wide fields together. If
1691 // we're not aligned, though, we want to shuffle one 32-bit field
1692 // into place. If we can't find one, we'll have to pad it.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001693 if (i != num_fields && (field_offset & 0x04) != 0) {
1694 Field* pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001695 char c = pField->GetType();
1696
1697 if (c != 'J' && c != 'D') {
1698 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001699 DCHECK(c != '[');
1700 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001701 pField->SetOffset(field_offset);
1702 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001703 i++;
1704 } else {
1705 // Next field is 64-bit, so search for a 32-bit field we can
1706 // swap into it.
1707 bool found = false;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001708 for (size_t j = num_fields - 1; j > i; j--) {
1709 Field* singleField = fields->Get(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001710 char rc = singleField->GetType();
1711 if (rc != 'J' && rc != 'D') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001712 fields->Set(i, singleField);
1713 fields->Set(j, pField);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001714 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001715 pField->SetOffset(field_offset);
1716 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001717 found = true;
1718 i++;
1719 break;
1720 }
1721 }
1722 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001723 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001724 }
1725 }
1726 }
1727
1728 // Alignment is good, shuffle any double-wide fields forward, and
1729 // finish assigning field offsets to all fields.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001730 DCHECK(i == num_fields || (field_offset & 0x04) == 0);
1731 for ( ; i < num_fields; i++) {
1732 Field* pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001733 char c = pField->GetType();
1734 if (c != 'D' && c != 'J') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001735 for (size_t j = num_fields - 1; j > i; j--) {
1736 Field* doubleField = fields->Get(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001737 char rc = doubleField->GetType();
1738 if (rc == 'D' || rc == 'J') {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001739 fields->Set(i, doubleField);
1740 fields->Set(j, pField);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001741 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001742 c = rc;
1743 break;
1744 }
1745 }
1746 } else {
1747 // This is a double-wide field, leave it be.
1748 }
1749
Brian Carlstroma0808032011-07-18 00:39:23 -07001750 pField->SetOffset(field_offset);
1751 field_offset += sizeof(uint32_t);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001752 if (c == 'J' || c == 'D') {
Brian Carlstroma0808032011-07-18 00:39:23 -07001753 field_offset += sizeof(uint32_t);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001754 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001755 }
1756
1757#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001758 // Make sure that all reference fields appear before
1759 // non-reference fields, and all double-wide fields are aligned.
1760 bool seen_non_ref = false;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001761 for (i = 0; i < num_fields; i++) {
1762 Field *pField = fields->Get(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001763 char c = pField->GetType();
1764
1765 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001766 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001767 }
1768
1769 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001770 if (!seen_non_ref) {
1771 seen_non_ref = true;
Brian Carlstrom4873d462011-08-21 15:23:39 -07001772 DCHECK_EQ(num_reference_fields, i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001773 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001774 } else {
1775 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001776 }
1777 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001778 if (!seen_non_ref) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001779 DCHECK_EQ(num_fields, num_reference_fields);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001780 }
1781#endif
Brian Carlstrom4873d462011-08-21 15:23:39 -07001782 size = field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001783 return true;
1784}
1785
1786// Set the bitmap of reference offsets, refOffsets, from the ifields
1787// list.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001788void ClassLinker::CreateReferenceInstanceOffsets(Class* klass) {
1789 klass->reference_instance_offsets_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001790 if (klass->HasSuperClass()) {
Brian Carlstrom4873d462011-08-21 15:23:39 -07001791 klass->reference_instance_offsets_ = klass->GetSuperClass()->GetReferenceInstanceOffsets();
1792 // If our superclass overflowed, we don't stand a chance.
1793 if (klass->reference_instance_offsets_ == CLASS_WALK_SUPER) {
1794 return;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001795 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001796 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001797 CreateReferenceOffsets(klass->reference_instance_offsets_,
1798 klass->NumReferenceInstanceFields(),
1799 klass->ifields_);
1800}
1801
1802void ClassLinker::CreateReferenceStaticOffsets(Class* klass) {
1803 klass->reference_static_offsets_ = 0;
1804 CreateReferenceOffsets(klass->reference_static_offsets_,
1805 klass->NumReferenceStaticFields(),
1806 klass->sfields_);
1807}
1808
1809void ClassLinker::CreateReferenceOffsets(uint32_t& reference_offsets,
1810 size_t num_reference_fields,
1811 const ObjectArray<Field>* fields) {
1812 // All of the fields that contain object references are guaranteed
1813 // to be at the beginning of the fields list.
1814 for (size_t i = 0; i < num_reference_fields; ++i) {
1815 // Note that byte_offset is the offset from the beginning of
1816 // object, not the offset into instance data
1817 const Field* field = fields->Get(i);
1818 size_t byte_offset = field->GetOffset();
1819 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
1820 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
1821 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1822 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
1823 CHECK_NE(new_bit, 0U);
1824 reference_offsets |= new_bit;
1825 } else {
1826 reference_offsets = CLASS_WALK_SUPER;
1827 break;
1828 }
1829 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001830}
1831
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001832String* ClassLinker::ResolveString(const DexFile& dex_file,
1833 uint32_t string_idx,
1834 DexCache* dex_cache) {
1835 String* resolved = dex_cache->GetResolvedString(string_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001836 if (resolved != NULL) {
1837 return resolved;
1838 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001839 const DexFile::StringId& string_id = dex_file.GetStringId(string_idx);
1840 int32_t utf16_length = dex_file.GetStringLength(string_id);
1841 const char* utf8_data = dex_file.GetStringData(string_id);
1842 String* string = intern_table_.Intern(utf16_length, utf8_data);
1843 dex_cache->SetResolvedString(string_idx, string);
1844 return string;
1845}
1846
1847Class* ClassLinker::ResolveType(const DexFile& dex_file,
1848 uint32_t type_idx,
1849 DexCache* dex_cache,
1850 const ClassLoader* class_loader) {
1851 Class* resolved = dex_cache->GetResolvedType(type_idx);
1852 if (resolved != NULL) {
1853 return resolved;
1854 }
1855 const char* descriptor = dex_file.dexStringByTypeIdx(type_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001856 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001857 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001858 } else {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001859 resolved = FindClass(descriptor, class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001860 }
1861 if (resolved != NULL) {
1862 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001863 if (dex_cache != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001864 if (check->GetClassLoader() != NULL) {
1865 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1866 return NULL;
1867 }
1868 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001869 dex_cache->SetResolvedType(type_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001870 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001871 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001872 }
1873 return resolved;
1874}
1875
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001876Method* ClassLinker::ResolveMethod(const DexFile& dex_file,
1877 uint32_t method_idx,
1878 DexCache* dex_cache,
1879 const ClassLoader* class_loader,
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07001880 bool is_direct) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001881 Method* resolved = dex_cache->GetResolvedMethod(method_idx);
1882 if (resolved != NULL) {
1883 return resolved;
1884 }
1885 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
1886 Class* klass = ResolveType(dex_file, method_id.class_idx_, dex_cache, class_loader);
1887 if (klass == NULL) {
1888 return NULL;
1889 }
1890
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07001891 const char* name = dex_file.dexStringById(method_id.name_idx_);
Elliott Hughes0c424cb2011-08-26 10:16:25 -07001892 std::string signature(dex_file.CreateMethodDescriptor(method_id.proto_idx_, NULL));
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07001893 if (is_direct) {
1894 resolved = klass->FindDirectMethod(name, signature);
1895 } else {
1896 resolved = klass->FindVirtualMethod(name, signature);
1897 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001898 if (resolved != NULL) {
1899 dex_cache->SetResolvedMethod(method_idx, resolved);
1900 } else {
1901 // DCHECK(Thread::Current()->IsExceptionPending());
1902 }
1903 return resolved;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001904}
1905
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001906Field* ClassLinker::ResolveField(const DexFile& dex_file,
1907 uint32_t field_idx,
1908 DexCache* dex_cache,
1909 const ClassLoader* class_loader,
1910 bool is_static) {
1911 Field* resolved = dex_cache->GetResolvedField(field_idx);
1912 if (resolved != NULL) {
1913 return resolved;
1914 }
1915 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx);
1916 Class* klass = ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader);
1917 if (klass == NULL) {
1918 return NULL;
1919 }
1920
Brian Carlstrom20cfffa2011-08-26 02:31:27 -07001921 const char* name = dex_file.dexStringById(field_id.name_idx_);
1922 const char* type = dex_file.dexStringByTypeIdx(field_id.type_idx_);
1923 if (is_static) {
1924 resolved = klass->FindStaticField(name, type);
1925 } else {
1926 resolved = klass->FindInstanceField(name, type);
1927 }
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001928 if (resolved != NULL) {
1929 dex_cache->SetResolvedfield(field_idx, resolved);
1930 } else {
1931 // DCHECK(Thread::Current()->IsExceptionPending());
1932 }
1933 return resolved;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001934}
1935
Elliott Hughese27955c2011-08-26 15:21:24 -07001936size_t ClassLinker::NumLoadedClasses() const {
1937 MutexLock mu(classes_lock_);
1938 return classes_.size();
1939}
1940
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001941} // namespace art