blob: 3f9f7ab5fe098a517b26cfcc62ec07edb5dd60d5 [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2// Author: cshapiro@google.com (Carl Shapiro)
3
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07004#include "class_linker.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005
6#include <vector>
7#include <utility>
8
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "casts.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070010#include "dex_cache.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "dex_verifier.h"
12#include "heap.h"
13#include "logging.h"
14#include "monitor.h"
15#include "object.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070016#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "scoped_ptr.h"
18#include "thread.h"
19#include "utils.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070020
21namespace art {
22
Carl Shapiro2ed144c2011-07-26 16:52:08 -070023ClassLinker* ClassLinker::Create(const std::vector<DexFile*>& boot_class_path) {
Carl Shapiro61e019d2011-07-14 16:53:09 -070024 scoped_ptr<ClassLinker> class_linker(new ClassLinker);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025 class_linker->Init(boot_class_path);
Carl Shapiro61e019d2011-07-14 16:53:09 -070026 // TODO: check for failure during initialization
27 return class_linker.release();
28}
29
Carl Shapiro2ed144c2011-07-26 16:52:08 -070030void ClassLinker::Init(const std::vector<DexFile*>& boot_class_path) {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070031 init_done_ = false;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070033 // java_lang_Class comes first, its needed for AllocClass
34 Class* java_lang_Class = down_cast<Class*>(Heap::AllocObject(NULL, sizeof(Class)));
35 CHECK(java_lang_Class != NULL);
36 java_lang_Class->descriptor_ = "Ljava/lang/Class;";
37 java_lang_Class->object_size_ = sizeof(Class);
38 java_lang_Class->klass_ = java_lang_Class;
39 // AllocClass(Class*) can now be used
Brian Carlstroma0808032011-07-18 00:39:23 -070040
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070041 // java_lang_Object comes next so that object_array_class can be created
42 Class* java_lang_Object = AllocClass(java_lang_Class);
43 CHECK(java_lang_Object != NULL);
44 java_lang_Object->descriptor_ = "Ljava/lang/Object;";
45 // backfill Object as the super class of Class
46 java_lang_Class->super_class_ = java_lang_Object;
Brian Carlstroma0808032011-07-18 00:39:23 -070047
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070048 // object_array_class is for root_classes to provide the storage for these classes
49 Class* object_array_class = AllocClass(java_lang_Class);
50 CHECK(object_array_class != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -070051
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070052 // String and char[] are necessary so that FindClass can assign names to members
Jesse Wilson14150742011-07-29 19:04:44 -040053 Class* java_lang_String = AllocClass(java_lang_Class);
54 CHECK(java_lang_String != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070055 java_lang_String->descriptor_ = "Ljava/lang/String;";
56 CHECK_LT(java_lang_String->object_size_, sizeof(String));
Jesse Wilson14150742011-07-29 19:04:44 -040057 java_lang_String->object_size_ = sizeof(String);
Jesse Wilson8989d992011-08-02 13:39:42 -070058 Class* char_array_class = AllocClass(java_lang_Class);
59 CHECK(char_array_class != NULL);
Jesse Wilson14150742011-07-29 19:04:44 -040060
Jesse Wilson7833bd22011-08-09 18:31:44 -040061 // int[] and long[] are used for static field storage
62 Class* int_array_class = AllocClass(java_lang_Class);
63 CHECK(int_array_class != NULL);
64 Class* long_array_class = AllocClass(java_lang_Class);
65 CHECK(long_array_class != NULL);
66
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070067 // Field and Method are necessary so that FindClass can link members
68 Class* java_lang_reflect_Field = AllocClass(java_lang_Class);
69 CHECK(java_lang_reflect_Field != NULL);
70 java_lang_reflect_Field->descriptor_ = "Ljava/lang/reflect/Field;";
71 CHECK_LT(java_lang_reflect_Field->object_size_, std::max(sizeof(StaticField), sizeof(InstanceField)));
72 java_lang_reflect_Field->object_size_ = std::max(sizeof(StaticField), sizeof(InstanceField));
73 Class* java_lang_reflect_Method = AllocClass(java_lang_Class);
74 java_lang_reflect_Method->descriptor_ = "Ljava/lang/reflect/Method;";
75 CHECK(java_lang_reflect_Method != NULL);
76 CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method));
77 java_lang_reflect_Method->object_size_ = sizeof(Method);
78
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070079 // create storage for root classes, save away our work so far
80 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
81 class_roots_->Set(kJavaLangClass, java_lang_Class);
82 class_roots_->Set(kJavaLangObject, java_lang_Object);
83 class_roots_->Set(kObjectArrayClass, object_array_class);
Jesse Wilson14150742011-07-29 19:04:44 -040084 class_roots_->Set(kJavaLangString, java_lang_String);
Jesse Wilson8989d992011-08-02 13:39:42 -070085 class_roots_->Set(kCharArrayClass, char_array_class);
Jesse Wilson7833bd22011-08-09 18:31:44 -040086 class_roots_->Set(kIntArrayClass, int_array_class);
87 class_roots_->Set(kLongArrayClass, long_array_class);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070088 class_roots_->Set(kJavaLangReflectField, java_lang_reflect_Field);
89 class_roots_->Set(kJavaLangReflectMethod, java_lang_reflect_Method);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070090 // now that these are registered, we can use AllocClass() and AllocObjectArray
Brian Carlstroma0808032011-07-18 00:39:23 -070091
Jesse Wilson8989d992011-08-02 13:39:42 -070092 String::InitClasses(java_lang_String, char_array_class);
93 // Now AllocString* can be used
94
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070095 // setup boot_class_path_ now that we can use AllocObjectArray to
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070096 // create DexCache instances
Brian Carlstrom913af1b2011-07-23 21:41:13 -070097 for (size_t i = 0; i != boot_class_path.size(); ++i) {
98 AppendToBootClassPath(boot_class_path[i]);
99 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700100 // now we can use FindSystemClass, at least for non-arrays classes.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700101
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700102 // run Class, Field, and Method through FindSystemClass.
103 // this initializes their dex_cache_ fields and register them in classes_.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700104 // we also override their object_size_ values to accommodate the extra C++ fields.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700105 Class* Class_class = FindSystemClass(java_lang_Class->GetDescriptor());
106 CHECK_EQ(java_lang_Class, Class_class);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700107 CHECK_LT(java_lang_Class->object_size_, sizeof(Class));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700108 java_lang_Class->object_size_ = sizeof(Class);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700109 Class* Field_class = FindSystemClass(java_lang_reflect_Field->GetDescriptor());
110 CHECK_EQ(java_lang_reflect_Field, Field_class);
111 CHECK_LT(java_lang_reflect_Field->object_size_, std::max(sizeof(StaticField), sizeof(InstanceField)));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700112 java_lang_reflect_Field->object_size_ = std::max(sizeof(StaticField), sizeof(InstanceField));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700113 Class* Method_class = FindSystemClass(java_lang_reflect_Method->GetDescriptor());
114 CHECK_EQ(java_lang_reflect_Method, Method_class);
115 CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700116 java_lang_reflect_Method->object_size_ = sizeof(Method);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700117
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700118 // Object and String just need more minimal setup, since they do not have extra C++ fields.
119 Class* Object_class = FindSystemClass(java_lang_Object->GetDescriptor());
120 CHECK_EQ(java_lang_Object, Object_class);
121 CHECK_EQ(java_lang_Object->object_size_, sizeof(Object));
122 Class* String_class = FindSystemClass(java_lang_String->GetDescriptor());
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700123 CHECK_EQ(java_lang_String, String_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700124 CHECK_EQ(java_lang_String->object_size_, sizeof(String));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700125
126 // Setup the ClassLoaders, adjusting the object_size_ as necessary
127 Class* java_lang_ClassLoader = FindSystemClass("Ljava/lang/ClassLoader;");
128 CHECK(java_lang_ClassLoader != NULL);
129 CHECK_LT(java_lang_ClassLoader->object_size_, sizeof(ClassLoader));
130 java_lang_ClassLoader->object_size_ = sizeof(ClassLoader);
131 class_roots_->Set(kJavaLangClassLoader, java_lang_ClassLoader);
132 Class* dalvik_system_BaseDexClassLoader = FindSystemClass("Ldalvik/system/BaseDexClassLoader;");
133 CHECK(dalvik_system_BaseDexClassLoader != NULL);
134 CHECK_EQ(dalvik_system_BaseDexClassLoader->object_size_, sizeof(BaseDexClassLoader));
135 class_roots_->Set(kDalvikSystemBaseDexClassLoader, dalvik_system_BaseDexClassLoader);
136 Class* dalvik_system_PathClassLoader = FindSystemClass("Ldalvik/system/PathClassLoader;");
137 CHECK(dalvik_system_PathClassLoader != NULL);
138 CHECK_EQ(dalvik_system_PathClassLoader->object_size_, sizeof(PathClassLoader));
139 class_roots_->Set(kDalvikSystemPathClassLoader, dalvik_system_PathClassLoader);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700140
141 // Setup a single, global copy of "interfaces" and "iftable" for
142 // reuse across array classes
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700143 Class* java_lang_Cloneable = FindSystemClass("Ljava/lang/Cloneable;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700144 CHECK(java_lang_Cloneable != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700145 Class* java_io_Serializable = FindSystemClass("Ljava/io/Serializable;");
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700146 CHECK(java_io_Serializable != NULL);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700147
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700148 array_interfaces_ = AllocObjectArray<Class>(2);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700149 CHECK(array_interfaces_ != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700150 array_interfaces_->Set(0, java_lang_Cloneable);
151 array_interfaces_->Set(1, java_io_Serializable);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700152
153 // We assume that Cloneable/Serializable don't have superinterfaces --
154 // normally we'd have to crawl up and explicitly list all of the
155 // supers as well. These interfaces don't have any methods, so we
156 // don't have to worry about the ifviPool either.
157 array_iftable_ = new InterfaceEntry[2];
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700158 array_iftable_[0].SetClass(array_interfaces_->Get(0));
159 array_iftable_[1].SetClass(array_interfaces_->Get(1));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700160 // now FindClass can be used for non-primitive array classes
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700161
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700162 // run Object[] through FindClass to complete initialization
Jesse Wilson8989d992011-08-02 13:39:42 -0700163 Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;");
164 CHECK_EQ(object_array_class, found_object_array_class);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700165 CHECK_EQ(java_lang_Cloneable, object_array_class->GetInterface(0));
166 CHECK_EQ(java_io_Serializable, object_array_class->GetInterface(1));
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700167
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700168 // Setup the primitive type classes.
169 class_roots_->Set(kPrimitiveByte, CreatePrimitiveClass("B"));
170 class_roots_->Set(kPrimitiveChar, CreatePrimitiveClass("C"));
171 class_roots_->Set(kPrimitiveDouble, CreatePrimitiveClass("D"));
172 class_roots_->Set(kPrimitiveFloat, CreatePrimitiveClass("F"));
173 class_roots_->Set(kPrimitiveInt, CreatePrimitiveClass("I"));
174 class_roots_->Set(kPrimitiveLong, CreatePrimitiveClass("J"));
175 class_roots_->Set(kPrimitiveShort, CreatePrimitiveClass("S"));
176 class_roots_->Set(kPrimitiveBoolean, CreatePrimitiveClass("Z"));
177 class_roots_->Set(kPrimitiveVoid, CreatePrimitiveClass("V"));
178 // now we can use FindSystemClass for anything, including for "[C"
179
Jesse Wilson7833bd22011-08-09 18:31:44 -0400180 // run char[], int[] and long[] through FindClass to complete initialization
Jesse Wilson8989d992011-08-02 13:39:42 -0700181 Class* found_char_array_class = FindSystemClass("[C");
182 CHECK_EQ(char_array_class, found_char_array_class);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400183 Class* found_int_array_class = FindSystemClass("[I");
184 CHECK_EQ(int_array_class, found_int_array_class);
185 Class* found_long_array_class = FindSystemClass("[J");
186 CHECK_EQ(long_array_class, found_long_array_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700187
188 // ensure all class_roots_ were initialized
189 for (size_t i = 0; i < kClassRootsMax; i++) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700190 CHECK(GetClassRoot(static_cast<ClassRoot>(i)));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700191 }
192
193 // disable the slow paths in FindClass and CreatePrimitiveClass now
194 // that Object, Class, and Object[] are setup
195 init_done_ = true;
196}
197
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700198void ClassLinker::VisitRoots(Heap::RootVistor* root_visitor, void* arg) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700199 root_visitor(class_roots_, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700200
201 for (size_t i = 0; i < dex_caches_.size(); i++) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700202 root_visitor(dex_caches_[i], arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700203 }
204
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700205 {
206 MutexLock mu(classes_lock_);
207 typedef Table::const_iterator It; // TODO: C++0x auto
208 for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
209 root_visitor(it->second, arg);
210 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700211 }
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700212
213 intern_table_.VisitRoots(root_visitor, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700214
Brian Carlstromb88e9442011-07-28 15:15:51 -0700215 root_visitor(array_interfaces_, arg);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700216}
217
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700218DexCache* ClassLinker::AllocDexCache() {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700219 return down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::kMax));
Brian Carlstroma0808032011-07-18 00:39:23 -0700220}
221
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700222Class* ClassLinker::AllocClass(Class* java_lang_Class) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700223 return down_cast<Class*>(java_lang_Class->NewInstance());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700224}
225
226Class* ClassLinker::AllocClass() {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700227 return AllocClass(GetClassRoot(kJavaLangClass));
Brian Carlstroma0808032011-07-18 00:39:23 -0700228}
229
230StaticField* ClassLinker::AllocStaticField() {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700231 return down_cast<StaticField*>(GetClassRoot(kJavaLangReflectField)->NewInstance());
Brian Carlstroma0808032011-07-18 00:39:23 -0700232}
233
234InstanceField* ClassLinker::AllocInstanceField() {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700235 return down_cast<InstanceField*>(GetClassRoot(kJavaLangReflectField)->NewInstance());
Brian Carlstroma0808032011-07-18 00:39:23 -0700236}
237
238Method* ClassLinker::AllocMethod() {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700239 return down_cast<Method*>(GetClassRoot(kJavaLangReflectMethod)->NewInstance());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700240}
241
242// TODO remove once we can use java.lang.Class.getSystemClassLoader
243PathClassLoader* ClassLinker::AllocPathClassLoader(std::vector<const DexFile*> dex_files) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700244 PathClassLoader* cl = down_cast<PathClassLoader*>(GetClassRoot(kDalvikSystemPathClassLoader)->NewInstance());
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700245 cl->SetClassPath(dex_files);
246 return cl;
Carl Shapiro565f5072011-07-10 13:39:43 -0700247}
248
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700249Class* ClassLinker::FindClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700250 ClassLoader* class_loader) {
251 // TODO remove this contrived parent class loader check when we have a real ClassLoader.
252 if (class_loader != NULL) {
253 Class* klass = FindClass(descriptor, NULL);
254 if (klass != NULL) {
255 return klass;
256 }
257 }
258
Carl Shapirob5573532011-07-12 18:22:59 -0700259 Thread* self = Thread::Current();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700260 DCHECK(self != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700261 CHECK(!self->IsExceptionPending());
262 // Find the class in the loaded classes table.
263 Class* klass = LookupClass(descriptor, class_loader);
264 if (klass == NULL) {
265 // Class is not yet loaded.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700266 if (descriptor[0] == '[') {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700267 return CreateArrayClass(descriptor, class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700268 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700269 DexFile::ClassPath& class_path = ((class_loader != NULL) ? class_loader->GetClassPath() : boot_class_path_);
270 DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, class_path);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700271 if (pair.second == NULL) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700272 LG << "Class " << descriptor << " not found in class loader " << class_loader; // TODO: NoClassDefFoundError
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700273 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700274 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700275 const DexFile& dex_file = *pair.first;
276 const DexFile::ClassDef& dex_class_def = *pair.second;
277 DexCache* dex_cache = FindDexCache(pair.first);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700278 // Load the class from the dex file.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700279 if (!init_done_) {
280 // finish up init of hand crafted class_roots_
281 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700282 klass = GetClassRoot(kJavaLangObject);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700283 } else if (descriptor == "Ljava/lang/Class;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700284 klass = GetClassRoot(kJavaLangClass);
Jesse Wilson14150742011-07-29 19:04:44 -0400285 } else if (descriptor == "Ljava/lang/String;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700286 klass = GetClassRoot(kJavaLangString);
287 } else if (descriptor == "Ljava/lang/reflect/Field;") {
288 klass = GetClassRoot(kJavaLangReflectField);
289 } else if (descriptor == "Ljava/lang/reflect/Method;") {
290 klass = GetClassRoot(kJavaLangReflectMethod);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700291 } else {
292 klass = AllocClass();
293 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700294 } else {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700295 klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700296 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700297 klass->dex_cache_ = dex_cache;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700298 LoadClass(dex_file, dex_class_def, klass, class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700299 // Check for a pending exception during load
300 if (self->IsExceptionPending()) {
301 // TODO: free native allocations in klass
302 return NULL;
303 }
304 {
305 ObjectLock lock(klass);
Carl Shapirob5573532011-07-12 18:22:59 -0700306 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700307 // Add the newly loaded class to the loaded classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700308 bool success = InsertClass(klass); // TODO just return collision
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700309 if (!success) {
310 // We may fail to insert if we raced with another thread.
311 klass->clinit_thread_id_ = 0;
312 // TODO: free native allocations in klass
313 klass = LookupClass(descriptor, class_loader);
314 CHECK(klass != NULL);
315 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700316 // Finish loading (if necessary) by finding parents
317 if (!klass->IsLoaded() && !LoadSuperAndInterfaces(klass, dex_file)) {
318 // Loading failed.
319 // TODO: CHECK(self->IsExceptionPending());
320 lock.NotifyAll();
321 return NULL;
322 }
323 CHECK(klass->IsLoaded());
324 // Link the class (if necessary)
325 if (!klass->IsLinked() && !LinkClass(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700326 // Linking failed.
327 // TODO: CHECK(self->IsExceptionPending());
328 lock.NotifyAll();
329 return NULL;
330 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700331 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700332 }
333 }
334 }
335 // Link the class if it has not already been linked.
336 if (!klass->IsLinked() && !klass->IsErroneous()) {
337 ObjectLock lock(klass);
338 // Check for circular dependencies between classes.
Carl Shapirob5573532011-07-12 18:22:59 -0700339 if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700340 LG << "Recursive link"; // TODO: ClassCircularityError
341 return NULL;
342 }
343 // Wait for the pending initialization to complete.
344 while (!klass->IsLinked() && !klass->IsErroneous()) {
345 lock.Wait();
346 }
347 }
348 if (klass->IsErroneous()) {
349 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
350 return NULL;
351 }
352 // Return the loaded class. No exceptions should be pending.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700353 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700354 CHECK(!self->IsExceptionPending());
355 return klass;
356}
357
Brian Carlstromf615a612011-07-23 12:50:34 -0700358void ClassLinker::LoadClass(const DexFile& dex_file,
359 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700360 Class* klass,
361 ClassLoader* class_loader) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700362 CHECK(klass != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700363 CHECK(klass->dex_cache_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700364 CHECK_EQ(Class::kStatusNotReady, klass->status_);
Brian Carlstromf615a612011-07-23 12:50:34 -0700365 const byte* class_data = dex_file.GetClassData(dex_class_def);
366 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700367
Brian Carlstromf615a612011-07-23 12:50:34 -0700368 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700369 CHECK(descriptor != NULL);
370
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700371 klass->klass_ = GetClassRoot(kJavaLangClass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700372 klass->descriptor_.set(descriptor);
373 klass->descriptor_alloc_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700374 klass->access_flags_ = dex_class_def.access_flags_;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700375 klass->class_loader_ = class_loader;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700376 klass->primitive_type_ = Class::kPrimNot;
377 klass->status_ = Class::kStatusIdx;
378
379 klass->super_class_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700380 klass->super_class_idx_ = dex_class_def.superclass_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700381
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700382 size_t num_static_fields = header.static_fields_size_;
383 size_t num_instance_fields = header.instance_fields_size_;
384 size_t num_direct_methods = header.direct_methods_size_;
385 size_t num_virtual_methods = header.virtual_methods_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700386
Brian Carlstromf615a612011-07-23 12:50:34 -0700387 klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700388
389 // Load class interfaces.
Brian Carlstromf615a612011-07-23 12:50:34 -0700390 LoadInterfaces(dex_file, dex_class_def, klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700391
392 // Load static fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700393 DCHECK(klass->sfields_ == NULL);
394 if (num_static_fields != 0) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700395 klass->sfields_ = AllocObjectArray<StaticField>(num_static_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700396 uint32_t last_idx = 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700397 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700398 DexFile::Field dex_field;
399 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700400 StaticField* sfield = AllocStaticField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700401 klass->SetStaticField(i, sfield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700402 LoadField(dex_file, dex_field, klass, sfield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700403 }
404 }
405
406 // Load instance fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700407 DCHECK(klass->ifields_ == NULL);
408 if (num_instance_fields != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700409 // TODO: allocate on the object heap.
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700410 klass->ifields_ = AllocObjectArray<InstanceField>(num_instance_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700411 uint32_t last_idx = 0;
412 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700413 DexFile::Field dex_field;
414 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700415 InstanceField* ifield = AllocInstanceField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700416 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700417 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700418 }
419 }
420
421 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700422 DCHECK(klass->direct_methods_ == NULL);
423 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700424 // TODO: append direct methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700425 klass->direct_methods_ = AllocObjectArray<Method>(num_direct_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700426 uint32_t last_idx = 0;
427 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700428 DexFile::Method dex_method;
429 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700430 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700431 klass->SetDirectMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700432 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700433 // TODO: register maps
434 }
435 }
436
437 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700438 DCHECK(klass->virtual_methods_ == NULL);
439 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700440 // TODO: append virtual methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700441 klass->virtual_methods_ = AllocObjectArray<Method>(num_virtual_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700442 uint32_t last_idx = 0;
443 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700444 DexFile::Method dex_method;
445 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700446 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700447 klass->SetVirtualMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700448 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700449 // TODO: register maps
450 }
451 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700452}
453
Brian Carlstromf615a612011-07-23 12:50:34 -0700454void ClassLinker::LoadInterfaces(const DexFile& dex_file,
455 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700456 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700457 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700458 if (list != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700459 DCHECK(klass->interfaces_ == NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700460 klass->interfaces_ = AllocObjectArray<Class>(list->Size());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700461 DCHECK(klass->interfaces_idx_ == NULL);
462 klass->interfaces_idx_ = new uint32_t[list->Size()];
Brian Carlstrom934486c2011-07-12 23:42:50 -0700463 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700464 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700465 klass->interfaces_idx_[i] = type_item.type_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700466 }
467 }
468}
469
Brian Carlstromf615a612011-07-23 12:50:34 -0700470void ClassLinker::LoadField(const DexFile& dex_file,
471 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700472 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700473 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700474 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400475 dst->java_declaring_class_ = klass;
476 dst->declaring_class_ = klass;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700477 dst->name_ = ResolveString(klass, field_id.name_idx_, dex_file);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700478 dst->descriptor_.set(dex_file.dexStringByTypeIdx(field_id.type_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700479 dst->access_flags_ = src.access_flags_;
480}
481
Brian Carlstromf615a612011-07-23 12:50:34 -0700482void ClassLinker::LoadMethod(const DexFile& dex_file,
483 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700484 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700485 Method* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700486 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400487 dst->java_declaring_class_ = klass;
488 dst->declaring_class_ = klass;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700489 dst->name_ = ResolveString(klass, method_id.name_idx_, dex_file);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700490 {
491 int32_t utf16_length;
492 scoped_ptr<char> utf8(dex_file.CreateMethodDescriptor(method_id.proto_idx_,
493 &utf16_length));
494 dst->descriptor_ = String::AllocFromModifiedUtf8(utf16_length, utf8.get());
495 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700496 dst->proto_idx_ = method_id.proto_idx_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700497 dst->shorty_ = dex_file.GetShorty(method_id.proto_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700498 dst->access_flags_ = src.access_flags_;
499
500 // TODO: check for finalize method
501
Brian Carlstromf615a612011-07-23 12:50:34 -0700502 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700503 if (code_item != NULL) {
504 dst->num_registers_ = code_item->registers_size_;
505 dst->num_ins_ = code_item->ins_size_;
506 dst->num_outs_ = code_item->outs_size_;
507 dst->insns_ = code_item->insns_;
508 } else {
509 uint16_t num_args = dst->NumArgRegisters();
510 if (!dst->IsStatic()) {
511 ++num_args;
512 }
513 dst->num_registers_ = dst->num_ins_ + num_args;
514 // TODO: native methods
515 }
516}
517
Brian Carlstromf615a612011-07-23 12:50:34 -0700518void ClassLinker::AppendToBootClassPath(DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700519 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700520 boot_class_path_.push_back(dex_file);
521 RegisterDexFile(dex_file);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700522}
523
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700524void ClassLinker::RegisterDexFile(const DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700525 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700526 dex_files_.push_back(dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700527 DexCache* dex_cache = AllocDexCache();
528 CHECK(dex_cache != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700529 dex_cache->Init(AllocObjectArray<String>(dex_file->NumStringIds()),
530 AllocObjectArray<Class>(dex_file->NumTypeIds()),
531 AllocObjectArray<Method>(dex_file->NumMethodIds()),
532 AllocObjectArray<Field>(dex_file->NumFieldIds()));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700533 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700534}
535
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700536const DexFile& ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700537 for (size_t i = 0; i != dex_caches_.size(); ++i) {
538 if (dex_caches_[i] == dex_cache) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700539 return *dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700540 }
541 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700542 CHECK(false) << "Could not find DexFile";
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700543 return *dex_files_[-1];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700544}
545
Brian Carlstromf615a612011-07-23 12:50:34 -0700546DexCache* ClassLinker::FindDexCache(const DexFile* dex_file) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700547 for (size_t i = 0; i != dex_files_.size(); ++i) {
548 if (dex_files_[i] == dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700549 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700550 }
551 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700552 CHECK(false) << "Could not find DexCache";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700553 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700554}
555
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700556Class* ClassLinker::CreatePrimitiveClass(const StringPiece& descriptor) {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700557 Class* klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700558 CHECK(klass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700559 klass->super_class_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700560 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
561 klass->descriptor_ = descriptor;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700562 klass->descriptor_alloc_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700563 klass->status_ = Class::kStatusInitialized;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700564 bool success = InsertClass(klass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700565 CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed";
Carl Shapiro565f5072011-07-10 13:39:43 -0700566 return klass;
567}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700568
Brian Carlstrombe977852011-07-19 14:54:54 -0700569// Create an array class (i.e. the class object for the array, not the
570// array itself). "descriptor" looks like "[C" or "[[[[B" or
571// "[Ljava/lang/String;".
572//
573// If "descriptor" refers to an array of primitives, look up the
574// primitive type's internally-generated class object.
575//
576// "loader" is the class loader of the class that's referring to us. It's
577// used to ensure that we're looking for the element type in the right
578// context. It does NOT become the class loader for the array class; that
579// always comes from the base element class.
580//
581// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700582Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700583 ClassLoader* class_loader)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700584{
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700585 CHECK(descriptor[0] == '[');
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700586
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700587 // Identify the underlying element class and the array dimension depth.
588 Class* component_type_ = NULL;
589 int array_rank;
590 if (descriptor[1] == '[') {
591 // array of arrays; keep descriptor and grab stuff from parent
592 Class* outer = FindClass(descriptor.substr(1), class_loader);
593 if (outer != NULL) {
594 // want the base class, not "outer", in our component_type_
595 component_type_ = outer->component_type_;
596 array_rank = outer->array_rank_ + 1;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700597 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700598 DCHECK(component_type_ == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700599 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700600 } else {
601 array_rank = 1;
602 if (descriptor[1] == 'L') {
603 // array of objects; strip off "[" and look up descriptor.
604 const StringPiece subDescriptor = descriptor.substr(1);
605 component_type_ = FindClass(subDescriptor, class_loader);
606 } else {
607 // array of a primitive type
608 component_type_ = FindPrimitiveClass(descriptor[1]);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700609 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700610 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700611
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700612 if (component_type_ == NULL) {
613 // failed
614 // DCHECK(Thread::Current()->IsExceptionPending()); // TODO
615 return NULL;
616 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700617
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700618 // See if the component type is already loaded. Array classes are
619 // always associated with the class loader of their underlying
620 // element type -- an array of Strings goes with the loader for
621 // java/lang/String -- so we need to look for it there. (The
622 // caller should have checked for the existence of the class
623 // before calling here, but they did so with *their* class loader,
624 // not the component type's loader.)
625 //
626 // If we find it, the caller adds "loader" to the class' initiating
627 // loader list, which should prevent us from going through this again.
628 //
629 // This call is unnecessary if "loader" and "component_type_->class_loader_"
630 // are the same, because our caller (FindClass) just did the
631 // lookup. (Even if we get this wrong we still have correct behavior,
632 // because we effectively do this lookup again when we add the new
633 // class to the hash table --- necessary because of possible races with
634 // other threads.)
635 if (class_loader != component_type_->class_loader_) {
636 Class* new_class = LookupClass(descriptor, component_type_->class_loader_);
637 if (new_class != NULL) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700638 return new_class;
639 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700640 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700641
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700642 // Fill out the fields in the Class.
643 //
644 // It is possible to execute some methods against arrays, because
645 // all arrays are subclasses of java_lang_Object_, so we need to set
646 // up a vtable. We can just point at the one in java_lang_Object_.
647 //
648 // Array classes are simple enough that we don't need to do a full
649 // link step.
650
651 Class* new_class = NULL;
652 if (!init_done_) {
653 if (descriptor == "[Ljava/lang/Object;") {
654 new_class = GetClassRoot(kObjectArrayClass);
655 } else if (descriptor == "[C") {
656 new_class = GetClassRoot(kCharArrayClass);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400657 } else if (descriptor == "[I") {
658 new_class = GetClassRoot(kIntArrayClass);
659 } else if (descriptor == "[J") {
660 new_class = GetClassRoot(kLongArrayClass);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700661 }
662 }
663 if (new_class == NULL) {
664 new_class = AllocClass();
665 if (new_class == NULL) {
666 return NULL;
667 }
668 }
669 new_class->descriptor_alloc_ = new std::string(descriptor.data(),
670 descriptor.size());
671 new_class->descriptor_.set(new_class->descriptor_alloc_->data(),
672 new_class->descriptor_alloc_->size());
673 Class* java_lang_Object = GetClassRoot(kJavaLangObject);
674 new_class->super_class_ = java_lang_Object;
675 new_class->vtable_ = java_lang_Object->vtable_;
676 new_class->primitive_type_ = Class::kPrimNot;
677 new_class->component_type_ = component_type_;
678 new_class->class_loader_ = component_type_->class_loader_;
679 new_class->array_rank_ = array_rank;
680 new_class->status_ = Class::kStatusInitialized;
681 // don't need to set new_class->object_size_
682
683
684 // All arrays have java/lang/Cloneable and java/io/Serializable as
685 // interfaces. We need to set that up here, so that stuff like
686 // "instanceof" works right.
687 //
688 // Note: The GC could run during the call to FindSystemClass,
689 // so we need to make sure the class object is GC-valid while we're in
690 // there. Do this by clearing the interface list so the GC will just
691 // think that the entries are null.
692
693
694 // Use the single, global copies of "interfaces" and "iftable"
695 // (remember not to free them for arrays).
696 DCHECK(array_interfaces_ != NULL);
697 new_class->interfaces_ = array_interfaces_;
698 new_class->iftable_count_ = 2;
699 DCHECK(array_iftable_ != NULL);
700 new_class->iftable_ = array_iftable_;
701
702 // Inherit access flags from the component type. Arrays can't be
703 // used as a superclass or interface, so we want to add "final"
704 // and remove "interface".
705 //
706 // Don't inherit any non-standard flags (e.g., kAccFinal)
707 // from component_type_. We assume that the array class does not
708 // override finalize().
709 new_class->access_flags_ = ((new_class->component_type_->access_flags_ &
710 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask;
711
712 if (InsertClass(new_class)) {
713 return new_class;
714 }
715 // Another thread must have loaded the class after we
716 // started but before we finished. Abandon what we've
717 // done.
718 //
719 // (Yes, this happens.)
720
721 // Grab the winning class.
722 Class* other_class = LookupClass(descriptor, component_type_->class_loader_);
723 DCHECK(other_class != NULL);
724 return other_class;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700725}
726
727Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700728 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700729 case 'B':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700730 return GetClassRoot(kPrimitiveByte);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700731 case 'C':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700732 return GetClassRoot(kPrimitiveChar);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700733 case 'D':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700734 return GetClassRoot(kPrimitiveDouble);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700735 case 'F':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700736 return GetClassRoot(kPrimitiveFloat);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700737 case 'I':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700738 return GetClassRoot(kPrimitiveInt);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700739 case 'J':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700740 return GetClassRoot(kPrimitiveLong);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700741 case 'S':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700742 return GetClassRoot(kPrimitiveShort);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700743 case 'Z':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700744 return GetClassRoot(kPrimitiveBoolean);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700745 case 'V':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700746 return GetClassRoot(kPrimitiveVoid);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700747 case 'L':
748 case '[':
749 LOG(ERROR) << "Not a primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700750 default:
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700751 LOG(ERROR) << "Unknown primitive type " << static_cast<int>(type);
Carl Shapiro744ad052011-08-06 15:53:36 -0700752 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700753 return NULL; // Not reachable.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700754}
755
756bool ClassLinker::InsertClass(Class* klass) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700757 MutexLock mu(classes_lock_);
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700758 const StringPiece& key = klass->GetDescriptor();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700759 Table::iterator it = classes_.insert(std::make_pair(key, klass));
760 return ((*it).second == klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700761}
762
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700763Class* ClassLinker::LookupClass(const StringPiece& descriptor, ClassLoader* class_loader) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700764 MutexLock mu(classes_lock_);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700765 typedef Table::const_iterator It; // TODO: C++0x auto
766 for (It it = classes_.find(descriptor), end = classes_.end(); it != end; ++it) {
767 Class* klass = it->second;
768 if (klass->descriptor_ == descriptor && klass->class_loader_ == class_loader) {
769 return klass;
770 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700771 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700772 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700773}
774
775bool ClassLinker::InitializeClass(Class* klass) {
776 CHECK(klass->GetStatus() == Class::kStatusResolved ||
777 klass->GetStatus() == Class::kStatusError);
778
Carl Shapirob5573532011-07-12 18:22:59 -0700779 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700780
781 {
782 ObjectLock lock(klass);
783
784 if (klass->GetStatus() < Class::kStatusVerified) {
785 if (klass->IsErroneous()) {
786 LG << "re-initializing failed class"; // TODO: throw
787 return false;
788 }
789
790 CHECK(klass->GetStatus() == Class::kStatusResolved);
791
792 klass->status_ = Class::kStatusVerifying;
793 if (!DexVerify::VerifyClass(klass)) {
794 LG << "Verification failed"; // TODO: ThrowVerifyError
795 Object* exception = self->GetException();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700796 klass->SetVerifyErrorClass(exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700797 klass->SetStatus(Class::kStatusError);
798 return false;
799 }
800
801 klass->SetStatus(Class::kStatusVerified);
802 }
803
804 if (klass->status_ == Class::kStatusInitialized) {
805 return true;
806 }
807
808 while (klass->status_ == Class::kStatusInitializing) {
809 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -0700810 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700811 LG << "recursive <clinit>";
812 return true;
813 }
814
815 CHECK(!self->IsExceptionPending());
816
817 lock.Wait(); // TODO: check for interruption
818
819 // When we wake up, repeat the test for init-in-progress. If
820 // there's an exception pending (only possible if
821 // "interruptShouldThrow" was set), bail out.
822 if (self->IsExceptionPending()) {
823 CHECK(false);
824 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
825 klass->SetStatus(Class::kStatusError);
826 return false;
827 }
828 if (klass->GetStatus() == Class::kStatusInitializing) {
829 continue;
830 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700831 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700832 klass->GetStatus() == Class::kStatusError);
833 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700834 // The caller wants an exception, but it was thrown in a
835 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700836 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
837 return false;
838 }
839 return true; // otherwise, initialized
840 }
841
842 // see if we failed previously
843 if (klass->IsErroneous()) {
844 // might be wise to unlock before throwing; depends on which class
845 // it is that we have locked
846
847 // TODO: throwEarlierClassFailure(klass);
848 return false;
849 }
850
851 if (!ValidateSuperClassDescriptors(klass)) {
852 klass->SetStatus(Class::kStatusError);
853 return false;
854 }
855
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700856 DCHECK(klass->status_ < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700857
Carl Shapirob5573532011-07-12 18:22:59 -0700858 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700859 klass->status_ = Class::kStatusInitializing;
860 }
861
862 if (!InitializeSuperClass(klass)) {
863 return false;
864 }
865
866 InitializeStaticFields(klass);
867
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700868 Method* clinit = klass->FindDeclaredDirectMethod("<clinit>", "()V");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700869 if (clinit != NULL) {
870 } else {
871 // JValue unused;
872 // TODO: dvmCallMethod(self, method, NULL, &unused);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700873 //CHECK(!"unimplemented");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700874 }
875
876 {
877 ObjectLock lock(klass);
878
879 if (self->IsExceptionPending()) {
880 klass->SetStatus(Class::kStatusError);
881 } else {
882 klass->SetStatus(Class::kStatusInitialized);
883 }
884 lock.NotifyAll();
885 }
886
887 return true;
888}
889
890bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
891 if (klass->IsInterface()) {
892 return true;
893 }
894 // begin with the methods local to the superclass
895 if (klass->HasSuperClass() &&
896 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
897 const Class* super = klass->GetSuperClass();
898 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
899 const Method* method = klass->GetVirtualMethod(i);
900 if (method != super->GetVirtualMethod(i) &&
901 !HasSameMethodDescriptorClasses(method, super, klass)) {
902 LG << "Classes resolve differently in superclass";
903 return false;
904 }
905 }
906 }
907 for (size_t i = 0; i < klass->iftable_count_; ++i) {
908 const InterfaceEntry* iftable = &klass->iftable_[i];
909 Class* interface = iftable->GetClass();
910 if (klass->GetClassLoader() != interface->GetClassLoader()) {
911 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
912 uint32_t vtable_index = iftable->method_index_array_[j];
913 const Method* method = klass->GetVirtualMethod(vtable_index);
914 if (!HasSameMethodDescriptorClasses(method, interface,
915 method->GetClass())) {
916 LG << "Classes resolve differently in interface"; // TODO: LinkageError
917 return false;
918 }
919 }
920 }
921 }
922 return true;
923}
924
925bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700926 const Class* klass1,
927 const Class* klass2) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700928 const DexFile& dex_file = FindDexFile(method->GetClass()->GetDexCache());
929 const DexFile::ProtoId& proto_id = dex_file.GetProtoId(method->proto_idx_);
Brian Carlstromf615a612011-07-23 12:50:34 -0700930 DexFile::ParameterIterator *it;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700931 for (it = dex_file.GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700932 const char* descriptor = it->GetDescriptor();
933 if (descriptor == NULL) {
934 break;
935 }
936 if (descriptor[0] == 'L' || descriptor[0] == '[') {
937 // Found a non-primitive type.
938 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
939 return false;
940 }
941 }
942 }
943 // Check the return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700944 const char* descriptor = dex_file.GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700945 if (descriptor[0] == 'L' || descriptor[0] == '[') {
946 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
947 return false;
948 }
949 }
950 return true;
951}
952
953// Returns true if classes referenced by the descriptor are the
954// same classes in klass1 as they are in klass2.
955bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700956 const Class* klass1,
957 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700958 CHECK(descriptor != NULL);
959 CHECK(klass1 != NULL);
960 CHECK(klass2 != NULL);
961#if 0
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700962 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700963 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700964 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700965 // TODO: found2 == NULL
966 // TODO: lookup found1 in initiating loader list
967 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -0700968 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700969 if (found1 == found2) {
970 return true;
971 } else {
972 return false;
973 }
974 }
975#endif
976 return true;
977}
978
979bool ClassLinker::InitializeSuperClass(Class* klass) {
980 CHECK(klass != NULL);
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700981 MutexLock mu(classes_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700982 if (!klass->IsInterface() && klass->HasSuperClass()) {
983 Class* super_class = klass->GetSuperClass();
984 if (super_class->GetStatus() != Class::kStatusInitialized) {
985 CHECK(!super_class->IsInterface());
986 klass->MonitorExit();
987 bool super_initialized = InitializeClass(super_class);
988 klass->MonitorEnter();
989 // TODO: check for a pending exception
990 if (!super_initialized) {
991 klass->SetStatus(Class::kStatusError);
992 klass->NotifyAll();
993 return false;
994 }
995 }
996 }
997 return true;
998}
999
1000void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001001 size_t num_static_fields = klass->NumStaticFields();
1002 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001003 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001004 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001005 DexCache* dex_cache = klass->GetDexCache();
1006 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001007 return;
1008 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -07001009 const StringPiece& descriptor = klass->GetDescriptor();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001010 const DexFile& dex_file = FindDexFile(dex_cache);
1011 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Brian Carlstromf615a612011-07-23 12:50:34 -07001012 CHECK(dex_class_def != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001013 const byte* addr = dex_file.GetEncodedArray(*dex_class_def);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001014 size_t array_size = DecodeUnsignedLeb128(&addr);
1015 for (size_t i = 0; i < array_size; ++i) {
1016 StaticField* field = klass->GetStaticField(i);
1017 JValue value;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001018 DexFile::ValueType type = dex_file.ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001019 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001020 case DexFile::kByte:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001021 field->SetByte(value.b);
1022 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001023 case DexFile::kShort:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001024 field->SetShort(value.s);
1025 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001026 case DexFile::kChar:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001027 field->SetChar(value.c);
1028 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001029 case DexFile::kInt:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001030 field->SetInt(value.i);
1031 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001032 case DexFile::kLong:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001033 field->SetLong(value.j);
1034 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001035 case DexFile::kFloat:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001036 field->SetFloat(value.f);
1037 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001038 case DexFile::kDouble:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001039 field->SetDouble(value.d);
1040 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001041 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001042 uint32_t string_idx = value.i;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001043 String* resolved = ResolveString(klass, string_idx, dex_file);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001044 field->SetObject(resolved);
1045 break;
1046 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001047 case DexFile::kBoolean:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001048 field->SetBoolean(value.z);
1049 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001050 case DexFile::kNull:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001051 field->SetObject(value.l);
1052 break;
1053 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001054 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001055 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001056 }
1057}
1058
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001059bool ClassLinker::LinkClass(Class* klass, const DexFile& dex_file) {
1060 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001061 if (!LinkSuperClass(klass)) {
1062 return false;
1063 }
1064 if (!LinkMethods(klass)) {
1065 return false;
1066 }
Jesse Wilson7833bd22011-08-09 18:31:44 -04001067 if (!LinkStaticFields(klass)) {
1068 return false;
1069 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001070 if (!LinkInstanceFields(klass)) {
1071 return false;
1072 }
1073 CreateReferenceOffsets(klass);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001074 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001075 klass->status_ = Class::kStatusResolved;
1076 return true;
1077}
1078
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001079bool ClassLinker::LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file) {
1080 CHECK_EQ(Class::kStatusIdx, klass->status_);
Brian Carlstromf615a612011-07-23 12:50:34 -07001081 if (klass->super_class_idx_ != DexFile::kDexNoIndex) {
1082 Class* super_class = ResolveClass(klass, klass->super_class_idx_, dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001083 if (super_class == NULL) {
1084 LG << "Failed to resolve superclass";
1085 return false;
1086 }
1087 klass->super_class_ = super_class; // TODO: write barrier
1088 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001089 if (klass->NumInterfaces() > 0) {
1090 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
1091 uint32_t idx = klass->interfaces_idx_[i];
1092 klass->SetInterface(i, ResolveClass(klass, idx, dex_file));
1093 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001094 LG << "Failed to resolve interface";
1095 return false;
1096 }
1097 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001098 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001099 LG << "Inaccessible interface";
1100 return false;
1101 }
1102 }
1103 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001104 // Mark the class as loaded.
1105 klass->status_ = Class::kStatusLoaded;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001106 return true;
1107}
1108
1109bool ClassLinker::LinkSuperClass(Class* klass) {
1110 CHECK(!klass->IsPrimitive());
1111 const Class* super = klass->GetSuperClass();
1112 if (klass->GetDescriptor() == "Ljava/lang/Object;") {
1113 if (super != NULL) {
1114 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1115 return false;
1116 }
1117 // TODO: clear finalize attribute
1118 return true;
1119 }
1120 if (super == NULL) {
1121 LG << "No superclass defined"; // TODO: LinkageError
1122 return false;
1123 }
1124 // Verify
1125 if (super->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001126 LG << "Superclass " << super->descriptor_ << " is declared final"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001127 return false;
1128 }
1129 if (super->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001130 LG << "Superclass " << super->descriptor_ << " is an interface"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001131 return false;
1132 }
1133 if (!klass->CanAccess(super)) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001134 LG << "Superclass " << super->descriptor_ << " is inaccessible"; // TODO: IllegalAccessError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001135 return false;
1136 }
1137 return true;
1138}
1139
1140// Populate the class vtable and itable.
1141bool ClassLinker::LinkMethods(Class* klass) {
1142 if (klass->IsInterface()) {
1143 // No vtable.
1144 size_t count = klass->NumVirtualMethods();
1145 if (!IsUint(16, count)) {
1146 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1147 return false;
1148 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001149 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001150 klass->GetVirtualMethod(i)->method_index_ = i;
1151 }
1152 } else {
1153 // Link virtual method tables
1154 LinkVirtualMethods(klass);
1155
1156 // Link interface method tables
1157 LinkInterfaceMethods(klass);
1158
1159 // Insert stubs.
1160 LinkAbstractMethods(klass);
1161 }
1162 return true;
1163}
1164
1165bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001166 if (klass->HasSuperClass()) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001167 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001168 size_t actual_count = klass->GetSuperClass()->vtable_->GetLength();
1169 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001170 // TODO: do not assign to the vtable field until it is fully constructed.
1171 klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001172 // See if any of our virtual methods override the superclass.
1173 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1174 Method* local_method = klass->GetVirtualMethod(i);
1175 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001176 for (; j < actual_count; ++j) {
1177 Method* super_method = klass->vtable_->Get(j);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001178 if (local_method->HasSameNameAndDescriptor(super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001179 // Verify
1180 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001181 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001182 return false;
1183 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001184 klass->vtable_->Set(j, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001185 local_method->method_index_ = j;
1186 break;
1187 }
1188 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001189 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001190 // Not overriding, append.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001191 klass->vtable_->Set(actual_count, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001192 local_method->method_index_ = actual_count;
1193 actual_count += 1;
1194 }
1195 }
1196 if (!IsUint(16, actual_count)) {
1197 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1198 return false;
1199 }
1200 CHECK_LE(actual_count, max_count);
1201 if (actual_count < max_count) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001202 // TODO: do not assign to the vtable field until it is fully constructed.
1203 klass->vtable_ = klass->vtable_->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001204 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001205 } else {
1206 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001207 uint32_t num_virtual_methods = klass->NumVirtualMethods();
1208 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
1209 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001210 LG << "Too many methods"; // TODO: VirtualMachineError
1211 return false;
1212 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001213 // TODO: do not assign to the vtable field until it is fully constructed.
1214 klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods);
1215 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001216 klass->vtable_->Set(i, klass->GetVirtualMethod(i));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001217 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1218 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001219 }
1220 return true;
1221}
1222
1223bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1224 int pool_offset = 0;
1225 int pool_size = 0;
1226 int miranda_count = 0;
1227 int miranda_alloc = 0;
1228 size_t super_ifcount;
1229 if (klass->HasSuperClass()) {
1230 super_ifcount = klass->GetSuperClass()->iftable_count_;
1231 } else {
1232 super_ifcount = 0;
1233 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001234 size_t ifcount = super_ifcount;
1235 ifcount += klass->NumInterfaces();
1236 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1237 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001238 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001239 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001240 DCHECK(klass->iftable_count_ == 0);
1241 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001242 return true;
1243 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001244 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1245 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001246 if (super_ifcount != 0) {
1247 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1248 sizeof(InterfaceEntry) * super_ifcount);
1249 }
1250 // Flatten the interface inheritance hierarchy.
1251 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001252 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1253 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001254 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001255 if (!interf->IsInterface()) {
1256 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1257 return false;
1258 }
1259 klass->iftable_[idx++].SetClass(interf);
1260 for (size_t j = 0; j < interf->iftable_count_; j++) {
1261 klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
1262 }
1263 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001264 CHECK_EQ(idx, ifcount);
1265 klass->iftable_count_ = ifcount;
1266 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001267 return true;
1268 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001269 for (size_t i = super_ifcount; i < ifcount; i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001270 pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
1271 }
1272 if (pool_size == 0) {
1273 return true;
1274 }
1275 klass->ifvi_pool_count_ = pool_size;
1276 klass->ifvi_pool_ = new uint32_t[pool_size];
1277 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001278 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001279 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
1280 Class* interface = klass->iftable_[i].GetClass();
1281 pool_offset += interface->NumVirtualMethods(); // end here
1282 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1283 Method* interface_method = interface->GetVirtualMethod(j);
1284 int k; // must be signed
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001285 for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001286 Method* vtable_method = klass->vtable_->Get(k);
1287 if (interface_method->HasSameNameAndDescriptor(vtable_method)) {
1288 if (!vtable_method->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001289 LG << "Implementation not public";
1290 return false;
1291 }
1292 klass->iftable_[i].method_index_array_[j] = k;
1293 break;
1294 }
1295 }
1296 if (k < 0) {
1297 if (miranda_count == miranda_alloc) {
1298 miranda_alloc += 8;
1299 if (miranda_list.empty()) {
1300 miranda_list.resize(miranda_alloc);
1301 } else {
1302 miranda_list.resize(miranda_alloc);
1303 }
1304 }
1305 int mir;
1306 for (mir = 0; mir < miranda_count; mir++) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001307 Method* miranda_method = miranda_list[mir];
1308 if (miranda_method->HasSameNameAndDescriptor(interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001309 break;
1310 }
1311 }
1312 // point the interface table at a phantom slot index
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001313 klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001314 if (mir == miranda_count) {
1315 miranda_list[miranda_count++] = interface_method;
1316 }
1317 }
1318 }
1319 }
1320 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001321 int old_method_count = klass->NumVirtualMethods();
1322 int new_method_count = old_method_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001323 klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001324
1325 CHECK(klass->vtable_ != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001326 int old_vtable_count = klass->vtable_->GetLength();
1327 int new_vtable_count = old_vtable_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001328 // TODO: do not assign to the vtable field until it is fully constructed.
1329 klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001330
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001331 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001332 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001333 memcpy(meth, miranda_list[i], sizeof(Method));
1334 meth->klass_ = klass;
1335 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001336 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1337 klass->SetVirtualMethod(old_method_count + i, meth);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001338 klass->vtable_->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001339 }
1340 }
1341 return true;
1342}
1343
1344void ClassLinker::LinkAbstractMethods(Class* klass) {
1345 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1346 Method* method = klass->GetVirtualMethod(i);
1347 if (method->IsAbstract()) {
1348 method->insns_ = reinterpret_cast<uint16_t*>(0xFFFFFFFF); // TODO: AbstractMethodError
1349 }
1350 }
1351}
1352
Jesse Wilson7833bd22011-08-09 18:31:44 -04001353// Each static field will be stored in one of three arrays: static_references_,
1354// static_32bit_primitives_, or static_64bit_primitives_. This assigns each
1355// field a slot in its array and create the arrays.
1356bool ClassLinker::LinkStaticFields(Class* klass) {
1357 size_t next_reference_slot = 0;
1358 size_t next_32bit_primitive_slot = 0;
1359 size_t next_64bit_primitive_slot = 0;
1360
1361 for (size_t i = 0; i < klass->NumStaticFields(); i++) {
1362 StaticField* field = klass->GetStaticField(i);
1363 char type = field->GetType();
1364 if (type == '[' || type == 'L') {
1365 field->java_slot_ = next_reference_slot++;
1366 } else if (type == 'J' || type == 'D') {
1367 field->java_slot_ = next_64bit_primitive_slot++;
1368 } else {
1369 field->java_slot_ = next_32bit_primitive_slot++;
1370 }
1371 }
1372
1373 if (next_reference_slot > 0) {
1374 Class* array_class = GetClassRoot(kObjectArrayClass);
1375 klass->static_references_ = ObjectArray<Object>::Alloc(array_class, next_reference_slot);
1376 }
1377 if (next_32bit_primitive_slot > 0) {
1378 Class* array_class = GetClassRoot(kIntArrayClass);
1379 klass->static_32bit_primitives_ = IntArray::Alloc(array_class, next_32bit_primitive_slot);
1380 }
1381 if (next_64bit_primitive_slot > 0) {
1382 Class* array_class = GetClassRoot(kLongArrayClass);
1383 klass->static_64bit_primitives_ = LongArray::Alloc(array_class, next_64bit_primitive_slot);
1384 }
1385
1386 return true;
1387}
1388
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001389bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001390 int field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001391 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001392 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001393 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001394 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001395 }
1396 // Move references to the front.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001397 klass->num_reference_instance_fields_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001398 size_t i = 0;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001399 for ( ; i < klass->NumInstanceFields(); i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001400 InstanceField* pField = klass->GetInstanceField(i);
1401 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001402 if (c != '[' && c != 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001403 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1404 InstanceField* refField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001405 char rc = refField->GetType();
1406 if (rc == '[' || rc == 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001407 klass->SetInstanceField(i, refField);
1408 klass->SetInstanceField(j, pField);
1409 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001410 c = rc;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001411 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001412 break;
1413 }
1414 }
1415 } else {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001416 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001417 }
1418 if (c != '[' && c != 'L') {
1419 break;
1420 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001421 pField->SetOffset(field_offset);
1422 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001423 }
1424
1425 // Now we want to pack all of the double-wide fields together. If
1426 // we're not aligned, though, we want to shuffle one 32-bit field
1427 // into place. If we can't find one, we'll have to pad it.
Brian Carlstroma0808032011-07-18 00:39:23 -07001428 if (i != klass->NumInstanceFields() && (field_offset & 0x04) != 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001429 InstanceField* pField = klass->GetInstanceField(i);
1430 char c = pField->GetType();
1431
1432 if (c != 'J' && c != 'D') {
1433 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001434 DCHECK(c != '[');
1435 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001436 pField->SetOffset(field_offset);
1437 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001438 i++;
1439 } else {
1440 // Next field is 64-bit, so search for a 32-bit field we can
1441 // swap into it.
1442 bool found = false;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001443 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1444 InstanceField* singleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001445 char rc = singleField->GetType();
1446 if (rc != 'J' && rc != 'D') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001447 klass->SetInstanceField(i, singleField);
1448 klass->SetInstanceField(j, pField);
1449 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001450 pField->SetOffset(field_offset);
1451 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001452 found = true;
1453 i++;
1454 break;
1455 }
1456 }
1457 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001458 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001459 }
1460 }
1461 }
1462
1463 // Alignment is good, shuffle any double-wide fields forward, and
1464 // finish assigning field offsets to all fields.
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001465 DCHECK(i == klass->NumInstanceFields() || (field_offset & 0x04) == 0);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001466 for ( ; i < klass->NumInstanceFields(); i++) {
1467 InstanceField* pField = klass->GetInstanceField(i);
1468 char c = pField->GetType();
1469 if (c != 'D' && c != 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001470 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1471 InstanceField* doubleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001472 char rc = doubleField->GetType();
1473 if (rc == 'D' || rc == 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001474 klass->SetInstanceField(i, doubleField);
1475 klass->SetInstanceField(j, pField);
1476 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001477 c = rc;
1478 break;
1479 }
1480 }
1481 } else {
1482 // This is a double-wide field, leave it be.
1483 }
1484
Brian Carlstroma0808032011-07-18 00:39:23 -07001485 pField->SetOffset(field_offset);
1486 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001487 if (c == 'J' || c == 'D')
Brian Carlstroma0808032011-07-18 00:39:23 -07001488 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001489 }
1490
1491#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001492 // Make sure that all reference fields appear before
1493 // non-reference fields, and all double-wide fields are aligned.
1494 bool seen_non_ref = false;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001495 for (i = 0; i < klass->NumInstanceFields(); i++) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001496 InstanceField *pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001497 char c = pField->GetType();
1498
1499 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001500 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001501 }
1502
1503 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001504 if (!seen_non_ref) {
1505 seen_non_ref = true;
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001506 DCHECK_EQ(klass->NumReferenceInstanceFields(), i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001507 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001508 } else {
1509 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001510 }
1511 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001512 if (!seen_non_ref) {
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001513 DCHECK_EQ(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001514 }
1515#endif
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001516 klass->object_size_ = field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001517 return true;
1518}
1519
1520// Set the bitmap of reference offsets, refOffsets, from the ifields
1521// list.
1522void ClassLinker::CreateReferenceOffsets(Class* klass) {
1523 uint32_t reference_offsets = 0;
1524 if (klass->HasSuperClass()) {
1525 reference_offsets = klass->GetSuperClass()->GetReferenceOffsets();
1526 }
1527 // If our superclass overflowed, we don't stand a chance.
1528 if (reference_offsets != CLASS_WALK_SUPER) {
1529 // All of the fields that contain object references are guaranteed
1530 // to be at the beginning of the ifields list.
1531 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) {
1532 // Note that, per the comment on struct InstField, f->byteOffset
1533 // is the offset from the beginning of obj, not the offset into
1534 // obj->instanceData.
1535 const InstanceField* field = klass->GetInstanceField(i);
1536 size_t byte_offset = field->GetOffset();
1537 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001538 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001539 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1540 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001541 CHECK_NE(new_bit, 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001542 reference_offsets |= new_bit;
1543 } else {
1544 reference_offsets = CLASS_WALK_SUPER;
1545 break;
1546 }
1547 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001548 }
Brian Carlstromae3ac012011-07-27 01:30:28 -07001549 klass->SetReferenceOffsets(reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001550}
1551
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001552Class* ClassLinker::ResolveClass(const Class* referrer,
1553 uint32_t class_idx,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001554 const DexFile& dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001555 DexCache* dex_cache = referrer->GetDexCache();
1556 Class* resolved = dex_cache->GetResolvedClass(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001557 if (resolved != NULL) {
1558 return resolved;
1559 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001560 const char* descriptor = dex_file.dexStringByTypeIdx(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001561 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001562 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001563 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001564 resolved = FindClass(descriptor, referrer->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001565 }
1566 if (resolved != NULL) {
1567 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001568 if (referrer->GetDexCache() != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001569 if (check->GetClassLoader() != NULL) {
1570 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1571 return NULL;
1572 }
1573 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001574 dex_cache->SetResolvedClass(class_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001575 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001576 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001577 }
1578 return resolved;
1579}
1580
1581Method* ResolveMethod(const Class* referrer, uint32_t method_idx,
1582 /*MethodType*/ int method_type) {
1583 CHECK(false);
1584 return NULL;
1585}
1586
Carl Shapiro69759ea2011-07-21 18:13:35 -07001587String* ClassLinker::ResolveString(const Class* referring,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001588 uint32_t string_idx,
1589 const DexFile& dex_file) {
1590 const DexFile::StringId& string_id = dex_file.GetStringId(string_idx);
1591 int32_t utf16_length = dex_file.GetStringLength(string_id);
1592 const char* utf8_data = dex_file.GetStringData(string_id);
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001593 String* string = intern_table_.Intern(utf16_length, utf8_data);
1594 referring->GetDexCache()->SetResolvedString(string_idx, string);
1595 return string;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001596}
1597
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001598} // namespace art