blob: 1bfa00929afa4d5c2fd7f54505af994f1edda36d [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
964bool ClassLinker::InitializeSuperClass(Class* klass) {
965 CHECK(klass != NULL);
966 // TODO: assert klass lock is acquired
967 if (!klass->IsInterface() && klass->HasSuperClass()) {
968 Class* super_class = klass->GetSuperClass();
969 if (super_class->GetStatus() != Class::kStatusInitialized) {
970 CHECK(!super_class->IsInterface());
971 klass->MonitorExit();
972 bool super_initialized = InitializeClass(super_class);
973 klass->MonitorEnter();
974 // TODO: check for a pending exception
975 if (!super_initialized) {
976 klass->SetStatus(Class::kStatusError);
977 klass->NotifyAll();
978 return false;
979 }
980 }
981 }
982 return true;
983}
984
985void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700986 size_t num_static_fields = klass->NumStaticFields();
987 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700988 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700989 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700990 DexCache* dex_cache = klass->GetDexCache();
991 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700992 return;
993 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700994 const StringPiece& descriptor = klass->GetDescriptor();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700995 const DexFile& dex_file = FindDexFile(dex_cache);
996 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Brian Carlstromf615a612011-07-23 12:50:34 -0700997 CHECK(dex_class_def != NULL);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700998 const byte* addr = dex_file.GetEncodedArray(*dex_class_def);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700999 size_t array_size = DecodeUnsignedLeb128(&addr);
1000 for (size_t i = 0; i < array_size; ++i) {
1001 StaticField* field = klass->GetStaticField(i);
1002 JValue value;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001003 DexFile::ValueType type = dex_file.ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001004 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001005 case DexFile::kByte:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001006 field->SetByte(value.b);
1007 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001008 case DexFile::kShort:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001009 field->SetShort(value.s);
1010 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001011 case DexFile::kChar:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001012 field->SetChar(value.c);
1013 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001014 case DexFile::kInt:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001015 field->SetInt(value.i);
1016 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001017 case DexFile::kLong:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001018 field->SetLong(value.j);
1019 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001020 case DexFile::kFloat:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001021 field->SetFloat(value.f);
1022 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001023 case DexFile::kDouble:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001024 field->SetDouble(value.d);
1025 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001026 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001027 uint32_t string_idx = value.i;
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001028 String* resolved = ResolveString(klass, string_idx, dex_file);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001029 field->SetObject(resolved);
1030 break;
1031 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001032 case DexFile::kBoolean:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001033 field->SetBoolean(value.z);
1034 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001035 case DexFile::kNull:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001036 field->SetObject(value.l);
1037 break;
1038 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001039 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001040 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001041 }
1042}
1043
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001044bool ClassLinker::LinkClass(Class* klass, const DexFile& dex_file) {
1045 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001046 if (!LinkSuperClass(klass)) {
1047 return false;
1048 }
1049 if (!LinkMethods(klass)) {
1050 return false;
1051 }
1052 if (!LinkInstanceFields(klass)) {
1053 return false;
1054 }
1055 CreateReferenceOffsets(klass);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001056 CHECK_EQ(Class::kStatusLoaded, klass->status_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001057 klass->status_ = Class::kStatusResolved;
1058 return true;
1059}
1060
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001061bool ClassLinker::LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file) {
1062 CHECK_EQ(Class::kStatusIdx, klass->status_);
Brian Carlstromf615a612011-07-23 12:50:34 -07001063 if (klass->super_class_idx_ != DexFile::kDexNoIndex) {
1064 Class* super_class = ResolveClass(klass, klass->super_class_idx_, dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001065 if (super_class == NULL) {
1066 LG << "Failed to resolve superclass";
1067 return false;
1068 }
1069 klass->super_class_ = super_class; // TODO: write barrier
1070 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001071 if (klass->NumInterfaces() > 0) {
1072 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
1073 uint32_t idx = klass->interfaces_idx_[i];
1074 klass->SetInterface(i, ResolveClass(klass, idx, dex_file));
1075 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001076 LG << "Failed to resolve interface";
1077 return false;
1078 }
1079 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001080 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001081 LG << "Inaccessible interface";
1082 return false;
1083 }
1084 }
1085 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001086 // Mark the class as loaded.
1087 klass->status_ = Class::kStatusLoaded;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001088 return true;
1089}
1090
1091bool ClassLinker::LinkSuperClass(Class* klass) {
1092 CHECK(!klass->IsPrimitive());
1093 const Class* super = klass->GetSuperClass();
1094 if (klass->GetDescriptor() == "Ljava/lang/Object;") {
1095 if (super != NULL) {
1096 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1097 return false;
1098 }
1099 // TODO: clear finalize attribute
1100 return true;
1101 }
1102 if (super == NULL) {
1103 LG << "No superclass defined"; // TODO: LinkageError
1104 return false;
1105 }
1106 // Verify
1107 if (super->IsFinal()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001108 LG << "Superclass " << super->descriptor_ << " is declared final"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001109 return false;
1110 }
1111 if (super->IsInterface()) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001112 LG << "Superclass " << super->descriptor_ << " is an interface"; // TODO: IncompatibleClassChangeError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001113 return false;
1114 }
1115 if (!klass->CanAccess(super)) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001116 LG << "Superclass " << super->descriptor_ << " is inaccessible"; // TODO: IllegalAccessError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001117 return false;
1118 }
1119 return true;
1120}
1121
1122// Populate the class vtable and itable.
1123bool ClassLinker::LinkMethods(Class* klass) {
1124 if (klass->IsInterface()) {
1125 // No vtable.
1126 size_t count = klass->NumVirtualMethods();
1127 if (!IsUint(16, count)) {
1128 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1129 return false;
1130 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001131 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001132 klass->GetVirtualMethod(i)->method_index_ = i;
1133 }
1134 } else {
1135 // Link virtual method tables
1136 LinkVirtualMethods(klass);
1137
1138 // Link interface method tables
1139 LinkInterfaceMethods(klass);
1140
1141 // Insert stubs.
1142 LinkAbstractMethods(klass);
1143 }
1144 return true;
1145}
1146
1147bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001148 if (klass->HasSuperClass()) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001149 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001150 size_t actual_count = klass->GetSuperClass()->vtable_->GetLength();
1151 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001152 // TODO: do not assign to the vtable field until it is fully constructed.
1153 klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001154 // See if any of our virtual methods override the superclass.
1155 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1156 Method* local_method = klass->GetVirtualMethod(i);
1157 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001158 for (; j < actual_count; ++j) {
1159 Method* super_method = klass->vtable_->Get(j);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001160 if (local_method->HasSameNameAndDescriptor(super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001161 // Verify
1162 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001163 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001164 return false;
1165 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001166 klass->vtable_->Set(j, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001167 local_method->method_index_ = j;
1168 break;
1169 }
1170 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001171 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001172 // Not overriding, append.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001173 klass->vtable_->Set(actual_count, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001174 local_method->method_index_ = actual_count;
1175 actual_count += 1;
1176 }
1177 }
1178 if (!IsUint(16, actual_count)) {
1179 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1180 return false;
1181 }
1182 CHECK_LE(actual_count, max_count);
1183 if (actual_count < max_count) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001184 // TODO: do not assign to the vtable field until it is fully constructed.
1185 klass->vtable_ = klass->vtable_->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001186 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001187 } else {
1188 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001189 uint32_t num_virtual_methods = klass->NumVirtualMethods();
1190 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
1191 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001192 LG << "Too many methods"; // TODO: VirtualMachineError
1193 return false;
1194 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001195 // TODO: do not assign to the vtable field until it is fully constructed.
1196 klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods);
1197 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001198 klass->vtable_->Set(i, klass->GetVirtualMethod(i));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001199 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1200 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001201 }
1202 return true;
1203}
1204
1205bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1206 int pool_offset = 0;
1207 int pool_size = 0;
1208 int miranda_count = 0;
1209 int miranda_alloc = 0;
1210 size_t super_ifcount;
1211 if (klass->HasSuperClass()) {
1212 super_ifcount = klass->GetSuperClass()->iftable_count_;
1213 } else {
1214 super_ifcount = 0;
1215 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001216 size_t ifcount = super_ifcount;
1217 ifcount += klass->NumInterfaces();
1218 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1219 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001220 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001221 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001222 DCHECK(klass->iftable_count_ == 0);
1223 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001224 return true;
1225 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001226 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1227 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001228 if (super_ifcount != 0) {
1229 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1230 sizeof(InterfaceEntry) * super_ifcount);
1231 }
1232 // Flatten the interface inheritance hierarchy.
1233 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001234 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1235 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001236 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001237 if (!interf->IsInterface()) {
1238 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1239 return false;
1240 }
1241 klass->iftable_[idx++].SetClass(interf);
1242 for (size_t j = 0; j < interf->iftable_count_; j++) {
1243 klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
1244 }
1245 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001246 CHECK_EQ(idx, ifcount);
1247 klass->iftable_count_ = ifcount;
1248 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001249 return true;
1250 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001251 for (size_t i = super_ifcount; i < ifcount; i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001252 pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
1253 }
1254 if (pool_size == 0) {
1255 return true;
1256 }
1257 klass->ifvi_pool_count_ = pool_size;
1258 klass->ifvi_pool_ = new uint32_t[pool_size];
1259 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001260 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001261 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
1262 Class* interface = klass->iftable_[i].GetClass();
1263 pool_offset += interface->NumVirtualMethods(); // end here
1264 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1265 Method* interface_method = interface->GetVirtualMethod(j);
1266 int k; // must be signed
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001267 for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001268 Method* vtable_method = klass->vtable_->Get(k);
1269 if (interface_method->HasSameNameAndDescriptor(vtable_method)) {
1270 if (!vtable_method->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001271 LG << "Implementation not public";
1272 return false;
1273 }
1274 klass->iftable_[i].method_index_array_[j] = k;
1275 break;
1276 }
1277 }
1278 if (k < 0) {
1279 if (miranda_count == miranda_alloc) {
1280 miranda_alloc += 8;
1281 if (miranda_list.empty()) {
1282 miranda_list.resize(miranda_alloc);
1283 } else {
1284 miranda_list.resize(miranda_alloc);
1285 }
1286 }
1287 int mir;
1288 for (mir = 0; mir < miranda_count; mir++) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001289 Method* miranda_method = miranda_list[mir];
1290 if (miranda_method->HasSameNameAndDescriptor(interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001291 break;
1292 }
1293 }
1294 // point the interface table at a phantom slot index
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001295 klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001296 if (mir == miranda_count) {
1297 miranda_list[miranda_count++] = interface_method;
1298 }
1299 }
1300 }
1301 }
1302 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001303 int old_method_count = klass->NumVirtualMethods();
1304 int new_method_count = old_method_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001305 klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001306
1307 CHECK(klass->vtable_ != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001308 int old_vtable_count = klass->vtable_->GetLength();
1309 int new_vtable_count = old_vtable_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001310 // TODO: do not assign to the vtable field until it is fully constructed.
1311 klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001312
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001313 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001314 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001315 memcpy(meth, miranda_list[i], sizeof(Method));
1316 meth->klass_ = klass;
1317 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001318 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1319 klass->SetVirtualMethod(old_method_count + i, meth);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001320 klass->vtable_->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001321 }
1322 }
1323 return true;
1324}
1325
1326void ClassLinker::LinkAbstractMethods(Class* klass) {
1327 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1328 Method* method = klass->GetVirtualMethod(i);
1329 if (method->IsAbstract()) {
1330 method->insns_ = reinterpret_cast<uint16_t*>(0xFFFFFFFF); // TODO: AbstractMethodError
1331 }
1332 }
1333}
1334
1335bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001336 int field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001337 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001338 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001339 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001340 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001341 }
1342 // Move references to the front.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001343 klass->num_reference_instance_fields_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001344 size_t i = 0;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001345 for ( ; i < klass->NumInstanceFields(); i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001346 InstanceField* pField = klass->GetInstanceField(i);
1347 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001348 if (c != '[' && c != 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001349 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1350 InstanceField* refField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001351 char rc = refField->GetType();
1352 if (rc == '[' || rc == 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001353 klass->SetInstanceField(i, refField);
1354 klass->SetInstanceField(j, pField);
1355 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001356 c = rc;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001357 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001358 break;
1359 }
1360 }
1361 } else {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001362 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001363 }
1364 if (c != '[' && c != 'L') {
1365 break;
1366 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001367 pField->SetOffset(field_offset);
1368 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001369 }
1370
1371 // Now we want to pack all of the double-wide fields together. If
1372 // we're not aligned, though, we want to shuffle one 32-bit field
1373 // into place. If we can't find one, we'll have to pad it.
Brian Carlstroma0808032011-07-18 00:39:23 -07001374 if (i != klass->NumInstanceFields() && (field_offset & 0x04) != 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001375 InstanceField* pField = klass->GetInstanceField(i);
1376 char c = pField->GetType();
1377
1378 if (c != 'J' && c != 'D') {
1379 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001380 DCHECK(c != '[');
1381 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001382 pField->SetOffset(field_offset);
1383 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001384 i++;
1385 } else {
1386 // Next field is 64-bit, so search for a 32-bit field we can
1387 // swap into it.
1388 bool found = false;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001389 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1390 InstanceField* singleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001391 char rc = singleField->GetType();
1392 if (rc != 'J' && rc != 'D') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001393 klass->SetInstanceField(i, singleField);
1394 klass->SetInstanceField(j, pField);
1395 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001396 pField->SetOffset(field_offset);
1397 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001398 found = true;
1399 i++;
1400 break;
1401 }
1402 }
1403 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001404 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001405 }
1406 }
1407 }
1408
1409 // Alignment is good, shuffle any double-wide fields forward, and
1410 // finish assigning field offsets to all fields.
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001411 DCHECK(i == klass->NumInstanceFields() || (field_offset & 0x04) == 0);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001412 for ( ; i < klass->NumInstanceFields(); i++) {
1413 InstanceField* pField = klass->GetInstanceField(i);
1414 char c = pField->GetType();
1415 if (c != 'D' && c != 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001416 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1417 InstanceField* doubleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001418 char rc = doubleField->GetType();
1419 if (rc == 'D' || rc == 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001420 klass->SetInstanceField(i, doubleField);
1421 klass->SetInstanceField(j, pField);
1422 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001423 c = rc;
1424 break;
1425 }
1426 }
1427 } else {
1428 // This is a double-wide field, leave it be.
1429 }
1430
Brian Carlstroma0808032011-07-18 00:39:23 -07001431 pField->SetOffset(field_offset);
1432 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001433 if (c == 'J' || c == 'D')
Brian Carlstroma0808032011-07-18 00:39:23 -07001434 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001435 }
1436
1437#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001438 // Make sure that all reference fields appear before
1439 // non-reference fields, and all double-wide fields are aligned.
1440 bool seen_non_ref = false;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001441 for (i = 0; i < klass->NumInstanceFields(); i++) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001442 InstanceField *pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001443 char c = pField->GetType();
1444
1445 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001446 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001447 }
1448
1449 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001450 if (!seen_non_ref) {
1451 seen_non_ref = true;
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001452 DCHECK_EQ(klass->NumReferenceInstanceFields(), i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001453 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001454 } else {
1455 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001456 }
1457 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001458 if (!seen_non_ref) {
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001459 DCHECK_EQ(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001460 }
1461#endif
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001462 klass->object_size_ = field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001463 return true;
1464}
1465
1466// Set the bitmap of reference offsets, refOffsets, from the ifields
1467// list.
1468void ClassLinker::CreateReferenceOffsets(Class* klass) {
1469 uint32_t reference_offsets = 0;
1470 if (klass->HasSuperClass()) {
1471 reference_offsets = klass->GetSuperClass()->GetReferenceOffsets();
1472 }
1473 // If our superclass overflowed, we don't stand a chance.
1474 if (reference_offsets != CLASS_WALK_SUPER) {
1475 // All of the fields that contain object references are guaranteed
1476 // to be at the beginning of the ifields list.
1477 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) {
1478 // Note that, per the comment on struct InstField, f->byteOffset
1479 // is the offset from the beginning of obj, not the offset into
1480 // obj->instanceData.
1481 const InstanceField* field = klass->GetInstanceField(i);
1482 size_t byte_offset = field->GetOffset();
1483 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001484 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001485 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1486 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001487 CHECK_NE(new_bit, 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001488 reference_offsets |= new_bit;
1489 } else {
1490 reference_offsets = CLASS_WALK_SUPER;
1491 break;
1492 }
1493 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001494 }
Brian Carlstromae3ac012011-07-27 01:30:28 -07001495 klass->SetReferenceOffsets(reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001496}
1497
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001498Class* ClassLinker::ResolveClass(const Class* referrer,
1499 uint32_t class_idx,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001500 const DexFile& dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001501 DexCache* dex_cache = referrer->GetDexCache();
1502 Class* resolved = dex_cache->GetResolvedClass(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001503 if (resolved != NULL) {
1504 return resolved;
1505 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001506 const char* descriptor = dex_file.dexStringByTypeIdx(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001507 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001508 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001509 } else {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001510 resolved = FindClass(descriptor, referrer->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001511 }
1512 if (resolved != NULL) {
1513 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001514 if (referrer->GetDexCache() != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001515 if (check->GetClassLoader() != NULL) {
1516 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1517 return NULL;
1518 }
1519 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001520 dex_cache->SetResolvedClass(class_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001521 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001522 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001523 }
1524 return resolved;
1525}
1526
1527Method* ResolveMethod(const Class* referrer, uint32_t method_idx,
1528 /*MethodType*/ int method_type) {
1529 CHECK(false);
1530 return NULL;
1531}
1532
Carl Shapiro69759ea2011-07-21 18:13:35 -07001533String* ClassLinker::ResolveString(const Class* referring,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001534 uint32_t string_idx,
1535 const DexFile& dex_file) {
1536 const DexFile::StringId& string_id = dex_file.GetStringId(string_idx);
1537 int32_t utf16_length = dex_file.GetStringLength(string_id);
1538 const char* utf8_data = dex_file.GetStringData(string_id);
Jesse Wilson8989d992011-08-02 13:39:42 -07001539 String* new_string = String::AllocFromModifiedUtf8(utf16_length, utf8_data);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001540 // TODO: intern the new string
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001541 referring->GetDexCache()->SetResolvedString(string_idx, new_string);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001542 return new_string;
1543}
1544
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001545} // namespace art