blob: 5db2bea32ac192d199355549c97ecb0044e42fb4 [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 Carlstromf615a612011-07-23 12:50:34 -0700370 dst->name_ = dex_file.dexStringById(field_id.name_idx_);
371 dst->signature_ = 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 LG << "shrunk vtable: "
1148 << "was " << max_count << ", "
1149 << "now " << actual_count;
1150 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001151 } else {
1152 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001153 uint32_t num_virtual_methods = klass->NumVirtualMethods();
1154 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
1155 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001156 LG << "Too many methods"; // TODO: VirtualMachineError
1157 return false;
1158 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001159 // TODO: do not assign to the vtable field until it is fully constructed.
1160 klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods);
1161 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001162 klass->vtable_->Set(i, klass->GetVirtualMethod(i));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001163 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1164 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001165 }
1166 return true;
1167}
1168
1169bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1170 int pool_offset = 0;
1171 int pool_size = 0;
1172 int miranda_count = 0;
1173 int miranda_alloc = 0;
1174 size_t super_ifcount;
1175 if (klass->HasSuperClass()) {
1176 super_ifcount = klass->GetSuperClass()->iftable_count_;
1177 } else {
1178 super_ifcount = 0;
1179 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001180 size_t ifcount = super_ifcount;
1181 ifcount += klass->NumInterfaces();
1182 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1183 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001184 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001185 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001186 DCHECK(klass->iftable_count_ == 0);
1187 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001188 return true;
1189 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001190 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1191 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001192 if (super_ifcount != 0) {
1193 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1194 sizeof(InterfaceEntry) * super_ifcount);
1195 }
1196 // Flatten the interface inheritance hierarchy.
1197 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001198 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1199 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001200 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001201 if (!interf->IsInterface()) {
1202 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1203 return false;
1204 }
1205 klass->iftable_[idx++].SetClass(interf);
1206 for (size_t j = 0; j < interf->iftable_count_; j++) {
1207 klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
1208 }
1209 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001210 CHECK_EQ(idx, ifcount);
1211 klass->iftable_count_ = ifcount;
1212 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001213 return true;
1214 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001215 for (size_t i = super_ifcount; i < ifcount; i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001216 pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
1217 }
1218 if (pool_size == 0) {
1219 return true;
1220 }
1221 klass->ifvi_pool_count_ = pool_size;
1222 klass->ifvi_pool_ = new uint32_t[pool_size];
1223 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001224 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001225 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
1226 Class* interface = klass->iftable_[i].GetClass();
1227 pool_offset += interface->NumVirtualMethods(); // end here
1228 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1229 Method* interface_method = interface->GetVirtualMethod(j);
1230 int k; // must be signed
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001231 for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) {
1232 if (HasSameNameAndPrototype(interface_method, klass->vtable_->Get(k))) {
1233 if (!klass->vtable_->Get(k)->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001234 LG << "Implementation not public";
1235 return false;
1236 }
1237 klass->iftable_[i].method_index_array_[j] = k;
1238 break;
1239 }
1240 }
1241 if (k < 0) {
1242 if (miranda_count == miranda_alloc) {
1243 miranda_alloc += 8;
1244 if (miranda_list.empty()) {
1245 miranda_list.resize(miranda_alloc);
1246 } else {
1247 miranda_list.resize(miranda_alloc);
1248 }
1249 }
1250 int mir;
1251 for (mir = 0; mir < miranda_count; mir++) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001252 if (HasSameNameAndPrototype(miranda_list[mir], interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001253 break;
1254 }
1255 }
1256 // point the interface table at a phantom slot index
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001257 klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001258 if (mir == miranda_count) {
1259 miranda_list[miranda_count++] = interface_method;
1260 }
1261 }
1262 }
1263 }
1264 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001265 int old_method_count = klass->NumVirtualMethods();
1266 int new_method_count = old_method_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001267 klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001268
1269 CHECK(klass->vtable_ != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001270 int old_vtable_count = klass->vtable_->GetLength();
1271 int new_vtable_count = old_vtable_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001272 // TODO: do not assign to the vtable field until it is fully constructed.
1273 klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001274
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001275 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001276 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001277 memcpy(meth, miranda_list[i], sizeof(Method));
1278 meth->klass_ = klass;
1279 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001280 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1281 klass->SetVirtualMethod(old_method_count + i, meth);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001282 klass->vtable_->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001283 }
1284 }
1285 return true;
1286}
1287
1288void ClassLinker::LinkAbstractMethods(Class* klass) {
1289 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1290 Method* method = klass->GetVirtualMethod(i);
1291 if (method->IsAbstract()) {
1292 method->insns_ = reinterpret_cast<uint16_t*>(0xFFFFFFFF); // TODO: AbstractMethodError
1293 }
1294 }
1295}
1296
1297bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001298 int field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001299 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001300 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001301 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001302 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001303 }
1304 // Move references to the front.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001305 klass->num_reference_instance_fields_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001306 size_t i = 0;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001307 for ( ; i < klass->NumInstanceFields(); i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001308 InstanceField* pField = klass->GetInstanceField(i);
1309 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001310 if (c != '[' && c != 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001311 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1312 InstanceField* refField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001313 char rc = refField->GetType();
1314 if (rc == '[' || rc == 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001315 klass->SetInstanceField(i, refField);
1316 klass->SetInstanceField(j, pField);
1317 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001318 c = rc;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001319 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001320 break;
1321 }
1322 }
1323 } else {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001324 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001325 }
1326 if (c != '[' && c != 'L') {
1327 break;
1328 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001329 pField->SetOffset(field_offset);
1330 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001331 }
1332
1333 // Now we want to pack all of the double-wide fields together. If
1334 // we're not aligned, though, we want to shuffle one 32-bit field
1335 // into place. If we can't find one, we'll have to pad it.
Brian Carlstroma0808032011-07-18 00:39:23 -07001336 if (i != klass->NumInstanceFields() && (field_offset & 0x04) != 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001337 InstanceField* pField = klass->GetInstanceField(i);
1338 char c = pField->GetType();
1339
1340 if (c != 'J' && c != 'D') {
1341 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001342 DCHECK(c != '[');
1343 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001344 pField->SetOffset(field_offset);
1345 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001346 i++;
1347 } else {
1348 // Next field is 64-bit, so search for a 32-bit field we can
1349 // swap into it.
1350 bool found = false;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001351 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1352 InstanceField* singleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001353 char rc = singleField->GetType();
1354 if (rc != 'J' && rc != 'D') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001355 klass->SetInstanceField(i, singleField);
1356 klass->SetInstanceField(j, pField);
1357 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001358 pField->SetOffset(field_offset);
1359 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001360 found = true;
1361 i++;
1362 break;
1363 }
1364 }
1365 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001366 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001367 }
1368 }
1369 }
1370
1371 // Alignment is good, shuffle any double-wide fields forward, and
1372 // finish assigning field offsets to all fields.
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001373 DCHECK(i == klass->NumInstanceFields() || (field_offset & 0x04) == 0);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001374 for ( ; i < klass->NumInstanceFields(); i++) {
1375 InstanceField* pField = klass->GetInstanceField(i);
1376 char c = pField->GetType();
1377 if (c != 'D' && c != 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001378 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1379 InstanceField* doubleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001380 char rc = doubleField->GetType();
1381 if (rc == 'D' || rc == 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001382 klass->SetInstanceField(i, doubleField);
1383 klass->SetInstanceField(j, pField);
1384 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001385 c = rc;
1386 break;
1387 }
1388 }
1389 } else {
1390 // This is a double-wide field, leave it be.
1391 }
1392
Brian Carlstroma0808032011-07-18 00:39:23 -07001393 pField->SetOffset(field_offset);
1394 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001395 if (c == 'J' || c == 'D')
Brian Carlstroma0808032011-07-18 00:39:23 -07001396 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001397 }
1398
1399#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001400 // Make sure that all reference fields appear before
1401 // non-reference fields, and all double-wide fields are aligned.
1402 bool seen_non_ref = false;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001403 for (i = 0; i < klass->NumInstanceFields(); i++) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001404 InstanceField *pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001405 char c = pField->GetType();
1406
1407 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001408 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001409 }
1410
1411 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001412 if (!seen_non_ref) {
1413 seen_non_ref = true;
1414 DCHECK_EQ(klass->num_reference_ifields_, i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001415 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001416 } else {
1417 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001418 }
1419 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001420 if (!seen_non_ref) {
1421 DCHECK(klass->NumInstanceFields(), klass->num_reference_ifields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001422 }
1423#endif
1424
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001425 if (klass->object_size_ == 0) {
1426 // avoid overwriting object_size_ of special crafted classes such as java_lang_*_
1427 klass->object_size_ = field_offset;
1428 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001429 return true;
1430}
1431
1432// Set the bitmap of reference offsets, refOffsets, from the ifields
1433// list.
1434void ClassLinker::CreateReferenceOffsets(Class* klass) {
1435 uint32_t reference_offsets = 0;
1436 if (klass->HasSuperClass()) {
1437 reference_offsets = klass->GetSuperClass()->GetReferenceOffsets();
1438 }
1439 // If our superclass overflowed, we don't stand a chance.
1440 if (reference_offsets != CLASS_WALK_SUPER) {
1441 // All of the fields that contain object references are guaranteed
1442 // to be at the beginning of the ifields list.
1443 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) {
1444 // Note that, per the comment on struct InstField, f->byteOffset
1445 // is the offset from the beginning of obj, not the offset into
1446 // obj->instanceData.
1447 const InstanceField* field = klass->GetInstanceField(i);
1448 size_t byte_offset = field->GetOffset();
1449 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001450 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001451 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1452 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001453 CHECK_NE(new_bit, 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001454 reference_offsets |= new_bit;
1455 } else {
1456 reference_offsets = CLASS_WALK_SUPER;
1457 break;
1458 }
1459 }
1460 klass->SetReferenceOffsets(reference_offsets);
1461 }
1462}
1463
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001464Class* ClassLinker::ResolveClass(const Class* referrer,
1465 uint32_t class_idx,
Brian Carlstromf615a612011-07-23 12:50:34 -07001466 const DexFile* dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001467 DexCache* dex_cache = referrer->GetDexCache();
1468 Class* resolved = dex_cache->GetResolvedClass(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001469 if (resolved != NULL) {
1470 return resolved;
1471 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001472 const char* descriptor = dex_file->dexStringByTypeIdx(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001473 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001474 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001475 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -07001476 resolved = FindClass(descriptor, referrer->GetClassLoader(), dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001477 }
1478 if (resolved != NULL) {
1479 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001480 if (referrer->GetDexCache() != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001481 if (check->GetClassLoader() != NULL) {
1482 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1483 return NULL;
1484 }
1485 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001486 dex_cache->SetResolvedClass(class_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001487 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001488 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001489 }
1490 return resolved;
1491}
1492
1493Method* ResolveMethod(const Class* referrer, uint32_t method_idx,
1494 /*MethodType*/ int method_type) {
1495 CHECK(false);
1496 return NULL;
1497}
1498
Carl Shapiro69759ea2011-07-21 18:13:35 -07001499String* ClassLinker::ResolveString(const Class* referring,
1500 uint32_t string_idx) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001501 const DexFile* dex_file = FindDexFile(referring->GetDexCache());
1502 const DexFile::StringId& string_id = dex_file->GetStringId(string_idx);
1503 const char* string_data = dex_file->GetStringData(string_id);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001504 String* new_string = String::AllocFromModifiedUtf8(java_lang_String_,
1505 char_array_class_,
1506 string_data);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001507 // TODO: intern the new string
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001508 referring->GetDexCache()->SetResolvedString(string_idx, new_string);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001509 return new_string;
1510}
1511
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001512} // namespace art