blob: 78e2c31ccd7620cdcaa3e7e809b952c6ea8cecdf [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
Jesse Wilson8989d992011-08-02 13:39:42 -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);
55 java_lang_String->object_size_ = sizeof(String);
Jesse Wilson8989d992011-08-02 13:39:42 -070056 Class* char_array_class = AllocClass(java_lang_Class);
57 CHECK(char_array_class != NULL);
Jesse Wilson14150742011-07-29 19:04:44 -040058
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070059 // create storage for root classes, save away our work so far
60 class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax);
61 class_roots_->Set(kJavaLangClass, java_lang_Class);
62 class_roots_->Set(kJavaLangObject, java_lang_Object);
63 class_roots_->Set(kObjectArrayClass, object_array_class);
Jesse Wilson14150742011-07-29 19:04:44 -040064 class_roots_->Set(kJavaLangString, java_lang_String);
Jesse Wilson8989d992011-08-02 13:39:42 -070065 class_roots_->Set(kCharArrayClass, char_array_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070066 // now that these are registered, we can use AllocClass() and AllocObjectArray
Brian Carlstroma0808032011-07-18 00:39:23 -070067
Jesse Wilson8989d992011-08-02 13:39:42 -070068 String::InitClasses(java_lang_String, char_array_class);
69 // Now AllocString* can be used
70
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070071 // setup boot_class_path_ now that we can use AllocObjectArray to
72 // DexCache instances
Brian Carlstrom913af1b2011-07-23 21:41:13 -070073 for (size_t i = 0; i != boot_class_path.size(); ++i) {
74 AppendToBootClassPath(boot_class_path[i]);
75 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070076 // now we can use FindSystemClass, at least for non-arrays classes.
Brian Carlstrom913af1b2011-07-23 21:41:13 -070077
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070078 // run Object and Class to setup their dex_cache_ fields and register them in classes_.
79 // we also override their object_size_ values to accommodate the extra C++ fields.
80 Class* Object_class = FindSystemClass(java_lang_Object->GetDescriptor());
81 CHECK_EQ(java_lang_Object, Object_class);
82 CHECK_LE(java_lang_Object->object_size_, sizeof(Object));
83 java_lang_Object->object_size_ = sizeof(Object);
84 Class* Class_class = FindSystemClass(java_lang_Class->GetDescriptor());
85 CHECK_EQ(java_lang_Class, Class_class);
86 CHECK_LE(java_lang_Class->object_size_, sizeof(Class));
87 java_lang_Class->object_size_ = sizeof(Class);
Brian Carlstrom913af1b2011-07-23 21:41:13 -070088
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070089 // set special sizes for these C++ extended classes (Field, Method, String).
90 // we also remember them in class_roots_ to construct them within ClassLinker
91 Class* java_lang_reflect_Field = FindSystemClass("Ljava/lang/reflect/Field;");
92 CHECK(java_lang_reflect_Field != NULL);
93 CHECK_LE(java_lang_reflect_Field->object_size_, std::max(sizeof(StaticField), sizeof(InstanceField)));
94 java_lang_reflect_Field->object_size_ = std::max(sizeof(StaticField), sizeof(InstanceField));
95 class_roots_->Set(kJavaLangReflectField, java_lang_reflect_Field);
96
97 Class* java_lang_reflect_Method = FindSystemClass("Ljava/lang/reflect/Method;");
98 CHECK(java_lang_reflect_Method != NULL);
99 CHECK_LE(java_lang_reflect_Method->object_size_, sizeof(Method));
100 java_lang_reflect_Method->object_size_ = sizeof(Method);
101 class_roots_->Set(kJavaLangReflectMethod, java_lang_reflect_Method);
102
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700103 Class* String_class = FindSystemClass("Ljava/lang/String;");
104 CHECK_EQ(java_lang_String, String_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700105 CHECK_EQ(java_lang_String->object_size_, sizeof(String));
106 java_lang_String->object_size_ = sizeof(String);
107 class_roots_->Set(kJavaLangString, java_lang_String);
108
109 // Setup a single, global copy of "interfaces" and "iftable" for
110 // reuse across array classes
111 Class* java_lang_Cloneable = AllocClass();
112 CHECK(java_lang_Cloneable != NULL);
113 java_lang_Cloneable->descriptor_ = "Ljava/lang/Cloneable;";
114
115 Class* java_io_Serializable = AllocClass();
116 CHECK(java_io_Serializable != NULL);
117 java_io_Serializable->descriptor_ = "Ljava/io/Serializable;";
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700118
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700119 array_interfaces_ = AllocObjectArray<Class>(2);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700120 CHECK(array_interfaces_ != NULL);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700121 array_interfaces_->Set(0, java_lang_Cloneable);
122 array_interfaces_->Set(1, java_io_Serializable);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700123
124 // We assume that Cloneable/Serializable don't have superinterfaces --
125 // normally we'd have to crawl up and explicitly list all of the
126 // supers as well. These interfaces don't have any methods, so we
127 // don't have to worry about the ifviPool either.
128 array_iftable_ = new InterfaceEntry[2];
129 CHECK(array_iftable_ != NULL);
130 memset(array_iftable_, 0, sizeof(InterfaceEntry) * 2);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700131 array_iftable_[0].SetClass(array_interfaces_->Get(0));
132 array_iftable_[1].SetClass(array_interfaces_->Get(1));
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700133 // now FindClass can be used for non-primitive array classes
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700134
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700135 // run Object[] through FindClass to complete initialization
Jesse Wilson8989d992011-08-02 13:39:42 -0700136 Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;");
137 CHECK_EQ(object_array_class, found_object_array_class);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700138
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700139 // Setup the primitive type classes.
140 class_roots_->Set(kPrimitiveByte, CreatePrimitiveClass("B"));
141 class_roots_->Set(kPrimitiveChar, CreatePrimitiveClass("C"));
142 class_roots_->Set(kPrimitiveDouble, CreatePrimitiveClass("D"));
143 class_roots_->Set(kPrimitiveFloat, CreatePrimitiveClass("F"));
144 class_roots_->Set(kPrimitiveInt, CreatePrimitiveClass("I"));
145 class_roots_->Set(kPrimitiveLong, CreatePrimitiveClass("J"));
146 class_roots_->Set(kPrimitiveShort, CreatePrimitiveClass("S"));
147 class_roots_->Set(kPrimitiveBoolean, CreatePrimitiveClass("Z"));
148 class_roots_->Set(kPrimitiveVoid, CreatePrimitiveClass("V"));
149 // now we can use FindSystemClass for anything, including for "[C"
150
Jesse Wilson8989d992011-08-02 13:39:42 -0700151 // run char[] through FindClass to complete initialization
152 Class* found_char_array_class = FindSystemClass("[C");
153 CHECK_EQ(char_array_class, found_char_array_class);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700154
155 // ensure all class_roots_ were initialized
156 for (size_t i = 0; i < kClassRootsMax; i++) {
157 CHECK(class_roots_->Get(i) != NULL);
158 }
159
160 // disable the slow paths in FindClass and CreatePrimitiveClass now
161 // that Object, Class, and Object[] are setup
162 init_done_ = true;
163}
164
Brian Carlstromb88e9442011-07-28 15:15:51 -0700165void ClassLinker::VisitRoots(RootVistor* root_visitor, void* arg) {
166 root_visitor(class_roots_, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700167
168 for (size_t i = 0; i < dex_caches_.size(); i++) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700169 root_visitor(dex_caches_[i], arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700170 }
171
172 // TODO: acquire classes_lock_
173 typedef Table::const_iterator It; // TODO: C++0x auto
174 for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) {
Brian Carlstromb88e9442011-07-28 15:15:51 -0700175 root_visitor(it->second, arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700176 }
177 // TODO: release classes_lock_
178
Brian Carlstromb88e9442011-07-28 15:15:51 -0700179 root_visitor(array_interfaces_, arg);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700180}
181
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700182DexCache* ClassLinker::AllocDexCache() {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700183 return down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::kMax));
Brian Carlstroma0808032011-07-18 00:39:23 -0700184}
185
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700186Class* ClassLinker::AllocClass(Class* java_lang_Class) {
187 return down_cast<Class*>(Object::Alloc(java_lang_Class));
188}
189
190Class* ClassLinker::AllocClass() {
191 return AllocClass(class_roots_->Get(kJavaLangClass));
Brian Carlstroma0808032011-07-18 00:39:23 -0700192}
193
194StaticField* ClassLinker::AllocStaticField() {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700195 return down_cast<StaticField*>(Heap::AllocObject(class_roots_->Get(kJavaLangReflectField),
Carl Shapiro69759ea2011-07-21 18:13:35 -0700196 sizeof(StaticField)));
Brian Carlstroma0808032011-07-18 00:39:23 -0700197}
198
199InstanceField* ClassLinker::AllocInstanceField() {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700200 return down_cast<InstanceField*>(Heap::AllocObject(class_roots_->Get(kJavaLangReflectField),
Carl Shapiro69759ea2011-07-21 18:13:35 -0700201 sizeof(InstanceField)));
Brian Carlstroma0808032011-07-18 00:39:23 -0700202}
203
204Method* ClassLinker::AllocMethod() {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700205 return down_cast<Method*>(Heap::AllocObject(class_roots_->Get(kJavaLangReflectMethod),
Carl Shapiro69759ea2011-07-21 18:13:35 -0700206 sizeof(Method)));
Carl Shapiro565f5072011-07-10 13:39:43 -0700207}
208
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700209Class* ClassLinker::FindClass(const StringPiece& descriptor,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700210 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -0700211 const DexFile* dex_file) {
Carl Shapirob5573532011-07-12 18:22:59 -0700212 Thread* self = Thread::Current();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700213 DCHECK(self != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700214 CHECK(!self->IsExceptionPending());
215 // Find the class in the loaded classes table.
216 Class* klass = LookupClass(descriptor, class_loader);
217 if (klass == NULL) {
218 // Class is not yet loaded.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700219 if (descriptor[0] == '[') {
Brian Carlstromf615a612011-07-23 12:50:34 -0700220 return CreateArrayClass(descriptor, class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700221 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700222 ClassPathEntry pair;
Brian Carlstromf615a612011-07-23 12:50:34 -0700223 if (dex_file == NULL) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700224 pair = FindInBootClassPath(descriptor);
225 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700226 pair.first = dex_file;
227 pair.second = dex_file->FindClassDef(descriptor);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700228 }
229 if (pair.second == NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700230 LG << "Class " << descriptor << " not found"; // TODO: NoClassDefFoundError
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700231 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700232 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700233 const DexFile* dex_file = pair.first;
234 const DexFile::ClassDef* dex_class_def = pair.second;
235 DexCache* dex_cache = FindDexCache(dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700236 // Load the class from the dex file.
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700237 if (!init_done_) {
238 // finish up init of hand crafted class_roots_
239 if (descriptor == "Ljava/lang/Object;") {
240 klass = class_roots_->Get(kJavaLangObject);
241 } else if (descriptor == "Ljava/lang/Class;") {
242 klass = class_roots_->Get(kJavaLangClass);
Jesse Wilson14150742011-07-29 19:04:44 -0400243 } else if (descriptor == "Ljava/lang/String;") {
244 klass = class_roots_->Get(kJavaLangString);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700245 } else {
246 klass = AllocClass();
247 }
Carl Shapiro565f5072011-07-10 13:39:43 -0700248 } else {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700249 klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700250 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700251 klass->dex_cache_ = dex_cache;
Brian Carlstromf615a612011-07-23 12:50:34 -0700252 LoadClass(*dex_file, *dex_class_def, klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700253 // Check for a pending exception during load
254 if (self->IsExceptionPending()) {
255 // TODO: free native allocations in klass
256 return NULL;
257 }
258 {
259 ObjectLock lock(klass);
Carl Shapirob5573532011-07-12 18:22:59 -0700260 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700261 // Add the newly loaded class to the loaded classes table.
262 bool success = InsertClass(klass);
263 if (!success) {
264 // We may fail to insert if we raced with another thread.
265 klass->clinit_thread_id_ = 0;
266 // TODO: free native allocations in klass
267 klass = LookupClass(descriptor, class_loader);
268 CHECK(klass != NULL);
269 } else {
270 // Link the class.
Brian Carlstromf615a612011-07-23 12:50:34 -0700271 if (!LinkClass(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700272 // Linking failed.
273 // TODO: CHECK(self->IsExceptionPending());
274 lock.NotifyAll();
275 return NULL;
276 }
277 }
278 }
279 }
280 // Link the class if it has not already been linked.
281 if (!klass->IsLinked() && !klass->IsErroneous()) {
282 ObjectLock lock(klass);
283 // Check for circular dependencies between classes.
Carl Shapirob5573532011-07-12 18:22:59 -0700284 if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700285 LG << "Recursive link"; // TODO: ClassCircularityError
286 return NULL;
287 }
288 // Wait for the pending initialization to complete.
289 while (!klass->IsLinked() && !klass->IsErroneous()) {
290 lock.Wait();
291 }
292 }
293 if (klass->IsErroneous()) {
294 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
295 return NULL;
296 }
297 // Return the loaded class. No exceptions should be pending.
298 CHECK(!self->IsExceptionPending());
299 return klass;
300}
301
Brian Carlstromf615a612011-07-23 12:50:34 -0700302void ClassLinker::LoadClass(const DexFile& dex_file,
303 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700304 Class* klass) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700305 CHECK(klass != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700306 CHECK(klass->dex_cache_ != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700307 const byte* class_data = dex_file.GetClassData(dex_class_def);
308 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700309
Brian Carlstromf615a612011-07-23 12:50:34 -0700310 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700311 CHECK(descriptor != NULL);
312
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700313 klass->klass_ = class_roots_->Get(kJavaLangClass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700314 klass->descriptor_.set(descriptor);
315 klass->descriptor_alloc_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700316 klass->access_flags_ = dex_class_def.access_flags_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700317 klass->class_loader_ = NULL; // TODO
318 klass->primitive_type_ = Class::kPrimNot;
319 klass->status_ = Class::kStatusIdx;
320
321 klass->super_class_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700322 klass->super_class_idx_ = dex_class_def.superclass_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700323
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700324 size_t num_static_fields = header.static_fields_size_;
325 size_t num_instance_fields = header.instance_fields_size_;
326 size_t num_direct_methods = header.direct_methods_size_;
327 size_t num_virtual_methods = header.virtual_methods_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700328
Brian Carlstromf615a612011-07-23 12:50:34 -0700329 klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700330
331 // Load class interfaces.
Brian Carlstromf615a612011-07-23 12:50:34 -0700332 LoadInterfaces(dex_file, dex_class_def, klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700333
334 // Load static fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700335 DCHECK(klass->sfields_ == NULL);
336 if (num_static_fields != 0) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700337 klass->sfields_ = AllocObjectArray<StaticField>(num_static_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700338 uint32_t last_idx = 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700339 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700340 DexFile::Field dex_field;
341 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700342 StaticField* sfield = AllocStaticField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700343 klass->SetStaticField(i, sfield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700344 LoadField(dex_file, dex_field, klass, sfield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700345 }
346 }
347
348 // Load instance fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700349 DCHECK(klass->ifields_ == NULL);
350 if (num_instance_fields != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700351 // TODO: allocate on the object heap.
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700352 klass->ifields_ = AllocObjectArray<InstanceField>(num_instance_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700353 uint32_t last_idx = 0;
354 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700355 DexFile::Field dex_field;
356 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700357 InstanceField* ifield = AllocInstanceField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700358 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700359 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700360 }
361 }
362
363 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700364 DCHECK(klass->direct_methods_ == NULL);
365 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700366 // TODO: append direct methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700367 klass->direct_methods_ = AllocObjectArray<Method>(num_direct_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700368 uint32_t last_idx = 0;
369 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700370 DexFile::Method dex_method;
371 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700372 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700373 klass->SetDirectMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700374 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700375 // TODO: register maps
376 }
377 }
378
379 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700380 DCHECK(klass->virtual_methods_ == NULL);
381 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700382 // TODO: append virtual methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700383 klass->virtual_methods_ = AllocObjectArray<Method>(num_virtual_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700384 uint32_t last_idx = 0;
385 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700386 DexFile::Method dex_method;
387 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700388 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700389 klass->SetVirtualMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700390 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700391 // TODO: register maps
392 }
393 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700394}
395
Brian Carlstromf615a612011-07-23 12:50:34 -0700396void ClassLinker::LoadInterfaces(const DexFile& dex_file,
397 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700398 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700399 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700400 if (list != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700401 DCHECK(klass->interfaces_ == NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700402 klass->interfaces_ = AllocObjectArray<Class>(list->Size());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700403 DCHECK(klass->interfaces_idx_ == NULL);
404 klass->interfaces_idx_ = new uint32_t[list->Size()];
Brian Carlstrom934486c2011-07-12 23:42:50 -0700405 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700406 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700407 klass->interfaces_idx_[i] = type_item.type_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700408 }
409 }
410}
411
Brian Carlstromf615a612011-07-23 12:50:34 -0700412void ClassLinker::LoadField(const DexFile& dex_file,
413 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700414 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700415 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700416 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700417 dst->klass_ = klass;
Jesse Wilson14150742011-07-29 19:04:44 -0400418 dst->java_name_ = ResolveString(klass, field_id.name_idx_);
Brian Carlstromae3ac012011-07-27 01:30:28 -0700419 dst->descriptor_.set(dex_file.dexStringByTypeIdx(field_id.type_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700420 dst->access_flags_ = src.access_flags_;
421}
422
Brian Carlstromf615a612011-07-23 12:50:34 -0700423void ClassLinker::LoadMethod(const DexFile& dex_file,
424 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700425 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700426 Method* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700427 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700428 dst->klass_ = klass;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700429 dst->java_name_ = ResolveString(klass, method_id.name_idx_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700430 {
431 int32_t utf16_length;
432 scoped_ptr<char> utf8(dex_file.CreateMethodDescriptor(method_id.proto_idx_,
433 &utf16_length));
434 dst->descriptor_ = String::AllocFromModifiedUtf8(utf16_length, utf8.get());
435 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700436 dst->proto_idx_ = method_id.proto_idx_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700437 dst->shorty_ = dex_file.GetShorty(method_id.proto_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700438 dst->access_flags_ = src.access_flags_;
439
440 // TODO: check for finalize method
441
Brian Carlstromf615a612011-07-23 12:50:34 -0700442 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700443 if (code_item != NULL) {
444 dst->num_registers_ = code_item->registers_size_;
445 dst->num_ins_ = code_item->ins_size_;
446 dst->num_outs_ = code_item->outs_size_;
447 dst->insns_ = code_item->insns_;
448 } else {
449 uint16_t num_args = dst->NumArgRegisters();
450 if (!dst->IsStatic()) {
451 ++num_args;
452 }
453 dst->num_registers_ = dst->num_ins_ + num_args;
454 // TODO: native methods
455 }
456}
457
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700458ClassLinker::ClassPathEntry ClassLinker::FindInBootClassPath(const StringPiece& descriptor) {
459 for (size_t i = 0; i != boot_class_path_.size(); ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700460 const DexFile* dex_file = boot_class_path_[i];
Brian Carlstromf615a612011-07-23 12:50:34 -0700461 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
462 if (dex_class_def != NULL) {
463 return ClassPathEntry(dex_file, dex_class_def);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700464 }
465 }
Brian Carlstroma0808032011-07-18 00:39:23 -0700466 return ClassPathEntry(NULL, NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700467}
468
Brian Carlstromf615a612011-07-23 12:50:34 -0700469void ClassLinker::AppendToBootClassPath(DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700470 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700471 boot_class_path_.push_back(dex_file);
472 RegisterDexFile(dex_file);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700473}
474
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700475void ClassLinker::RegisterDexFile(const DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700476 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700477 dex_files_.push_back(dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700478 DexCache* dex_cache = AllocDexCache();
479 CHECK(dex_cache != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700480 dex_cache->Init(AllocObjectArray<String>(dex_file->NumStringIds()),
481 AllocObjectArray<Class>(dex_file->NumTypeIds()),
482 AllocObjectArray<Method>(dex_file->NumMethodIds()),
483 AllocObjectArray<Field>(dex_file->NumFieldIds()));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700484 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700485}
486
Brian Carlstromf615a612011-07-23 12:50:34 -0700487const DexFile* ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700488 CHECK(dex_cache != NULL);
489 for (size_t i = 0; i != dex_caches_.size(); ++i) {
490 if (dex_caches_[i] == dex_cache) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700491 return dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700492 }
493 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700494 CHECK(false) << "Could not find DexFile";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700495 return NULL;
496}
497
Brian Carlstromf615a612011-07-23 12:50:34 -0700498DexCache* ClassLinker::FindDexCache(const DexFile* dex_file) const {
499 CHECK(dex_file != NULL);
500 for (size_t i = 0; i != dex_files_.size(); ++i) {
501 if (dex_files_[i] == dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700502 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700503 }
504 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700505 CHECK(false) << "Could not find DexCache";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700506 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700507}
508
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700509Class* ClassLinker::CreatePrimitiveClass(const StringPiece& descriptor) {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700510 Class* klass = AllocClass();
Carl Shapiro565f5072011-07-10 13:39:43 -0700511 CHECK(klass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700512 klass->super_class_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700513 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
514 klass->descriptor_ = descriptor;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700515 klass->descriptor_alloc_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700516 klass->status_ = Class::kStatusInitialized;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700517 bool success = InsertClass(klass);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700518 CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed";
Carl Shapiro565f5072011-07-10 13:39:43 -0700519 return klass;
520}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700521
Brian Carlstrombe977852011-07-19 14:54:54 -0700522// Create an array class (i.e. the class object for the array, not the
523// array itself). "descriptor" looks like "[C" or "[[[[B" or
524// "[Ljava/lang/String;".
525//
526// If "descriptor" refers to an array of primitives, look up the
527// primitive type's internally-generated class object.
528//
529// "loader" is the class loader of the class that's referring to us. It's
530// used to ensure that we're looking for the element type in the right
531// context. It does NOT become the class loader for the array class; that
532// always comes from the base element class.
533//
534// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700535Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
536 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -0700537 const DexFile* dex_file)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700538{
539 CHECK(descriptor[0] == '[');
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700540
Brian Carlstrombe977852011-07-19 14:54:54 -0700541 // Identify the underlying element class and the array dimension depth.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700542 Class* component_type_ = NULL;
543 int array_rank;
544 if (descriptor[1] == '[') {
Brian Carlstrombe977852011-07-19 14:54:54 -0700545 // array of arrays; keep descriptor and grab stuff from parent
Brian Carlstromf615a612011-07-23 12:50:34 -0700546 Class* outer = FindClass(descriptor.substr(1), class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700547 if (outer != NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700548 // want the base class, not "outer", in our component_type_
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700549 component_type_ = outer->component_type_;
550 array_rank = outer->array_rank_ + 1;
551 } else {
Brian Carlstrombe977852011-07-19 14:54:54 -0700552 DCHECK(component_type_ == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700553 }
554 } else {
555 array_rank = 1;
556 if (descriptor[1] == 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -0700557 // array of objects; strip off "[" and look up descriptor.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700558 const StringPiece subDescriptor = descriptor.substr(1);
Brian Carlstromf615a612011-07-23 12:50:34 -0700559 component_type_ = FindClass(subDescriptor, class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700560 } else {
Brian Carlstrombe977852011-07-19 14:54:54 -0700561 // array of a primitive type
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700562 component_type_ = FindPrimitiveClass(descriptor[1]);
563 }
564 }
565
566 if (component_type_ == NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700567 // failed
Brian Carlstrom8ecd08c2011-07-27 17:50:51 -0700568 // DCHECK(Thread::Current()->IsExceptionPending()); // TODO
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700569 return NULL;
570 }
571
Brian Carlstrombe977852011-07-19 14:54:54 -0700572 // See if the component type is already loaded. Array classes are
573 // always associated with the class loader of their underlying
574 // element type -- an array of Strings goes with the loader for
575 // java/lang/String -- so we need to look for it there. (The
576 // caller should have checked for the existence of the class
577 // before calling here, but they did so with *their* class loader,
578 // not the component type's loader.)
579 //
580 // If we find it, the caller adds "loader" to the class' initiating
581 // loader list, which should prevent us from going through this again.
582 //
583 // This call is unnecessary if "loader" and "component_type_->class_loader_"
584 // are the same, because our caller (FindClass) just did the
585 // lookup. (Even if we get this wrong we still have correct behavior,
586 // because we effectively do this lookup again when we add the new
587 // class to the hash table --- necessary because of possible races with
588 // other threads.)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700589 if (class_loader != component_type_->class_loader_) {
590 Class* new_class = LookupClass(descriptor, component_type_->class_loader_);
591 if (new_class != NULL) {
592 return new_class;
593 }
594 }
595
Brian Carlstrombe977852011-07-19 14:54:54 -0700596 // Fill out the fields in the Class.
597 //
598 // It is possible to execute some methods against arrays, because
599 // all arrays are subclasses of java_lang_Object_, so we need to set
600 // up a vtable. We can just point at the one in java_lang_Object_.
601 //
602 // Array classes are simple enough that we don't need to do a full
603 // link step.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700604
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700605 Class* new_class = NULL;
Jesse Wilson8989d992011-08-02 13:39:42 -0700606 if (!init_done_) {
607 if (descriptor == "[Ljava/lang/Object;") {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700608 new_class = class_roots_->Get(kObjectArrayClass);
609 CHECK(new_class);
Jesse Wilson8989d992011-08-02 13:39:42 -0700610 } else if (descriptor == "[C") {
611 new_class = class_roots_->Get(kCharArrayClass);
612 CHECK(new_class);
613 }
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700614 }
615 if (new_class == NULL) {
616 new_class = AllocClass();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700617 if (new_class == NULL) {
618 return NULL;
619 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700620 }
621 new_class->descriptor_alloc_ = new std::string(descriptor.data(),
622 descriptor.size());
623 new_class->descriptor_.set(new_class->descriptor_alloc_->data(),
624 new_class->descriptor_alloc_->size());
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700625 Class* java_lang_Object = class_roots_->Get(kJavaLangObject);
626 new_class->super_class_ = java_lang_Object;
627 new_class->vtable_ = java_lang_Object->vtable_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700628 new_class->primitive_type_ = Class::kPrimNot;
629 new_class->component_type_ = component_type_;
630 new_class->class_loader_ = component_type_->class_loader_;
631 new_class->array_rank_ = array_rank;
632 new_class->status_ = Class::kStatusInitialized;
Brian Carlstrombe977852011-07-19 14:54:54 -0700633 // don't need to set new_class->object_size_
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700634
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700635
Brian Carlstrombe977852011-07-19 14:54:54 -0700636 // All arrays have java/lang/Cloneable and java/io/Serializable as
637 // interfaces. We need to set that up here, so that stuff like
638 // "instanceof" works right.
639 //
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700640 // Note: The GC could run during the call to FindSystemClass,
Brian Carlstrombe977852011-07-19 14:54:54 -0700641 // so we need to make sure the class object is GC-valid while we're in
642 // there. Do this by clearing the interface list so the GC will just
643 // think that the entries are null.
Brian Carlstrombe977852011-07-19 14:54:54 -0700644
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700645
646 // Use the single, global copies of "interfaces" and "iftable"
647 // (remember not to free them for arrays).
648 DCHECK(array_interfaces_ != NULL);
649 new_class->interfaces_ = array_interfaces_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700650 new_class->iftable_count_ = 2;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700651 DCHECK(array_iftable_ != NULL);
652 new_class->iftable_ = array_iftable_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700653
Brian Carlstrombe977852011-07-19 14:54:54 -0700654 // Inherit access flags from the component type. Arrays can't be
655 // used as a superclass or interface, so we want to add "final"
656 // and remove "interface".
657 //
658 // Don't inherit any non-standard flags (e.g., kAccFinal)
659 // from component_type_. We assume that the array class does not
660 // override finalize().
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700661 new_class->access_flags_ = ((new_class->component_type_->access_flags_ &
662 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask;
663
664 if (InsertClass(new_class)) {
665 return new_class;
666 }
Brian Carlstrombe977852011-07-19 14:54:54 -0700667 // Another thread must have loaded the class after we
668 // started but before we finished. Abandon what we've
669 // done.
670 //
671 // (Yes, this happens.)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700672
Brian Carlstrombe977852011-07-19 14:54:54 -0700673 // Grab the winning class.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700674 Class* other_class = LookupClass(descriptor, component_type_->class_loader_);
675 DCHECK(other_class != NULL);
676 return other_class;
677}
678
679Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700680 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700681 case 'B':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700682 DCHECK(class_roots_->Get(kPrimitiveByte) != NULL);
683 return class_roots_->Get(kPrimitiveByte);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700684 case 'C':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700685 DCHECK(class_roots_->Get(kPrimitiveChar) != NULL);
686 return class_roots_->Get(kPrimitiveChar);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700687 case 'D':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700688 DCHECK(class_roots_->Get(kPrimitiveDouble) != NULL);
689 return class_roots_->Get(kPrimitiveDouble);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700690 case 'F':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700691 DCHECK(class_roots_->Get(kPrimitiveFloat) != NULL);
692 return class_roots_->Get(kPrimitiveFloat);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700693 case 'I':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700694 DCHECK(class_roots_->Get(kPrimitiveInt) != NULL);
695 return class_roots_->Get(kPrimitiveInt);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700696 case 'J':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700697 DCHECK(class_roots_->Get(kPrimitiveLong) != NULL);
698 return class_roots_->Get(kPrimitiveLong);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700699 case 'S':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700700 DCHECK(class_roots_->Get(kPrimitiveShort) != NULL);
701 return class_roots_->Get(kPrimitiveShort);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700702 case 'Z':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700703 DCHECK(class_roots_->Get(kPrimitiveBoolean) != NULL);
704 return class_roots_->Get(kPrimitiveBoolean);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700705 case 'V':
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700706 DCHECK(class_roots_->Get(kPrimitiveVoid) != NULL);
707 return class_roots_->Get(kPrimitiveVoid);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700708 case 'L':
709 case '[':
710 LOG(ERROR) << "Not a primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700711 default:
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700712 LOG(ERROR) << "Unknown primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700713 };
714 return NULL; // Not reachable.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700715}
716
717bool ClassLinker::InsertClass(Class* klass) {
718 // TODO: acquire classes_lock_
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700719 const StringPiece& key = klass->GetDescriptor();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700720 bool success = classes_.insert(std::make_pair(key, klass)).second;
721 // TODO: release classes_lock_
722 return success;
723}
724
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700725Class* ClassLinker::LookupClass(const StringPiece& descriptor, Object* class_loader) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700726 // TODO: acquire classes_lock_
727 Table::iterator it = classes_.find(descriptor);
728 // TODO: release classes_lock_
729 if (it == classes_.end()) {
730 return NULL;
731 } else {
732 return (*it).second;
733 }
734}
735
736bool ClassLinker::InitializeClass(Class* klass) {
737 CHECK(klass->GetStatus() == Class::kStatusResolved ||
738 klass->GetStatus() == Class::kStatusError);
739
Carl Shapirob5573532011-07-12 18:22:59 -0700740 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700741
742 {
743 ObjectLock lock(klass);
744
745 if (klass->GetStatus() < Class::kStatusVerified) {
746 if (klass->IsErroneous()) {
747 LG << "re-initializing failed class"; // TODO: throw
748 return false;
749 }
750
751 CHECK(klass->GetStatus() == Class::kStatusResolved);
752
753 klass->status_ = Class::kStatusVerifying;
754 if (!DexVerify::VerifyClass(klass)) {
755 LG << "Verification failed"; // TODO: ThrowVerifyError
756 Object* exception = self->GetException();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700757 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
758 klass->SetFieldObject(field_offset, exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700759 klass->SetStatus(Class::kStatusError);
760 return false;
761 }
762
763 klass->SetStatus(Class::kStatusVerified);
764 }
765
766 if (klass->status_ == Class::kStatusInitialized) {
767 return true;
768 }
769
770 while (klass->status_ == Class::kStatusInitializing) {
771 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -0700772 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700773 LG << "recursive <clinit>";
774 return true;
775 }
776
777 CHECK(!self->IsExceptionPending());
778
779 lock.Wait(); // TODO: check for interruption
780
781 // When we wake up, repeat the test for init-in-progress. If
782 // there's an exception pending (only possible if
783 // "interruptShouldThrow" was set), bail out.
784 if (self->IsExceptionPending()) {
785 CHECK(false);
786 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
787 klass->SetStatus(Class::kStatusError);
788 return false;
789 }
790 if (klass->GetStatus() == Class::kStatusInitializing) {
791 continue;
792 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700793 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700794 klass->GetStatus() == Class::kStatusError);
795 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700796 // The caller wants an exception, but it was thrown in a
797 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700798 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
799 return false;
800 }
801 return true; // otherwise, initialized
802 }
803
804 // see if we failed previously
805 if (klass->IsErroneous()) {
806 // might be wise to unlock before throwing; depends on which class
807 // it is that we have locked
808
809 // TODO: throwEarlierClassFailure(klass);
810 return false;
811 }
812
813 if (!ValidateSuperClassDescriptors(klass)) {
814 klass->SetStatus(Class::kStatusError);
815 return false;
816 }
817
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700818 DCHECK(klass->status_ < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700819
Carl Shapirob5573532011-07-12 18:22:59 -0700820 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700821 klass->status_ = Class::kStatusInitializing;
822 }
823
824 if (!InitializeSuperClass(klass)) {
825 return false;
826 }
827
828 InitializeStaticFields(klass);
829
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700830 Method* clinit = klass->FindDeclaredDirectMethod("<clinit>", "()V");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700831 if (clinit != NULL) {
832 } else {
833 // JValue unused;
834 // TODO: dvmCallMethod(self, method, NULL, &unused);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700835 //CHECK(!"unimplemented");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700836 }
837
838 {
839 ObjectLock lock(klass);
840
841 if (self->IsExceptionPending()) {
842 klass->SetStatus(Class::kStatusError);
843 } else {
844 klass->SetStatus(Class::kStatusInitialized);
845 }
846 lock.NotifyAll();
847 }
848
849 return true;
850}
851
852bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
853 if (klass->IsInterface()) {
854 return true;
855 }
856 // begin with the methods local to the superclass
857 if (klass->HasSuperClass() &&
858 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
859 const Class* super = klass->GetSuperClass();
860 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
861 const Method* method = klass->GetVirtualMethod(i);
862 if (method != super->GetVirtualMethod(i) &&
863 !HasSameMethodDescriptorClasses(method, super, klass)) {
864 LG << "Classes resolve differently in superclass";
865 return false;
866 }
867 }
868 }
869 for (size_t i = 0; i < klass->iftable_count_; ++i) {
870 const InterfaceEntry* iftable = &klass->iftable_[i];
871 Class* interface = iftable->GetClass();
872 if (klass->GetClassLoader() != interface->GetClassLoader()) {
873 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
874 uint32_t vtable_index = iftable->method_index_array_[j];
875 const Method* method = klass->GetVirtualMethod(vtable_index);
876 if (!HasSameMethodDescriptorClasses(method, interface,
877 method->GetClass())) {
878 LG << "Classes resolve differently in interface"; // TODO: LinkageError
879 return false;
880 }
881 }
882 }
883 }
884 return true;
885}
886
887bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700888 const Class* klass1,
889 const Class* klass2) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700890 const DexFile* dex_file = FindDexFile(method->GetClass()->GetDexCache());
891 const DexFile::ProtoId& proto_id = dex_file->GetProtoId(method->proto_idx_);
892 DexFile::ParameterIterator *it;
893 for (it = dex_file->GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700894 const char* descriptor = it->GetDescriptor();
895 if (descriptor == NULL) {
896 break;
897 }
898 if (descriptor[0] == 'L' || descriptor[0] == '[') {
899 // Found a non-primitive type.
900 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
901 return false;
902 }
903 }
904 }
905 // Check the return type
Brian Carlstromf615a612011-07-23 12:50:34 -0700906 const char* descriptor = dex_file->GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700907 if (descriptor[0] == 'L' || descriptor[0] == '[') {
908 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
909 return false;
910 }
911 }
912 return true;
913}
914
915// Returns true if classes referenced by the descriptor are the
916// same classes in klass1 as they are in klass2.
917bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700918 const Class* klass1,
919 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700920 CHECK(descriptor != NULL);
921 CHECK(klass1 != NULL);
922 CHECK(klass2 != NULL);
923#if 0
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700924 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700925 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700926 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700927 // TODO: found2 == NULL
928 // TODO: lookup found1 in initiating loader list
929 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -0700930 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700931 if (found1 == found2) {
932 return true;
933 } else {
934 return false;
935 }
936 }
937#endif
938 return true;
939}
940
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700941bool ClassLinker::HasSameArgumentTypes(const Method* m1, const Method* m2) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700942 const DexFile* dex1 = FindDexFile(m1->GetClass()->GetDexCache());
943 const DexFile* dex2 = FindDexFile(m2->GetClass()->GetDexCache());
944 const DexFile::ProtoId& proto1 = dex1->GetProtoId(m1->proto_idx_);
945 const DexFile::ProtoId& proto2 = dex2->GetProtoId(m2->proto_idx_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700946
947 // TODO: compare ProtoId objects for equality and exit early
Brian Carlstromf615a612011-07-23 12:50:34 -0700948 const DexFile::TypeList* type_list1 = dex1->GetProtoParameters(proto1);
949 const DexFile::TypeList* type_list2 = dex2->GetProtoParameters(proto2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700950 size_t arity1 = (type_list1 == NULL) ? 0 : type_list1->Size();
951 size_t arity2 = (type_list2 == NULL) ? 0 : type_list2->Size();
952 if (arity1 != arity2) {
953 return false;
954 }
955
956 for (size_t i = 0; i < arity1; ++i) {
957 uint32_t type_idx1 = type_list1->GetTypeItem(i).type_idx_;
958 uint32_t type_idx2 = type_list2->GetTypeItem(i).type_idx_;
Brian Carlstromf615a612011-07-23 12:50:34 -0700959 const char* type1 = dex1->dexStringByTypeIdx(type_idx1);
960 const char* type2 = dex2->dexStringByTypeIdx(type_idx2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700961 if (strcmp(type1, type2) != 0) {
962 return false;
963 }
964 }
965
966 return true;
967}
968
969bool ClassLinker::HasSameReturnType(const Method* m1, const Method* m2) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700970 const DexFile* dex1 = FindDexFile(m1->GetClass()->GetDexCache());
971 const DexFile* dex2 = FindDexFile(m2->GetClass()->GetDexCache());
972 const DexFile::ProtoId& proto1 = dex1->GetProtoId(m1->proto_idx_);
973 const DexFile::ProtoId& proto2 = dex2->GetProtoId(m2->proto_idx_);
974 const char* type1 = dex1->dexStringByTypeIdx(proto1.return_type_idx_);
975 const char* type2 = dex2->dexStringByTypeIdx(proto2.return_type_idx_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700976 return (strcmp(type1, type2) == 0);
977}
978
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700979bool ClassLinker::InitializeSuperClass(Class* klass) {
980 CHECK(klass != NULL);
981 // TODO: assert klass lock is acquired
982 if (!klass->IsInterface() && klass->HasSuperClass()) {
983 Class* super_class = klass->GetSuperClass();
984 if (super_class->GetStatus() != Class::kStatusInitialized) {
985 CHECK(!super_class->IsInterface());
986 klass->MonitorExit();
987 bool super_initialized = InitializeClass(super_class);
988 klass->MonitorEnter();
989 // TODO: check for a pending exception
990 if (!super_initialized) {
991 klass->SetStatus(Class::kStatusError);
992 klass->NotifyAll();
993 return false;
994 }
995 }
996 }
997 return true;
998}
999
1000void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001001 size_t num_static_fields = klass->NumStaticFields();
1002 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001003 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001004 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001005 DexCache* dex_cache = klass->GetDexCache();
1006 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001007 return;
1008 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -07001009 const StringPiece& descriptor = klass->GetDescriptor();
Brian Carlstromf615a612011-07-23 12:50:34 -07001010 const DexFile* dex_file = FindDexFile(dex_cache);
1011 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
1012 CHECK(dex_class_def != NULL);
1013 const byte* addr = dex_file->GetEncodedArray(*dex_class_def);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001014 size_t array_size = DecodeUnsignedLeb128(&addr);
1015 for (size_t i = 0; i < array_size; ++i) {
1016 StaticField* field = klass->GetStaticField(i);
1017 JValue value;
Brian Carlstromf615a612011-07-23 12:50:34 -07001018 DexFile::ValueType type = dex_file->ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001019 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001020 case DexFile::kByte:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001021 field->SetByte(value.b);
1022 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001023 case DexFile::kShort:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001024 field->SetShort(value.s);
1025 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001026 case DexFile::kChar:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001027 field->SetChar(value.c);
1028 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001029 case DexFile::kInt:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001030 field->SetInt(value.i);
1031 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001032 case DexFile::kLong:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001033 field->SetLong(value.j);
1034 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001035 case DexFile::kFloat:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001036 field->SetFloat(value.f);
1037 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001038 case DexFile::kDouble:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001039 field->SetDouble(value.d);
1040 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001041 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001042 uint32_t string_idx = value.i;
1043 String* resolved = ResolveString(klass, string_idx);
1044 field->SetObject(resolved);
1045 break;
1046 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001047 case DexFile::kBoolean:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001048 field->SetBoolean(value.z);
1049 break;
Brian Carlstromf615a612011-07-23 12:50:34 -07001050 case DexFile::kNull:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001051 field->SetObject(value.l);
1052 break;
1053 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001054 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001055 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001056 }
1057}
1058
Brian Carlstromf615a612011-07-23 12:50:34 -07001059bool ClassLinker::LinkClass(Class* klass, const DexFile* dex_file) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001060 CHECK(klass->status_ == Class::kStatusIdx ||
1061 klass->status_ == Class::kStatusLoaded);
1062 if (klass->status_ == Class::kStatusIdx) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001063 if (!LinkInterfaces(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001064 return false;
1065 }
1066 }
1067 if (!LinkSuperClass(klass)) {
1068 return false;
1069 }
1070 if (!LinkMethods(klass)) {
1071 return false;
1072 }
1073 if (!LinkInstanceFields(klass)) {
1074 return false;
1075 }
1076 CreateReferenceOffsets(klass);
1077 CHECK_EQ(klass->status_, Class::kStatusLoaded);
1078 klass->status_ = Class::kStatusResolved;
1079 return true;
1080}
1081
Brian Carlstromf615a612011-07-23 12:50:34 -07001082bool ClassLinker::LinkInterfaces(Class* klass, const DexFile* dex_file) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001083 // Mark the class as loaded.
1084 klass->status_ = Class::kStatusLoaded;
Brian Carlstromf615a612011-07-23 12:50:34 -07001085 if (klass->super_class_idx_ != DexFile::kDexNoIndex) {
1086 Class* super_class = ResolveClass(klass, klass->super_class_idx_, dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001087 if (super_class == NULL) {
1088 LG << "Failed to resolve superclass";
1089 return false;
1090 }
1091 klass->super_class_ = super_class; // TODO: write barrier
1092 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001093 if (klass->NumInterfaces() > 0) {
1094 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
1095 uint32_t idx = klass->interfaces_idx_[i];
1096 klass->SetInterface(i, ResolveClass(klass, idx, dex_file));
1097 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001098 LG << "Failed to resolve interface";
1099 return false;
1100 }
1101 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001102 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001103 LG << "Inaccessible interface";
1104 return false;
1105 }
1106 }
1107 }
1108 return true;
1109}
1110
1111bool ClassLinker::LinkSuperClass(Class* klass) {
1112 CHECK(!klass->IsPrimitive());
1113 const Class* super = klass->GetSuperClass();
1114 if (klass->GetDescriptor() == "Ljava/lang/Object;") {
1115 if (super != NULL) {
1116 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1117 return false;
1118 }
1119 // TODO: clear finalize attribute
1120 return true;
1121 }
1122 if (super == NULL) {
1123 LG << "No superclass defined"; // TODO: LinkageError
1124 return false;
1125 }
1126 // Verify
1127 if (super->IsFinal()) {
1128 LG << "Superclass is declared final"; // TODO: IncompatibleClassChangeError
1129 return false;
1130 }
1131 if (super->IsInterface()) {
1132 LG << "Superclass is an interface"; // TODO: IncompatibleClassChangeError
1133 return false;
1134 }
1135 if (!klass->CanAccess(super)) {
1136 LG << "Superclass is inaccessible"; // TODO: IllegalAccessError
1137 return false;
1138 }
1139 return true;
1140}
1141
1142// Populate the class vtable and itable.
1143bool ClassLinker::LinkMethods(Class* klass) {
1144 if (klass->IsInterface()) {
1145 // No vtable.
1146 size_t count = klass->NumVirtualMethods();
1147 if (!IsUint(16, count)) {
1148 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1149 return false;
1150 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001151 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001152 klass->GetVirtualMethod(i)->method_index_ = i;
1153 }
1154 } else {
1155 // Link virtual method tables
1156 LinkVirtualMethods(klass);
1157
1158 // Link interface method tables
1159 LinkInterfaceMethods(klass);
1160
1161 // Insert stubs.
1162 LinkAbstractMethods(klass);
1163 }
1164 return true;
1165}
1166
1167bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001168 if (klass->HasSuperClass()) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001169 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001170 size_t actual_count = klass->GetSuperClass()->vtable_->GetLength();
1171 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001172 // TODO: do not assign to the vtable field until it is fully constructed.
1173 klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001174 // See if any of our virtual methods override the superclass.
1175 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1176 Method* local_method = klass->GetVirtualMethod(i);
1177 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001178 for (; j < actual_count; ++j) {
1179 Method* super_method = klass->vtable_->Get(j);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001180 if (HasSameNameAndPrototype(local_method, super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001181 // Verify
1182 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001183 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001184 return false;
1185 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001186 klass->vtable_->Set(j, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001187 local_method->method_index_ = j;
1188 break;
1189 }
1190 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001191 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001192 // Not overriding, append.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001193 klass->vtable_->Set(actual_count, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001194 local_method->method_index_ = actual_count;
1195 actual_count += 1;
1196 }
1197 }
1198 if (!IsUint(16, actual_count)) {
1199 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1200 return false;
1201 }
1202 CHECK_LE(actual_count, max_count);
1203 if (actual_count < max_count) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001204 // TODO: do not assign to the vtable field until it is fully constructed.
1205 klass->vtable_ = klass->vtable_->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001206 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001207 } else {
1208 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001209 uint32_t num_virtual_methods = klass->NumVirtualMethods();
1210 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
1211 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001212 LG << "Too many methods"; // TODO: VirtualMachineError
1213 return false;
1214 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001215 // TODO: do not assign to the vtable field until it is fully constructed.
1216 klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods);
1217 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001218 klass->vtable_->Set(i, klass->GetVirtualMethod(i));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001219 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1220 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001221 }
1222 return true;
1223}
1224
1225bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1226 int pool_offset = 0;
1227 int pool_size = 0;
1228 int miranda_count = 0;
1229 int miranda_alloc = 0;
1230 size_t super_ifcount;
1231 if (klass->HasSuperClass()) {
1232 super_ifcount = klass->GetSuperClass()->iftable_count_;
1233 } else {
1234 super_ifcount = 0;
1235 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001236 size_t ifcount = super_ifcount;
1237 ifcount += klass->NumInterfaces();
1238 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1239 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001240 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001241 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001242 DCHECK(klass->iftable_count_ == 0);
1243 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001244 return true;
1245 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001246 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1247 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001248 if (super_ifcount != 0) {
1249 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1250 sizeof(InterfaceEntry) * super_ifcount);
1251 }
1252 // Flatten the interface inheritance hierarchy.
1253 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001254 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1255 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001256 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001257 if (!interf->IsInterface()) {
1258 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1259 return false;
1260 }
1261 klass->iftable_[idx++].SetClass(interf);
1262 for (size_t j = 0; j < interf->iftable_count_; j++) {
1263 klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
1264 }
1265 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001266 CHECK_EQ(idx, ifcount);
1267 klass->iftable_count_ = ifcount;
1268 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001269 return true;
1270 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001271 for (size_t i = super_ifcount; i < ifcount; i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001272 pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
1273 }
1274 if (pool_size == 0) {
1275 return true;
1276 }
1277 klass->ifvi_pool_count_ = pool_size;
1278 klass->ifvi_pool_ = new uint32_t[pool_size];
1279 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001280 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001281 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
1282 Class* interface = klass->iftable_[i].GetClass();
1283 pool_offset += interface->NumVirtualMethods(); // end here
1284 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1285 Method* interface_method = interface->GetVirtualMethod(j);
1286 int k; // must be signed
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001287 for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) {
1288 if (HasSameNameAndPrototype(interface_method, klass->vtable_->Get(k))) {
1289 if (!klass->vtable_->Get(k)->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001290 LG << "Implementation not public";
1291 return false;
1292 }
1293 klass->iftable_[i].method_index_array_[j] = k;
1294 break;
1295 }
1296 }
1297 if (k < 0) {
1298 if (miranda_count == miranda_alloc) {
1299 miranda_alloc += 8;
1300 if (miranda_list.empty()) {
1301 miranda_list.resize(miranda_alloc);
1302 } else {
1303 miranda_list.resize(miranda_alloc);
1304 }
1305 }
1306 int mir;
1307 for (mir = 0; mir < miranda_count; mir++) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001308 if (HasSameNameAndPrototype(miranda_list[mir], interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001309 break;
1310 }
1311 }
1312 // point the interface table at a phantom slot index
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001313 klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001314 if (mir == miranda_count) {
1315 miranda_list[miranda_count++] = interface_method;
1316 }
1317 }
1318 }
1319 }
1320 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001321 int old_method_count = klass->NumVirtualMethods();
1322 int new_method_count = old_method_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001323 klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001324
1325 CHECK(klass->vtable_ != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001326 int old_vtable_count = klass->vtable_->GetLength();
1327 int new_vtable_count = old_vtable_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001328 // TODO: do not assign to the vtable field until it is fully constructed.
1329 klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001330
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001331 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001332 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001333 memcpy(meth, miranda_list[i], sizeof(Method));
1334 meth->klass_ = klass;
1335 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001336 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1337 klass->SetVirtualMethod(old_method_count + i, meth);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001338 klass->vtable_->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001339 }
1340 }
1341 return true;
1342}
1343
1344void ClassLinker::LinkAbstractMethods(Class* klass) {
1345 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1346 Method* method = klass->GetVirtualMethod(i);
1347 if (method->IsAbstract()) {
1348 method->insns_ = reinterpret_cast<uint16_t*>(0xFFFFFFFF); // TODO: AbstractMethodError
1349 }
1350 }
1351}
1352
1353bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001354 int field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001355 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001356 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001357 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001358 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001359 }
1360 // Move references to the front.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001361 klass->num_reference_instance_fields_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001362 size_t i = 0;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001363 for ( ; i < klass->NumInstanceFields(); i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001364 InstanceField* pField = klass->GetInstanceField(i);
1365 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001366 if (c != '[' && c != 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001367 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1368 InstanceField* refField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001369 char rc = refField->GetType();
1370 if (rc == '[' || rc == 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001371 klass->SetInstanceField(i, refField);
1372 klass->SetInstanceField(j, pField);
1373 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001374 c = rc;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001375 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001376 break;
1377 }
1378 }
1379 } else {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001380 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001381 }
1382 if (c != '[' && c != 'L') {
1383 break;
1384 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001385 pField->SetOffset(field_offset);
1386 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001387 }
1388
1389 // Now we want to pack all of the double-wide fields together. If
1390 // we're not aligned, though, we want to shuffle one 32-bit field
1391 // into place. If we can't find one, we'll have to pad it.
Brian Carlstroma0808032011-07-18 00:39:23 -07001392 if (i != klass->NumInstanceFields() && (field_offset & 0x04) != 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001393 InstanceField* pField = klass->GetInstanceField(i);
1394 char c = pField->GetType();
1395
1396 if (c != 'J' && c != 'D') {
1397 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001398 DCHECK(c != '[');
1399 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001400 pField->SetOffset(field_offset);
1401 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001402 i++;
1403 } else {
1404 // Next field is 64-bit, so search for a 32-bit field we can
1405 // swap into it.
1406 bool found = false;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001407 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1408 InstanceField* singleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001409 char rc = singleField->GetType();
1410 if (rc != 'J' && rc != 'D') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001411 klass->SetInstanceField(i, singleField);
1412 klass->SetInstanceField(j, pField);
1413 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001414 pField->SetOffset(field_offset);
1415 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001416 found = true;
1417 i++;
1418 break;
1419 }
1420 }
1421 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001422 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001423 }
1424 }
1425 }
1426
1427 // Alignment is good, shuffle any double-wide fields forward, and
1428 // finish assigning field offsets to all fields.
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001429 DCHECK(i == klass->NumInstanceFields() || (field_offset & 0x04) == 0);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001430 for ( ; i < klass->NumInstanceFields(); i++) {
1431 InstanceField* pField = klass->GetInstanceField(i);
1432 char c = pField->GetType();
1433 if (c != 'D' && c != 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001434 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1435 InstanceField* doubleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001436 char rc = doubleField->GetType();
1437 if (rc == 'D' || rc == 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001438 klass->SetInstanceField(i, doubleField);
1439 klass->SetInstanceField(j, pField);
1440 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001441 c = rc;
1442 break;
1443 }
1444 }
1445 } else {
1446 // This is a double-wide field, leave it be.
1447 }
1448
Brian Carlstroma0808032011-07-18 00:39:23 -07001449 pField->SetOffset(field_offset);
1450 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001451 if (c == 'J' || c == 'D')
Brian Carlstroma0808032011-07-18 00:39:23 -07001452 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001453 }
1454
1455#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001456 // Make sure that all reference fields appear before
1457 // non-reference fields, and all double-wide fields are aligned.
1458 bool seen_non_ref = false;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001459 for (i = 0; i < klass->NumInstanceFields(); i++) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001460 InstanceField *pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001461 char c = pField->GetType();
1462
1463 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001464 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001465 }
1466
1467 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001468 if (!seen_non_ref) {
1469 seen_non_ref = true;
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001470 DCHECK_EQ(klass->NumReferenceInstanceFields(), i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001471 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001472 } else {
1473 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001474 }
1475 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001476 if (!seen_non_ref) {
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001477 DCHECK_EQ(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001478 }
1479#endif
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001480 klass->object_size_ = field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001481 return true;
1482}
1483
1484// Set the bitmap of reference offsets, refOffsets, from the ifields
1485// list.
1486void ClassLinker::CreateReferenceOffsets(Class* klass) {
1487 uint32_t reference_offsets = 0;
1488 if (klass->HasSuperClass()) {
1489 reference_offsets = klass->GetSuperClass()->GetReferenceOffsets();
1490 }
1491 // If our superclass overflowed, we don't stand a chance.
1492 if (reference_offsets != CLASS_WALK_SUPER) {
1493 // All of the fields that contain object references are guaranteed
1494 // to be at the beginning of the ifields list.
1495 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) {
1496 // Note that, per the comment on struct InstField, f->byteOffset
1497 // is the offset from the beginning of obj, not the offset into
1498 // obj->instanceData.
1499 const InstanceField* field = klass->GetInstanceField(i);
1500 size_t byte_offset = field->GetOffset();
1501 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001502 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001503 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1504 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001505 CHECK_NE(new_bit, 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001506 reference_offsets |= new_bit;
1507 } else {
1508 reference_offsets = CLASS_WALK_SUPER;
1509 break;
1510 }
1511 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001512 }
Brian Carlstromae3ac012011-07-27 01:30:28 -07001513 klass->SetReferenceOffsets(reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001514}
1515
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001516Class* ClassLinker::ResolveClass(const Class* referrer,
1517 uint32_t class_idx,
Brian Carlstromf615a612011-07-23 12:50:34 -07001518 const DexFile* dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001519 DexCache* dex_cache = referrer->GetDexCache();
1520 Class* resolved = dex_cache->GetResolvedClass(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001521 if (resolved != NULL) {
1522 return resolved;
1523 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001524 const char* descriptor = dex_file->dexStringByTypeIdx(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001525 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001526 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001527 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -07001528 resolved = FindClass(descriptor, referrer->GetClassLoader(), dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001529 }
1530 if (resolved != NULL) {
1531 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001532 if (referrer->GetDexCache() != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001533 if (check->GetClassLoader() != NULL) {
1534 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1535 return NULL;
1536 }
1537 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001538 dex_cache->SetResolvedClass(class_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001539 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001540 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001541 }
1542 return resolved;
1543}
1544
1545Method* ResolveMethod(const Class* referrer, uint32_t method_idx,
1546 /*MethodType*/ int method_type) {
1547 CHECK(false);
1548 return NULL;
1549}
1550
Carl Shapiro69759ea2011-07-21 18:13:35 -07001551String* ClassLinker::ResolveString(const Class* referring,
1552 uint32_t string_idx) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001553 const DexFile* dex_file = FindDexFile(referring->GetDexCache());
1554 const DexFile::StringId& string_id = dex_file->GetStringId(string_idx);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001555 int32_t utf16_length = dex_file->GetStringLength(string_id);
1556 const char* utf8_data = dex_file->GetStringData(string_id);
Jesse Wilson8989d992011-08-02 13:39:42 -07001557 String* new_string = String::AllocFromModifiedUtf8(utf16_length, utf8_data);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001558 // TODO: intern the new string
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001559 referring->GetDexCache()->SetResolvedString(string_idx, new_string);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001560 return new_string;
1561}
1562
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001563} // namespace art