blob: 78b6d3309d3970cb8b354933ccf156a5eb01b0fd [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 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) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700420 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700421 boot_class_path_.push_back(dex_file);
422 RegisterDexFile(dex_file);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700423}
424
Brian Carlstromf615a612011-07-23 12:50:34 -0700425void ClassLinker::RegisterDexFile(DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700426 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700427 dex_files_.push_back(dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700428 DexCache* dex_cache = AllocDexCache();
429 CHECK(dex_cache != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700430 dex_cache->Init(AllocObjectArray(dex_file->NumStringIds()),
431 AllocObjectArray(dex_file->NumTypeIds()),
432 AllocObjectArray(dex_file->NumMethodIds()),
433 AllocObjectArray(dex_file->NumFieldIds()));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700434 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700435}
436
Brian Carlstromf615a612011-07-23 12:50:34 -0700437const DexFile* ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700438 CHECK(dex_cache != NULL);
439 for (size_t i = 0; i != dex_caches_.size(); ++i) {
440 if (dex_caches_[i] == dex_cache) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700441 return dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700442 }
443 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700444 CHECK(false) << "Could not find DexFile";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700445 return NULL;
446}
447
Brian Carlstromf615a612011-07-23 12:50:34 -0700448DexCache* ClassLinker::FindDexCache(const DexFile* dex_file) const {
449 CHECK(dex_file != NULL);
450 for (size_t i = 0; i != dex_files_.size(); ++i) {
451 if (dex_files_[i] == dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700452 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700453 }
454 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700455 CHECK(false) << "Could not find DexCache";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700456 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700457}
458
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700459Class* ClassLinker::CreatePrimitiveClass(const StringPiece& descriptor) {
Brian Carlstroma0808032011-07-18 00:39:23 -0700460 Class* klass = AllocClass(NULL);
Carl Shapiro565f5072011-07-10 13:39:43 -0700461 CHECK(klass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700462 klass->super_class_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700463 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
464 klass->descriptor_ = descriptor;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700465 klass->descriptor_alloc_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700466 klass->status_ = Class::kStatusInitialized;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700467 bool success = InsertClass(klass);
468 CHECK(success);
Carl Shapiro565f5072011-07-10 13:39:43 -0700469 return klass;
470}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700471
Brian Carlstrombe977852011-07-19 14:54:54 -0700472// Create an array class (i.e. the class object for the array, not the
473// array itself). "descriptor" looks like "[C" or "[[[[B" or
474// "[Ljava/lang/String;".
475//
476// If "descriptor" refers to an array of primitives, look up the
477// primitive type's internally-generated class object.
478//
479// "loader" is the class loader of the class that's referring to us. It's
480// used to ensure that we're looking for the element type in the right
481// context. It does NOT become the class loader for the array class; that
482// always comes from the base element class.
483//
484// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700485Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
486 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -0700487 const DexFile* dex_file)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700488{
489 CHECK(descriptor[0] == '[');
490 DCHECK(java_lang_Class_ != NULL);
491 DCHECK(java_lang_Object_ != NULL);
492
Brian Carlstrombe977852011-07-19 14:54:54 -0700493 // Identify the underlying element class and the array dimension depth.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700494 Class* component_type_ = NULL;
495 int array_rank;
496 if (descriptor[1] == '[') {
Brian Carlstrombe977852011-07-19 14:54:54 -0700497 // array of arrays; keep descriptor and grab stuff from parent
Brian Carlstromf615a612011-07-23 12:50:34 -0700498 Class* outer = FindClass(descriptor.substr(1), class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700499 if (outer != NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700500 // want the base class, not "outer", in our component_type_
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700501 component_type_ = outer->component_type_;
502 array_rank = outer->array_rank_ + 1;
503 } else {
Brian Carlstrombe977852011-07-19 14:54:54 -0700504 DCHECK(component_type_ == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700505 }
506 } else {
507 array_rank = 1;
508 if (descriptor[1] == 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -0700509 // array of objects; strip off "[" and look up descriptor.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700510 const StringPiece subDescriptor = descriptor.substr(1);
Brian Carlstromf615a612011-07-23 12:50:34 -0700511 component_type_ = FindClass(subDescriptor, class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700512 } else {
Brian Carlstrombe977852011-07-19 14:54:54 -0700513 // array of a primitive type
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700514 component_type_ = FindPrimitiveClass(descriptor[1]);
515 }
516 }
517
518 if (component_type_ == NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700519 // failed
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700520 DCHECK(Thread::Current()->IsExceptionPending());
521 return NULL;
522 }
523
Brian Carlstrombe977852011-07-19 14:54:54 -0700524 // See if the component type is already loaded. Array classes are
525 // always associated with the class loader of their underlying
526 // element type -- an array of Strings goes with the loader for
527 // java/lang/String -- so we need to look for it there. (The
528 // caller should have checked for the existence of the class
529 // before calling here, but they did so with *their* class loader,
530 // not the component type's loader.)
531 //
532 // If we find it, the caller adds "loader" to the class' initiating
533 // loader list, which should prevent us from going through this again.
534 //
535 // This call is unnecessary if "loader" and "component_type_->class_loader_"
536 // are the same, because our caller (FindClass) just did the
537 // lookup. (Even if we get this wrong we still have correct behavior,
538 // because we effectively do this lookup again when we add the new
539 // class to the hash table --- necessary because of possible races with
540 // other threads.)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700541 if (class_loader != component_type_->class_loader_) {
542 Class* new_class = LookupClass(descriptor, component_type_->class_loader_);
543 if (new_class != NULL) {
544 return new_class;
545 }
546 }
547
Brian Carlstrombe977852011-07-19 14:54:54 -0700548 // Fill out the fields in the Class.
549 //
550 // It is possible to execute some methods against arrays, because
551 // all arrays are subclasses of java_lang_Object_, so we need to set
552 // up a vtable. We can just point at the one in java_lang_Object_.
553 //
554 // Array classes are simple enough that we don't need to do a full
555 // link step.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700556
557 Class* new_class;
558 if (descriptor == "[Ljava/lang/Object;") {
559 CHECK(object_array_class_);
560 new_class = object_array_class_;
561 } else {
562 new_class = AllocClass(NULL);
563 if (new_class == NULL) {
564 return NULL;
565 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700566 }
567 new_class->descriptor_alloc_ = new std::string(descriptor.data(),
568 descriptor.size());
569 new_class->descriptor_.set(new_class->descriptor_alloc_->data(),
570 new_class->descriptor_alloc_->size());
571 new_class->super_class_ = java_lang_Object_;
572 new_class->vtable_count_ = java_lang_Object_->vtable_count_;
573 new_class->vtable_ = java_lang_Object_->vtable_;
574 new_class->primitive_type_ = Class::kPrimNot;
575 new_class->component_type_ = component_type_;
576 new_class->class_loader_ = component_type_->class_loader_;
577 new_class->array_rank_ = array_rank;
578 new_class->status_ = Class::kStatusInitialized;
Brian Carlstrombe977852011-07-19 14:54:54 -0700579 // don't need to set new_class->object_size_
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700580
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700581
Brian Carlstrombe977852011-07-19 14:54:54 -0700582 // All arrays have java/lang/Cloneable and java/io/Serializable as
583 // interfaces. We need to set that up here, so that stuff like
584 // "instanceof" works right.
585 //
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700586 // Note: The GC could run during the call to FindSystemClass,
Brian Carlstrombe977852011-07-19 14:54:54 -0700587 // so we need to make sure the class object is GC-valid while we're in
588 // there. Do this by clearing the interface list so the GC will just
589 // think that the entries are null.
Brian Carlstrombe977852011-07-19 14:54:54 -0700590
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700591
592 // Use the single, global copies of "interfaces" and "iftable"
593 // (remember not to free them for arrays).
594 DCHECK(array_interfaces_ != NULL);
595 new_class->interfaces_ = array_interfaces_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700596 new_class->iftable_count_ = 2;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700597 DCHECK(array_iftable_ != NULL);
598 new_class->iftable_ = array_iftable_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700599
Brian Carlstrombe977852011-07-19 14:54:54 -0700600 // Inherit access flags from the component type. Arrays can't be
601 // used as a superclass or interface, so we want to add "final"
602 // and remove "interface".
603 //
604 // Don't inherit any non-standard flags (e.g., kAccFinal)
605 // from component_type_. We assume that the array class does not
606 // override finalize().
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700607 new_class->access_flags_ = ((new_class->component_type_->access_flags_ &
608 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask;
609
610 if (InsertClass(new_class)) {
611 return new_class;
612 }
Brian Carlstrombe977852011-07-19 14:54:54 -0700613 // Another thread must have loaded the class after we
614 // started but before we finished. Abandon what we've
615 // done.
616 //
617 // (Yes, this happens.)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700618
Brian Carlstrombe977852011-07-19 14:54:54 -0700619 // Grab the winning class.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700620 Class* other_class = LookupClass(descriptor, component_type_->class_loader_);
621 DCHECK(other_class != NULL);
622 return other_class;
623}
624
625Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700626 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700627 case 'B':
Carl Shapiro565f5072011-07-10 13:39:43 -0700628 CHECK(primitive_byte_ != NULL);
629 return primitive_byte_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700630 case 'C':
Carl Shapiro565f5072011-07-10 13:39:43 -0700631 CHECK(primitive_char_ != NULL);
632 return primitive_char_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700633 case 'D':
Carl Shapiro565f5072011-07-10 13:39:43 -0700634 CHECK(primitive_double_ != NULL);
635 return primitive_double_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700636 case 'F':
Carl Shapiro565f5072011-07-10 13:39:43 -0700637 CHECK(primitive_float_ != NULL);
638 return primitive_float_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700639 case 'I':
Carl Shapiro565f5072011-07-10 13:39:43 -0700640 CHECK(primitive_int_ != NULL);
641 return primitive_int_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700642 case 'J':
Carl Shapiro565f5072011-07-10 13:39:43 -0700643 CHECK(primitive_long_ != NULL);
644 return primitive_long_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700645 case 'S':
Carl Shapiro565f5072011-07-10 13:39:43 -0700646 CHECK(primitive_short_ != NULL);
647 return primitive_short_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700648 case 'Z':
Carl Shapiro565f5072011-07-10 13:39:43 -0700649 CHECK(primitive_boolean_ != NULL);
650 return primitive_boolean_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700651 case 'V':
Carl Shapiro565f5072011-07-10 13:39:43 -0700652 CHECK(primitive_void_ != NULL);
653 return primitive_void_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700654 case 'L':
655 case '[':
656 LOG(ERROR) << "Not a primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700657 default:
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700658 LOG(ERROR) << "Unknown primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700659 };
660 return NULL; // Not reachable.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700661}
662
663bool ClassLinker::InsertClass(Class* klass) {
664 // TODO: acquire classes_lock_
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700665 const StringPiece& key = klass->GetDescriptor();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700666 bool success = classes_.insert(std::make_pair(key, klass)).second;
667 // TODO: release classes_lock_
668 return success;
669}
670
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700671Class* ClassLinker::LookupClass(const StringPiece& descriptor, Object* class_loader) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700672 // TODO: acquire classes_lock_
673 Table::iterator it = classes_.find(descriptor);
674 // TODO: release classes_lock_
675 if (it == classes_.end()) {
676 return NULL;
677 } else {
678 return (*it).second;
679 }
680}
681
682bool ClassLinker::InitializeClass(Class* klass) {
683 CHECK(klass->GetStatus() == Class::kStatusResolved ||
684 klass->GetStatus() == Class::kStatusError);
685
Carl Shapirob5573532011-07-12 18:22:59 -0700686 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700687
688 {
689 ObjectLock lock(klass);
690
691 if (klass->GetStatus() < Class::kStatusVerified) {
692 if (klass->IsErroneous()) {
693 LG << "re-initializing failed class"; // TODO: throw
694 return false;
695 }
696
697 CHECK(klass->GetStatus() == Class::kStatusResolved);
698
699 klass->status_ = Class::kStatusVerifying;
700 if (!DexVerify::VerifyClass(klass)) {
701 LG << "Verification failed"; // TODO: ThrowVerifyError
702 Object* exception = self->GetException();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700703 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
704 klass->SetFieldObject(field_offset, exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700705 klass->SetStatus(Class::kStatusError);
706 return false;
707 }
708
709 klass->SetStatus(Class::kStatusVerified);
710 }
711
712 if (klass->status_ == Class::kStatusInitialized) {
713 return true;
714 }
715
716 while (klass->status_ == Class::kStatusInitializing) {
717 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -0700718 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700719 LG << "recursive <clinit>";
720 return true;
721 }
722
723 CHECK(!self->IsExceptionPending());
724
725 lock.Wait(); // TODO: check for interruption
726
727 // When we wake up, repeat the test for init-in-progress. If
728 // there's an exception pending (only possible if
729 // "interruptShouldThrow" was set), bail out.
730 if (self->IsExceptionPending()) {
731 CHECK(false);
732 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
733 klass->SetStatus(Class::kStatusError);
734 return false;
735 }
736 if (klass->GetStatus() == Class::kStatusInitializing) {
737 continue;
738 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700739 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700740 klass->GetStatus() == Class::kStatusError);
741 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700742 // The caller wants an exception, but it was thrown in a
743 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700744 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
745 return false;
746 }
747 return true; // otherwise, initialized
748 }
749
750 // see if we failed previously
751 if (klass->IsErroneous()) {
752 // might be wise to unlock before throwing; depends on which class
753 // it is that we have locked
754
755 // TODO: throwEarlierClassFailure(klass);
756 return false;
757 }
758
759 if (!ValidateSuperClassDescriptors(klass)) {
760 klass->SetStatus(Class::kStatusError);
761 return false;
762 }
763
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700764 DCHECK(klass->status_ < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700765
Carl Shapirob5573532011-07-12 18:22:59 -0700766 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700767 klass->status_ = Class::kStatusInitializing;
768 }
769
770 if (!InitializeSuperClass(klass)) {
771 return false;
772 }
773
774 InitializeStaticFields(klass);
775
776 Method* clinit = klass->FindDirectMethodLocally("<clinit>", "()V");
777 if (clinit != NULL) {
778 } else {
779 // JValue unused;
780 // TODO: dvmCallMethod(self, method, NULL, &unused);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700781 //CHECK(!"unimplemented");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700782 }
783
784 {
785 ObjectLock lock(klass);
786
787 if (self->IsExceptionPending()) {
788 klass->SetStatus(Class::kStatusError);
789 } else {
790 klass->SetStatus(Class::kStatusInitialized);
791 }
792 lock.NotifyAll();
793 }
794
795 return true;
796}
797
798bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
799 if (klass->IsInterface()) {
800 return true;
801 }
802 // begin with the methods local to the superclass
803 if (klass->HasSuperClass() &&
804 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
805 const Class* super = klass->GetSuperClass();
806 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
807 const Method* method = klass->GetVirtualMethod(i);
808 if (method != super->GetVirtualMethod(i) &&
809 !HasSameMethodDescriptorClasses(method, super, klass)) {
810 LG << "Classes resolve differently in superclass";
811 return false;
812 }
813 }
814 }
815 for (size_t i = 0; i < klass->iftable_count_; ++i) {
816 const InterfaceEntry* iftable = &klass->iftable_[i];
817 Class* interface = iftable->GetClass();
818 if (klass->GetClassLoader() != interface->GetClassLoader()) {
819 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
820 uint32_t vtable_index = iftable->method_index_array_[j];
821 const Method* method = klass->GetVirtualMethod(vtable_index);
822 if (!HasSameMethodDescriptorClasses(method, interface,
823 method->GetClass())) {
824 LG << "Classes resolve differently in interface"; // TODO: LinkageError
825 return false;
826 }
827 }
828 }
829 }
830 return true;
831}
832
833bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700834 const Class* klass1,
835 const Class* klass2) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700836 const DexFile* dex_file = FindDexFile(method->GetClass()->GetDexCache());
837 const DexFile::ProtoId& proto_id = dex_file->GetProtoId(method->proto_idx_);
838 DexFile::ParameterIterator *it;
839 for (it = dex_file->GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700840 const char* descriptor = it->GetDescriptor();
841 if (descriptor == NULL) {
842 break;
843 }
844 if (descriptor[0] == 'L' || descriptor[0] == '[') {
845 // Found a non-primitive type.
846 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
847 return false;
848 }
849 }
850 }
851 // Check the return type
Brian Carlstromf615a612011-07-23 12:50:34 -0700852 const char* descriptor = dex_file->GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700853 if (descriptor[0] == 'L' || descriptor[0] == '[') {
854 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
855 return false;
856 }
857 }
858 return true;
859}
860
861// Returns true if classes referenced by the descriptor are the
862// same classes in klass1 as they are in klass2.
863bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700864 const Class* klass1,
865 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700866 CHECK(descriptor != NULL);
867 CHECK(klass1 != NULL);
868 CHECK(klass2 != NULL);
869#if 0
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700870 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700871 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700872 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700873 // TODO: found2 == NULL
874 // TODO: lookup found1 in initiating loader list
875 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -0700876 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700877 if (found1 == found2) {
878 return true;
879 } else {
880 return false;
881 }
882 }
883#endif
884 return true;
885}
886
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700887bool ClassLinker::HasSameArgumentTypes(const Method* m1, const Method* m2) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700888 const DexFile* dex1 = FindDexFile(m1->GetClass()->GetDexCache());
889 const DexFile* dex2 = FindDexFile(m2->GetClass()->GetDexCache());
890 const DexFile::ProtoId& proto1 = dex1->GetProtoId(m1->proto_idx_);
891 const DexFile::ProtoId& proto2 = dex2->GetProtoId(m2->proto_idx_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700892
893 // TODO: compare ProtoId objects for equality and exit early
Brian Carlstromf615a612011-07-23 12:50:34 -0700894 const DexFile::TypeList* type_list1 = dex1->GetProtoParameters(proto1);
895 const DexFile::TypeList* type_list2 = dex2->GetProtoParameters(proto2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700896 size_t arity1 = (type_list1 == NULL) ? 0 : type_list1->Size();
897 size_t arity2 = (type_list2 == NULL) ? 0 : type_list2->Size();
898 if (arity1 != arity2) {
899 return false;
900 }
901
902 for (size_t i = 0; i < arity1; ++i) {
903 uint32_t type_idx1 = type_list1->GetTypeItem(i).type_idx_;
904 uint32_t type_idx2 = type_list2->GetTypeItem(i).type_idx_;
Brian Carlstromf615a612011-07-23 12:50:34 -0700905 const char* type1 = dex1->dexStringByTypeIdx(type_idx1);
906 const char* type2 = dex2->dexStringByTypeIdx(type_idx2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700907 if (strcmp(type1, type2) != 0) {
908 return false;
909 }
910 }
911
912 return true;
913}
914
915bool ClassLinker::HasSameReturnType(const Method* m1, const Method* m2) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700916 const DexFile* dex1 = FindDexFile(m1->GetClass()->GetDexCache());
917 const DexFile* dex2 = FindDexFile(m2->GetClass()->GetDexCache());
918 const DexFile::ProtoId& proto1 = dex1->GetProtoId(m1->proto_idx_);
919 const DexFile::ProtoId& proto2 = dex2->GetProtoId(m2->proto_idx_);
920 const char* type1 = dex1->dexStringByTypeIdx(proto1.return_type_idx_);
921 const char* type2 = dex2->dexStringByTypeIdx(proto2.return_type_idx_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700922 return (strcmp(type1, type2) == 0);
923}
924
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700925bool ClassLinker::InitializeSuperClass(Class* klass) {
926 CHECK(klass != NULL);
927 // TODO: assert klass lock is acquired
928 if (!klass->IsInterface() && klass->HasSuperClass()) {
929 Class* super_class = klass->GetSuperClass();
930 if (super_class->GetStatus() != Class::kStatusInitialized) {
931 CHECK(!super_class->IsInterface());
932 klass->MonitorExit();
933 bool super_initialized = InitializeClass(super_class);
934 klass->MonitorEnter();
935 // TODO: check for a pending exception
936 if (!super_initialized) {
937 klass->SetStatus(Class::kStatusError);
938 klass->NotifyAll();
939 return false;
940 }
941 }
942 }
943 return true;
944}
945
946void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700947 size_t num_static_fields = klass->NumStaticFields();
948 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700949 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700950 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700951 DexCache* dex_cache = klass->GetDexCache();
952 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700953 return;
954 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700955 const StringPiece& descriptor = klass->GetDescriptor();
Brian Carlstromf615a612011-07-23 12:50:34 -0700956 const DexFile* dex_file = FindDexFile(dex_cache);
957 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
958 CHECK(dex_class_def != NULL);
959 const byte* addr = dex_file->GetEncodedArray(*dex_class_def);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700960 size_t array_size = DecodeUnsignedLeb128(&addr);
961 for (size_t i = 0; i < array_size; ++i) {
962 StaticField* field = klass->GetStaticField(i);
963 JValue value;
Brian Carlstromf615a612011-07-23 12:50:34 -0700964 DexFile::ValueType type = dex_file->ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700965 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700966 case DexFile::kByte:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700967 field->SetByte(value.b);
968 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700969 case DexFile::kShort:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700970 field->SetShort(value.s);
971 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700972 case DexFile::kChar:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700973 field->SetChar(value.c);
974 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700975 case DexFile::kInt:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700976 field->SetInt(value.i);
977 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700978 case DexFile::kLong:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700979 field->SetLong(value.j);
980 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700981 case DexFile::kFloat:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700982 field->SetFloat(value.f);
983 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700984 case DexFile::kDouble:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700985 field->SetDouble(value.d);
986 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700987 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700988 uint32_t string_idx = value.i;
989 String* resolved = ResolveString(klass, string_idx);
990 field->SetObject(resolved);
991 break;
992 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700993 case DexFile::kBoolean:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700994 field->SetBoolean(value.z);
995 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700996 case DexFile::kNull:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700997 field->SetObject(value.l);
998 break;
999 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001000 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001001 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001002 }
1003}
1004
Brian Carlstromf615a612011-07-23 12:50:34 -07001005bool ClassLinker::LinkClass(Class* klass, const DexFile* dex_file) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001006 CHECK(klass->status_ == Class::kStatusIdx ||
1007 klass->status_ == Class::kStatusLoaded);
1008 if (klass->status_ == Class::kStatusIdx) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001009 if (!LinkInterfaces(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001010 return false;
1011 }
1012 }
1013 if (!LinkSuperClass(klass)) {
1014 return false;
1015 }
1016 if (!LinkMethods(klass)) {
1017 return false;
1018 }
1019 if (!LinkInstanceFields(klass)) {
1020 return false;
1021 }
1022 CreateReferenceOffsets(klass);
1023 CHECK_EQ(klass->status_, Class::kStatusLoaded);
1024 klass->status_ = Class::kStatusResolved;
1025 return true;
1026}
1027
Brian Carlstromf615a612011-07-23 12:50:34 -07001028bool ClassLinker::LinkInterfaces(Class* klass, const DexFile* dex_file) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001029 // Mark the class as loaded.
1030 klass->status_ = Class::kStatusLoaded;
Brian Carlstromf615a612011-07-23 12:50:34 -07001031 if (klass->super_class_idx_ != DexFile::kDexNoIndex) {
1032 Class* super_class = ResolveClass(klass, klass->super_class_idx_, dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001033 if (super_class == NULL) {
1034 LG << "Failed to resolve superclass";
1035 return false;
1036 }
1037 klass->super_class_ = super_class; // TODO: write barrier
1038 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001039 if (klass->NumInterfaces() > 0) {
1040 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
1041 uint32_t idx = klass->interfaces_idx_[i];
1042 klass->SetInterface(i, ResolveClass(klass, idx, dex_file));
1043 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001044 LG << "Failed to resolve interface";
1045 return false;
1046 }
1047 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001048 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001049 LG << "Inaccessible interface";
1050 return false;
1051 }
1052 }
1053 }
1054 return true;
1055}
1056
1057bool ClassLinker::LinkSuperClass(Class* klass) {
1058 CHECK(!klass->IsPrimitive());
1059 const Class* super = klass->GetSuperClass();
1060 if (klass->GetDescriptor() == "Ljava/lang/Object;") {
1061 if (super != NULL) {
1062 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1063 return false;
1064 }
1065 // TODO: clear finalize attribute
1066 return true;
1067 }
1068 if (super == NULL) {
1069 LG << "No superclass defined"; // TODO: LinkageError
1070 return false;
1071 }
1072 // Verify
1073 if (super->IsFinal()) {
1074 LG << "Superclass is declared final"; // TODO: IncompatibleClassChangeError
1075 return false;
1076 }
1077 if (super->IsInterface()) {
1078 LG << "Superclass is an interface"; // TODO: IncompatibleClassChangeError
1079 return false;
1080 }
1081 if (!klass->CanAccess(super)) {
1082 LG << "Superclass is inaccessible"; // TODO: IllegalAccessError
1083 return false;
1084 }
1085 return true;
1086}
1087
1088// Populate the class vtable and itable.
1089bool ClassLinker::LinkMethods(Class* klass) {
1090 if (klass->IsInterface()) {
1091 // No vtable.
1092 size_t count = klass->NumVirtualMethods();
1093 if (!IsUint(16, count)) {
1094 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1095 return false;
1096 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001097 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001098 klass->GetVirtualMethod(i)->method_index_ = i;
1099 }
1100 } else {
1101 // Link virtual method tables
1102 LinkVirtualMethods(klass);
1103
1104 // Link interface method tables
1105 LinkInterfaceMethods(klass);
1106
1107 // Insert stubs.
1108 LinkAbstractMethods(klass);
1109 }
1110 return true;
1111}
1112
1113bool ClassLinker::LinkVirtualMethods(Class* klass) {
1114 uint32_t max_count = klass->NumVirtualMethods();
1115 if (klass->GetSuperClass() != NULL) {
1116 max_count += klass->GetSuperClass()->NumVirtualMethods();
1117 } else {
1118 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
1119 }
1120 // TODO: do not assign to the vtable field until it is fully constructed.
1121 // TODO: make this a vector<Method*> instead?
1122 klass->vtable_ = new Method*[max_count];
1123 if (klass->HasSuperClass()) {
1124 memcpy(klass->vtable_,
1125 klass->GetSuperClass()->vtable_,
1126 klass->GetSuperClass()->vtable_count_ * sizeof(Method*));
1127 size_t actual_count = klass->GetSuperClass()->vtable_count_;
1128 // See if any of our virtual methods override the superclass.
1129 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1130 Method* local_method = klass->GetVirtualMethod(i);
1131 size_t j = 0;
1132 for (; j < klass->GetSuperClass()->vtable_count_; ++j) {
1133 const Method* super_method = klass->vtable_[j];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001134 if (HasSameNameAndPrototype(local_method, super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001135 // Verify
1136 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001137 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001138 return false;
1139 }
1140 klass->vtable_[j] = local_method;
1141 local_method->method_index_ = j;
1142 break;
1143 }
1144 }
1145 if (j == klass->GetSuperClass()->vtable_count_) {
1146 // Not overriding, append.
1147 klass->vtable_[actual_count] = local_method;
1148 local_method->method_index_ = actual_count;
1149 actual_count += 1;
1150 }
1151 }
1152 if (!IsUint(16, actual_count)) {
1153 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1154 return false;
1155 }
1156 CHECK_LE(actual_count, max_count);
1157 if (actual_count < max_count) {
1158 Method** new_vtable = new Method*[actual_count];
1159 memcpy(new_vtable, klass->vtable_, actual_count * sizeof(Method*));
Carl Shapiro565f5072011-07-10 13:39:43 -07001160 delete[] klass->vtable_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001161 klass->vtable_ = new_vtable;
1162 LG << "shrunk vtable: "
1163 << "was " << max_count << ", "
1164 << "now " << actual_count;
1165 }
1166 klass->vtable_count_ = actual_count;
1167 } else {
1168 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
1169 if (!IsUint(16, klass->NumVirtualMethods())) {
1170 LG << "Too many methods"; // TODO: VirtualMachineError
1171 return false;
1172 }
1173 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1174 klass->vtable_[i] = klass->GetVirtualMethod(i);
1175 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1176 }
1177 klass->vtable_count_ = klass->NumVirtualMethods();
1178 }
1179 return true;
1180}
1181
1182bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1183 int pool_offset = 0;
1184 int pool_size = 0;
1185 int miranda_count = 0;
1186 int miranda_alloc = 0;
1187 size_t super_ifcount;
1188 if (klass->HasSuperClass()) {
1189 super_ifcount = klass->GetSuperClass()->iftable_count_;
1190 } else {
1191 super_ifcount = 0;
1192 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001193 size_t ifcount = super_ifcount;
1194 ifcount += klass->NumInterfaces();
1195 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1196 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001197 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001198 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001199 DCHECK(klass->iftable_count_ == 0);
1200 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001201 return true;
1202 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001203 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1204 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001205 if (super_ifcount != 0) {
1206 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1207 sizeof(InterfaceEntry) * super_ifcount);
1208 }
1209 // Flatten the interface inheritance hierarchy.
1210 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001211 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1212 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001213 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001214 if (!interf->IsInterface()) {
1215 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1216 return false;
1217 }
1218 klass->iftable_[idx++].SetClass(interf);
1219 for (size_t j = 0; j < interf->iftable_count_; j++) {
1220 klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
1221 }
1222 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001223 CHECK_EQ(idx, ifcount);
1224 klass->iftable_count_ = ifcount;
1225 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001226 return true;
1227 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001228 for (size_t i = super_ifcount; i < ifcount; i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001229 pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
1230 }
1231 if (pool_size == 0) {
1232 return true;
1233 }
1234 klass->ifvi_pool_count_ = pool_size;
1235 klass->ifvi_pool_ = new uint32_t[pool_size];
1236 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001237 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001238 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
1239 Class* interface = klass->iftable_[i].GetClass();
1240 pool_offset += interface->NumVirtualMethods(); // end here
1241 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1242 Method* interface_method = interface->GetVirtualMethod(j);
1243 int k; // must be signed
1244 for (k = klass->vtable_count_ - 1; k >= 0; --k) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001245 if (HasSameNameAndPrototype(interface_method, klass->vtable_[k])) {
Carl Shapiro565f5072011-07-10 13:39:43 -07001246 if (!klass->vtable_[k]->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001247 LG << "Implementation not public";
1248 return false;
1249 }
1250 klass->iftable_[i].method_index_array_[j] = k;
1251 break;
1252 }
1253 }
1254 if (k < 0) {
1255 if (miranda_count == miranda_alloc) {
1256 miranda_alloc += 8;
1257 if (miranda_list.empty()) {
1258 miranda_list.resize(miranda_alloc);
1259 } else {
1260 miranda_list.resize(miranda_alloc);
1261 }
1262 }
1263 int mir;
1264 for (mir = 0; mir < miranda_count; mir++) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001265 if (HasSameNameAndPrototype(miranda_list[mir], interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001266 break;
1267 }
1268 }
1269 // point the interface table at a phantom slot index
1270 klass->iftable_[i].method_index_array_[j] = klass->vtable_count_ + mir;
1271 if (mir == miranda_count) {
1272 miranda_list[miranda_count++] = interface_method;
1273 }
1274 }
1275 }
1276 }
1277 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001278 int old_method_count = klass->NumVirtualMethods();
1279 int new_method_count = old_method_count + miranda_count;
1280 ObjectArray* new_virtual_methods = AllocObjectArray(new_method_count);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001281 if (klass->virtual_methods_ != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001282 ObjectArray::Copy(klass->virtual_methods_, 0,
1283 new_virtual_methods, 0,
1284 old_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001285 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001286 klass->virtual_methods_ = new_virtual_methods;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001287
1288 CHECK(klass->vtable_ != NULL);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001289 int old_vtable_count = klass->vtable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001290 klass->vtable_count_ += miranda_count;
1291
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001292 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001293 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001294 memcpy(meth, miranda_list[i], sizeof(Method));
1295 meth->klass_ = klass;
1296 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001297 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1298 klass->SetVirtualMethod(old_method_count + i, meth);
1299 klass->vtable_[old_vtable_count + i] = meth;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001300 }
1301 }
1302 return true;
1303}
1304
1305void ClassLinker::LinkAbstractMethods(Class* klass) {
1306 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1307 Method* method = klass->GetVirtualMethod(i);
1308 if (method->IsAbstract()) {
1309 method->insns_ = reinterpret_cast<uint16_t*>(0xFFFFFFFF); // TODO: AbstractMethodError
1310 }
1311 }
1312}
1313
1314bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001315 int field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001316 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001317 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001318 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001319 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001320 }
1321 // Move references to the front.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001322 klass->num_reference_instance_fields_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001323 size_t i = 0;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001324 for ( ; i < klass->NumInstanceFields(); i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001325 InstanceField* pField = klass->GetInstanceField(i);
1326 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001327 if (c != '[' && c != 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001328 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1329 InstanceField* refField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001330 char rc = refField->GetType();
1331 if (rc == '[' || rc == 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001332 klass->SetInstanceField(i, refField);
1333 klass->SetInstanceField(j, pField);
1334 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001335 c = rc;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001336 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001337 break;
1338 }
1339 }
1340 } else {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001341 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001342 }
1343 if (c != '[' && c != 'L') {
1344 break;
1345 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001346 pField->SetOffset(field_offset);
1347 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001348 }
1349
1350 // Now we want to pack all of the double-wide fields together. If
1351 // we're not aligned, though, we want to shuffle one 32-bit field
1352 // into place. If we can't find one, we'll have to pad it.
Brian Carlstroma0808032011-07-18 00:39:23 -07001353 if (i != klass->NumInstanceFields() && (field_offset & 0x04) != 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001354 InstanceField* pField = klass->GetInstanceField(i);
1355 char c = pField->GetType();
1356
1357 if (c != 'J' && c != 'D') {
1358 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001359 DCHECK(c != '[');
1360 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001361 pField->SetOffset(field_offset);
1362 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001363 i++;
1364 } else {
1365 // Next field is 64-bit, so search for a 32-bit field we can
1366 // swap into it.
1367 bool found = false;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001368 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1369 InstanceField* singleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001370 char rc = singleField->GetType();
1371 if (rc != 'J' && rc != 'D') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001372 klass->SetInstanceField(i, singleField);
1373 klass->SetInstanceField(j, pField);
1374 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001375 pField->SetOffset(field_offset);
1376 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001377 found = true;
1378 i++;
1379 break;
1380 }
1381 }
1382 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001383 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001384 }
1385 }
1386 }
1387
1388 // Alignment is good, shuffle any double-wide fields forward, and
1389 // finish assigning field offsets to all fields.
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001390 DCHECK(i == klass->NumInstanceFields() || (field_offset & 0x04) == 0);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001391 for ( ; i < klass->NumInstanceFields(); i++) {
1392 InstanceField* pField = klass->GetInstanceField(i);
1393 char c = pField->GetType();
1394 if (c != 'D' && c != 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001395 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1396 InstanceField* doubleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001397 char rc = doubleField->GetType();
1398 if (rc == 'D' || rc == 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001399 klass->SetInstanceField(i, doubleField);
1400 klass->SetInstanceField(j, pField);
1401 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001402 c = rc;
1403 break;
1404 }
1405 }
1406 } else {
1407 // This is a double-wide field, leave it be.
1408 }
1409
Brian Carlstroma0808032011-07-18 00:39:23 -07001410 pField->SetOffset(field_offset);
1411 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001412 if (c == 'J' || c == 'D')
Brian Carlstroma0808032011-07-18 00:39:23 -07001413 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001414 }
1415
1416#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001417 // Make sure that all reference fields appear before
1418 // non-reference fields, and all double-wide fields are aligned.
1419 bool seen_non_ref = false;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001420 for (i = 0; i < klass->NumInstanceFields(); i++) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001421 InstanceField *pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001422 char c = pField->GetType();
1423
1424 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001425 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001426 }
1427
1428 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001429 if (!seen_non_ref) {
1430 seen_non_ref = true;
1431 DCHECK_EQ(klass->num_reference_ifields_, i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001432 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001433 } else {
1434 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001435 }
1436 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001437 if (!seen_non_ref) {
1438 DCHECK(klass->NumInstanceFields(), klass->num_reference_ifields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001439 }
1440#endif
1441
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001442 if (klass->object_size_ == 0) {
1443 // avoid overwriting object_size_ of special crafted classes such as java_lang_*_
1444 klass->object_size_ = field_offset;
1445 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001446 return true;
1447}
1448
1449// Set the bitmap of reference offsets, refOffsets, from the ifields
1450// list.
1451void ClassLinker::CreateReferenceOffsets(Class* klass) {
1452 uint32_t reference_offsets = 0;
1453 if (klass->HasSuperClass()) {
1454 reference_offsets = klass->GetSuperClass()->GetReferenceOffsets();
1455 }
1456 // If our superclass overflowed, we don't stand a chance.
1457 if (reference_offsets != CLASS_WALK_SUPER) {
1458 // All of the fields that contain object references are guaranteed
1459 // to be at the beginning of the ifields list.
1460 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) {
1461 // Note that, per the comment on struct InstField, f->byteOffset
1462 // is the offset from the beginning of obj, not the offset into
1463 // obj->instanceData.
1464 const InstanceField* field = klass->GetInstanceField(i);
1465 size_t byte_offset = field->GetOffset();
1466 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001467 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001468 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1469 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001470 CHECK_NE(new_bit, 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001471 reference_offsets |= new_bit;
1472 } else {
1473 reference_offsets = CLASS_WALK_SUPER;
1474 break;
1475 }
1476 }
1477 klass->SetReferenceOffsets(reference_offsets);
1478 }
1479}
1480
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001481Class* ClassLinker::ResolveClass(const Class* referrer,
1482 uint32_t class_idx,
Brian Carlstromf615a612011-07-23 12:50:34 -07001483 const DexFile* dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001484 DexCache* dex_cache = referrer->GetDexCache();
1485 Class* resolved = dex_cache->GetResolvedClass(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001486 if (resolved != NULL) {
1487 return resolved;
1488 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001489 const char* descriptor = dex_file->dexStringByTypeIdx(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001490 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001491 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001492 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -07001493 resolved = FindClass(descriptor, referrer->GetClassLoader(), dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001494 }
1495 if (resolved != NULL) {
1496 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001497 if (referrer->GetDexCache() != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001498 if (check->GetClassLoader() != NULL) {
1499 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1500 return NULL;
1501 }
1502 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001503 dex_cache->SetResolvedClass(class_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001504 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001505 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001506 }
1507 return resolved;
1508}
1509
1510Method* ResolveMethod(const Class* referrer, uint32_t method_idx,
1511 /*MethodType*/ int method_type) {
1512 CHECK(false);
1513 return NULL;
1514}
1515
Carl Shapiro69759ea2011-07-21 18:13:35 -07001516String* ClassLinker::ResolveString(const Class* referring,
1517 uint32_t string_idx) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001518 const DexFile* dex_file = FindDexFile(referring->GetDexCache());
1519 const DexFile::StringId& string_id = dex_file->GetStringId(string_idx);
1520 const char* string_data = dex_file->GetStringData(string_id);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001521 String* new_string = Heap::AllocStringFromModifiedUtf8(java_lang_String_,
1522 char_array_class_,
1523 string_data);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001524 // TODO: intern the new string
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001525 referring->GetDexCache()->SetResolvedString(string_idx, new_string);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001526 return new_string;
1527}
1528
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001529} // namespace art