blob: 6ee3af31a1ed2844abfc81e3584b333fc498a782 [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
Brian Carlstromf615a612011-07-23 12:50:34 -070023ClassLinker* ClassLinker::Create(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
Brian Carlstromf615a612011-07-23 12:50:34 -070030void ClassLinker::Init(std::vector<DexFile*> boot_class_path) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070031
Brian Carlstroma0808032011-07-18 00:39:23 -070032 // Allocate and partially initialize the Class, Object, Field, Method classes.
Brian Carlstrom934486c2011-07-12 23:42:50 -070033 // Initialization will be completed when the definitions are loaded.
Carl Shapiro69759ea2011-07-21 18:13:35 -070034 java_lang_Class_ = down_cast<Class*>(Heap::AllocObject(NULL, sizeof(Class)));
Brian Carlstroma331b3c2011-07-18 17:47:56 -070035 CHECK(java_lang_Class_ != NULL);
Carl Shapiro565f5072011-07-10 13:39:43 -070036 java_lang_Class_->descriptor_ = "Ljava/lang/Class;";
Brian Carlstroma0808032011-07-18 00:39:23 -070037 java_lang_Class_->object_size_ = sizeof(Class);
38 java_lang_Class_->klass_ = java_lang_Class_;
39
40 java_lang_Object_ = AllocClass(NULL);
Brian Carlstroma331b3c2011-07-18 17:47:56 -070041 CHECK(java_lang_Object_ != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -070042 java_lang_Object_->descriptor_ = "Ljava/lang/Object;";
43
44 java_lang_Class_->super_class_ = java_lang_Object_;
45
Brian Carlstrom913af1b2011-07-23 21:41:13 -070046 java_lang_reflect_Field_ = AllocClass(NULL);
47 CHECK(java_lang_reflect_Field_ != NULL);
48 java_lang_reflect_Field_->descriptor_ = "Ljava/lang/reflect/Field;";
Brian Carlstroma0808032011-07-18 00:39:23 -070049
Brian Carlstrom913af1b2011-07-23 21:41:13 -070050 java_lang_reflect_Method_ = AllocClass(NULL);
51 CHECK(java_lang_reflect_Method_ != NULL);
52 java_lang_reflect_Method_->descriptor_ = "Ljava/lang/reflect/Method;";
Brian Carlstroma331b3c2011-07-18 17:47:56 -070053
54 java_lang_String_ = AllocClass(NULL);
55 CHECK(java_lang_String_ != NULL);
56 java_lang_String_->descriptor_ = "Ljava/lang/String;";
57
58 // Allocate and initialize the primitive type classes.
59 primitive_byte_ = CreatePrimitiveClass("B");
60 primitive_char_ = CreatePrimitiveClass("C");
61 primitive_double_ = CreatePrimitiveClass("D");
62 primitive_float_ = CreatePrimitiveClass("F");
63 primitive_int_ = CreatePrimitiveClass("I");
64 primitive_long_ = CreatePrimitiveClass("J");
65 primitive_short_ = CreatePrimitiveClass("S");
66 primitive_boolean_ = CreatePrimitiveClass("Z");
67 primitive_void_ = CreatePrimitiveClass("V");
68
Brian Carlstrom913af1b2011-07-23 21:41:13 -070069 // object_array_class_ is needed to heap alloc DexCache instances
70 // created by AppendToBootClassPath below
71 object_array_class_ = AllocClass(NULL);
72 CHECK(object_array_class_ != NULL);
73
74 // setup boot_class_path_ so that the below array classes can be
75 // initialized using the normal FindSystemClass API
76 for (size_t i = 0; i != boot_class_path.size(); ++i) {
77 AppendToBootClassPath(boot_class_path[i]);
78 }
79
80 // A single, global copy of "interfaces" and "iftable" for reuse across array classes
81 java_lang_Cloneable_ = AllocClass(NULL);
82 CHECK(java_lang_Cloneable_ != NULL);
83 java_lang_Cloneable_->descriptor_ = "Ljava/lang/Cloneable;";
84
85 java_io_Serializable_ = AllocClass(NULL);
86 CHECK(java_io_Serializable_ != NULL);
87 java_io_Serializable_->descriptor_ = "Ljava/io/Serializable;";
88
89 array_interfaces_ = AllocObjectArray(2);
90 CHECK(array_interfaces_ != NULL);
91 array_interfaces_->Set(0, java_lang_Cloneable_);
92 array_interfaces_->Set(1, java_io_Serializable_);
93
94 // We assume that Cloneable/Serializable don't have superinterfaces --
95 // normally we'd have to crawl up and explicitly list all of the
96 // supers as well. These interfaces don't have any methods, so we
97 // don't have to worry about the ifviPool either.
98 array_iftable_ = new InterfaceEntry[2];
99 CHECK(array_iftable_ != NULL);
100 memset(array_iftable_, 0, sizeof(InterfaceEntry) * 2);
101 array_iftable_[0].SetClass(down_cast<Class*>(array_interfaces_->Get(0)));
102 array_iftable_[1].SetClass(down_cast<Class*>(array_interfaces_->Get(1)));
103
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700104 char_array_class_ = FindSystemClass("[C");
105 CHECK(char_array_class_ != NULL);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700106 class_array_class_ = FindSystemClass("[Ljava/lang/Class;");
107 CHECK(class_array_class_ != NULL);
108 field_array_class_ = FindSystemClass("[Ljava/lang/reflect/Field;");
109 CHECK(field_array_class_ != NULL);
110 method_array_class_ = FindSystemClass("[Ljava/lang/reflect/Method;");
111 CHECK(method_array_class_ != NULL);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700112
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700113}
114
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700115DexCache* ClassLinker::AllocDexCache() {
116 return down_cast<DexCache*>(Heap::AllocObjectArray(object_array_class_, DexCache::kMax));
Brian Carlstroma0808032011-07-18 00:39:23 -0700117}
118
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700119Class* ClassLinker::AllocClass(DexCache* dex_cache) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700120 Class* klass = down_cast<Class*>(Heap::AllocObject(java_lang_Class_));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700121 klass->dex_cache_ = dex_cache;
Brian Carlstroma0808032011-07-18 00:39:23 -0700122 return klass;
123}
124
125StaticField* ClassLinker::AllocStaticField() {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700126 return down_cast<StaticField*>(Heap::AllocObject(java_lang_reflect_Field_,
Carl Shapiro69759ea2011-07-21 18:13:35 -0700127 sizeof(StaticField)));
Brian Carlstroma0808032011-07-18 00:39:23 -0700128}
129
130InstanceField* ClassLinker::AllocInstanceField() {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700131 return down_cast<InstanceField*>(Heap::AllocObject(java_lang_reflect_Field_,
Carl Shapiro69759ea2011-07-21 18:13:35 -0700132 sizeof(InstanceField)));
Brian Carlstroma0808032011-07-18 00:39:23 -0700133}
134
135Method* ClassLinker::AllocMethod() {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700136 return down_cast<Method*>(Heap::AllocObject(java_lang_reflect_Method_,
Carl Shapiro69759ea2011-07-21 18:13:35 -0700137 sizeof(Method)));
Carl Shapiro565f5072011-07-10 13:39:43 -0700138}
139
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700140ObjectArray* ClassLinker::AllocObjectArray(size_t length) {
141 return Heap::AllocObjectArray(object_array_class_, length);
142}
143
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700144Class* ClassLinker::FindClass(const StringPiece& descriptor,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700145 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -0700146 const DexFile* dex_file) {
Carl Shapirob5573532011-07-12 18:22:59 -0700147 Thread* self = Thread::Current();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700148 DCHECK(self != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700149 CHECK(!self->IsExceptionPending());
150 // Find the class in the loaded classes table.
151 Class* klass = LookupClass(descriptor, class_loader);
152 if (klass == NULL) {
153 // Class is not yet loaded.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700154 if (descriptor[0] == '[') {
Brian Carlstromf615a612011-07-23 12:50:34 -0700155 return CreateArrayClass(descriptor, class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700156 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700157 ClassPathEntry pair;
Brian Carlstromf615a612011-07-23 12:50:34 -0700158 if (dex_file == NULL) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700159 pair = FindInBootClassPath(descriptor);
160 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700161 pair.first = dex_file;
162 pair.second = dex_file->FindClassDef(descriptor);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700163 }
164 if (pair.second == NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700165 LG << "Class " << descriptor << " not found"; // TODO: NoClassDefFoundError
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700166 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700167 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700168 const DexFile* dex_file = pair.first;
169 const DexFile::ClassDef* dex_class_def = pair.second;
170 DexCache* dex_cache = FindDexCache(dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700171 // Load the class from the dex file.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700172 // TODO add fast path to avoid all these comparisons once special cases are found
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700173 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700174 klass = java_lang_Object_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700175 klass->dex_cache_ = dex_cache;
Brian Carlstroma0808032011-07-18 00:39:23 -0700176 klass->object_size_ = sizeof(Object);
Brian Carlstromf615a612011-07-23 12:50:34 -0700177 char_array_class_->super_class_idx_ = dex_class_def->class_idx_;
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700178 } else if (descriptor == "Ljava/lang/Class;") {
Carl Shapiro565f5072011-07-10 13:39:43 -0700179 klass = java_lang_Class_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700180 klass->dex_cache_ = dex_cache;
Brian Carlstroma0808032011-07-18 00:39:23 -0700181 klass->object_size_ = sizeof(Class);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700182 } else if (descriptor == "Ljava/lang/reflect/Field;") {
183 klass = java_lang_reflect_Field_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700184 klass->dex_cache_ = dex_cache;
Brian Carlstroma0808032011-07-18 00:39:23 -0700185 klass->object_size_ = sizeof(Field);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700186 } else if (descriptor == "Ljava/lang/reflect/Method;") {
187 klass = java_lang_reflect_Method_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700188 klass->dex_cache_ = dex_cache;
Brian Carlstroma0808032011-07-18 00:39:23 -0700189 klass->object_size_ = sizeof(Method);
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700190 } else if (descriptor == "Ljava/lang/String;") {
Brian Carlstroma0808032011-07-18 00:39:23 -0700191 klass = java_lang_String_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700192 klass->dex_cache_ = dex_cache;
Brian Carlstroma0808032011-07-18 00:39:23 -0700193 klass->object_size_ = sizeof(String);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700194 } else if (descriptor == "Ljava/lang/Cloneable;") {
195 klass = java_lang_Cloneable_;
196 klass->dex_cache_ = dex_cache;
197 klass->object_size_ = 0;
198 } else if (descriptor == "Ljava/io/Serializable;") {
199 klass = java_io_Serializable_;
200 klass->dex_cache_ = dex_cache;
201 klass->object_size_ = 0;
Carl Shapiro565f5072011-07-10 13:39:43 -0700202 } else {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700203 klass = AllocClass(dex_cache);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700204 // LinkInstanceFields only will update object_size_ if it is 0
205 // to avoid overwriting the special initialized cases above.
206 klass->object_size_ = 0;
Carl Shapiro565f5072011-07-10 13:39:43 -0700207 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700208 LoadClass(*dex_file, *dex_class_def, klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700209 // Check for a pending exception during load
210 if (self->IsExceptionPending()) {
211 // TODO: free native allocations in klass
212 return NULL;
213 }
214 {
215 ObjectLock lock(klass);
Carl Shapirob5573532011-07-12 18:22:59 -0700216 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700217 // Add the newly loaded class to the loaded classes table.
218 bool success = InsertClass(klass);
219 if (!success) {
220 // We may fail to insert if we raced with another thread.
221 klass->clinit_thread_id_ = 0;
222 // TODO: free native allocations in klass
223 klass = LookupClass(descriptor, class_loader);
224 CHECK(klass != NULL);
225 } else {
226 // Link the class.
Brian Carlstromf615a612011-07-23 12:50:34 -0700227 if (!LinkClass(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700228 // Linking failed.
229 // TODO: CHECK(self->IsExceptionPending());
230 lock.NotifyAll();
231 return NULL;
232 }
233 }
234 }
235 }
236 // Link the class if it has not already been linked.
237 if (!klass->IsLinked() && !klass->IsErroneous()) {
238 ObjectLock lock(klass);
239 // Check for circular dependencies between classes.
Carl Shapirob5573532011-07-12 18:22:59 -0700240 if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700241 LG << "Recursive link"; // TODO: ClassCircularityError
242 return NULL;
243 }
244 // Wait for the pending initialization to complete.
245 while (!klass->IsLinked() && !klass->IsErroneous()) {
246 lock.Wait();
247 }
248 }
249 if (klass->IsErroneous()) {
250 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
251 return NULL;
252 }
253 // Return the loaded class. No exceptions should be pending.
254 CHECK(!self->IsExceptionPending());
255 return klass;
256}
257
Brian Carlstromf615a612011-07-23 12:50:34 -0700258void ClassLinker::LoadClass(const DexFile& dex_file,
259 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700260 Class* klass) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700261 CHECK(klass != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700262 CHECK(klass->dex_cache_ != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700263 const byte* class_data = dex_file.GetClassData(dex_class_def);
264 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700265
Brian Carlstromf615a612011-07-23 12:50:34 -0700266 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700267 CHECK(descriptor != NULL);
268
269 klass->klass_ = java_lang_Class_;
270 klass->descriptor_.set(descriptor);
271 klass->descriptor_alloc_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700272 klass->access_flags_ = dex_class_def.access_flags_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700273 klass->class_loader_ = NULL; // TODO
274 klass->primitive_type_ = Class::kPrimNot;
275 klass->status_ = Class::kStatusIdx;
276
277 klass->super_class_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700278 klass->super_class_idx_ = dex_class_def.superclass_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700279
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700280 size_t num_static_fields = header.static_fields_size_;
281 size_t num_instance_fields = header.instance_fields_size_;
282 size_t num_direct_methods = header.direct_methods_size_;
283 size_t num_virtual_methods = header.virtual_methods_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700284
Brian Carlstromf615a612011-07-23 12:50:34 -0700285 klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700286
287 // Load class interfaces.
Brian Carlstromf615a612011-07-23 12:50:34 -0700288 LoadInterfaces(dex_file, dex_class_def, klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700289
290 // Load static fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700291 DCHECK(klass->sfields_ == NULL);
292 if (num_static_fields != 0) {
293 klass->sfields_ = AllocObjectArray(num_static_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700294 uint32_t last_idx = 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700295 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700296 DexFile::Field dex_field;
297 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700298 StaticField* sfield = AllocStaticField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700299 klass->SetStaticField(i, sfield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700300 LoadField(dex_file, dex_field, klass, sfield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700301 }
302 }
303
304 // Load instance fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700305 DCHECK(klass->ifields_ == NULL);
306 if (num_instance_fields != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700307 // TODO: allocate on the object heap.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700308 klass->ifields_ = AllocObjectArray(num_instance_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700309 uint32_t last_idx = 0;
310 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700311 DexFile::Field dex_field;
312 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700313 InstanceField* ifield = AllocInstanceField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700314 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700315 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700316 }
317 }
318
319 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700320 DCHECK(klass->direct_methods_ == NULL);
321 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700322 // TODO: append direct methods to class object
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700323 klass->direct_methods_ = AllocObjectArray(num_direct_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700324 uint32_t last_idx = 0;
325 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700326 DexFile::Method dex_method;
327 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700328 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700329 klass->SetDirectMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700330 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700331 // TODO: register maps
332 }
333 }
334
335 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700336 DCHECK(klass->virtual_methods_ == NULL);
337 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700338 // TODO: append virtual methods to class object
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700339 klass->virtual_methods_ = AllocObjectArray(num_virtual_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700340 uint32_t last_idx = 0;
341 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700342 DexFile::Method dex_method;
343 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700344 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700345 klass->SetVirtualMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700346 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700347 // TODO: register maps
348 }
349 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700350}
351
Brian Carlstromf615a612011-07-23 12:50:34 -0700352void ClassLinker::LoadInterfaces(const DexFile& dex_file,
353 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700354 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700355 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700356 if (list != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700357 DCHECK(klass->interfaces_ == NULL);
358 klass->interfaces_ = AllocObjectArray(list->Size());
359 DCHECK(klass->interfaces_idx_ == NULL);
360 klass->interfaces_idx_ = new uint32_t[list->Size()];
Brian Carlstrom934486c2011-07-12 23:42:50 -0700361 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700362 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700363 klass->interfaces_idx_[i] = type_item.type_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700364 }
365 }
366}
367
Brian Carlstromf615a612011-07-23 12:50:34 -0700368void ClassLinker::LoadField(const DexFile& dex_file,
369 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700370 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700371 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700372 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700373 dst->klass_ = klass;
Brian Carlstromf615a612011-07-23 12:50:34 -0700374 dst->name_ = dex_file.dexStringById(field_id.name_idx_);
375 dst->signature_ = dex_file.dexStringByTypeIdx(field_id.type_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700376 dst->access_flags_ = src.access_flags_;
377}
378
Brian Carlstromf615a612011-07-23 12:50:34 -0700379void ClassLinker::LoadMethod(const DexFile& dex_file,
380 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700381 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700382 Method* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700383 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700384 dst->klass_ = klass;
Brian Carlstromf615a612011-07-23 12:50:34 -0700385 dst->name_.set(dex_file.dexStringById(method_id.name_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700386 dst->proto_idx_ = method_id.proto_idx_;
Brian Carlstromf615a612011-07-23 12:50:34 -0700387 dst->shorty_.set(dex_file.GetShorty(method_id.proto_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700388 dst->access_flags_ = src.access_flags_;
389
390 // TODO: check for finalize method
391
Brian Carlstromf615a612011-07-23 12:50:34 -0700392 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700393 if (code_item != NULL) {
394 dst->num_registers_ = code_item->registers_size_;
395 dst->num_ins_ = code_item->ins_size_;
396 dst->num_outs_ = code_item->outs_size_;
397 dst->insns_ = code_item->insns_;
398 } else {
399 uint16_t num_args = dst->NumArgRegisters();
400 if (!dst->IsStatic()) {
401 ++num_args;
402 }
403 dst->num_registers_ = dst->num_ins_ + num_args;
404 // TODO: native methods
405 }
406}
407
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700408ClassLinker::ClassPathEntry ClassLinker::FindInBootClassPath(const StringPiece& descriptor) {
409 for (size_t i = 0; i != boot_class_path_.size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700410 DexFile* dex_file = boot_class_path_[i];
411 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
412 if (dex_class_def != NULL) {
413 return ClassPathEntry(dex_file, dex_class_def);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700414 }
415 }
Brian Carlstroma0808032011-07-18 00:39:23 -0700416 return ClassPathEntry(NULL, NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700417}
418
Brian Carlstromf615a612011-07-23 12:50:34 -0700419void ClassLinker::AppendToBootClassPath(DexFile* dex_file) {
420 boot_class_path_.push_back(dex_file);
421 RegisterDexFile(dex_file);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700422}
423
Brian Carlstromf615a612011-07-23 12:50:34 -0700424void ClassLinker::RegisterDexFile(DexFile* dex_file) {
425 dex_files_.push_back(dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700426 DexCache* dex_cache = AllocDexCache();
427 CHECK(dex_cache != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700428 dex_cache->Init(AllocObjectArray(dex_file->NumStringIds()),
429 AllocObjectArray(dex_file->NumTypeIds()),
430 AllocObjectArray(dex_file->NumMethodIds()),
431 AllocObjectArray(dex_file->NumFieldIds()));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700432 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700433}
434
Brian Carlstromf615a612011-07-23 12:50:34 -0700435const DexFile* ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700436 CHECK(dex_cache != NULL);
437 for (size_t i = 0; i != dex_caches_.size(); ++i) {
438 if (dex_caches_[i] == dex_cache) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700439 return dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700440 }
441 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700442 CHECK(false) << "Could not find DexFile";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700443 return NULL;
444}
445
Brian Carlstromf615a612011-07-23 12:50:34 -0700446DexCache* ClassLinker::FindDexCache(const DexFile* dex_file) const {
447 CHECK(dex_file != NULL);
448 for (size_t i = 0; i != dex_files_.size(); ++i) {
449 if (dex_files_[i] == dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700450 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700451 }
452 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700453 CHECK(false) << "Could not find DexCache";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700454 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700455}
456
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700457Class* ClassLinker::CreatePrimitiveClass(const StringPiece& descriptor) {
Brian Carlstroma0808032011-07-18 00:39:23 -0700458 Class* klass = AllocClass(NULL);
Carl Shapiro565f5072011-07-10 13:39:43 -0700459 CHECK(klass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700460 klass->super_class_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700461 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
462 klass->descriptor_ = descriptor;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700463 klass->descriptor_alloc_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700464 klass->status_ = Class::kStatusInitialized;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700465 bool success = InsertClass(klass);
466 CHECK(success);
Carl Shapiro565f5072011-07-10 13:39:43 -0700467 return klass;
468}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700469
Brian Carlstrombe977852011-07-19 14:54:54 -0700470// Create an array class (i.e. the class object for the array, not the
471// array itself). "descriptor" looks like "[C" or "[[[[B" or
472// "[Ljava/lang/String;".
473//
474// If "descriptor" refers to an array of primitives, look up the
475// primitive type's internally-generated class object.
476//
477// "loader" is the class loader of the class that's referring to us. It's
478// used to ensure that we're looking for the element type in the right
479// context. It does NOT become the class loader for the array class; that
480// always comes from the base element class.
481//
482// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700483Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
484 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -0700485 const DexFile* dex_file)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700486{
487 CHECK(descriptor[0] == '[');
488 DCHECK(java_lang_Class_ != NULL);
489 DCHECK(java_lang_Object_ != NULL);
490
Brian Carlstrombe977852011-07-19 14:54:54 -0700491 // Identify the underlying element class and the array dimension depth.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700492 Class* component_type_ = NULL;
493 int array_rank;
494 if (descriptor[1] == '[') {
Brian Carlstrombe977852011-07-19 14:54:54 -0700495 // array of arrays; keep descriptor and grab stuff from parent
Brian Carlstromf615a612011-07-23 12:50:34 -0700496 Class* outer = FindClass(descriptor.substr(1), class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700497 if (outer != NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700498 // want the base class, not "outer", in our component_type_
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700499 component_type_ = outer->component_type_;
500 array_rank = outer->array_rank_ + 1;
501 } else {
Brian Carlstrombe977852011-07-19 14:54:54 -0700502 DCHECK(component_type_ == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700503 }
504 } else {
505 array_rank = 1;
506 if (descriptor[1] == 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -0700507 // array of objects; strip off "[" and look up descriptor.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700508 const StringPiece subDescriptor = descriptor.substr(1);
Brian Carlstromf615a612011-07-23 12:50:34 -0700509 component_type_ = FindClass(subDescriptor, class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700510 } else {
Brian Carlstrombe977852011-07-19 14:54:54 -0700511 // array of a primitive type
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700512 component_type_ = FindPrimitiveClass(descriptor[1]);
513 }
514 }
515
516 if (component_type_ == NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700517 // failed
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700518 DCHECK(Thread::Current()->IsExceptionPending());
519 return NULL;
520 }
521
Brian Carlstrombe977852011-07-19 14:54:54 -0700522 // See if the component type is already loaded. Array classes are
523 // always associated with the class loader of their underlying
524 // element type -- an array of Strings goes with the loader for
525 // java/lang/String -- so we need to look for it there. (The
526 // caller should have checked for the existence of the class
527 // before calling here, but they did so with *their* class loader,
528 // not the component type's loader.)
529 //
530 // If we find it, the caller adds "loader" to the class' initiating
531 // loader list, which should prevent us from going through this again.
532 //
533 // This call is unnecessary if "loader" and "component_type_->class_loader_"
534 // are the same, because our caller (FindClass) just did the
535 // lookup. (Even if we get this wrong we still have correct behavior,
536 // because we effectively do this lookup again when we add the new
537 // class to the hash table --- necessary because of possible races with
538 // other threads.)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700539 if (class_loader != component_type_->class_loader_) {
540 Class* new_class = LookupClass(descriptor, component_type_->class_loader_);
541 if (new_class != NULL) {
542 return new_class;
543 }
544 }
545
Brian Carlstrombe977852011-07-19 14:54:54 -0700546 // Fill out the fields in the Class.
547 //
548 // It is possible to execute some methods against arrays, because
549 // all arrays are subclasses of java_lang_Object_, so we need to set
550 // up a vtable. We can just point at the one in java_lang_Object_.
551 //
552 // Array classes are simple enough that we don't need to do a full
553 // link step.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700554
555 Class* new_class;
556 if (descriptor == "[Ljava/lang/Object;") {
557 CHECK(object_array_class_);
558 new_class = object_array_class_;
559 } else {
560 new_class = AllocClass(NULL);
561 if (new_class == NULL) {
562 return NULL;
563 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700564 }
565 new_class->descriptor_alloc_ = new std::string(descriptor.data(),
566 descriptor.size());
567 new_class->descriptor_.set(new_class->descriptor_alloc_->data(),
568 new_class->descriptor_alloc_->size());
569 new_class->super_class_ = java_lang_Object_;
570 new_class->vtable_count_ = java_lang_Object_->vtable_count_;
571 new_class->vtable_ = java_lang_Object_->vtable_;
572 new_class->primitive_type_ = Class::kPrimNot;
573 new_class->component_type_ = component_type_;
574 new_class->class_loader_ = component_type_->class_loader_;
575 new_class->array_rank_ = array_rank;
576 new_class->status_ = Class::kStatusInitialized;
Brian Carlstrombe977852011-07-19 14:54:54 -0700577 // don't need to set new_class->object_size_
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700578
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700579
Brian Carlstrombe977852011-07-19 14:54:54 -0700580 // All arrays have java/lang/Cloneable and java/io/Serializable as
581 // interfaces. We need to set that up here, so that stuff like
582 // "instanceof" works right.
583 //
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700584 // Note: The GC could run during the call to FindSystemClass,
Brian Carlstrombe977852011-07-19 14:54:54 -0700585 // so we need to make sure the class object is GC-valid while we're in
586 // there. Do this by clearing the interface list so the GC will just
587 // think that the entries are null.
Brian Carlstrombe977852011-07-19 14:54:54 -0700588
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700589
590 // Use the single, global copies of "interfaces" and "iftable"
591 // (remember not to free them for arrays).
592 DCHECK(array_interfaces_ != NULL);
593 new_class->interfaces_ = array_interfaces_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700594 new_class->iftable_count_ = 2;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700595 DCHECK(array_iftable_ != NULL);
596 new_class->iftable_ = array_iftable_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700597
Brian Carlstrombe977852011-07-19 14:54:54 -0700598 // Inherit access flags from the component type. Arrays can't be
599 // used as a superclass or interface, so we want to add "final"
600 // and remove "interface".
601 //
602 // Don't inherit any non-standard flags (e.g., kAccFinal)
603 // from component_type_. We assume that the array class does not
604 // override finalize().
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700605 new_class->access_flags_ = ((new_class->component_type_->access_flags_ &
606 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask;
607
608 if (InsertClass(new_class)) {
609 return new_class;
610 }
Brian Carlstrombe977852011-07-19 14:54:54 -0700611 // Another thread must have loaded the class after we
612 // started but before we finished. Abandon what we've
613 // done.
614 //
615 // (Yes, this happens.)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700616
Brian Carlstrombe977852011-07-19 14:54:54 -0700617 // Grab the winning class.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700618 Class* other_class = LookupClass(descriptor, component_type_->class_loader_);
619 DCHECK(other_class != NULL);
620 return other_class;
621}
622
623Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700624 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700625 case 'B':
Carl Shapiro565f5072011-07-10 13:39:43 -0700626 CHECK(primitive_byte_ != NULL);
627 return primitive_byte_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700628 case 'C':
Carl Shapiro565f5072011-07-10 13:39:43 -0700629 CHECK(primitive_char_ != NULL);
630 return primitive_char_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700631 case 'D':
Carl Shapiro565f5072011-07-10 13:39:43 -0700632 CHECK(primitive_double_ != NULL);
633 return primitive_double_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700634 case 'F':
Carl Shapiro565f5072011-07-10 13:39:43 -0700635 CHECK(primitive_float_ != NULL);
636 return primitive_float_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700637 case 'I':
Carl Shapiro565f5072011-07-10 13:39:43 -0700638 CHECK(primitive_int_ != NULL);
639 return primitive_int_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700640 case 'J':
Carl Shapiro565f5072011-07-10 13:39:43 -0700641 CHECK(primitive_long_ != NULL);
642 return primitive_long_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700643 case 'S':
Carl Shapiro565f5072011-07-10 13:39:43 -0700644 CHECK(primitive_short_ != NULL);
645 return primitive_short_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700646 case 'Z':
Carl Shapiro565f5072011-07-10 13:39:43 -0700647 CHECK(primitive_boolean_ != NULL);
648 return primitive_boolean_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700649 case 'V':
Carl Shapiro565f5072011-07-10 13:39:43 -0700650 CHECK(primitive_void_ != NULL);
651 return primitive_void_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700652 case 'L':
653 case '[':
654 LOG(ERROR) << "Not a primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700655 default:
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700656 LOG(ERROR) << "Unknown primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700657 };
658 return NULL; // Not reachable.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700659}
660
661bool ClassLinker::InsertClass(Class* klass) {
662 // TODO: acquire classes_lock_
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700663 const StringPiece& key = klass->GetDescriptor();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700664 bool success = classes_.insert(std::make_pair(key, klass)).second;
665 // TODO: release classes_lock_
666 return success;
667}
668
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700669Class* ClassLinker::LookupClass(const StringPiece& descriptor, Object* class_loader) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700670 // TODO: acquire classes_lock_
671 Table::iterator it = classes_.find(descriptor);
672 // TODO: release classes_lock_
673 if (it == classes_.end()) {
674 return NULL;
675 } else {
676 return (*it).second;
677 }
678}
679
680bool ClassLinker::InitializeClass(Class* klass) {
681 CHECK(klass->GetStatus() == Class::kStatusResolved ||
682 klass->GetStatus() == Class::kStatusError);
683
Carl Shapirob5573532011-07-12 18:22:59 -0700684 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700685
686 {
687 ObjectLock lock(klass);
688
689 if (klass->GetStatus() < Class::kStatusVerified) {
690 if (klass->IsErroneous()) {
691 LG << "re-initializing failed class"; // TODO: throw
692 return false;
693 }
694
695 CHECK(klass->GetStatus() == Class::kStatusResolved);
696
697 klass->status_ = Class::kStatusVerifying;
698 if (!DexVerify::VerifyClass(klass)) {
699 LG << "Verification failed"; // TODO: ThrowVerifyError
700 Object* exception = self->GetException();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700701 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
702 klass->SetFieldObject(field_offset, exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700703 klass->SetStatus(Class::kStatusError);
704 return false;
705 }
706
707 klass->SetStatus(Class::kStatusVerified);
708 }
709
710 if (klass->status_ == Class::kStatusInitialized) {
711 return true;
712 }
713
714 while (klass->status_ == Class::kStatusInitializing) {
715 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -0700716 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700717 LG << "recursive <clinit>";
718 return true;
719 }
720
721 CHECK(!self->IsExceptionPending());
722
723 lock.Wait(); // TODO: check for interruption
724
725 // When we wake up, repeat the test for init-in-progress. If
726 // there's an exception pending (only possible if
727 // "interruptShouldThrow" was set), bail out.
728 if (self->IsExceptionPending()) {
729 CHECK(false);
730 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
731 klass->SetStatus(Class::kStatusError);
732 return false;
733 }
734 if (klass->GetStatus() == Class::kStatusInitializing) {
735 continue;
736 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700737 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700738 klass->GetStatus() == Class::kStatusError);
739 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700740 // The caller wants an exception, but it was thrown in a
741 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700742 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
743 return false;
744 }
745 return true; // otherwise, initialized
746 }
747
748 // see if we failed previously
749 if (klass->IsErroneous()) {
750 // might be wise to unlock before throwing; depends on which class
751 // it is that we have locked
752
753 // TODO: throwEarlierClassFailure(klass);
754 return false;
755 }
756
757 if (!ValidateSuperClassDescriptors(klass)) {
758 klass->SetStatus(Class::kStatusError);
759 return false;
760 }
761
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700762 DCHECK(klass->status_ < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700763
Carl Shapirob5573532011-07-12 18:22:59 -0700764 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700765 klass->status_ = Class::kStatusInitializing;
766 }
767
768 if (!InitializeSuperClass(klass)) {
769 return false;
770 }
771
772 InitializeStaticFields(klass);
773
774 Method* clinit = klass->FindDirectMethodLocally("<clinit>", "()V");
775 if (clinit != NULL) {
776 } else {
777 // JValue unused;
778 // TODO: dvmCallMethod(self, method, NULL, &unused);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700779 //CHECK(!"unimplemented");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700780 }
781
782 {
783 ObjectLock lock(klass);
784
785 if (self->IsExceptionPending()) {
786 klass->SetStatus(Class::kStatusError);
787 } else {
788 klass->SetStatus(Class::kStatusInitialized);
789 }
790 lock.NotifyAll();
791 }
792
793 return true;
794}
795
796bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
797 if (klass->IsInterface()) {
798 return true;
799 }
800 // begin with the methods local to the superclass
801 if (klass->HasSuperClass() &&
802 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
803 const Class* super = klass->GetSuperClass();
804 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
805 const Method* method = klass->GetVirtualMethod(i);
806 if (method != super->GetVirtualMethod(i) &&
807 !HasSameMethodDescriptorClasses(method, super, klass)) {
808 LG << "Classes resolve differently in superclass";
809 return false;
810 }
811 }
812 }
813 for (size_t i = 0; i < klass->iftable_count_; ++i) {
814 const InterfaceEntry* iftable = &klass->iftable_[i];
815 Class* interface = iftable->GetClass();
816 if (klass->GetClassLoader() != interface->GetClassLoader()) {
817 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
818 uint32_t vtable_index = iftable->method_index_array_[j];
819 const Method* method = klass->GetVirtualMethod(vtable_index);
820 if (!HasSameMethodDescriptorClasses(method, interface,
821 method->GetClass())) {
822 LG << "Classes resolve differently in interface"; // TODO: LinkageError
823 return false;
824 }
825 }
826 }
827 }
828 return true;
829}
830
831bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700832 const Class* klass1,
833 const Class* klass2) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700834 const DexFile* dex_file = FindDexFile(method->GetClass()->GetDexCache());
835 const DexFile::ProtoId& proto_id = dex_file->GetProtoId(method->proto_idx_);
836 DexFile::ParameterIterator *it;
837 for (it = dex_file->GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700838 const char* descriptor = it->GetDescriptor();
839 if (descriptor == NULL) {
840 break;
841 }
842 if (descriptor[0] == 'L' || descriptor[0] == '[') {
843 // Found a non-primitive type.
844 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
845 return false;
846 }
847 }
848 }
849 // Check the return type
Brian Carlstromf615a612011-07-23 12:50:34 -0700850 const char* descriptor = dex_file->GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700851 if (descriptor[0] == 'L' || descriptor[0] == '[') {
852 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
853 return false;
854 }
855 }
856 return true;
857}
858
859// Returns true if classes referenced by the descriptor are the
860// same classes in klass1 as they are in klass2.
861bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700862 const Class* klass1,
863 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700864 CHECK(descriptor != NULL);
865 CHECK(klass1 != NULL);
866 CHECK(klass2 != NULL);
867#if 0
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700868 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700869 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700870 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700871 // TODO: found2 == NULL
872 // TODO: lookup found1 in initiating loader list
873 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -0700874 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700875 if (found1 == found2) {
876 return true;
877 } else {
878 return false;
879 }
880 }
881#endif
882 return true;
883}
884
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700885bool ClassLinker::HasSameArgumentTypes(const Method* m1, const Method* m2) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700886 const DexFile* dex1 = FindDexFile(m1->GetClass()->GetDexCache());
887 const DexFile* dex2 = FindDexFile(m2->GetClass()->GetDexCache());
888 const DexFile::ProtoId& proto1 = dex1->GetProtoId(m1->proto_idx_);
889 const DexFile::ProtoId& proto2 = dex2->GetProtoId(m2->proto_idx_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700890
891 // TODO: compare ProtoId objects for equality and exit early
Brian Carlstromf615a612011-07-23 12:50:34 -0700892 const DexFile::TypeList* type_list1 = dex1->GetProtoParameters(proto1);
893 const DexFile::TypeList* type_list2 = dex2->GetProtoParameters(proto2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700894 size_t arity1 = (type_list1 == NULL) ? 0 : type_list1->Size();
895 size_t arity2 = (type_list2 == NULL) ? 0 : type_list2->Size();
896 if (arity1 != arity2) {
897 return false;
898 }
899
900 for (size_t i = 0; i < arity1; ++i) {
901 uint32_t type_idx1 = type_list1->GetTypeItem(i).type_idx_;
902 uint32_t type_idx2 = type_list2->GetTypeItem(i).type_idx_;
Brian Carlstromf615a612011-07-23 12:50:34 -0700903 const char* type1 = dex1->dexStringByTypeIdx(type_idx1);
904 const char* type2 = dex2->dexStringByTypeIdx(type_idx2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700905 if (strcmp(type1, type2) != 0) {
906 return false;
907 }
908 }
909
910 return true;
911}
912
913bool ClassLinker::HasSameReturnType(const Method* m1, const Method* m2) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700914 const DexFile* dex1 = FindDexFile(m1->GetClass()->GetDexCache());
915 const DexFile* dex2 = FindDexFile(m2->GetClass()->GetDexCache());
916 const DexFile::ProtoId& proto1 = dex1->GetProtoId(m1->proto_idx_);
917 const DexFile::ProtoId& proto2 = dex2->GetProtoId(m2->proto_idx_);
918 const char* type1 = dex1->dexStringByTypeIdx(proto1.return_type_idx_);
919 const char* type2 = dex2->dexStringByTypeIdx(proto2.return_type_idx_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700920 return (strcmp(type1, type2) == 0);
921}
922
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700923bool ClassLinker::InitializeSuperClass(Class* klass) {
924 CHECK(klass != NULL);
925 // TODO: assert klass lock is acquired
926 if (!klass->IsInterface() && klass->HasSuperClass()) {
927 Class* super_class = klass->GetSuperClass();
928 if (super_class->GetStatus() != Class::kStatusInitialized) {
929 CHECK(!super_class->IsInterface());
930 klass->MonitorExit();
931 bool super_initialized = InitializeClass(super_class);
932 klass->MonitorEnter();
933 // TODO: check for a pending exception
934 if (!super_initialized) {
935 klass->SetStatus(Class::kStatusError);
936 klass->NotifyAll();
937 return false;
938 }
939 }
940 }
941 return true;
942}
943
944void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700945 size_t num_static_fields = klass->NumStaticFields();
946 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700947 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700948 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700949 DexCache* dex_cache = klass->GetDexCache();
950 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700951 return;
952 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700953 const StringPiece& descriptor = klass->GetDescriptor();
Brian Carlstromf615a612011-07-23 12:50:34 -0700954 const DexFile* dex_file = FindDexFile(dex_cache);
955 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
956 CHECK(dex_class_def != NULL);
957 const byte* addr = dex_file->GetEncodedArray(*dex_class_def);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700958 size_t array_size = DecodeUnsignedLeb128(&addr);
959 for (size_t i = 0; i < array_size; ++i) {
960 StaticField* field = klass->GetStaticField(i);
961 JValue value;
Brian Carlstromf615a612011-07-23 12:50:34 -0700962 DexFile::ValueType type = dex_file->ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700963 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700964 case DexFile::kByte:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700965 field->SetByte(value.b);
966 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700967 case DexFile::kShort:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700968 field->SetShort(value.s);
969 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700970 case DexFile::kChar:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700971 field->SetChar(value.c);
972 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700973 case DexFile::kInt:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700974 field->SetInt(value.i);
975 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700976 case DexFile::kLong:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700977 field->SetLong(value.j);
978 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700979 case DexFile::kFloat:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700980 field->SetFloat(value.f);
981 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700982 case DexFile::kDouble:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700983 field->SetDouble(value.d);
984 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700985 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700986 uint32_t string_idx = value.i;
987 String* resolved = ResolveString(klass, string_idx);
988 field->SetObject(resolved);
989 break;
990 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700991 case DexFile::kBoolean:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700992 field->SetBoolean(value.z);
993 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700994 case DexFile::kNull:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700995 field->SetObject(value.l);
996 break;
997 default:
Carl Shapiro606258b2011-07-09 16:09:09 -0700998 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700999 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001000 }
1001}
1002
Brian Carlstromf615a612011-07-23 12:50:34 -07001003bool ClassLinker::LinkClass(Class* klass, const DexFile* dex_file) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001004 CHECK(klass->status_ == Class::kStatusIdx ||
1005 klass->status_ == Class::kStatusLoaded);
1006 if (klass->status_ == Class::kStatusIdx) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001007 if (!LinkInterfaces(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001008 return false;
1009 }
1010 }
1011 if (!LinkSuperClass(klass)) {
1012 return false;
1013 }
1014 if (!LinkMethods(klass)) {
1015 return false;
1016 }
1017 if (!LinkInstanceFields(klass)) {
1018 return false;
1019 }
1020 CreateReferenceOffsets(klass);
1021 CHECK_EQ(klass->status_, Class::kStatusLoaded);
1022 klass->status_ = Class::kStatusResolved;
1023 return true;
1024}
1025
Brian Carlstromf615a612011-07-23 12:50:34 -07001026bool ClassLinker::LinkInterfaces(Class* klass, const DexFile* dex_file) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001027 // Mark the class as loaded.
1028 klass->status_ = Class::kStatusLoaded;
Brian Carlstromf615a612011-07-23 12:50:34 -07001029 if (klass->super_class_idx_ != DexFile::kDexNoIndex) {
1030 Class* super_class = ResolveClass(klass, klass->super_class_idx_, dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001031 if (super_class == NULL) {
1032 LG << "Failed to resolve superclass";
1033 return false;
1034 }
1035 klass->super_class_ = super_class; // TODO: write barrier
1036 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001037 if (klass->NumInterfaces() > 0) {
1038 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
1039 uint32_t idx = klass->interfaces_idx_[i];
1040 klass->SetInterface(i, ResolveClass(klass, idx, dex_file));
1041 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001042 LG << "Failed to resolve interface";
1043 return false;
1044 }
1045 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001046 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001047 LG << "Inaccessible interface";
1048 return false;
1049 }
1050 }
1051 }
1052 return true;
1053}
1054
1055bool ClassLinker::LinkSuperClass(Class* klass) {
1056 CHECK(!klass->IsPrimitive());
1057 const Class* super = klass->GetSuperClass();
1058 if (klass->GetDescriptor() == "Ljava/lang/Object;") {
1059 if (super != NULL) {
1060 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1061 return false;
1062 }
1063 // TODO: clear finalize attribute
1064 return true;
1065 }
1066 if (super == NULL) {
1067 LG << "No superclass defined"; // TODO: LinkageError
1068 return false;
1069 }
1070 // Verify
1071 if (super->IsFinal()) {
1072 LG << "Superclass is declared final"; // TODO: IncompatibleClassChangeError
1073 return false;
1074 }
1075 if (super->IsInterface()) {
1076 LG << "Superclass is an interface"; // TODO: IncompatibleClassChangeError
1077 return false;
1078 }
1079 if (!klass->CanAccess(super)) {
1080 LG << "Superclass is inaccessible"; // TODO: IllegalAccessError
1081 return false;
1082 }
1083 return true;
1084}
1085
1086// Populate the class vtable and itable.
1087bool ClassLinker::LinkMethods(Class* klass) {
1088 if (klass->IsInterface()) {
1089 // No vtable.
1090 size_t count = klass->NumVirtualMethods();
1091 if (!IsUint(16, count)) {
1092 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1093 return false;
1094 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001095 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001096 klass->GetVirtualMethod(i)->method_index_ = i;
1097 }
1098 } else {
1099 // Link virtual method tables
1100 LinkVirtualMethods(klass);
1101
1102 // Link interface method tables
1103 LinkInterfaceMethods(klass);
1104
1105 // Insert stubs.
1106 LinkAbstractMethods(klass);
1107 }
1108 return true;
1109}
1110
1111bool ClassLinker::LinkVirtualMethods(Class* klass) {
1112 uint32_t max_count = klass->NumVirtualMethods();
1113 if (klass->GetSuperClass() != NULL) {
1114 max_count += klass->GetSuperClass()->NumVirtualMethods();
1115 } else {
1116 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
1117 }
1118 // TODO: do not assign to the vtable field until it is fully constructed.
1119 // TODO: make this a vector<Method*> instead?
1120 klass->vtable_ = new Method*[max_count];
1121 if (klass->HasSuperClass()) {
1122 memcpy(klass->vtable_,
1123 klass->GetSuperClass()->vtable_,
1124 klass->GetSuperClass()->vtable_count_ * sizeof(Method*));
1125 size_t actual_count = klass->GetSuperClass()->vtable_count_;
1126 // See if any of our virtual methods override the superclass.
1127 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1128 Method* local_method = klass->GetVirtualMethod(i);
1129 size_t j = 0;
1130 for (; j < klass->GetSuperClass()->vtable_count_; ++j) {
1131 const Method* super_method = klass->vtable_[j];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001132 if (HasSameNameAndPrototype(local_method, super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001133 // Verify
1134 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001135 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001136 return false;
1137 }
1138 klass->vtable_[j] = local_method;
1139 local_method->method_index_ = j;
1140 break;
1141 }
1142 }
1143 if (j == klass->GetSuperClass()->vtable_count_) {
1144 // Not overriding, append.
1145 klass->vtable_[actual_count] = local_method;
1146 local_method->method_index_ = actual_count;
1147 actual_count += 1;
1148 }
1149 }
1150 if (!IsUint(16, actual_count)) {
1151 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1152 return false;
1153 }
1154 CHECK_LE(actual_count, max_count);
1155 if (actual_count < max_count) {
1156 Method** new_vtable = new Method*[actual_count];
1157 memcpy(new_vtable, klass->vtable_, actual_count * sizeof(Method*));
Carl Shapiro565f5072011-07-10 13:39:43 -07001158 delete[] klass->vtable_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001159 klass->vtable_ = new_vtable;
1160 LG << "shrunk vtable: "
1161 << "was " << max_count << ", "
1162 << "now " << actual_count;
1163 }
1164 klass->vtable_count_ = actual_count;
1165 } else {
1166 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
1167 if (!IsUint(16, klass->NumVirtualMethods())) {
1168 LG << "Too many methods"; // TODO: VirtualMachineError
1169 return false;
1170 }
1171 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1172 klass->vtable_[i] = klass->GetVirtualMethod(i);
1173 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1174 }
1175 klass->vtable_count_ = klass->NumVirtualMethods();
1176 }
1177 return true;
1178}
1179
1180bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1181 int pool_offset = 0;
1182 int pool_size = 0;
1183 int miranda_count = 0;
1184 int miranda_alloc = 0;
1185 size_t super_ifcount;
1186 if (klass->HasSuperClass()) {
1187 super_ifcount = klass->GetSuperClass()->iftable_count_;
1188 } else {
1189 super_ifcount = 0;
1190 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001191 size_t ifcount = super_ifcount;
1192 ifcount += klass->NumInterfaces();
1193 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1194 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001195 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001196 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001197 DCHECK(klass->iftable_count_ == 0);
1198 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001199 return true;
1200 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001201 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1202 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001203 if (super_ifcount != 0) {
1204 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1205 sizeof(InterfaceEntry) * super_ifcount);
1206 }
1207 // Flatten the interface inheritance hierarchy.
1208 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001209 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1210 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001211 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001212 if (!interf->IsInterface()) {
1213 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1214 return false;
1215 }
1216 klass->iftable_[idx++].SetClass(interf);
1217 for (size_t j = 0; j < interf->iftable_count_; j++) {
1218 klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
1219 }
1220 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001221 CHECK_EQ(idx, ifcount);
1222 klass->iftable_count_ = ifcount;
1223 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001224 return true;
1225 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001226 for (size_t i = super_ifcount; i < ifcount; i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001227 pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
1228 }
1229 if (pool_size == 0) {
1230 return true;
1231 }
1232 klass->ifvi_pool_count_ = pool_size;
1233 klass->ifvi_pool_ = new uint32_t[pool_size];
1234 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001235 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001236 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
1237 Class* interface = klass->iftable_[i].GetClass();
1238 pool_offset += interface->NumVirtualMethods(); // end here
1239 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1240 Method* interface_method = interface->GetVirtualMethod(j);
1241 int k; // must be signed
1242 for (k = klass->vtable_count_ - 1; k >= 0; --k) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001243 if (HasSameNameAndPrototype(interface_method, klass->vtable_[k])) {
Carl Shapiro565f5072011-07-10 13:39:43 -07001244 if (!klass->vtable_[k]->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001245 LG << "Implementation not public";
1246 return false;
1247 }
1248 klass->iftable_[i].method_index_array_[j] = k;
1249 break;
1250 }
1251 }
1252 if (k < 0) {
1253 if (miranda_count == miranda_alloc) {
1254 miranda_alloc += 8;
1255 if (miranda_list.empty()) {
1256 miranda_list.resize(miranda_alloc);
1257 } else {
1258 miranda_list.resize(miranda_alloc);
1259 }
1260 }
1261 int mir;
1262 for (mir = 0; mir < miranda_count; mir++) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001263 if (HasSameNameAndPrototype(miranda_list[mir], interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001264 break;
1265 }
1266 }
1267 // point the interface table at a phantom slot index
1268 klass->iftable_[i].method_index_array_[j] = klass->vtable_count_ + mir;
1269 if (mir == miranda_count) {
1270 miranda_list[miranda_count++] = interface_method;
1271 }
1272 }
1273 }
1274 }
1275 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001276 int old_method_count = klass->NumVirtualMethods();
1277 int new_method_count = old_method_count + miranda_count;
1278 ObjectArray* new_virtual_methods = AllocObjectArray(new_method_count);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001279 if (klass->virtual_methods_ != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001280 ObjectArray::Copy(klass->virtual_methods_, 0,
1281 new_virtual_methods, 0,
1282 old_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001283 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001284 klass->virtual_methods_ = new_virtual_methods;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001285
1286 CHECK(klass->vtable_ != NULL);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001287 int old_vtable_count = klass->vtable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001288 klass->vtable_count_ += miranda_count;
1289
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001290 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001291 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001292 memcpy(meth, miranda_list[i], sizeof(Method));
1293 meth->klass_ = klass;
1294 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001295 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1296 klass->SetVirtualMethod(old_method_count + i, meth);
1297 klass->vtable_[old_vtable_count + i] = meth;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001298 }
1299 }
1300 return true;
1301}
1302
1303void ClassLinker::LinkAbstractMethods(Class* klass) {
1304 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1305 Method* method = klass->GetVirtualMethod(i);
1306 if (method->IsAbstract()) {
1307 method->insns_ = reinterpret_cast<uint16_t*>(0xFFFFFFFF); // TODO: AbstractMethodError
1308 }
1309 }
1310}
1311
1312bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001313 int field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001314 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001315 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001316 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001317 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001318 }
1319 // Move references to the front.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001320 klass->num_reference_instance_fields_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001321 size_t i = 0;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001322 for ( ; i < klass->NumInstanceFields(); i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001323 InstanceField* pField = klass->GetInstanceField(i);
1324 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001325 if (c != '[' && c != 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001326 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1327 InstanceField* refField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001328 char rc = refField->GetType();
1329 if (rc == '[' || rc == 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001330 klass->SetInstanceField(i, refField);
1331 klass->SetInstanceField(j, pField);
1332 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001333 c = rc;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001334 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001335 break;
1336 }
1337 }
1338 } else {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001339 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001340 }
1341 if (c != '[' && c != 'L') {
1342 break;
1343 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001344 pField->SetOffset(field_offset);
1345 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001346 }
1347
1348 // Now we want to pack all of the double-wide fields together. If
1349 // we're not aligned, though, we want to shuffle one 32-bit field
1350 // into place. If we can't find one, we'll have to pad it.
Brian Carlstroma0808032011-07-18 00:39:23 -07001351 if (i != klass->NumInstanceFields() && (field_offset & 0x04) != 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001352 InstanceField* pField = klass->GetInstanceField(i);
1353 char c = pField->GetType();
1354
1355 if (c != 'J' && c != 'D') {
1356 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001357 DCHECK(c != '[');
1358 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001359 pField->SetOffset(field_offset);
1360 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001361 i++;
1362 } else {
1363 // Next field is 64-bit, so search for a 32-bit field we can
1364 // swap into it.
1365 bool found = false;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001366 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1367 InstanceField* singleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001368 char rc = singleField->GetType();
1369 if (rc != 'J' && rc != 'D') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001370 klass->SetInstanceField(i, singleField);
1371 klass->SetInstanceField(j, pField);
1372 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001373 pField->SetOffset(field_offset);
1374 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001375 found = true;
1376 i++;
1377 break;
1378 }
1379 }
1380 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001381 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001382 }
1383 }
1384 }
1385
1386 // Alignment is good, shuffle any double-wide fields forward, and
1387 // finish assigning field offsets to all fields.
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001388 DCHECK(i == klass->NumInstanceFields() || (field_offset & 0x04) == 0);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001389 for ( ; i < klass->NumInstanceFields(); i++) {
1390 InstanceField* pField = klass->GetInstanceField(i);
1391 char c = pField->GetType();
1392 if (c != 'D' && c != 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001393 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1394 InstanceField* doubleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001395 char rc = doubleField->GetType();
1396 if (rc == 'D' || rc == 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001397 klass->SetInstanceField(i, doubleField);
1398 klass->SetInstanceField(j, pField);
1399 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001400 c = rc;
1401 break;
1402 }
1403 }
1404 } else {
1405 // This is a double-wide field, leave it be.
1406 }
1407
Brian Carlstroma0808032011-07-18 00:39:23 -07001408 pField->SetOffset(field_offset);
1409 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001410 if (c == 'J' || c == 'D')
Brian Carlstroma0808032011-07-18 00:39:23 -07001411 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001412 }
1413
1414#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001415 // Make sure that all reference fields appear before
1416 // non-reference fields, and all double-wide fields are aligned.
1417 bool seen_non_ref = false;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001418 for (i = 0; i < klass->NumInstanceFields(); i++) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001419 InstanceField *pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001420 char c = pField->GetType();
1421
1422 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001423 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001424 }
1425
1426 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001427 if (!seen_non_ref) {
1428 seen_non_ref = true;
1429 DCHECK_EQ(klass->num_reference_ifields_, i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001430 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001431 } else {
1432 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001433 }
1434 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001435 if (!seen_non_ref) {
1436 DCHECK(klass->NumInstanceFields(), klass->num_reference_ifields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001437 }
1438#endif
1439
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001440 if (klass->object_size_ == 0) {
1441 // avoid overwriting object_size_ of special crafted classes such as java_lang_*_
1442 klass->object_size_ = field_offset;
1443 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001444 return true;
1445}
1446
1447// Set the bitmap of reference offsets, refOffsets, from the ifields
1448// list.
1449void ClassLinker::CreateReferenceOffsets(Class* klass) {
1450 uint32_t reference_offsets = 0;
1451 if (klass->HasSuperClass()) {
1452 reference_offsets = klass->GetSuperClass()->GetReferenceOffsets();
1453 }
1454 // If our superclass overflowed, we don't stand a chance.
1455 if (reference_offsets != CLASS_WALK_SUPER) {
1456 // All of the fields that contain object references are guaranteed
1457 // to be at the beginning of the ifields list.
1458 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) {
1459 // Note that, per the comment on struct InstField, f->byteOffset
1460 // is the offset from the beginning of obj, not the offset into
1461 // obj->instanceData.
1462 const InstanceField* field = klass->GetInstanceField(i);
1463 size_t byte_offset = field->GetOffset();
1464 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001465 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001466 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1467 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001468 CHECK_NE(new_bit, 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001469 reference_offsets |= new_bit;
1470 } else {
1471 reference_offsets = CLASS_WALK_SUPER;
1472 break;
1473 }
1474 }
1475 klass->SetReferenceOffsets(reference_offsets);
1476 }
1477}
1478
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001479Class* ClassLinker::ResolveClass(const Class* referrer,
1480 uint32_t class_idx,
Brian Carlstromf615a612011-07-23 12:50:34 -07001481 const DexFile* dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001482 DexCache* dex_cache = referrer->GetDexCache();
1483 Class* resolved = dex_cache->GetResolvedClass(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001484 if (resolved != NULL) {
1485 return resolved;
1486 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001487 const char* descriptor = dex_file->dexStringByTypeIdx(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001488 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001489 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001490 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -07001491 resolved = FindClass(descriptor, referrer->GetClassLoader(), dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001492 }
1493 if (resolved != NULL) {
1494 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001495 if (referrer->GetDexCache() != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001496 if (check->GetClassLoader() != NULL) {
1497 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1498 return NULL;
1499 }
1500 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001501 dex_cache->SetResolvedClass(class_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001502 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001503 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001504 }
1505 return resolved;
1506}
1507
1508Method* ResolveMethod(const Class* referrer, uint32_t method_idx,
1509 /*MethodType*/ int method_type) {
1510 CHECK(false);
1511 return NULL;
1512}
1513
Carl Shapiro69759ea2011-07-21 18:13:35 -07001514String* ClassLinker::ResolveString(const Class* referring,
1515 uint32_t string_idx) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001516 const DexFile* dex_file = FindDexFile(referring->GetDexCache());
1517 const DexFile::StringId& string_id = dex_file->GetStringId(string_idx);
1518 const char* string_data = dex_file->GetStringData(string_id);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001519 String* new_string = Heap::AllocStringFromModifiedUtf8(java_lang_String_,
1520 char_array_class_,
1521 string_data);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001522 // TODO: intern the new string
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001523 referring->GetDexCache()->SetResolvedString(string_idx, new_string);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001524 return new_string;
1525}
1526
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001527} // namespace art