blob: 01e1119c1f749ec5f43fe29718d5d69b427bb204 [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
Brian Carlstrom4a96b602011-07-26 16:40:23 -070089 array_interfaces_ = AllocObjectArray<Class>(2);
Brian Carlstrom913af1b2011-07-23 21:41:13 -070090 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);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700101 array_iftable_[0].SetClass(array_interfaces_->Get(0));
102 array_iftable_[1].SetClass(array_interfaces_->Get(1));
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700103
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() {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700116 return down_cast<DexCache*>(AllocObjectArray<Object>(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 Carlstroma40f9bc2011-07-26 21:26:07 -0700120 Class* klass = down_cast<Class*>(Object::Alloc(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 Carlstrom6cc18452011-07-18 15:10:33 -0700140Class* ClassLinker::FindClass(const StringPiece& descriptor,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700141 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -0700142 const DexFile* dex_file) {
Carl Shapirob5573532011-07-12 18:22:59 -0700143 Thread* self = Thread::Current();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700144 DCHECK(self != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700145 CHECK(!self->IsExceptionPending());
146 // Find the class in the loaded classes table.
147 Class* klass = LookupClass(descriptor, class_loader);
148 if (klass == NULL) {
149 // Class is not yet loaded.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700150 if (descriptor[0] == '[') {
Brian Carlstromf615a612011-07-23 12:50:34 -0700151 return CreateArrayClass(descriptor, class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700152 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700153 ClassPathEntry pair;
Brian Carlstromf615a612011-07-23 12:50:34 -0700154 if (dex_file == NULL) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700155 pair = FindInBootClassPath(descriptor);
156 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700157 pair.first = dex_file;
158 pair.second = dex_file->FindClassDef(descriptor);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700159 }
160 if (pair.second == NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700161 LG << "Class " << descriptor << " not found"; // TODO: NoClassDefFoundError
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700162 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700163 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700164 const DexFile* dex_file = pair.first;
165 const DexFile::ClassDef* dex_class_def = pair.second;
166 DexCache* dex_cache = FindDexCache(dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700167 // Load the class from the dex file.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700168 // TODO add fast path to avoid all these comparisons once special cases are found
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700169 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700170 klass = java_lang_Object_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700171 klass->dex_cache_ = dex_cache;
Brian Carlstroma0808032011-07-18 00:39:23 -0700172 klass->object_size_ = sizeof(Object);
Brian Carlstromf615a612011-07-23 12:50:34 -0700173 char_array_class_->super_class_idx_ = dex_class_def->class_idx_;
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700174 } else if (descriptor == "Ljava/lang/Class;") {
Carl Shapiro565f5072011-07-10 13:39:43 -0700175 klass = java_lang_Class_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700176 klass->dex_cache_ = dex_cache;
Brian Carlstroma0808032011-07-18 00:39:23 -0700177 klass->object_size_ = sizeof(Class);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700178 } else if (descriptor == "Ljava/lang/reflect/Field;") {
179 klass = java_lang_reflect_Field_;
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(Field);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700182 } else if (descriptor == "Ljava/lang/reflect/Method;") {
183 klass = java_lang_reflect_Method_;
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(Method);
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700186 } else if (descriptor == "Ljava/lang/String;") {
Brian Carlstroma0808032011-07-18 00:39:23 -0700187 klass = java_lang_String_;
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(String);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700190 } else if (descriptor == "Ljava/lang/Cloneable;") {
191 klass = java_lang_Cloneable_;
192 klass->dex_cache_ = dex_cache;
193 klass->object_size_ = 0;
194 } else if (descriptor == "Ljava/io/Serializable;") {
195 klass = java_io_Serializable_;
196 klass->dex_cache_ = dex_cache;
197 klass->object_size_ = 0;
Carl Shapiro565f5072011-07-10 13:39:43 -0700198 } else {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700199 klass = AllocClass(dex_cache);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700200 // LinkInstanceFields only will update object_size_ if it is 0
201 // to avoid overwriting the special initialized cases above.
202 klass->object_size_ = 0;
Carl Shapiro565f5072011-07-10 13:39:43 -0700203 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700204 LoadClass(*dex_file, *dex_class_def, klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700205 // Check for a pending exception during load
206 if (self->IsExceptionPending()) {
207 // TODO: free native allocations in klass
208 return NULL;
209 }
210 {
211 ObjectLock lock(klass);
Carl Shapirob5573532011-07-12 18:22:59 -0700212 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700213 // Add the newly loaded class to the loaded classes table.
214 bool success = InsertClass(klass);
215 if (!success) {
216 // We may fail to insert if we raced with another thread.
217 klass->clinit_thread_id_ = 0;
218 // TODO: free native allocations in klass
219 klass = LookupClass(descriptor, class_loader);
220 CHECK(klass != NULL);
221 } else {
222 // Link the class.
Brian Carlstromf615a612011-07-23 12:50:34 -0700223 if (!LinkClass(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700224 // Linking failed.
225 // TODO: CHECK(self->IsExceptionPending());
226 lock.NotifyAll();
227 return NULL;
228 }
229 }
230 }
231 }
232 // Link the class if it has not already been linked.
233 if (!klass->IsLinked() && !klass->IsErroneous()) {
234 ObjectLock lock(klass);
235 // Check for circular dependencies between classes.
Carl Shapirob5573532011-07-12 18:22:59 -0700236 if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700237 LG << "Recursive link"; // TODO: ClassCircularityError
238 return NULL;
239 }
240 // Wait for the pending initialization to complete.
241 while (!klass->IsLinked() && !klass->IsErroneous()) {
242 lock.Wait();
243 }
244 }
245 if (klass->IsErroneous()) {
246 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
247 return NULL;
248 }
249 // Return the loaded class. No exceptions should be pending.
250 CHECK(!self->IsExceptionPending());
251 return klass;
252}
253
Brian Carlstromf615a612011-07-23 12:50:34 -0700254void ClassLinker::LoadClass(const DexFile& dex_file,
255 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700256 Class* klass) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700257 CHECK(klass != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700258 CHECK(klass->dex_cache_ != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700259 const byte* class_data = dex_file.GetClassData(dex_class_def);
260 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700261
Brian Carlstromf615a612011-07-23 12:50:34 -0700262 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700263 CHECK(descriptor != NULL);
264
265 klass->klass_ = java_lang_Class_;
266 klass->descriptor_.set(descriptor);
267 klass->descriptor_alloc_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700268 klass->access_flags_ = dex_class_def.access_flags_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700269 klass->class_loader_ = NULL; // TODO
270 klass->primitive_type_ = Class::kPrimNot;
271 klass->status_ = Class::kStatusIdx;
272
273 klass->super_class_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700274 klass->super_class_idx_ = dex_class_def.superclass_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700275
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700276 size_t num_static_fields = header.static_fields_size_;
277 size_t num_instance_fields = header.instance_fields_size_;
278 size_t num_direct_methods = header.direct_methods_size_;
279 size_t num_virtual_methods = header.virtual_methods_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700280
Brian Carlstromf615a612011-07-23 12:50:34 -0700281 klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700282
283 // Load class interfaces.
Brian Carlstromf615a612011-07-23 12:50:34 -0700284 LoadInterfaces(dex_file, dex_class_def, klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700285
286 // Load static fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700287 DCHECK(klass->sfields_ == NULL);
288 if (num_static_fields != 0) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700289 klass->sfields_ = AllocObjectArray<StaticField>(num_static_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700290 uint32_t last_idx = 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700291 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700292 DexFile::Field dex_field;
293 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700294 StaticField* sfield = AllocStaticField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700295 klass->SetStaticField(i, sfield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700296 LoadField(dex_file, dex_field, klass, sfield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700297 }
298 }
299
300 // Load instance fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700301 DCHECK(klass->ifields_ == NULL);
302 if (num_instance_fields != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700303 // TODO: allocate on the object heap.
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700304 klass->ifields_ = AllocObjectArray<InstanceField>(num_instance_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700305 uint32_t last_idx = 0;
306 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700307 DexFile::Field dex_field;
308 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700309 InstanceField* ifield = AllocInstanceField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700310 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700311 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700312 }
313 }
314
315 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700316 DCHECK(klass->direct_methods_ == NULL);
317 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700318 // TODO: append direct methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700319 klass->direct_methods_ = AllocObjectArray<Method>(num_direct_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700320 uint32_t last_idx = 0;
321 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700322 DexFile::Method dex_method;
323 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700324 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700325 klass->SetDirectMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700326 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700327 // TODO: register maps
328 }
329 }
330
331 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700332 DCHECK(klass->virtual_methods_ == NULL);
333 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700334 // TODO: append virtual methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700335 klass->virtual_methods_ = AllocObjectArray<Method>(num_virtual_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700336 uint32_t last_idx = 0;
337 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700338 DexFile::Method dex_method;
339 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700340 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700341 klass->SetVirtualMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700342 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700343 // TODO: register maps
344 }
345 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700346}
347
Brian Carlstromf615a612011-07-23 12:50:34 -0700348void ClassLinker::LoadInterfaces(const DexFile& dex_file,
349 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700350 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700351 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700352 if (list != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700353 DCHECK(klass->interfaces_ == NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700354 klass->interfaces_ = AllocObjectArray<Class>(list->Size());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700355 DCHECK(klass->interfaces_idx_ == NULL);
356 klass->interfaces_idx_ = new uint32_t[list->Size()];
Brian Carlstrom934486c2011-07-12 23:42:50 -0700357 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700358 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700359 klass->interfaces_idx_[i] = type_item.type_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700360 }
361 }
362}
363
Brian Carlstromf615a612011-07-23 12:50:34 -0700364void ClassLinker::LoadField(const DexFile& dex_file,
365 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700366 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700367 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700368 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700369 dst->klass_ = klass;
Brian Carlstromae3ac012011-07-27 01:30:28 -0700370 dst->name_.set(dex_file.dexStringById(field_id.name_idx_));
371 dst->descriptor_.set(dex_file.dexStringByTypeIdx(field_id.type_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700372 dst->access_flags_ = src.access_flags_;
373}
374
Brian Carlstromf615a612011-07-23 12:50:34 -0700375void ClassLinker::LoadMethod(const DexFile& dex_file,
376 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700377 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700378 Method* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700379 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700380 dst->klass_ = klass;
Brian Carlstromf615a612011-07-23 12:50:34 -0700381 dst->name_.set(dex_file.dexStringById(method_id.name_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700382 dst->proto_idx_ = method_id.proto_idx_;
Brian Carlstromf615a612011-07-23 12:50:34 -0700383 dst->shorty_.set(dex_file.GetShorty(method_id.proto_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700384 dst->access_flags_ = src.access_flags_;
385
386 // TODO: check for finalize method
387
Brian Carlstromf615a612011-07-23 12:50:34 -0700388 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700389 if (code_item != NULL) {
390 dst->num_registers_ = code_item->registers_size_;
391 dst->num_ins_ = code_item->ins_size_;
392 dst->num_outs_ = code_item->outs_size_;
393 dst->insns_ = code_item->insns_;
394 } else {
395 uint16_t num_args = dst->NumArgRegisters();
396 if (!dst->IsStatic()) {
397 ++num_args;
398 }
399 dst->num_registers_ = dst->num_ins_ + num_args;
400 // TODO: native methods
401 }
402}
403
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700404ClassLinker::ClassPathEntry ClassLinker::FindInBootClassPath(const StringPiece& descriptor) {
405 for (size_t i = 0; i != boot_class_path_.size(); ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700406 const DexFile* dex_file = boot_class_path_[i];
Brian Carlstromf615a612011-07-23 12:50:34 -0700407 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
408 if (dex_class_def != NULL) {
409 return ClassPathEntry(dex_file, dex_class_def);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700410 }
411 }
Brian Carlstroma0808032011-07-18 00:39:23 -0700412 return ClassPathEntry(NULL, NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700413}
414
Brian Carlstromf615a612011-07-23 12:50:34 -0700415void ClassLinker::AppendToBootClassPath(DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700416 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700417 boot_class_path_.push_back(dex_file);
418 RegisterDexFile(dex_file);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700419}
420
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700421void ClassLinker::RegisterDexFile(const DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700422 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700423 dex_files_.push_back(dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700424 DexCache* dex_cache = AllocDexCache();
425 CHECK(dex_cache != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700426 dex_cache->Init(AllocObjectArray<String>(dex_file->NumStringIds()),
427 AllocObjectArray<Class>(dex_file->NumTypeIds()),
428 AllocObjectArray<Method>(dex_file->NumMethodIds()),
429 AllocObjectArray<Field>(dex_file->NumFieldIds()));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700430 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700431}
432
Brian Carlstromf615a612011-07-23 12:50:34 -0700433const DexFile* ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700434 CHECK(dex_cache != NULL);
435 for (size_t i = 0; i != dex_caches_.size(); ++i) {
436 if (dex_caches_[i] == dex_cache) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700437 return dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700438 }
439 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700440 CHECK(false) << "Could not find DexFile";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700441 return NULL;
442}
443
Brian Carlstromf615a612011-07-23 12:50:34 -0700444DexCache* ClassLinker::FindDexCache(const DexFile* dex_file) const {
445 CHECK(dex_file != NULL);
446 for (size_t i = 0; i != dex_files_.size(); ++i) {
447 if (dex_files_[i] == dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700448 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700449 }
450 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700451 CHECK(false) << "Could not find DexCache";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700452 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700453}
454
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700455Class* ClassLinker::CreatePrimitiveClass(const StringPiece& descriptor) {
Brian Carlstroma0808032011-07-18 00:39:23 -0700456 Class* klass = AllocClass(NULL);
Carl Shapiro565f5072011-07-10 13:39:43 -0700457 CHECK(klass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700458 klass->super_class_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700459 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
460 klass->descriptor_ = descriptor;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700461 klass->descriptor_alloc_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700462 klass->status_ = Class::kStatusInitialized;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700463 bool success = InsertClass(klass);
464 CHECK(success);
Carl Shapiro565f5072011-07-10 13:39:43 -0700465 return klass;
466}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700467
Brian Carlstrombe977852011-07-19 14:54:54 -0700468// Create an array class (i.e. the class object for the array, not the
469// array itself). "descriptor" looks like "[C" or "[[[[B" or
470// "[Ljava/lang/String;".
471//
472// If "descriptor" refers to an array of primitives, look up the
473// primitive type's internally-generated class object.
474//
475// "loader" is the class loader of the class that's referring to us. It's
476// used to ensure that we're looking for the element type in the right
477// context. It does NOT become the class loader for the array class; that
478// always comes from the base element class.
479//
480// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700481Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
482 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -0700483 const DexFile* dex_file)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700484{
485 CHECK(descriptor[0] == '[');
486 DCHECK(java_lang_Class_ != NULL);
487 DCHECK(java_lang_Object_ != NULL);
488
Brian Carlstrombe977852011-07-19 14:54:54 -0700489 // Identify the underlying element class and the array dimension depth.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700490 Class* component_type_ = NULL;
491 int array_rank;
492 if (descriptor[1] == '[') {
Brian Carlstrombe977852011-07-19 14:54:54 -0700493 // array of arrays; keep descriptor and grab stuff from parent
Brian Carlstromf615a612011-07-23 12:50:34 -0700494 Class* outer = FindClass(descriptor.substr(1), class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700495 if (outer != NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700496 // want the base class, not "outer", in our component_type_
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700497 component_type_ = outer->component_type_;
498 array_rank = outer->array_rank_ + 1;
499 } else {
Brian Carlstrombe977852011-07-19 14:54:54 -0700500 DCHECK(component_type_ == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700501 }
502 } else {
503 array_rank = 1;
504 if (descriptor[1] == 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -0700505 // array of objects; strip off "[" and look up descriptor.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700506 const StringPiece subDescriptor = descriptor.substr(1);
Brian Carlstromf615a612011-07-23 12:50:34 -0700507 component_type_ = FindClass(subDescriptor, class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700508 } else {
Brian Carlstrombe977852011-07-19 14:54:54 -0700509 // array of a primitive type
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700510 component_type_ = FindPrimitiveClass(descriptor[1]);
511 }
512 }
513
514 if (component_type_ == NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700515 // failed
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700516 DCHECK(Thread::Current()->IsExceptionPending());
517 return NULL;
518 }
519
Brian Carlstrombe977852011-07-19 14:54:54 -0700520 // See if the component type is already loaded. Array classes are
521 // always associated with the class loader of their underlying
522 // element type -- an array of Strings goes with the loader for
523 // java/lang/String -- so we need to look for it there. (The
524 // caller should have checked for the existence of the class
525 // before calling here, but they did so with *their* class loader,
526 // not the component type's loader.)
527 //
528 // If we find it, the caller adds "loader" to the class' initiating
529 // loader list, which should prevent us from going through this again.
530 //
531 // This call is unnecessary if "loader" and "component_type_->class_loader_"
532 // are the same, because our caller (FindClass) just did the
533 // lookup. (Even if we get this wrong we still have correct behavior,
534 // because we effectively do this lookup again when we add the new
535 // class to the hash table --- necessary because of possible races with
536 // other threads.)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700537 if (class_loader != component_type_->class_loader_) {
538 Class* new_class = LookupClass(descriptor, component_type_->class_loader_);
539 if (new_class != NULL) {
540 return new_class;
541 }
542 }
543
Brian Carlstrombe977852011-07-19 14:54:54 -0700544 // Fill out the fields in the Class.
545 //
546 // It is possible to execute some methods against arrays, because
547 // all arrays are subclasses of java_lang_Object_, so we need to set
548 // up a vtable. We can just point at the one in java_lang_Object_.
549 //
550 // Array classes are simple enough that we don't need to do a full
551 // link step.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700552
553 Class* new_class;
554 if (descriptor == "[Ljava/lang/Object;") {
555 CHECK(object_array_class_);
556 new_class = object_array_class_;
557 } else {
558 new_class = AllocClass(NULL);
559 if (new_class == NULL) {
560 return NULL;
561 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700562 }
563 new_class->descriptor_alloc_ = new std::string(descriptor.data(),
564 descriptor.size());
565 new_class->descriptor_.set(new_class->descriptor_alloc_->data(),
566 new_class->descriptor_alloc_->size());
567 new_class->super_class_ = java_lang_Object_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700568 new_class->vtable_ = java_lang_Object_->vtable_;
569 new_class->primitive_type_ = Class::kPrimNot;
570 new_class->component_type_ = component_type_;
571 new_class->class_loader_ = component_type_->class_loader_;
572 new_class->array_rank_ = array_rank;
573 new_class->status_ = Class::kStatusInitialized;
Brian Carlstrombe977852011-07-19 14:54:54 -0700574 // don't need to set new_class->object_size_
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700575
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700576
Brian Carlstrombe977852011-07-19 14:54:54 -0700577 // All arrays have java/lang/Cloneable and java/io/Serializable as
578 // interfaces. We need to set that up here, so that stuff like
579 // "instanceof" works right.
580 //
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700581 // Note: The GC could run during the call to FindSystemClass,
Brian Carlstrombe977852011-07-19 14:54:54 -0700582 // so we need to make sure the class object is GC-valid while we're in
583 // there. Do this by clearing the interface list so the GC will just
584 // think that the entries are null.
Brian Carlstrombe977852011-07-19 14:54:54 -0700585
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700586
587 // Use the single, global copies of "interfaces" and "iftable"
588 // (remember not to free them for arrays).
589 DCHECK(array_interfaces_ != NULL);
590 new_class->interfaces_ = array_interfaces_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700591 new_class->iftable_count_ = 2;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700592 DCHECK(array_iftable_ != NULL);
593 new_class->iftable_ = array_iftable_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700594
Brian Carlstrombe977852011-07-19 14:54:54 -0700595 // Inherit access flags from the component type. Arrays can't be
596 // used as a superclass or interface, so we want to add "final"
597 // and remove "interface".
598 //
599 // Don't inherit any non-standard flags (e.g., kAccFinal)
600 // from component_type_. We assume that the array class does not
601 // override finalize().
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700602 new_class->access_flags_ = ((new_class->component_type_->access_flags_ &
603 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask;
604
605 if (InsertClass(new_class)) {
606 return new_class;
607 }
Brian Carlstrombe977852011-07-19 14:54:54 -0700608 // Another thread must have loaded the class after we
609 // started but before we finished. Abandon what we've
610 // done.
611 //
612 // (Yes, this happens.)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700613
Brian Carlstrombe977852011-07-19 14:54:54 -0700614 // Grab the winning class.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700615 Class* other_class = LookupClass(descriptor, component_type_->class_loader_);
616 DCHECK(other_class != NULL);
617 return other_class;
618}
619
620Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700621 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700622 case 'B':
Carl Shapiro565f5072011-07-10 13:39:43 -0700623 CHECK(primitive_byte_ != NULL);
624 return primitive_byte_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700625 case 'C':
Carl Shapiro565f5072011-07-10 13:39:43 -0700626 CHECK(primitive_char_ != NULL);
627 return primitive_char_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700628 case 'D':
Carl Shapiro565f5072011-07-10 13:39:43 -0700629 CHECK(primitive_double_ != NULL);
630 return primitive_double_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700631 case 'F':
Carl Shapiro565f5072011-07-10 13:39:43 -0700632 CHECK(primitive_float_ != NULL);
633 return primitive_float_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700634 case 'I':
Carl Shapiro565f5072011-07-10 13:39:43 -0700635 CHECK(primitive_int_ != NULL);
636 return primitive_int_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700637 case 'J':
Carl Shapiro565f5072011-07-10 13:39:43 -0700638 CHECK(primitive_long_ != NULL);
639 return primitive_long_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700640 case 'S':
Carl Shapiro565f5072011-07-10 13:39:43 -0700641 CHECK(primitive_short_ != NULL);
642 return primitive_short_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700643 case 'Z':
Carl Shapiro565f5072011-07-10 13:39:43 -0700644 CHECK(primitive_boolean_ != NULL);
645 return primitive_boolean_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700646 case 'V':
Carl Shapiro565f5072011-07-10 13:39:43 -0700647 CHECK(primitive_void_ != NULL);
648 return primitive_void_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700649 case 'L':
650 case '[':
651 LOG(ERROR) << "Not a primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700652 default:
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700653 LOG(ERROR) << "Unknown primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700654 };
655 return NULL; // Not reachable.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700656}
657
658bool ClassLinker::InsertClass(Class* klass) {
659 // TODO: acquire classes_lock_
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700660 const StringPiece& key = klass->GetDescriptor();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700661 bool success = classes_.insert(std::make_pair(key, klass)).second;
662 // TODO: release classes_lock_
663 return success;
664}
665
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700666Class* ClassLinker::LookupClass(const StringPiece& descriptor, Object* class_loader) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700667 // TODO: acquire classes_lock_
668 Table::iterator it = classes_.find(descriptor);
669 // TODO: release classes_lock_
670 if (it == classes_.end()) {
671 return NULL;
672 } else {
673 return (*it).second;
674 }
675}
676
677bool ClassLinker::InitializeClass(Class* klass) {
678 CHECK(klass->GetStatus() == Class::kStatusResolved ||
679 klass->GetStatus() == Class::kStatusError);
680
Carl Shapirob5573532011-07-12 18:22:59 -0700681 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700682
683 {
684 ObjectLock lock(klass);
685
686 if (klass->GetStatus() < Class::kStatusVerified) {
687 if (klass->IsErroneous()) {
688 LG << "re-initializing failed class"; // TODO: throw
689 return false;
690 }
691
692 CHECK(klass->GetStatus() == Class::kStatusResolved);
693
694 klass->status_ = Class::kStatusVerifying;
695 if (!DexVerify::VerifyClass(klass)) {
696 LG << "Verification failed"; // TODO: ThrowVerifyError
697 Object* exception = self->GetException();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700698 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
699 klass->SetFieldObject(field_offset, exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700700 klass->SetStatus(Class::kStatusError);
701 return false;
702 }
703
704 klass->SetStatus(Class::kStatusVerified);
705 }
706
707 if (klass->status_ == Class::kStatusInitialized) {
708 return true;
709 }
710
711 while (klass->status_ == Class::kStatusInitializing) {
712 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -0700713 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700714 LG << "recursive <clinit>";
715 return true;
716 }
717
718 CHECK(!self->IsExceptionPending());
719
720 lock.Wait(); // TODO: check for interruption
721
722 // When we wake up, repeat the test for init-in-progress. If
723 // there's an exception pending (only possible if
724 // "interruptShouldThrow" was set), bail out.
725 if (self->IsExceptionPending()) {
726 CHECK(false);
727 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
728 klass->SetStatus(Class::kStatusError);
729 return false;
730 }
731 if (klass->GetStatus() == Class::kStatusInitializing) {
732 continue;
733 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700734 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700735 klass->GetStatus() == Class::kStatusError);
736 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700737 // The caller wants an exception, but it was thrown in a
738 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700739 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
740 return false;
741 }
742 return true; // otherwise, initialized
743 }
744
745 // see if we failed previously
746 if (klass->IsErroneous()) {
747 // might be wise to unlock before throwing; depends on which class
748 // it is that we have locked
749
750 // TODO: throwEarlierClassFailure(klass);
751 return false;
752 }
753
754 if (!ValidateSuperClassDescriptors(klass)) {
755 klass->SetStatus(Class::kStatusError);
756 return false;
757 }
758
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700759 DCHECK(klass->status_ < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700760
Carl Shapirob5573532011-07-12 18:22:59 -0700761 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700762 klass->status_ = Class::kStatusInitializing;
763 }
764
765 if (!InitializeSuperClass(klass)) {
766 return false;
767 }
768
769 InitializeStaticFields(klass);
770
771 Method* clinit = klass->FindDirectMethodLocally("<clinit>", "()V");
772 if (clinit != NULL) {
773 } else {
774 // JValue unused;
775 // TODO: dvmCallMethod(self, method, NULL, &unused);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700776 //CHECK(!"unimplemented");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700777 }
778
779 {
780 ObjectLock lock(klass);
781
782 if (self->IsExceptionPending()) {
783 klass->SetStatus(Class::kStatusError);
784 } else {
785 klass->SetStatus(Class::kStatusInitialized);
786 }
787 lock.NotifyAll();
788 }
789
790 return true;
791}
792
793bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
794 if (klass->IsInterface()) {
795 return true;
796 }
797 // begin with the methods local to the superclass
798 if (klass->HasSuperClass() &&
799 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
800 const Class* super = klass->GetSuperClass();
801 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
802 const Method* method = klass->GetVirtualMethod(i);
803 if (method != super->GetVirtualMethod(i) &&
804 !HasSameMethodDescriptorClasses(method, super, klass)) {
805 LG << "Classes resolve differently in superclass";
806 return false;
807 }
808 }
809 }
810 for (size_t i = 0; i < klass->iftable_count_; ++i) {
811 const InterfaceEntry* iftable = &klass->iftable_[i];
812 Class* interface = iftable->GetClass();
813 if (klass->GetClassLoader() != interface->GetClassLoader()) {
814 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
815 uint32_t vtable_index = iftable->method_index_array_[j];
816 const Method* method = klass->GetVirtualMethod(vtable_index);
817 if (!HasSameMethodDescriptorClasses(method, interface,
818 method->GetClass())) {
819 LG << "Classes resolve differently in interface"; // TODO: LinkageError
820 return false;
821 }
822 }
823 }
824 }
825 return true;
826}
827
828bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700829 const Class* klass1,
830 const Class* klass2) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700831 const DexFile* dex_file = FindDexFile(method->GetClass()->GetDexCache());
832 const DexFile::ProtoId& proto_id = dex_file->GetProtoId(method->proto_idx_);
833 DexFile::ParameterIterator *it;
834 for (it = dex_file->GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700835 const char* descriptor = it->GetDescriptor();
836 if (descriptor == NULL) {
837 break;
838 }
839 if (descriptor[0] == 'L' || descriptor[0] == '[') {
840 // Found a non-primitive type.
841 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
842 return false;
843 }
844 }
845 }
846 // Check the return type
Brian Carlstromf615a612011-07-23 12:50:34 -0700847 const char* descriptor = dex_file->GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700848 if (descriptor[0] == 'L' || descriptor[0] == '[') {
849 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
850 return false;
851 }
852 }
853 return true;
854}
855
856// Returns true if classes referenced by the descriptor are the
857// same classes in klass1 as they are in klass2.
858bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700859 const Class* klass1,
860 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700861 CHECK(descriptor != NULL);
862 CHECK(klass1 != NULL);
863 CHECK(klass2 != NULL);
864#if 0
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700865 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700866 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700867 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700868 // TODO: found2 == NULL
869 // TODO: lookup found1 in initiating loader list
870 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -0700871 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700872 if (found1 == found2) {
873 return true;
874 } else {
875 return false;
876 }
877 }
878#endif
879 return true;
880}
881
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700882bool ClassLinker::HasSameArgumentTypes(const Method* m1, const Method* m2) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700883 const DexFile* dex1 = FindDexFile(m1->GetClass()->GetDexCache());
884 const DexFile* dex2 = FindDexFile(m2->GetClass()->GetDexCache());
885 const DexFile::ProtoId& proto1 = dex1->GetProtoId(m1->proto_idx_);
886 const DexFile::ProtoId& proto2 = dex2->GetProtoId(m2->proto_idx_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700887
888 // TODO: compare ProtoId objects for equality and exit early
Brian Carlstromf615a612011-07-23 12:50:34 -0700889 const DexFile::TypeList* type_list1 = dex1->GetProtoParameters(proto1);
890 const DexFile::TypeList* type_list2 = dex2->GetProtoParameters(proto2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700891 size_t arity1 = (type_list1 == NULL) ? 0 : type_list1->Size();
892 size_t arity2 = (type_list2 == NULL) ? 0 : type_list2->Size();
893 if (arity1 != arity2) {
894 return false;
895 }
896
897 for (size_t i = 0; i < arity1; ++i) {
898 uint32_t type_idx1 = type_list1->GetTypeItem(i).type_idx_;
899 uint32_t type_idx2 = type_list2->GetTypeItem(i).type_idx_;
Brian Carlstromf615a612011-07-23 12:50:34 -0700900 const char* type1 = dex1->dexStringByTypeIdx(type_idx1);
901 const char* type2 = dex2->dexStringByTypeIdx(type_idx2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700902 if (strcmp(type1, type2) != 0) {
903 return false;
904 }
905 }
906
907 return true;
908}
909
910bool ClassLinker::HasSameReturnType(const Method* m1, const Method* m2) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700911 const DexFile* dex1 = FindDexFile(m1->GetClass()->GetDexCache());
912 const DexFile* dex2 = FindDexFile(m2->GetClass()->GetDexCache());
913 const DexFile::ProtoId& proto1 = dex1->GetProtoId(m1->proto_idx_);
914 const DexFile::ProtoId& proto2 = dex2->GetProtoId(m2->proto_idx_);
915 const char* type1 = dex1->dexStringByTypeIdx(proto1.return_type_idx_);
916 const char* type2 = dex2->dexStringByTypeIdx(proto2.return_type_idx_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700917 return (strcmp(type1, type2) == 0);
918}
919
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700920bool ClassLinker::InitializeSuperClass(Class* klass) {
921 CHECK(klass != NULL);
922 // TODO: assert klass lock is acquired
923 if (!klass->IsInterface() && klass->HasSuperClass()) {
924 Class* super_class = klass->GetSuperClass();
925 if (super_class->GetStatus() != Class::kStatusInitialized) {
926 CHECK(!super_class->IsInterface());
927 klass->MonitorExit();
928 bool super_initialized = InitializeClass(super_class);
929 klass->MonitorEnter();
930 // TODO: check for a pending exception
931 if (!super_initialized) {
932 klass->SetStatus(Class::kStatusError);
933 klass->NotifyAll();
934 return false;
935 }
936 }
937 }
938 return true;
939}
940
941void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700942 size_t num_static_fields = klass->NumStaticFields();
943 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700944 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700945 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700946 DexCache* dex_cache = klass->GetDexCache();
947 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700948 return;
949 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700950 const StringPiece& descriptor = klass->GetDescriptor();
Brian Carlstromf615a612011-07-23 12:50:34 -0700951 const DexFile* dex_file = FindDexFile(dex_cache);
952 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
953 CHECK(dex_class_def != NULL);
954 const byte* addr = dex_file->GetEncodedArray(*dex_class_def);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700955 size_t array_size = DecodeUnsignedLeb128(&addr);
956 for (size_t i = 0; i < array_size; ++i) {
957 StaticField* field = klass->GetStaticField(i);
958 JValue value;
Brian Carlstromf615a612011-07-23 12:50:34 -0700959 DexFile::ValueType type = dex_file->ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700960 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700961 case DexFile::kByte:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700962 field->SetByte(value.b);
963 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700964 case DexFile::kShort:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700965 field->SetShort(value.s);
966 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700967 case DexFile::kChar:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700968 field->SetChar(value.c);
969 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700970 case DexFile::kInt:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700971 field->SetInt(value.i);
972 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700973 case DexFile::kLong:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700974 field->SetLong(value.j);
975 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700976 case DexFile::kFloat:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700977 field->SetFloat(value.f);
978 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700979 case DexFile::kDouble:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700980 field->SetDouble(value.d);
981 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700982 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700983 uint32_t string_idx = value.i;
984 String* resolved = ResolveString(klass, string_idx);
985 field->SetObject(resolved);
986 break;
987 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700988 case DexFile::kBoolean:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700989 field->SetBoolean(value.z);
990 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700991 case DexFile::kNull:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700992 field->SetObject(value.l);
993 break;
994 default:
Carl Shapiro606258b2011-07-09 16:09:09 -0700995 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700996 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700997 }
998}
999
Brian Carlstromf615a612011-07-23 12:50:34 -07001000bool ClassLinker::LinkClass(Class* klass, const DexFile* dex_file) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001001 CHECK(klass->status_ == Class::kStatusIdx ||
1002 klass->status_ == Class::kStatusLoaded);
1003 if (klass->status_ == Class::kStatusIdx) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001004 if (!LinkInterfaces(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001005 return false;
1006 }
1007 }
1008 if (!LinkSuperClass(klass)) {
1009 return false;
1010 }
1011 if (!LinkMethods(klass)) {
1012 return false;
1013 }
1014 if (!LinkInstanceFields(klass)) {
1015 return false;
1016 }
1017 CreateReferenceOffsets(klass);
1018 CHECK_EQ(klass->status_, Class::kStatusLoaded);
1019 klass->status_ = Class::kStatusResolved;
1020 return true;
1021}
1022
Brian Carlstromf615a612011-07-23 12:50:34 -07001023bool ClassLinker::LinkInterfaces(Class* klass, const DexFile* dex_file) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001024 // Mark the class as loaded.
1025 klass->status_ = Class::kStatusLoaded;
Brian Carlstromf615a612011-07-23 12:50:34 -07001026 if (klass->super_class_idx_ != DexFile::kDexNoIndex) {
1027 Class* super_class = ResolveClass(klass, klass->super_class_idx_, dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001028 if (super_class == NULL) {
1029 LG << "Failed to resolve superclass";
1030 return false;
1031 }
1032 klass->super_class_ = super_class; // TODO: write barrier
1033 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001034 if (klass->NumInterfaces() > 0) {
1035 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
1036 uint32_t idx = klass->interfaces_idx_[i];
1037 klass->SetInterface(i, ResolveClass(klass, idx, dex_file));
1038 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001039 LG << "Failed to resolve interface";
1040 return false;
1041 }
1042 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001043 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001044 LG << "Inaccessible interface";
1045 return false;
1046 }
1047 }
1048 }
1049 return true;
1050}
1051
1052bool ClassLinker::LinkSuperClass(Class* klass) {
1053 CHECK(!klass->IsPrimitive());
1054 const Class* super = klass->GetSuperClass();
1055 if (klass->GetDescriptor() == "Ljava/lang/Object;") {
1056 if (super != NULL) {
1057 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1058 return false;
1059 }
1060 // TODO: clear finalize attribute
1061 return true;
1062 }
1063 if (super == NULL) {
1064 LG << "No superclass defined"; // TODO: LinkageError
1065 return false;
1066 }
1067 // Verify
1068 if (super->IsFinal()) {
1069 LG << "Superclass is declared final"; // TODO: IncompatibleClassChangeError
1070 return false;
1071 }
1072 if (super->IsInterface()) {
1073 LG << "Superclass is an interface"; // TODO: IncompatibleClassChangeError
1074 return false;
1075 }
1076 if (!klass->CanAccess(super)) {
1077 LG << "Superclass is inaccessible"; // TODO: IllegalAccessError
1078 return false;
1079 }
1080 return true;
1081}
1082
1083// Populate the class vtable and itable.
1084bool ClassLinker::LinkMethods(Class* klass) {
1085 if (klass->IsInterface()) {
1086 // No vtable.
1087 size_t count = klass->NumVirtualMethods();
1088 if (!IsUint(16, count)) {
1089 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1090 return false;
1091 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001092 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001093 klass->GetVirtualMethod(i)->method_index_ = i;
1094 }
1095 } else {
1096 // Link virtual method tables
1097 LinkVirtualMethods(klass);
1098
1099 // Link interface method tables
1100 LinkInterfaceMethods(klass);
1101
1102 // Insert stubs.
1103 LinkAbstractMethods(klass);
1104 }
1105 return true;
1106}
1107
1108bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001109 if (klass->HasSuperClass()) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001110 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001111 size_t actual_count = klass->GetSuperClass()->vtable_->GetLength();
1112 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001113 // TODO: do not assign to the vtable field until it is fully constructed.
1114 klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001115 // See if any of our virtual methods override the superclass.
1116 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1117 Method* local_method = klass->GetVirtualMethod(i);
1118 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001119 for (; j < actual_count; ++j) {
1120 Method* super_method = klass->vtable_->Get(j);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001121 if (HasSameNameAndPrototype(local_method, super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001122 // Verify
1123 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001124 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001125 return false;
1126 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001127 klass->vtable_->Set(j, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001128 local_method->method_index_ = j;
1129 break;
1130 }
1131 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001132 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001133 // Not overriding, append.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001134 klass->vtable_->Set(actual_count, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001135 local_method->method_index_ = actual_count;
1136 actual_count += 1;
1137 }
1138 }
1139 if (!IsUint(16, actual_count)) {
1140 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1141 return false;
1142 }
1143 CHECK_LE(actual_count, max_count);
1144 if (actual_count < max_count) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001145 // TODO: do not assign to the vtable field until it is fully constructed.
1146 klass->vtable_ = klass->vtable_->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001147 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001148 } else {
1149 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001150 uint32_t num_virtual_methods = klass->NumVirtualMethods();
1151 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
1152 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001153 LG << "Too many methods"; // TODO: VirtualMachineError
1154 return false;
1155 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001156 // TODO: do not assign to the vtable field until it is fully constructed.
1157 klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods);
1158 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001159 klass->vtable_->Set(i, klass->GetVirtualMethod(i));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001160 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1161 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001162 }
1163 return true;
1164}
1165
1166bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1167 int pool_offset = 0;
1168 int pool_size = 0;
1169 int miranda_count = 0;
1170 int miranda_alloc = 0;
1171 size_t super_ifcount;
1172 if (klass->HasSuperClass()) {
1173 super_ifcount = klass->GetSuperClass()->iftable_count_;
1174 } else {
1175 super_ifcount = 0;
1176 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001177 size_t ifcount = super_ifcount;
1178 ifcount += klass->NumInterfaces();
1179 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1180 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001181 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001182 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001183 DCHECK(klass->iftable_count_ == 0);
1184 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001185 return true;
1186 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001187 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1188 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001189 if (super_ifcount != 0) {
1190 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1191 sizeof(InterfaceEntry) * super_ifcount);
1192 }
1193 // Flatten the interface inheritance hierarchy.
1194 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001195 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1196 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001197 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001198 if (!interf->IsInterface()) {
1199 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1200 return false;
1201 }
1202 klass->iftable_[idx++].SetClass(interf);
1203 for (size_t j = 0; j < interf->iftable_count_; j++) {
1204 klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
1205 }
1206 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001207 CHECK_EQ(idx, ifcount);
1208 klass->iftable_count_ = ifcount;
1209 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001210 return true;
1211 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001212 for (size_t i = super_ifcount; i < ifcount; i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001213 pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
1214 }
1215 if (pool_size == 0) {
1216 return true;
1217 }
1218 klass->ifvi_pool_count_ = pool_size;
1219 klass->ifvi_pool_ = new uint32_t[pool_size];
1220 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001221 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001222 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
1223 Class* interface = klass->iftable_[i].GetClass();
1224 pool_offset += interface->NumVirtualMethods(); // end here
1225 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1226 Method* interface_method = interface->GetVirtualMethod(j);
1227 int k; // must be signed
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001228 for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) {
1229 if (HasSameNameAndPrototype(interface_method, klass->vtable_->Get(k))) {
1230 if (!klass->vtable_->Get(k)->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001231 LG << "Implementation not public";
1232 return false;
1233 }
1234 klass->iftable_[i].method_index_array_[j] = k;
1235 break;
1236 }
1237 }
1238 if (k < 0) {
1239 if (miranda_count == miranda_alloc) {
1240 miranda_alloc += 8;
1241 if (miranda_list.empty()) {
1242 miranda_list.resize(miranda_alloc);
1243 } else {
1244 miranda_list.resize(miranda_alloc);
1245 }
1246 }
1247 int mir;
1248 for (mir = 0; mir < miranda_count; mir++) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001249 if (HasSameNameAndPrototype(miranda_list[mir], interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001250 break;
1251 }
1252 }
1253 // point the interface table at a phantom slot index
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001254 klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001255 if (mir == miranda_count) {
1256 miranda_list[miranda_count++] = interface_method;
1257 }
1258 }
1259 }
1260 }
1261 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001262 int old_method_count = klass->NumVirtualMethods();
1263 int new_method_count = old_method_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001264 klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001265
1266 CHECK(klass->vtable_ != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001267 int old_vtable_count = klass->vtable_->GetLength();
1268 int new_vtable_count = old_vtable_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001269 // TODO: do not assign to the vtable field until it is fully constructed.
1270 klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001271
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001272 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001273 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001274 memcpy(meth, miranda_list[i], sizeof(Method));
1275 meth->klass_ = klass;
1276 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001277 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1278 klass->SetVirtualMethod(old_method_count + i, meth);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001279 klass->vtable_->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001280 }
1281 }
1282 return true;
1283}
1284
1285void ClassLinker::LinkAbstractMethods(Class* klass) {
1286 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1287 Method* method = klass->GetVirtualMethod(i);
1288 if (method->IsAbstract()) {
1289 method->insns_ = reinterpret_cast<uint16_t*>(0xFFFFFFFF); // TODO: AbstractMethodError
1290 }
1291 }
1292}
1293
1294bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001295 int field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001296 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001297 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001298 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001299 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001300 }
1301 // Move references to the front.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001302 klass->num_reference_instance_fields_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001303 size_t i = 0;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001304 for ( ; i < klass->NumInstanceFields(); i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001305 InstanceField* pField = klass->GetInstanceField(i);
1306 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001307 if (c != '[' && c != 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001308 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1309 InstanceField* refField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001310 char rc = refField->GetType();
1311 if (rc == '[' || rc == 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001312 klass->SetInstanceField(i, refField);
1313 klass->SetInstanceField(j, pField);
1314 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001315 c = rc;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001316 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001317 break;
1318 }
1319 }
1320 } else {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001321 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001322 }
1323 if (c != '[' && c != 'L') {
1324 break;
1325 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001326 pField->SetOffset(field_offset);
1327 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001328 }
1329
1330 // Now we want to pack all of the double-wide fields together. If
1331 // we're not aligned, though, we want to shuffle one 32-bit field
1332 // into place. If we can't find one, we'll have to pad it.
Brian Carlstroma0808032011-07-18 00:39:23 -07001333 if (i != klass->NumInstanceFields() && (field_offset & 0x04) != 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001334 InstanceField* pField = klass->GetInstanceField(i);
1335 char c = pField->GetType();
1336
1337 if (c != 'J' && c != 'D') {
1338 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001339 DCHECK(c != '[');
1340 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001341 pField->SetOffset(field_offset);
1342 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001343 i++;
1344 } else {
1345 // Next field is 64-bit, so search for a 32-bit field we can
1346 // swap into it.
1347 bool found = false;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001348 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1349 InstanceField* singleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001350 char rc = singleField->GetType();
1351 if (rc != 'J' && rc != 'D') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001352 klass->SetInstanceField(i, singleField);
1353 klass->SetInstanceField(j, pField);
1354 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001355 pField->SetOffset(field_offset);
1356 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001357 found = true;
1358 i++;
1359 break;
1360 }
1361 }
1362 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001363 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001364 }
1365 }
1366 }
1367
1368 // Alignment is good, shuffle any double-wide fields forward, and
1369 // finish assigning field offsets to all fields.
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001370 DCHECK(i == klass->NumInstanceFields() || (field_offset & 0x04) == 0);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001371 for ( ; i < klass->NumInstanceFields(); i++) {
1372 InstanceField* pField = klass->GetInstanceField(i);
1373 char c = pField->GetType();
1374 if (c != 'D' && c != 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001375 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1376 InstanceField* doubleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001377 char rc = doubleField->GetType();
1378 if (rc == 'D' || rc == 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001379 klass->SetInstanceField(i, doubleField);
1380 klass->SetInstanceField(j, pField);
1381 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001382 c = rc;
1383 break;
1384 }
1385 }
1386 } else {
1387 // This is a double-wide field, leave it be.
1388 }
1389
Brian Carlstroma0808032011-07-18 00:39:23 -07001390 pField->SetOffset(field_offset);
1391 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001392 if (c == 'J' || c == 'D')
Brian Carlstroma0808032011-07-18 00:39:23 -07001393 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001394 }
1395
1396#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001397 // Make sure that all reference fields appear before
1398 // non-reference fields, and all double-wide fields are aligned.
1399 bool seen_non_ref = false;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001400 for (i = 0; i < klass->NumInstanceFields(); i++) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001401 InstanceField *pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001402 char c = pField->GetType();
1403
1404 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001405 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001406 }
1407
1408 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001409 if (!seen_non_ref) {
1410 seen_non_ref = true;
1411 DCHECK_EQ(klass->num_reference_ifields_, i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001412 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001413 } else {
1414 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001415 }
1416 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001417 if (!seen_non_ref) {
1418 DCHECK(klass->NumInstanceFields(), klass->num_reference_ifields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001419 }
1420#endif
1421
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001422 if (klass->object_size_ == 0) {
1423 // avoid overwriting object_size_ of special crafted classes such as java_lang_*_
1424 klass->object_size_ = field_offset;
1425 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001426 return true;
1427}
1428
1429// Set the bitmap of reference offsets, refOffsets, from the ifields
1430// list.
1431void ClassLinker::CreateReferenceOffsets(Class* klass) {
1432 uint32_t reference_offsets = 0;
1433 if (klass->HasSuperClass()) {
1434 reference_offsets = klass->GetSuperClass()->GetReferenceOffsets();
1435 }
1436 // If our superclass overflowed, we don't stand a chance.
1437 if (reference_offsets != CLASS_WALK_SUPER) {
1438 // All of the fields that contain object references are guaranteed
1439 // to be at the beginning of the ifields list.
1440 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) {
1441 // Note that, per the comment on struct InstField, f->byteOffset
1442 // is the offset from the beginning of obj, not the offset into
1443 // obj->instanceData.
1444 const InstanceField* field = klass->GetInstanceField(i);
1445 size_t byte_offset = field->GetOffset();
1446 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001447 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001448 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1449 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001450 CHECK_NE(new_bit, 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001451 reference_offsets |= new_bit;
1452 } else {
1453 reference_offsets = CLASS_WALK_SUPER;
1454 break;
1455 }
1456 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001457 }
Brian Carlstromae3ac012011-07-27 01:30:28 -07001458 klass->SetReferenceOffsets(reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001459}
1460
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001461Class* ClassLinker::ResolveClass(const Class* referrer,
1462 uint32_t class_idx,
Brian Carlstromf615a612011-07-23 12:50:34 -07001463 const DexFile* dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001464 DexCache* dex_cache = referrer->GetDexCache();
1465 Class* resolved = dex_cache->GetResolvedClass(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001466 if (resolved != NULL) {
1467 return resolved;
1468 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001469 const char* descriptor = dex_file->dexStringByTypeIdx(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001470 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001471 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001472 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -07001473 resolved = FindClass(descriptor, referrer->GetClassLoader(), dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001474 }
1475 if (resolved != NULL) {
1476 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001477 if (referrer->GetDexCache() != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001478 if (check->GetClassLoader() != NULL) {
1479 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1480 return NULL;
1481 }
1482 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001483 dex_cache->SetResolvedClass(class_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001484 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001485 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001486 }
1487 return resolved;
1488}
1489
1490Method* ResolveMethod(const Class* referrer, uint32_t method_idx,
1491 /*MethodType*/ int method_type) {
1492 CHECK(false);
1493 return NULL;
1494}
1495
Carl Shapiro69759ea2011-07-21 18:13:35 -07001496String* ClassLinker::ResolveString(const Class* referring,
1497 uint32_t string_idx) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001498 const DexFile* dex_file = FindDexFile(referring->GetDexCache());
1499 const DexFile::StringId& string_id = dex_file->GetStringId(string_idx);
1500 const char* string_data = dex_file->GetStringData(string_id);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001501 String* new_string = String::AllocFromModifiedUtf8(java_lang_String_,
1502 char_array_class_,
1503 string_data);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001504 // TODO: intern the new string
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001505 referring->GetDexCache()->SetResolvedString(string_idx, new_string);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001506 return new_string;
1507}
1508
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001509} // namespace art