blob: 0d7c1dce0aaa491f6d972b8d23a722fce74a2df3 [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
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070061 // Field and Method are necessary so that FindClass can link members
62 Class* java_lang_reflect_Field = AllocClass(java_lang_Class);
63 CHECK(java_lang_reflect_Field != NULL);
64 java_lang_reflect_Field->descriptor_ = "Ljava/lang/reflect/Field;";
65 CHECK_LT(java_lang_reflect_Field->object_size_, std::max(sizeof(StaticField), sizeof(InstanceField)));
66 java_lang_reflect_Field->object_size_ = std::max(sizeof(StaticField), sizeof(InstanceField));
67 Class* java_lang_reflect_Method = AllocClass(java_lang_Class);
68 java_lang_reflect_Method->descriptor_ = "Ljava/lang/reflect/Method;";
69 CHECK(java_lang_reflect_Method != NULL);
70 CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method));
71 java_lang_reflect_Method->object_size_ = sizeof(Method);
72
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070073 // create storage for root classes, save away our work so far
74 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
75 class_roots_->Set(kJavaLangClass, java_lang_Class);
76 class_roots_->Set(kJavaLangObject, java_lang_Object);
77 class_roots_->Set(kObjectArrayClass, object_array_class);
Jesse Wilson14150742011-07-29 19:04:44 -040078 class_roots_->Set(kJavaLangString, java_lang_String);
Jesse Wilson8989d992011-08-02 13:39:42 -070079 class_roots_->Set(kCharArrayClass, char_array_class);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070080 class_roots_->Set(kJavaLangReflectField, java_lang_reflect_Field);
81 class_roots_->Set(kJavaLangReflectMethod, java_lang_reflect_Method);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070082 // now that these are registered, we can use AllocClass() and AllocObjectArray
Brian Carlstroma0808032011-07-18 00:39:23 -070083
Jesse Wilson8989d992011-08-02 13:39:42 -070084 String::InitClasses(java_lang_String, char_array_class);
85 // Now AllocString* can be used
86
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070087 // setup boot_class_path_ now that we can use AllocObjectArray to
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070088 // create DexCache instances
Brian Carlstrom913af1b2011-07-23 21:41:13 -070089 for (size_t i = 0; i != boot_class_path.size(); ++i) {
90 AppendToBootClassPath(boot_class_path[i]);
91 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070092 // now we can use FindSystemClass, at least for non-arrays classes.
Brian Carlstrom913af1b2011-07-23 21:41:13 -070093
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070094 // run Class, Field, and Method through FindSystemClass.
95 // this initializes their dex_cache_ fields and register them in classes_.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070096 // we also override their object_size_ values to accommodate the extra C++ fields.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070097 Class* Class_class = FindSystemClass(java_lang_Class->GetDescriptor());
98 CHECK_EQ(java_lang_Class, Class_class);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070099 CHECK_LT(java_lang_Class->object_size_, sizeof(Class));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700100 java_lang_Class->object_size_ = sizeof(Class);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700101 Class* Field_class = FindSystemClass(java_lang_reflect_Field->GetDescriptor());
102 CHECK_EQ(java_lang_reflect_Field, Field_class);
103 CHECK_LT(java_lang_reflect_Field->object_size_, std::max(sizeof(StaticField), sizeof(InstanceField)));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700104 java_lang_reflect_Field->object_size_ = std::max(sizeof(StaticField), sizeof(InstanceField));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700105 Class* Method_class = FindSystemClass(java_lang_reflect_Method->GetDescriptor());
106 CHECK_EQ(java_lang_reflect_Method, Method_class);
107 CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700108 java_lang_reflect_Method->object_size_ = sizeof(Method);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700109
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700110 // Object and String just need more minimal setup, since they do not have extra C++ fields.
111 Class* Object_class = FindSystemClass(java_lang_Object->GetDescriptor());
112 CHECK_EQ(java_lang_Object, Object_class);
113 CHECK_EQ(java_lang_Object->object_size_, sizeof(Object));
114 Class* String_class = FindSystemClass(java_lang_String->GetDescriptor());
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700115 CHECK_EQ(java_lang_String, String_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700116 CHECK_EQ(java_lang_String->object_size_, sizeof(String));
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700117
118 // Setup the ClassLoaders, adjusting the object_size_ as necessary
119 Class* java_lang_ClassLoader = FindSystemClass("Ljava/lang/ClassLoader;");
120 CHECK(java_lang_ClassLoader != NULL);
121 CHECK_LT(java_lang_ClassLoader->object_size_, sizeof(ClassLoader));
122 java_lang_ClassLoader->object_size_ = sizeof(ClassLoader);
123 class_roots_->Set(kJavaLangClassLoader, java_lang_ClassLoader);
124 Class* dalvik_system_BaseDexClassLoader = FindSystemClass("Ldalvik/system/BaseDexClassLoader;");
125 CHECK(dalvik_system_BaseDexClassLoader != NULL);
126 CHECK_EQ(dalvik_system_BaseDexClassLoader->object_size_, sizeof(BaseDexClassLoader));
127 class_roots_->Set(kDalvikSystemBaseDexClassLoader, dalvik_system_BaseDexClassLoader);
128 Class* dalvik_system_PathClassLoader = FindSystemClass("Ldalvik/system/PathClassLoader;");
129 CHECK(dalvik_system_PathClassLoader != NULL);
130 CHECK_EQ(dalvik_system_PathClassLoader->object_size_, sizeof(PathClassLoader));
131 class_roots_->Set(kDalvikSystemPathClassLoader, dalvik_system_PathClassLoader);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700132
133 // Setup a single, global copy of "interfaces" and "iftable" for
134 // reuse across array classes
135 Class* java_lang_Cloneable = AllocClass();
136 CHECK(java_lang_Cloneable != NULL);
137 java_lang_Cloneable->descriptor_ = "Ljava/lang/Cloneable;";
138
139 Class* java_io_Serializable = AllocClass();
140 CHECK(java_io_Serializable != NULL);
141 java_io_Serializable->descriptor_ = "Ljava/io/Serializable;";
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700142
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700143 array_interfaces_ = AllocObjectArray<Class>(2);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700144 CHECK(array_interfaces_ != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700145 array_interfaces_->Set(0, java_lang_Cloneable);
146 array_interfaces_->Set(1, java_io_Serializable);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700147
148 // We assume that Cloneable/Serializable don't have superinterfaces --
149 // normally we'd have to crawl up and explicitly list all of the
150 // supers as well. These interfaces don't have any methods, so we
151 // don't have to worry about the ifviPool either.
152 array_iftable_ = new InterfaceEntry[2];
153 CHECK(array_iftable_ != NULL);
154 memset(array_iftable_, 0, sizeof(InterfaceEntry) * 2);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700155 array_iftable_[0].SetClass(array_interfaces_->Get(0));
156 array_iftable_[1].SetClass(array_interfaces_->Get(1));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700157 // now FindClass can be used for non-primitive array classes
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700158
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700159 // run Object[] through FindClass to complete initialization
Jesse Wilson8989d992011-08-02 13:39:42 -0700160 Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;");
161 CHECK_EQ(object_array_class, found_object_array_class);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700162
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700163 // Setup the primitive type classes.
164 class_roots_->Set(kPrimitiveByte, CreatePrimitiveClass("B"));
165 class_roots_->Set(kPrimitiveChar, CreatePrimitiveClass("C"));
166 class_roots_->Set(kPrimitiveDouble, CreatePrimitiveClass("D"));
167 class_roots_->Set(kPrimitiveFloat, CreatePrimitiveClass("F"));
168 class_roots_->Set(kPrimitiveInt, CreatePrimitiveClass("I"));
169 class_roots_->Set(kPrimitiveLong, CreatePrimitiveClass("J"));
170 class_roots_->Set(kPrimitiveShort, CreatePrimitiveClass("S"));
171 class_roots_->Set(kPrimitiveBoolean, CreatePrimitiveClass("Z"));
172 class_roots_->Set(kPrimitiveVoid, CreatePrimitiveClass("V"));
173 // now we can use FindSystemClass for anything, including for "[C"
174
Jesse Wilson8989d992011-08-02 13:39:42 -0700175 // run char[] through FindClass to complete initialization
176 Class* found_char_array_class = FindSystemClass("[C");
177 CHECK_EQ(char_array_class, found_char_array_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700178
179 // ensure all class_roots_ were initialized
180 for (size_t i = 0; i < kClassRootsMax; i++) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700181 CHECK(GetClassRoot(static_cast<ClassRoot>(i)));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700182 }
183
184 // disable the slow paths in FindClass and CreatePrimitiveClass now
185 // that Object, Class, and Object[] are setup
186 init_done_ = true;
187}
188
Brian Carlstromb88e9442011-07-28 15:15:51 -0700189void ClassLinker::VisitRoots(RootVistor* root_visitor, void* arg) {
190 root_visitor(class_roots_, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700191
192 for (size_t i = 0; i < dex_caches_.size(); i++) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700193 root_visitor(dex_caches_[i], arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700194 }
195
196 // TODO: acquire classes_lock_
197 typedef Table::const_iterator It; // TODO: C++0x auto
198 for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700199 root_visitor(it->second, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700200 }
201 // TODO: release classes_lock_
202
Brian Carlstromb88e9442011-07-28 15:15:51 -0700203 root_visitor(array_interfaces_, arg);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700204}
205
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700206DexCache* ClassLinker::AllocDexCache() {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700207 return down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::kMax));
Brian Carlstroma0808032011-07-18 00:39:23 -0700208}
209
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700210Class* ClassLinker::AllocClass(Class* java_lang_Class) {
211 return down_cast<Class*>(Object::Alloc(java_lang_Class));
212}
213
214Class* ClassLinker::AllocClass() {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700215 return AllocClass(GetClassRoot(kJavaLangClass));
Brian Carlstroma0808032011-07-18 00:39:23 -0700216}
217
218StaticField* ClassLinker::AllocStaticField() {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700219 return down_cast<StaticField*>(Object::Alloc(GetClassRoot(kJavaLangReflectField)));
Brian Carlstroma0808032011-07-18 00:39:23 -0700220}
221
222InstanceField* ClassLinker::AllocInstanceField() {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700223 return down_cast<InstanceField*>(Object::Alloc(GetClassRoot(kJavaLangReflectField)));
Brian Carlstroma0808032011-07-18 00:39:23 -0700224}
225
226Method* ClassLinker::AllocMethod() {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700227 return down_cast<Method*>(Object::Alloc(GetClassRoot(kJavaLangReflectMethod)));
228}
229
230// TODO remove once we can use java.lang.Class.getSystemClassLoader
231PathClassLoader* ClassLinker::AllocPathClassLoader(std::vector<const DexFile*> dex_files) {
232 PathClassLoader* cl = down_cast<PathClassLoader*>(Object::Alloc(GetClassRoot(kDalvikSystemPathClassLoader)));
233 cl->SetClassPath(dex_files);
234 return cl;
Carl Shapiro565f5072011-07-10 13:39:43 -0700235}
236
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700237Class* ClassLinker::FindClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700238 ClassLoader* class_loader) {
239 // TODO remove this contrived parent class loader check when we have a real ClassLoader.
240 if (class_loader != NULL) {
241 Class* klass = FindClass(descriptor, NULL);
242 if (klass != NULL) {
243 return klass;
244 }
245 }
246
Carl Shapirob5573532011-07-12 18:22:59 -0700247 Thread* self = Thread::Current();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700248 DCHECK(self != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700249 CHECK(!self->IsExceptionPending());
250 // Find the class in the loaded classes table.
251 Class* klass = LookupClass(descriptor, class_loader);
252 if (klass == NULL) {
253 // Class is not yet loaded.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700254 if (descriptor[0] == '[') {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700255 return CreateArrayClass(descriptor, class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700256 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700257 DexFile::ClassPath& class_path = ((class_loader != NULL) ? class_loader->GetClassPath() : boot_class_path_);
258 DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, class_path);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700259 if (pair.second == NULL) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700260 LG << "Class " << descriptor << " not found in class loader " << class_loader; // TODO: NoClassDefFoundError
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700261 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700262 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700263 const DexFile& dex_file = *pair.first;
264 const DexFile::ClassDef& dex_class_def = *pair.second;
265 DexCache* dex_cache = FindDexCache(pair.first);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700266 // Load the class from the dex file.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700267 if (!init_done_) {
268 // finish up init of hand crafted class_roots_
269 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700270 klass = GetClassRoot(kJavaLangObject);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700271 } else if (descriptor == "Ljava/lang/Class;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700272 klass = GetClassRoot(kJavaLangClass);
Jesse Wilson14150742011-07-29 19:04:44 -0400273 } else if (descriptor == "Ljava/lang/String;") {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700274 klass = GetClassRoot(kJavaLangString);
275 } else if (descriptor == "Ljava/lang/reflect/Field;") {
276 klass = GetClassRoot(kJavaLangReflectField);
277 } else if (descriptor == "Ljava/lang/reflect/Method;") {
278 klass = GetClassRoot(kJavaLangReflectMethod);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700279 } else {
280 klass = AllocClass();
281 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700282 } else {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700283 klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700284 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700285 klass->dex_cache_ = dex_cache;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700286 LoadClass(dex_file, dex_class_def, klass, class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700287 // Check for a pending exception during load
288 if (self->IsExceptionPending()) {
289 // TODO: free native allocations in klass
290 return NULL;
291 }
292 {
293 ObjectLock lock(klass);
Carl Shapirob5573532011-07-12 18:22:59 -0700294 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700295 // Add the newly loaded class to the loaded classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700296 bool success = InsertClass(klass); // TODO just return collision
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700297 if (!success) {
298 // We may fail to insert if we raced with another thread.
299 klass->clinit_thread_id_ = 0;
300 // TODO: free native allocations in klass
301 klass = LookupClass(descriptor, class_loader);
302 CHECK(klass != NULL);
303 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700304 // Finish loading (if necessary) by finding parents
305 if (!klass->IsLoaded() && !LoadSuperAndInterfaces(klass, dex_file)) {
306 // Loading failed.
307 // TODO: CHECK(self->IsExceptionPending());
308 lock.NotifyAll();
309 return NULL;
310 }
311 CHECK(klass->IsLoaded());
312 // Link the class (if necessary)
313 if (!klass->IsLinked() && !LinkClass(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700314 // Linking failed.
315 // TODO: CHECK(self->IsExceptionPending());
316 lock.NotifyAll();
317 return NULL;
318 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700319 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700320 }
321 }
322 }
323 // Link the class if it has not already been linked.
324 if (!klass->IsLinked() && !klass->IsErroneous()) {
325 ObjectLock lock(klass);
326 // Check for circular dependencies between classes.
Carl Shapirob5573532011-07-12 18:22:59 -0700327 if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700328 LG << "Recursive link"; // TODO: ClassCircularityError
329 return NULL;
330 }
331 // Wait for the pending initialization to complete.
332 while (!klass->IsLinked() && !klass->IsErroneous()) {
333 lock.Wait();
334 }
335 }
336 if (klass->IsErroneous()) {
337 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
338 return NULL;
339 }
340 // Return the loaded class. No exceptions should be pending.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700341 CHECK(klass->IsLinked());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700342 CHECK(!self->IsExceptionPending());
343 return klass;
344}
345
Brian Carlstromf615a612011-07-23 12:50:34 -0700346void ClassLinker::LoadClass(const DexFile& dex_file,
347 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700348 Class* klass,
349 ClassLoader* class_loader) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700350 CHECK(klass != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700351 CHECK(klass->dex_cache_ != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700352 CHECK_EQ(Class::kStatusNotReady, klass->status_);
Brian Carlstromf615a612011-07-23 12:50:34 -0700353 const byte* class_data = dex_file.GetClassData(dex_class_def);
354 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700355
Brian Carlstromf615a612011-07-23 12:50:34 -0700356 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700357 CHECK(descriptor != NULL);
358
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700359 klass->klass_ = GetClassRoot(kJavaLangClass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700360 klass->descriptor_.set(descriptor);
361 klass->descriptor_alloc_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700362 klass->access_flags_ = dex_class_def.access_flags_;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700363 klass->class_loader_ = class_loader;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700364 klass->primitive_type_ = Class::kPrimNot;
365 klass->status_ = Class::kStatusIdx;
366
367 klass->super_class_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700368 klass->super_class_idx_ = dex_class_def.superclass_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700369
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700370 size_t num_static_fields = header.static_fields_size_;
371 size_t num_instance_fields = header.instance_fields_size_;
372 size_t num_direct_methods = header.direct_methods_size_;
373 size_t num_virtual_methods = header.virtual_methods_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700374
Brian Carlstromf615a612011-07-23 12:50:34 -0700375 klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700376
377 // Load class interfaces.
Brian Carlstromf615a612011-07-23 12:50:34 -0700378 LoadInterfaces(dex_file, dex_class_def, klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700379
380 // Load static fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700381 DCHECK(klass->sfields_ == NULL);
382 if (num_static_fields != 0) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700383 klass->sfields_ = AllocObjectArray<StaticField>(num_static_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700384 uint32_t last_idx = 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700385 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700386 DexFile::Field dex_field;
387 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700388 StaticField* sfield = AllocStaticField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700389 klass->SetStaticField(i, sfield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700390 LoadField(dex_file, dex_field, klass, sfield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700391 }
392 }
393
394 // Load instance fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700395 DCHECK(klass->ifields_ == NULL);
396 if (num_instance_fields != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700397 // TODO: allocate on the object heap.
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700398 klass->ifields_ = AllocObjectArray<InstanceField>(num_instance_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700399 uint32_t last_idx = 0;
400 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700401 DexFile::Field dex_field;
402 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700403 InstanceField* ifield = AllocInstanceField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700404 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700405 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700406 }
407 }
408
409 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700410 DCHECK(klass->direct_methods_ == NULL);
411 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700412 // TODO: append direct methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700413 klass->direct_methods_ = AllocObjectArray<Method>(num_direct_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700414 uint32_t last_idx = 0;
415 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700416 DexFile::Method dex_method;
417 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700418 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700419 klass->SetDirectMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700420 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700421 // TODO: register maps
422 }
423 }
424
425 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700426 DCHECK(klass->virtual_methods_ == NULL);
427 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700428 // TODO: append virtual methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700429 klass->virtual_methods_ = AllocObjectArray<Method>(num_virtual_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700430 uint32_t last_idx = 0;
431 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700432 DexFile::Method dex_method;
433 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700434 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700435 klass->SetVirtualMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700436 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700437 // TODO: register maps
438 }
439 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700440}
441
Brian Carlstromf615a612011-07-23 12:50:34 -0700442void ClassLinker::LoadInterfaces(const DexFile& dex_file,
443 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700444 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700445 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700446 if (list != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700447 DCHECK(klass->interfaces_ == NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700448 klass->interfaces_ = AllocObjectArray<Class>(list->Size());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700449 DCHECK(klass->interfaces_idx_ == NULL);
450 klass->interfaces_idx_ = new uint32_t[list->Size()];
Brian Carlstrom934486c2011-07-12 23:42:50 -0700451 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700452 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700453 klass->interfaces_idx_[i] = type_item.type_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700454 }
455 }
456}
457
Brian Carlstromf615a612011-07-23 12:50:34 -0700458void ClassLinker::LoadField(const DexFile& dex_file,
459 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700460 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700461 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700462 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700463 dst->klass_ = klass;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700464 dst->java_name_ = ResolveString(klass, field_id.name_idx_, dex_file);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700465 dst->descriptor_.set(dex_file.dexStringByTypeIdx(field_id.type_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700466 dst->access_flags_ = src.access_flags_;
467}
468
Brian Carlstromf615a612011-07-23 12:50:34 -0700469void ClassLinker::LoadMethod(const DexFile& dex_file,
470 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700471 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700472 Method* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700473 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700474 dst->klass_ = klass;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700475 dst->java_name_ = ResolveString(klass, method_id.name_idx_, dex_file);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700476 {
477 int32_t utf16_length;
478 scoped_ptr<char> utf8(dex_file.CreateMethodDescriptor(method_id.proto_idx_,
479 &utf16_length));
480 dst->descriptor_ = String::AllocFromModifiedUtf8(utf16_length, utf8.get());
481 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700482 dst->proto_idx_ = method_id.proto_idx_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700483 dst->shorty_ = dex_file.GetShorty(method_id.proto_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700484 dst->access_flags_ = src.access_flags_;
485
486 // TODO: check for finalize method
487
Brian Carlstromf615a612011-07-23 12:50:34 -0700488 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700489 if (code_item != NULL) {
490 dst->num_registers_ = code_item->registers_size_;
491 dst->num_ins_ = code_item->ins_size_;
492 dst->num_outs_ = code_item->outs_size_;
493 dst->insns_ = code_item->insns_;
494 } else {
495 uint16_t num_args = dst->NumArgRegisters();
496 if (!dst->IsStatic()) {
497 ++num_args;
498 }
499 dst->num_registers_ = dst->num_ins_ + num_args;
500 // TODO: native methods
501 }
502}
503
Brian Carlstromf615a612011-07-23 12:50:34 -0700504void ClassLinker::AppendToBootClassPath(DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700505 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700506 boot_class_path_.push_back(dex_file);
507 RegisterDexFile(dex_file);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700508}
509
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700510void ClassLinker::RegisterDexFile(const DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700511 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700512 dex_files_.push_back(dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700513 DexCache* dex_cache = AllocDexCache();
514 CHECK(dex_cache != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700515 dex_cache->Init(AllocObjectArray<String>(dex_file->NumStringIds()),
516 AllocObjectArray<Class>(dex_file->NumTypeIds()),
517 AllocObjectArray<Method>(dex_file->NumMethodIds()),
518 AllocObjectArray<Field>(dex_file->NumFieldIds()));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700519 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700520}
521
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700522const DexFile& ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700523 for (size_t i = 0; i != dex_caches_.size(); ++i) {
524 if (dex_caches_[i] == dex_cache) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700525 return *dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700526 }
527 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700528 CHECK(false) << "Could not find DexFile";
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700529 return *dex_files_[-1];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700530}
531
Brian Carlstromf615a612011-07-23 12:50:34 -0700532DexCache* ClassLinker::FindDexCache(const DexFile* dex_file) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700533 for (size_t i = 0; i != dex_files_.size(); ++i) {
534 if (dex_files_[i] == dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700535 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700536 }
537 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700538 CHECK(false) << "Could not find DexCache";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700539 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700540}
541
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700542Class* ClassLinker::CreatePrimitiveClass(const StringPiece& descriptor) {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700543 Class* klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700544 CHECK(klass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700545 klass->super_class_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700546 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
547 klass->descriptor_ = descriptor;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700548 klass->descriptor_alloc_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700549 klass->status_ = Class::kStatusInitialized;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700550 bool success = InsertClass(klass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700551 CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed";
Carl Shapiro565f5072011-07-10 13:39:43 -0700552 return klass;
553}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700554
Brian Carlstrombe977852011-07-19 14:54:54 -0700555// Create an array class (i.e. the class object for the array, not the
556// array itself). "descriptor" looks like "[C" or "[[[[B" or
557// "[Ljava/lang/String;".
558//
559// If "descriptor" refers to an array of primitives, look up the
560// primitive type's internally-generated class object.
561//
562// "loader" is the class loader of the class that's referring to us. It's
563// used to ensure that we're looking for the element type in the right
564// context. It does NOT become the class loader for the array class; that
565// always comes from the base element class.
566//
567// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700568Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700569 ClassLoader* class_loader)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700570{
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700571 CHECK(descriptor[0] == '[');
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700572
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700573 // Identify the underlying element class and the array dimension depth.
574 Class* component_type_ = NULL;
575 int array_rank;
576 if (descriptor[1] == '[') {
577 // array of arrays; keep descriptor and grab stuff from parent
578 Class* outer = FindClass(descriptor.substr(1), class_loader);
579 if (outer != NULL) {
580 // want the base class, not "outer", in our component_type_
581 component_type_ = outer->component_type_;
582 array_rank = outer->array_rank_ + 1;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700583 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700584 DCHECK(component_type_ == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700585 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700586 } else {
587 array_rank = 1;
588 if (descriptor[1] == 'L') {
589 // array of objects; strip off "[" and look up descriptor.
590 const StringPiece subDescriptor = descriptor.substr(1);
591 component_type_ = FindClass(subDescriptor, class_loader);
592 } else {
593 // array of a primitive type
594 component_type_ = FindPrimitiveClass(descriptor[1]);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700595 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700596 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700597
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700598 if (component_type_ == NULL) {
599 // failed
600 // DCHECK(Thread::Current()->IsExceptionPending()); // TODO
601 return NULL;
602 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700603
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700604 // See if the component type is already loaded. Array classes are
605 // always associated with the class loader of their underlying
606 // element type -- an array of Strings goes with the loader for
607 // java/lang/String -- so we need to look for it there. (The
608 // caller should have checked for the existence of the class
609 // before calling here, but they did so with *their* class loader,
610 // not the component type's loader.)
611 //
612 // If we find it, the caller adds "loader" to the class' initiating
613 // loader list, which should prevent us from going through this again.
614 //
615 // This call is unnecessary if "loader" and "component_type_->class_loader_"
616 // are the same, because our caller (FindClass) just did the
617 // lookup. (Even if we get this wrong we still have correct behavior,
618 // because we effectively do this lookup again when we add the new
619 // class to the hash table --- necessary because of possible races with
620 // other threads.)
621 if (class_loader != component_type_->class_loader_) {
622 Class* new_class = LookupClass(descriptor, component_type_->class_loader_);
623 if (new_class != NULL) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700624 return new_class;
625 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700626 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700627
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700628 // Fill out the fields in the Class.
629 //
630 // It is possible to execute some methods against arrays, because
631 // all arrays are subclasses of java_lang_Object_, so we need to set
632 // up a vtable. We can just point at the one in java_lang_Object_.
633 //
634 // Array classes are simple enough that we don't need to do a full
635 // link step.
636
637 Class* new_class = NULL;
638 if (!init_done_) {
639 if (descriptor == "[Ljava/lang/Object;") {
640 new_class = GetClassRoot(kObjectArrayClass);
641 } else if (descriptor == "[C") {
642 new_class = GetClassRoot(kCharArrayClass);
643 }
644 }
645 if (new_class == NULL) {
646 new_class = AllocClass();
647 if (new_class == NULL) {
648 return NULL;
649 }
650 }
651 new_class->descriptor_alloc_ = new std::string(descriptor.data(),
652 descriptor.size());
653 new_class->descriptor_.set(new_class->descriptor_alloc_->data(),
654 new_class->descriptor_alloc_->size());
655 Class* java_lang_Object = GetClassRoot(kJavaLangObject);
656 new_class->super_class_ = java_lang_Object;
657 new_class->vtable_ = java_lang_Object->vtable_;
658 new_class->primitive_type_ = Class::kPrimNot;
659 new_class->component_type_ = component_type_;
660 new_class->class_loader_ = component_type_->class_loader_;
661 new_class->array_rank_ = array_rank;
662 new_class->status_ = Class::kStatusInitialized;
663 // don't need to set new_class->object_size_
664
665
666 // All arrays have java/lang/Cloneable and java/io/Serializable as
667 // interfaces. We need to set that up here, so that stuff like
668 // "instanceof" works right.
669 //
670 // Note: The GC could run during the call to FindSystemClass,
671 // so we need to make sure the class object is GC-valid while we're in
672 // there. Do this by clearing the interface list so the GC will just
673 // think that the entries are null.
674
675
676 // Use the single, global copies of "interfaces" and "iftable"
677 // (remember not to free them for arrays).
678 DCHECK(array_interfaces_ != NULL);
679 new_class->interfaces_ = array_interfaces_;
680 new_class->iftable_count_ = 2;
681 DCHECK(array_iftable_ != NULL);
682 new_class->iftable_ = array_iftable_;
683
684 // Inherit access flags from the component type. Arrays can't be
685 // used as a superclass or interface, so we want to add "final"
686 // and remove "interface".
687 //
688 // Don't inherit any non-standard flags (e.g., kAccFinal)
689 // from component_type_. We assume that the array class does not
690 // override finalize().
691 new_class->access_flags_ = ((new_class->component_type_->access_flags_ &
692 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask;
693
694 if (InsertClass(new_class)) {
695 return new_class;
696 }
697 // Another thread must have loaded the class after we
698 // started but before we finished. Abandon what we've
699 // done.
700 //
701 // (Yes, this happens.)
702
703 // Grab the winning class.
704 Class* other_class = LookupClass(descriptor, component_type_->class_loader_);
705 DCHECK(other_class != NULL);
706 return other_class;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700707}
708
709Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700710 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700711 case 'B':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700712 return GetClassRoot(kPrimitiveByte);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700713 case 'C':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700714 return GetClassRoot(kPrimitiveChar);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700715 case 'D':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700716 return GetClassRoot(kPrimitiveDouble);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700717 case 'F':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700718 return GetClassRoot(kPrimitiveFloat);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700719 case 'I':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700720 return GetClassRoot(kPrimitiveInt);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700721 case 'J':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700722 return GetClassRoot(kPrimitiveLong);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700723 case 'S':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700724 return GetClassRoot(kPrimitiveShort);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700725 case 'Z':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700726 return GetClassRoot(kPrimitiveBoolean);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700727 case 'V':
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700728 return GetClassRoot(kPrimitiveVoid);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700729 case 'L':
730 case '[':
731 LOG(ERROR) << "Not a primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700732 default:
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700733 LOG(ERROR) << "Unknown primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700734 };
735 return NULL; // Not reachable.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700736}
737
738bool ClassLinker::InsertClass(Class* klass) {
739 // TODO: acquire classes_lock_
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700740 const StringPiece& key = klass->GetDescriptor();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700741 Table::iterator it = classes_.insert(std::make_pair(key, klass));
742 return ((*it).second == klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700743 // TODO: release classes_lock_
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700744}
745
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700746Class* ClassLinker::LookupClass(const StringPiece& descriptor, ClassLoader* class_loader) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700747 // TODO: acquire classes_lock_
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700748 typedef Table::const_iterator It; // TODO: C++0x auto
749 for (It it = classes_.find(descriptor), end = classes_.end(); it != end; ++it) {
750 Class* klass = it->second;
751 if (klass->descriptor_ == descriptor && klass->class_loader_ == class_loader) {
752 return klass;
753 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700754 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700755 return NULL;
756 // TODO: release classes_lock_
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700757}
758
759bool ClassLinker::InitializeClass(Class* klass) {
760 CHECK(klass->GetStatus() == Class::kStatusResolved ||
761 klass->GetStatus() == Class::kStatusError);
762
Carl Shapirob5573532011-07-12 18:22:59 -0700763 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700764
765 {
766 ObjectLock lock(klass);
767
768 if (klass->GetStatus() < Class::kStatusVerified) {
769 if (klass->IsErroneous()) {
770 LG << "re-initializing failed class"; // TODO: throw
771 return false;
772 }
773
774 CHECK(klass->GetStatus() == Class::kStatusResolved);
775
776 klass->status_ = Class::kStatusVerifying;
777 if (!DexVerify::VerifyClass(klass)) {
778 LG << "Verification failed"; // TODO: ThrowVerifyError
779 Object* exception = self->GetException();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700780 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
781 klass->SetFieldObject(field_offset, exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700782 klass->SetStatus(Class::kStatusError);
783 return false;
784 }
785
786 klass->SetStatus(Class::kStatusVerified);
787 }
788
789 if (klass->status_ == Class::kStatusInitialized) {
790 return true;
791 }
792
793 while (klass->status_ == Class::kStatusInitializing) {
794 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -0700795 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700796 LG << "recursive <clinit>";
797 return true;
798 }
799
800 CHECK(!self->IsExceptionPending());
801
802 lock.Wait(); // TODO: check for interruption
803
804 // When we wake up, repeat the test for init-in-progress. If
805 // there's an exception pending (only possible if
806 // "interruptShouldThrow" was set), bail out.
807 if (self->IsExceptionPending()) {
808 CHECK(false);
809 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
810 klass->SetStatus(Class::kStatusError);
811 return false;
812 }
813 if (klass->GetStatus() == Class::kStatusInitializing) {
814 continue;
815 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700816 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700817 klass->GetStatus() == Class::kStatusError);
818 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700819 // The caller wants an exception, but it was thrown in a
820 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700821 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
822 return false;
823 }
824 return true; // otherwise, initialized
825 }
826
827 // see if we failed previously
828 if (klass->IsErroneous()) {
829 // might be wise to unlock before throwing; depends on which class
830 // it is that we have locked
831
832 // TODO: throwEarlierClassFailure(klass);
833 return false;
834 }
835
836 if (!ValidateSuperClassDescriptors(klass)) {
837 klass->SetStatus(Class::kStatusError);
838 return false;
839 }
840
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700841 DCHECK(klass->status_ < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700842
Carl Shapirob5573532011-07-12 18:22:59 -0700843 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700844 klass->status_ = Class::kStatusInitializing;
845 }
846
847 if (!InitializeSuperClass(klass)) {
848 return false;
849 }
850
851 InitializeStaticFields(klass);
852
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700853 Method* clinit = klass->FindDeclaredDirectMethod("<clinit>", "()V");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700854 if (clinit != NULL) {
855 } else {
856 // JValue unused;
857 // TODO: dvmCallMethod(self, method, NULL, &unused);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700858 //CHECK(!"unimplemented");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700859 }
860
861 {
862 ObjectLock lock(klass);
863
864 if (self->IsExceptionPending()) {
865 klass->SetStatus(Class::kStatusError);
866 } else {
867 klass->SetStatus(Class::kStatusInitialized);
868 }
869 lock.NotifyAll();
870 }
871
872 return true;
873}
874
875bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
876 if (klass->IsInterface()) {
877 return true;
878 }
879 // begin with the methods local to the superclass
880 if (klass->HasSuperClass() &&
881 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
882 const Class* super = klass->GetSuperClass();
883 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
884 const Method* method = klass->GetVirtualMethod(i);
885 if (method != super->GetVirtualMethod(i) &&
886 !HasSameMethodDescriptorClasses(method, super, klass)) {
887 LG << "Classes resolve differently in superclass";
888 return false;
889 }
890 }
891 }
892 for (size_t i = 0; i < klass->iftable_count_; ++i) {
893 const InterfaceEntry* iftable = &klass->iftable_[i];
894 Class* interface = iftable->GetClass();
895 if (klass->GetClassLoader() != interface->GetClassLoader()) {
896 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
897 uint32_t vtable_index = iftable->method_index_array_[j];
898 const Method* method = klass->GetVirtualMethod(vtable_index);
899 if (!HasSameMethodDescriptorClasses(method, interface,
900 method->GetClass())) {
901 LG << "Classes resolve differently in interface"; // TODO: LinkageError
902 return false;
903 }
904 }
905 }
906 }
907 return true;
908}
909
910bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700911 const Class* klass1,
912 const Class* klass2) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700913 const DexFile& dex_file = FindDexFile(method->GetClass()->GetDexCache());
914 const DexFile::ProtoId& proto_id = dex_file.GetProtoId(method->proto_idx_);
Brian Carlstromf615a612011-07-23 12:50:34 -0700915 DexFile::ParameterIterator *it;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700916 for (it = dex_file.GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700917 const char* descriptor = it->GetDescriptor();
918 if (descriptor == NULL) {
919 break;
920 }
921 if (descriptor[0] == 'L' || descriptor[0] == '[') {
922 // Found a non-primitive type.
923 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
924 return false;
925 }
926 }
927 }
928 // Check the return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700929 const char* descriptor = dex_file.GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700930 if (descriptor[0] == 'L' || descriptor[0] == '[') {
931 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
932 return false;
933 }
934 }
935 return true;
936}
937
938// Returns true if classes referenced by the descriptor are the
939// same classes in klass1 as they are in klass2.
940bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700941 const Class* klass1,
942 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700943 CHECK(descriptor != NULL);
944 CHECK(klass1 != NULL);
945 CHECK(klass2 != NULL);
946#if 0
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700947 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700948 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700949 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700950 // TODO: found2 == NULL
951 // TODO: lookup found1 in initiating loader list
952 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -0700953 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700954 if (found1 == found2) {
955 return true;
956 } else {
957 return false;
958 }
959 }
960#endif
961 return true;
962}
963
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700964bool ClassLinker::HasSameArgumentTypes(const Method* m1, const Method* m2) const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700965 const DexFile& dex1 = FindDexFile(m1->GetClass()->GetDexCache());
966 const DexFile& dex2 = FindDexFile(m2->GetClass()->GetDexCache());
967 const DexFile::ProtoId& proto1 = dex1.GetProtoId(m1->proto_idx_);
968 const DexFile::ProtoId& proto2 = dex2.GetProtoId(m2->proto_idx_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700969
970 // TODO: compare ProtoId objects for equality and exit early
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700971 const DexFile::TypeList* type_list1 = dex1.GetProtoParameters(proto1);
972 const DexFile::TypeList* type_list2 = dex2.GetProtoParameters(proto2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700973 size_t arity1 = (type_list1 == NULL) ? 0 : type_list1->Size();
974 size_t arity2 = (type_list2 == NULL) ? 0 : type_list2->Size();
975 if (arity1 != arity2) {
976 return false;
977 }
978
979 for (size_t i = 0; i < arity1; ++i) {
980 uint32_t type_idx1 = type_list1->GetTypeItem(i).type_idx_;
981 uint32_t type_idx2 = type_list2->GetTypeItem(i).type_idx_;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700982 const char* type1 = dex1.dexStringByTypeIdx(type_idx1);
983 const char* type2 = dex2.dexStringByTypeIdx(type_idx2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700984 if (strcmp(type1, type2) != 0) {
985 return false;
986 }
987 }
988
989 return true;
990}
991
992bool ClassLinker::HasSameReturnType(const Method* m1, const Method* m2) const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700993 const DexFile& dex1 = FindDexFile(m1->GetClass()->GetDexCache());
994 const DexFile& dex2 = FindDexFile(m2->GetClass()->GetDexCache());
995 const DexFile::ProtoId& proto1 = dex1.GetProtoId(m1->proto_idx_);
996 const DexFile::ProtoId& proto2 = dex2.GetProtoId(m2->proto_idx_);
997 const char* type1 = dex1.dexStringByTypeIdx(proto1.return_type_idx_);
998 const char* type2 = dex2.dexStringByTypeIdx(proto2.return_type_idx_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700999 return (strcmp(type1, type2) == 0);
1000}
1001
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001002bool ClassLinker::InitializeSuperClass(Class* klass) {
1003 CHECK(klass != NULL);
1004 // TODO: assert klass lock is acquired
1005 if (!klass->IsInterface() && klass->HasSuperClass()) {
1006 Class* super_class = klass->GetSuperClass();
1007 if (super_class->GetStatus() != Class::kStatusInitialized) {
1008 CHECK(!super_class->IsInterface());
1009 klass->MonitorExit();
1010 bool super_initialized = InitializeClass(super_class);
1011 klass->MonitorEnter();
1012 // TODO: check for a pending exception
1013 if (!super_initialized) {
1014 klass->SetStatus(Class::kStatusError);
1015 klass->NotifyAll();
1016 return false;
1017 }
1018 }
1019 }
1020 return true;
1021}
1022
1023void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001024 size_t num_static_fields = klass->NumStaticFields();
1025 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001026 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001027 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001028 DexCache* dex_cache = klass->GetDexCache();
1029 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001030 return;
1031 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -07001032 const StringPiece& descriptor = klass->GetDescriptor();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001033 const DexFile& dex_file = FindDexFile(dex_cache);
1034 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Brian Carlstromf615a612011-07-23 12:50:34 -07001035 CHECK(dex_class_def != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001036 const byte* addr = dex_file.GetEncodedArray(*dex_class_def);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001037 size_t array_size = DecodeUnsignedLeb128(&addr);
1038 for (size_t i = 0; i < array_size; ++i) {
1039 StaticField* field = klass->GetStaticField(i);
1040 JValue value;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001041 DexFile::ValueType type = dex_file.ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001042 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001043 case DexFile::kByte:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001044 field->SetByte(value.b);
1045 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001046 case DexFile::kShort:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001047 field->SetShort(value.s);
1048 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001049 case DexFile::kChar:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001050 field->SetChar(value.c);
1051 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001052 case DexFile::kInt:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001053 field->SetInt(value.i);
1054 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001055 case DexFile::kLong:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001056 field->SetLong(value.j);
1057 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001058 case DexFile::kFloat:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001059 field->SetFloat(value.f);
1060 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001061 case DexFile::kDouble:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001062 field->SetDouble(value.d);
1063 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001064 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001065 uint32_t string_idx = value.i;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001066 String* resolved = ResolveString(klass, string_idx, dex_file);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001067 field->SetObject(resolved);
1068 break;
1069 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001070 case DexFile::kBoolean:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001071 field->SetBoolean(value.z);
1072 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001073 case DexFile::kNull:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001074 field->SetObject(value.l);
1075 break;
1076 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001077 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001078 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001079 }
1080}
1081
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001082bool ClassLinker::LinkClass(Class* klass, const DexFile& dex_file) {
1083 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001084 if (!LinkSuperClass(klass)) {
1085 return false;
1086 }
1087 if (!LinkMethods(klass)) {
1088 return false;
1089 }
1090 if (!LinkInstanceFields(klass)) {
1091 return false;
1092 }
1093 CreateReferenceOffsets(klass);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001094 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001095 klass->status_ = Class::kStatusResolved;
1096 return true;
1097}
1098
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001099bool ClassLinker::LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file) {
1100 CHECK_EQ(Class::kStatusIdx, klass->status_);
Brian Carlstromf615a612011-07-23 12:50:34 -07001101 if (klass->super_class_idx_ != DexFile::kDexNoIndex) {
1102 Class* super_class = ResolveClass(klass, klass->super_class_idx_, dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001103 if (super_class == NULL) {
1104 LG << "Failed to resolve superclass";
1105 return false;
1106 }
1107 klass->super_class_ = super_class; // TODO: write barrier
1108 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001109 if (klass->NumInterfaces() > 0) {
1110 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
1111 uint32_t idx = klass->interfaces_idx_[i];
1112 klass->SetInterface(i, ResolveClass(klass, idx, dex_file));
1113 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001114 LG << "Failed to resolve interface";
1115 return false;
1116 }
1117 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001118 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001119 LG << "Inaccessible interface";
1120 return false;
1121 }
1122 }
1123 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001124 // Mark the class as loaded.
1125 klass->status_ = Class::kStatusLoaded;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001126 return true;
1127}
1128
1129bool ClassLinker::LinkSuperClass(Class* klass) {
1130 CHECK(!klass->IsPrimitive());
1131 const Class* super = klass->GetSuperClass();
1132 if (klass->GetDescriptor() == "Ljava/lang/Object;") {
1133 if (super != NULL) {
1134 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1135 return false;
1136 }
1137 // TODO: clear finalize attribute
1138 return true;
1139 }
1140 if (super == NULL) {
1141 LG << "No superclass defined"; // TODO: LinkageError
1142 return false;
1143 }
1144 // Verify
1145 if (super->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001146 LG << "Superclass " << super->descriptor_ << " is declared final"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001147 return false;
1148 }
1149 if (super->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001150 LG << "Superclass " << super->descriptor_ << " is an interface"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001151 return false;
1152 }
1153 if (!klass->CanAccess(super)) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001154 LG << "Superclass " << super->descriptor_ << " is inaccessible"; // TODO: IllegalAccessError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001155 return false;
1156 }
1157 return true;
1158}
1159
1160// Populate the class vtable and itable.
1161bool ClassLinker::LinkMethods(Class* klass) {
1162 if (klass->IsInterface()) {
1163 // No vtable.
1164 size_t count = klass->NumVirtualMethods();
1165 if (!IsUint(16, count)) {
1166 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1167 return false;
1168 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001169 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001170 klass->GetVirtualMethod(i)->method_index_ = i;
1171 }
1172 } else {
1173 // Link virtual method tables
1174 LinkVirtualMethods(klass);
1175
1176 // Link interface method tables
1177 LinkInterfaceMethods(klass);
1178
1179 // Insert stubs.
1180 LinkAbstractMethods(klass);
1181 }
1182 return true;
1183}
1184
1185bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001186 if (klass->HasSuperClass()) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001187 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001188 size_t actual_count = klass->GetSuperClass()->vtable_->GetLength();
1189 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001190 // TODO: do not assign to the vtable field until it is fully constructed.
1191 klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001192 // See if any of our virtual methods override the superclass.
1193 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1194 Method* local_method = klass->GetVirtualMethod(i);
1195 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001196 for (; j < actual_count; ++j) {
1197 Method* super_method = klass->vtable_->Get(j);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001198 if (HasSameNameAndPrototype(local_method, super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001199 // Verify
1200 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001201 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001202 return false;
1203 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001204 klass->vtable_->Set(j, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001205 local_method->method_index_ = j;
1206 break;
1207 }
1208 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001209 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001210 // Not overriding, append.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001211 klass->vtable_->Set(actual_count, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001212 local_method->method_index_ = actual_count;
1213 actual_count += 1;
1214 }
1215 }
1216 if (!IsUint(16, actual_count)) {
1217 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1218 return false;
1219 }
1220 CHECK_LE(actual_count, max_count);
1221 if (actual_count < max_count) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001222 // TODO: do not assign to the vtable field until it is fully constructed.
1223 klass->vtable_ = klass->vtable_->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001224 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001225 } else {
1226 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001227 uint32_t num_virtual_methods = klass->NumVirtualMethods();
1228 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
1229 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001230 LG << "Too many methods"; // TODO: VirtualMachineError
1231 return false;
1232 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001233 // TODO: do not assign to the vtable field until it is fully constructed.
1234 klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods);
1235 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001236 klass->vtable_->Set(i, klass->GetVirtualMethod(i));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001237 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1238 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001239 }
1240 return true;
1241}
1242
1243bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1244 int pool_offset = 0;
1245 int pool_size = 0;
1246 int miranda_count = 0;
1247 int miranda_alloc = 0;
1248 size_t super_ifcount;
1249 if (klass->HasSuperClass()) {
1250 super_ifcount = klass->GetSuperClass()->iftable_count_;
1251 } else {
1252 super_ifcount = 0;
1253 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001254 size_t ifcount = super_ifcount;
1255 ifcount += klass->NumInterfaces();
1256 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1257 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001258 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001259 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001260 DCHECK(klass->iftable_count_ == 0);
1261 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001262 return true;
1263 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001264 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1265 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001266 if (super_ifcount != 0) {
1267 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1268 sizeof(InterfaceEntry) * super_ifcount);
1269 }
1270 // Flatten the interface inheritance hierarchy.
1271 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001272 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1273 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001274 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001275 if (!interf->IsInterface()) {
1276 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1277 return false;
1278 }
1279 klass->iftable_[idx++].SetClass(interf);
1280 for (size_t j = 0; j < interf->iftable_count_; j++) {
1281 klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
1282 }
1283 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001284 CHECK_EQ(idx, ifcount);
1285 klass->iftable_count_ = ifcount;
1286 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001287 return true;
1288 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001289 for (size_t i = super_ifcount; i < ifcount; i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001290 pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
1291 }
1292 if (pool_size == 0) {
1293 return true;
1294 }
1295 klass->ifvi_pool_count_ = pool_size;
1296 klass->ifvi_pool_ = new uint32_t[pool_size];
1297 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001298 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001299 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
1300 Class* interface = klass->iftable_[i].GetClass();
1301 pool_offset += interface->NumVirtualMethods(); // end here
1302 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1303 Method* interface_method = interface->GetVirtualMethod(j);
1304 int k; // must be signed
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001305 for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) {
1306 if (HasSameNameAndPrototype(interface_method, klass->vtable_->Get(k))) {
1307 if (!klass->vtable_->Get(k)->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001308 LG << "Implementation not public";
1309 return false;
1310 }
1311 klass->iftable_[i].method_index_array_[j] = k;
1312 break;
1313 }
1314 }
1315 if (k < 0) {
1316 if (miranda_count == miranda_alloc) {
1317 miranda_alloc += 8;
1318 if (miranda_list.empty()) {
1319 miranda_list.resize(miranda_alloc);
1320 } else {
1321 miranda_list.resize(miranda_alloc);
1322 }
1323 }
1324 int mir;
1325 for (mir = 0; mir < miranda_count; mir++) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001326 if (HasSameNameAndPrototype(miranda_list[mir], interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001327 break;
1328 }
1329 }
1330 // point the interface table at a phantom slot index
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001331 klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001332 if (mir == miranda_count) {
1333 miranda_list[miranda_count++] = interface_method;
1334 }
1335 }
1336 }
1337 }
1338 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001339 int old_method_count = klass->NumVirtualMethods();
1340 int new_method_count = old_method_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001341 klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001342
1343 CHECK(klass->vtable_ != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001344 int old_vtable_count = klass->vtable_->GetLength();
1345 int new_vtable_count = old_vtable_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001346 // TODO: do not assign to the vtable field until it is fully constructed.
1347 klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001348
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001349 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001350 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001351 memcpy(meth, miranda_list[i], sizeof(Method));
1352 meth->klass_ = klass;
1353 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001354 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1355 klass->SetVirtualMethod(old_method_count + i, meth);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001356 klass->vtable_->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001357 }
1358 }
1359 return true;
1360}
1361
1362void ClassLinker::LinkAbstractMethods(Class* klass) {
1363 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1364 Method* method = klass->GetVirtualMethod(i);
1365 if (method->IsAbstract()) {
1366 method->insns_ = reinterpret_cast<uint16_t*>(0xFFFFFFFF); // TODO: AbstractMethodError
1367 }
1368 }
1369}
1370
1371bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001372 int field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001373 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001374 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001375 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001376 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001377 }
1378 // Move references to the front.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001379 klass->num_reference_instance_fields_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001380 size_t i = 0;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001381 for ( ; i < klass->NumInstanceFields(); i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001382 InstanceField* pField = klass->GetInstanceField(i);
1383 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001384 if (c != '[' && c != 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001385 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1386 InstanceField* refField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001387 char rc = refField->GetType();
1388 if (rc == '[' || rc == 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001389 klass->SetInstanceField(i, refField);
1390 klass->SetInstanceField(j, pField);
1391 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001392 c = rc;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001393 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001394 break;
1395 }
1396 }
1397 } else {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001398 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001399 }
1400 if (c != '[' && c != 'L') {
1401 break;
1402 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001403 pField->SetOffset(field_offset);
1404 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001405 }
1406
1407 // Now we want to pack all of the double-wide fields together. If
1408 // we're not aligned, though, we want to shuffle one 32-bit field
1409 // into place. If we can't find one, we'll have to pad it.
Brian Carlstroma0808032011-07-18 00:39:23 -07001410 if (i != klass->NumInstanceFields() && (field_offset & 0x04) != 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001411 InstanceField* pField = klass->GetInstanceField(i);
1412 char c = pField->GetType();
1413
1414 if (c != 'J' && c != 'D') {
1415 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001416 DCHECK(c != '[');
1417 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001418 pField->SetOffset(field_offset);
1419 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001420 i++;
1421 } else {
1422 // Next field is 64-bit, so search for a 32-bit field we can
1423 // swap into it.
1424 bool found = false;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001425 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1426 InstanceField* singleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001427 char rc = singleField->GetType();
1428 if (rc != 'J' && rc != 'D') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001429 klass->SetInstanceField(i, singleField);
1430 klass->SetInstanceField(j, pField);
1431 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001432 pField->SetOffset(field_offset);
1433 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001434 found = true;
1435 i++;
1436 break;
1437 }
1438 }
1439 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001440 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001441 }
1442 }
1443 }
1444
1445 // Alignment is good, shuffle any double-wide fields forward, and
1446 // finish assigning field offsets to all fields.
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001447 DCHECK(i == klass->NumInstanceFields() || (field_offset & 0x04) == 0);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001448 for ( ; i < klass->NumInstanceFields(); i++) {
1449 InstanceField* pField = klass->GetInstanceField(i);
1450 char c = pField->GetType();
1451 if (c != 'D' && c != 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001452 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1453 InstanceField* doubleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001454 char rc = doubleField->GetType();
1455 if (rc == 'D' || rc == 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001456 klass->SetInstanceField(i, doubleField);
1457 klass->SetInstanceField(j, pField);
1458 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001459 c = rc;
1460 break;
1461 }
1462 }
1463 } else {
1464 // This is a double-wide field, leave it be.
1465 }
1466
Brian Carlstroma0808032011-07-18 00:39:23 -07001467 pField->SetOffset(field_offset);
1468 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001469 if (c == 'J' || c == 'D')
Brian Carlstroma0808032011-07-18 00:39:23 -07001470 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001471 }
1472
1473#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001474 // Make sure that all reference fields appear before
1475 // non-reference fields, and all double-wide fields are aligned.
1476 bool seen_non_ref = false;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001477 for (i = 0; i < klass->NumInstanceFields(); i++) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001478 InstanceField *pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001479 char c = pField->GetType();
1480
1481 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001482 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001483 }
1484
1485 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001486 if (!seen_non_ref) {
1487 seen_non_ref = true;
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001488 DCHECK_EQ(klass->NumReferenceInstanceFields(), i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001489 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001490 } else {
1491 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001492 }
1493 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001494 if (!seen_non_ref) {
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001495 DCHECK_EQ(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001496 }
1497#endif
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001498 klass->object_size_ = field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001499 return true;
1500}
1501
1502// Set the bitmap of reference offsets, refOffsets, from the ifields
1503// list.
1504void ClassLinker::CreateReferenceOffsets(Class* klass) {
1505 uint32_t reference_offsets = 0;
1506 if (klass->HasSuperClass()) {
1507 reference_offsets = klass->GetSuperClass()->GetReferenceOffsets();
1508 }
1509 // If our superclass overflowed, we don't stand a chance.
1510 if (reference_offsets != CLASS_WALK_SUPER) {
1511 // All of the fields that contain object references are guaranteed
1512 // to be at the beginning of the ifields list.
1513 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) {
1514 // Note that, per the comment on struct InstField, f->byteOffset
1515 // is the offset from the beginning of obj, not the offset into
1516 // obj->instanceData.
1517 const InstanceField* field = klass->GetInstanceField(i);
1518 size_t byte_offset = field->GetOffset();
1519 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001520 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001521 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1522 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001523 CHECK_NE(new_bit, 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001524 reference_offsets |= new_bit;
1525 } else {
1526 reference_offsets = CLASS_WALK_SUPER;
1527 break;
1528 }
1529 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001530 }
Brian Carlstromae3ac012011-07-27 01:30:28 -07001531 klass->SetReferenceOffsets(reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001532}
1533
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001534Class* ClassLinker::ResolveClass(const Class* referrer,
1535 uint32_t class_idx,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001536 const DexFile& dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001537 DexCache* dex_cache = referrer->GetDexCache();
1538 Class* resolved = dex_cache->GetResolvedClass(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001539 if (resolved != NULL) {
1540 return resolved;
1541 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001542 const char* descriptor = dex_file.dexStringByTypeIdx(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001543 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001544 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001545 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001546 resolved = FindClass(descriptor, referrer->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001547 }
1548 if (resolved != NULL) {
1549 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001550 if (referrer->GetDexCache() != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001551 if (check->GetClassLoader() != NULL) {
1552 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1553 return NULL;
1554 }
1555 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001556 dex_cache->SetResolvedClass(class_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001557 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001558 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001559 }
1560 return resolved;
1561}
1562
1563Method* ResolveMethod(const Class* referrer, uint32_t method_idx,
1564 /*MethodType*/ int method_type) {
1565 CHECK(false);
1566 return NULL;
1567}
1568
Carl Shapiro69759ea2011-07-21 18:13:35 -07001569String* ClassLinker::ResolveString(const Class* referring,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001570 uint32_t string_idx,
1571 const DexFile& dex_file) {
1572 const DexFile::StringId& string_id = dex_file.GetStringId(string_idx);
1573 int32_t utf16_length = dex_file.GetStringLength(string_id);
1574 const char* utf8_data = dex_file.GetStringData(string_id);
Jesse Wilson8989d992011-08-02 13:39:42 -07001575 String* new_string = String::AllocFromModifiedUtf8(utf16_length, utf8_data);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001576 // TODO: intern the new string
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001577 referring->GetDexCache()->SetResolvedString(string_idx, new_string);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001578 return new_string;
1579}
1580
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001581} // namespace art