blob: 9a1db39ee324a0a3eeb4d4d8165dddfda691c486 [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 Carlstrom0b138b22011-07-27 15:19:17 -0700140String* ClassLinker::AllocStringFromModifiedUtf8(int32_t utf16_length,
141 const char* utf8_data_in) {
142 return String::AllocFromModifiedUtf8(java_lang_String_,
143 char_array_class_,
144 utf16_length,
145 utf8_data_in);
146}
147
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700148Class* ClassLinker::FindClass(const StringPiece& descriptor,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700149 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -0700150 const DexFile* dex_file) {
Carl Shapirob5573532011-07-12 18:22:59 -0700151 Thread* self = Thread::Current();
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700152 DCHECK(self != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700153 CHECK(!self->IsExceptionPending());
154 // Find the class in the loaded classes table.
155 Class* klass = LookupClass(descriptor, class_loader);
156 if (klass == NULL) {
157 // Class is not yet loaded.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700158 if (descriptor[0] == '[') {
Brian Carlstromf615a612011-07-23 12:50:34 -0700159 return CreateArrayClass(descriptor, class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700160 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700161 ClassPathEntry pair;
Brian Carlstromf615a612011-07-23 12:50:34 -0700162 if (dex_file == NULL) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700163 pair = FindInBootClassPath(descriptor);
164 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -0700165 pair.first = dex_file;
166 pair.second = dex_file->FindClassDef(descriptor);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700167 }
168 if (pair.second == NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700169 LG << "Class " << descriptor << " not found"; // TODO: NoClassDefFoundError
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700170 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700171 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700172 const DexFile* dex_file = pair.first;
173 const DexFile::ClassDef* dex_class_def = pair.second;
174 DexCache* dex_cache = FindDexCache(dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700175 // Load the class from the dex file.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700176 // TODO add fast path to avoid all these comparisons once special cases are found
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700177 if (descriptor == "Ljava/lang/Object;") {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700178 klass = java_lang_Object_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700179 klass->dex_cache_ = dex_cache;
Brian Carlstroma0808032011-07-18 00:39:23 -0700180 klass->object_size_ = sizeof(Object);
Brian Carlstromf615a612011-07-23 12:50:34 -0700181 char_array_class_->super_class_idx_ = dex_class_def->class_idx_;
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700182 } else if (descriptor == "Ljava/lang/Class;") {
Carl Shapiro565f5072011-07-10 13:39:43 -0700183 klass = java_lang_Class_;
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(Class);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700186 } else if (descriptor == "Ljava/lang/reflect/Field;") {
187 klass = java_lang_reflect_Field_;
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(Field);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700190 } else if (descriptor == "Ljava/lang/reflect/Method;") {
191 klass = java_lang_reflect_Method_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700192 klass->dex_cache_ = dex_cache;
Brian Carlstroma0808032011-07-18 00:39:23 -0700193 klass->object_size_ = sizeof(Method);
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700194 } else if (descriptor == "Ljava/lang/String;") {
Brian Carlstroma0808032011-07-18 00:39:23 -0700195 klass = java_lang_String_;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700196 klass->dex_cache_ = dex_cache;
Brian Carlstroma0808032011-07-18 00:39:23 -0700197 klass->object_size_ = sizeof(String);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700198 } else if (descriptor == "Ljava/lang/Cloneable;") {
199 klass = java_lang_Cloneable_;
200 klass->dex_cache_ = dex_cache;
201 klass->object_size_ = 0;
202 } else if (descriptor == "Ljava/io/Serializable;") {
203 klass = java_io_Serializable_;
204 klass->dex_cache_ = dex_cache;
205 klass->object_size_ = 0;
Carl Shapiro565f5072011-07-10 13:39:43 -0700206 } else {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700207 klass = AllocClass(dex_cache);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700208 // LinkInstanceFields only will update object_size_ if it is 0
209 // to avoid overwriting the special initialized cases above.
210 klass->object_size_ = 0;
Carl Shapiro565f5072011-07-10 13:39:43 -0700211 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700212 LoadClass(*dex_file, *dex_class_def, klass);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700213 // Check for a pending exception during load
214 if (self->IsExceptionPending()) {
215 // TODO: free native allocations in klass
216 return NULL;
217 }
218 {
219 ObjectLock lock(klass);
Carl Shapirob5573532011-07-12 18:22:59 -0700220 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700221 // Add the newly loaded class to the loaded classes table.
222 bool success = InsertClass(klass);
223 if (!success) {
224 // We may fail to insert if we raced with another thread.
225 klass->clinit_thread_id_ = 0;
226 // TODO: free native allocations in klass
227 klass = LookupClass(descriptor, class_loader);
228 CHECK(klass != NULL);
229 } else {
230 // Link the class.
Brian Carlstromf615a612011-07-23 12:50:34 -0700231 if (!LinkClass(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700232 // Linking failed.
233 // TODO: CHECK(self->IsExceptionPending());
234 lock.NotifyAll();
235 return NULL;
236 }
237 }
238 }
239 }
240 // Link the class if it has not already been linked.
241 if (!klass->IsLinked() && !klass->IsErroneous()) {
242 ObjectLock lock(klass);
243 // Check for circular dependencies between classes.
Carl Shapirob5573532011-07-12 18:22:59 -0700244 if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700245 LG << "Recursive link"; // TODO: ClassCircularityError
246 return NULL;
247 }
248 // Wait for the pending initialization to complete.
249 while (!klass->IsLinked() && !klass->IsErroneous()) {
250 lock.Wait();
251 }
252 }
253 if (klass->IsErroneous()) {
254 LG << "EarlierClassFailure"; // TODO: EarlierClassFailure
255 return NULL;
256 }
257 // Return the loaded class. No exceptions should be pending.
258 CHECK(!self->IsExceptionPending());
259 return klass;
260}
261
Brian Carlstromf615a612011-07-23 12:50:34 -0700262void ClassLinker::LoadClass(const DexFile& dex_file,
263 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700264 Class* klass) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700265 CHECK(klass != NULL);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700266 CHECK(klass->dex_cache_ != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700267 const byte* class_data = dex_file.GetClassData(dex_class_def);
268 DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700269
Brian Carlstromf615a612011-07-23 12:50:34 -0700270 const char* descriptor = dex_file.GetClassDescriptor(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700271 CHECK(descriptor != NULL);
272
273 klass->klass_ = java_lang_Class_;
274 klass->descriptor_.set(descriptor);
275 klass->descriptor_alloc_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700276 klass->access_flags_ = dex_class_def.access_flags_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700277 klass->class_loader_ = NULL; // TODO
278 klass->primitive_type_ = Class::kPrimNot;
279 klass->status_ = Class::kStatusIdx;
280
281 klass->super_class_ = NULL;
Brian Carlstromf615a612011-07-23 12:50:34 -0700282 klass->super_class_idx_ = dex_class_def.superclass_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700283
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700284 size_t num_static_fields = header.static_fields_size_;
285 size_t num_instance_fields = header.instance_fields_size_;
286 size_t num_direct_methods = header.direct_methods_size_;
287 size_t num_virtual_methods = header.virtual_methods_size_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700288
Brian Carlstromf615a612011-07-23 12:50:34 -0700289 klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700290
291 // Load class interfaces.
Brian Carlstromf615a612011-07-23 12:50:34 -0700292 LoadInterfaces(dex_file, dex_class_def, klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700293
294 // Load static fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700295 DCHECK(klass->sfields_ == NULL);
296 if (num_static_fields != 0) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700297 klass->sfields_ = AllocObjectArray<StaticField>(num_static_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700298 uint32_t last_idx = 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700299 for (size_t i = 0; i < klass->NumStaticFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700300 DexFile::Field dex_field;
301 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700302 StaticField* sfield = AllocStaticField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700303 klass->SetStaticField(i, sfield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700304 LoadField(dex_file, dex_field, klass, sfield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700305 }
306 }
307
308 // Load instance fields.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700309 DCHECK(klass->ifields_ == NULL);
310 if (num_instance_fields != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700311 // TODO: allocate on the object heap.
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700312 klass->ifields_ = AllocObjectArray<InstanceField>(num_instance_fields);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700313 uint32_t last_idx = 0;
314 for (size_t i = 0; i < klass->NumInstanceFields(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700315 DexFile::Field dex_field;
316 dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700317 InstanceField* ifield = AllocInstanceField();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700318 klass->SetInstanceField(i, ifield);
Brian Carlstromf615a612011-07-23 12:50:34 -0700319 LoadField(dex_file, dex_field, klass, ifield);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700320 }
321 }
322
323 // Load direct methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700324 DCHECK(klass->direct_methods_ == NULL);
325 if (num_direct_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700326 // TODO: append direct methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700327 klass->direct_methods_ = AllocObjectArray<Method>(num_direct_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700328 uint32_t last_idx = 0;
329 for (size_t i = 0; i < klass->NumDirectMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700330 DexFile::Method dex_method;
331 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700332 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700333 klass->SetDirectMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700334 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700335 // TODO: register maps
336 }
337 }
338
339 // Load virtual methods.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700340 DCHECK(klass->virtual_methods_ == NULL);
341 if (num_virtual_methods != 0) {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700342 // TODO: append virtual methods to class object
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700343 klass->virtual_methods_ = AllocObjectArray<Method>(num_virtual_methods);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700344 uint32_t last_idx = 0;
345 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700346 DexFile::Method dex_method;
347 dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx);
Brian Carlstroma0808032011-07-18 00:39:23 -0700348 Method* meth = AllocMethod();
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700349 klass->SetVirtualMethod(i, meth);
Brian Carlstromf615a612011-07-23 12:50:34 -0700350 LoadMethod(dex_file, dex_method, klass, meth);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700351 // TODO: register maps
352 }
353 }
Brian Carlstrom934486c2011-07-12 23:42:50 -0700354}
355
Brian Carlstromf615a612011-07-23 12:50:34 -0700356void ClassLinker::LoadInterfaces(const DexFile& dex_file,
357 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700358 Class* klass) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700359 const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700360 if (list != NULL) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700361 DCHECK(klass->interfaces_ == NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700362 klass->interfaces_ = AllocObjectArray<Class>(list->Size());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700363 DCHECK(klass->interfaces_idx_ == NULL);
364 klass->interfaces_idx_ = new uint32_t[list->Size()];
Brian Carlstrom934486c2011-07-12 23:42:50 -0700365 for (size_t i = 0; i < list->Size(); ++i) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700366 const DexFile::TypeItem& type_item = list->GetTypeItem(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700367 klass->interfaces_idx_[i] = type_item.type_idx_;
Brian Carlstrom934486c2011-07-12 23:42:50 -0700368 }
369 }
370}
371
Brian Carlstromf615a612011-07-23 12:50:34 -0700372void ClassLinker::LoadField(const DexFile& dex_file,
373 const DexFile::Field& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700374 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700375 Field* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700376 const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700377 dst->klass_ = klass;
Brian Carlstromae3ac012011-07-27 01:30:28 -0700378 dst->name_.set(dex_file.dexStringById(field_id.name_idx_));
379 dst->descriptor_.set(dex_file.dexStringByTypeIdx(field_id.type_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700380 dst->access_flags_ = src.access_flags_;
381}
382
Brian Carlstromf615a612011-07-23 12:50:34 -0700383void ClassLinker::LoadMethod(const DexFile& dex_file,
384 const DexFile::Method& src,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700385 Class* klass,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700386 Method* dst) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700387 const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700388 dst->klass_ = klass;
Brian Carlstromf615a612011-07-23 12:50:34 -0700389 dst->name_.set(dex_file.dexStringById(method_id.name_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700390 dst->proto_idx_ = method_id.proto_idx_;
Brian Carlstromf615a612011-07-23 12:50:34 -0700391 dst->shorty_.set(dex_file.GetShorty(method_id.proto_idx_));
Brian Carlstrom934486c2011-07-12 23:42:50 -0700392 dst->access_flags_ = src.access_flags_;
393
394 // TODO: check for finalize method
395
Brian Carlstromf615a612011-07-23 12:50:34 -0700396 const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700397 if (code_item != NULL) {
398 dst->num_registers_ = code_item->registers_size_;
399 dst->num_ins_ = code_item->ins_size_;
400 dst->num_outs_ = code_item->outs_size_;
401 dst->insns_ = code_item->insns_;
402 } else {
403 uint16_t num_args = dst->NumArgRegisters();
404 if (!dst->IsStatic()) {
405 ++num_args;
406 }
407 dst->num_registers_ = dst->num_ins_ + num_args;
408 // TODO: native methods
409 }
410}
411
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700412ClassLinker::ClassPathEntry ClassLinker::FindInBootClassPath(const StringPiece& descriptor) {
413 for (size_t i = 0; i != boot_class_path_.size(); ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700414 const DexFile* dex_file = boot_class_path_[i];
Brian Carlstromf615a612011-07-23 12:50:34 -0700415 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
416 if (dex_class_def != NULL) {
417 return ClassPathEntry(dex_file, dex_class_def);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700418 }
419 }
Brian Carlstroma0808032011-07-18 00:39:23 -0700420 return ClassPathEntry(NULL, NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700421}
422
Brian Carlstromf615a612011-07-23 12:50:34 -0700423void ClassLinker::AppendToBootClassPath(DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700424 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700425 boot_class_path_.push_back(dex_file);
426 RegisterDexFile(dex_file);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700427}
428
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700429void ClassLinker::RegisterDexFile(const DexFile* dex_file) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700430 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -0700431 dex_files_.push_back(dex_file);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700432 DexCache* dex_cache = AllocDexCache();
433 CHECK(dex_cache != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700434 dex_cache->Init(AllocObjectArray<String>(dex_file->NumStringIds()),
435 AllocObjectArray<Class>(dex_file->NumTypeIds()),
436 AllocObjectArray<Method>(dex_file->NumMethodIds()),
437 AllocObjectArray<Field>(dex_file->NumFieldIds()));
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700438 dex_caches_.push_back(dex_cache);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700439}
440
Brian Carlstromf615a612011-07-23 12:50:34 -0700441const DexFile* ClassLinker::FindDexFile(const DexCache* dex_cache) const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700442 CHECK(dex_cache != NULL);
443 for (size_t i = 0; i != dex_caches_.size(); ++i) {
444 if (dex_caches_[i] == dex_cache) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700445 return dex_files_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700446 }
447 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700448 CHECK(false) << "Could not find DexFile";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700449 return NULL;
450}
451
Brian Carlstromf615a612011-07-23 12:50:34 -0700452DexCache* ClassLinker::FindDexCache(const DexFile* dex_file) const {
453 CHECK(dex_file != NULL);
454 for (size_t i = 0; i != dex_files_.size(); ++i) {
455 if (dex_files_[i] == dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700456 return dex_caches_[i];
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700457 }
458 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700459 CHECK(false) << "Could not find DexCache";
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700460 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700461}
462
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700463Class* ClassLinker::CreatePrimitiveClass(const StringPiece& descriptor) {
Brian Carlstroma0808032011-07-18 00:39:23 -0700464 Class* klass = AllocClass(NULL);
Carl Shapiro565f5072011-07-10 13:39:43 -0700465 CHECK(klass != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700466 klass->super_class_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700467 klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract;
468 klass->descriptor_ = descriptor;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700469 klass->descriptor_alloc_ = NULL;
Carl Shapiro565f5072011-07-10 13:39:43 -0700470 klass->status_ = Class::kStatusInitialized;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700471 bool success = InsertClass(klass);
472 CHECK(success);
Carl Shapiro565f5072011-07-10 13:39:43 -0700473 return klass;
474}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700475
Brian Carlstrombe977852011-07-19 14:54:54 -0700476// Create an array class (i.e. the class object for the array, not the
477// array itself). "descriptor" looks like "[C" or "[[[[B" or
478// "[Ljava/lang/String;".
479//
480// If "descriptor" refers to an array of primitives, look up the
481// primitive type's internally-generated class object.
482//
483// "loader" is the class loader of the class that's referring to us. It's
484// used to ensure that we're looking for the element type in the right
485// context. It does NOT become the class loader for the array class; that
486// always comes from the base element class.
487//
488// Returns NULL with an exception raised on failure.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700489Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor,
490 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -0700491 const DexFile* dex_file)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700492{
493 CHECK(descriptor[0] == '[');
494 DCHECK(java_lang_Class_ != NULL);
495 DCHECK(java_lang_Object_ != NULL);
496
Brian Carlstrombe977852011-07-19 14:54:54 -0700497 // Identify the underlying element class and the array dimension depth.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700498 Class* component_type_ = NULL;
499 int array_rank;
500 if (descriptor[1] == '[') {
Brian Carlstrombe977852011-07-19 14:54:54 -0700501 // array of arrays; keep descriptor and grab stuff from parent
Brian Carlstromf615a612011-07-23 12:50:34 -0700502 Class* outer = FindClass(descriptor.substr(1), class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700503 if (outer != NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700504 // want the base class, not "outer", in our component_type_
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700505 component_type_ = outer->component_type_;
506 array_rank = outer->array_rank_ + 1;
507 } else {
Brian Carlstrombe977852011-07-19 14:54:54 -0700508 DCHECK(component_type_ == NULL); // make sure we fail
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700509 }
510 } else {
511 array_rank = 1;
512 if (descriptor[1] == 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -0700513 // array of objects; strip off "[" and look up descriptor.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700514 const StringPiece subDescriptor = descriptor.substr(1);
Brian Carlstromf615a612011-07-23 12:50:34 -0700515 component_type_ = FindClass(subDescriptor, class_loader, dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700516 } else {
Brian Carlstrombe977852011-07-19 14:54:54 -0700517 // array of a primitive type
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700518 component_type_ = FindPrimitiveClass(descriptor[1]);
519 }
520 }
521
522 if (component_type_ == NULL) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700523 // failed
Brian Carlstrom8ecd08c2011-07-27 17:50:51 -0700524 // DCHECK(Thread::Current()->IsExceptionPending()); // TODO
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700525 return NULL;
526 }
527
Brian Carlstrombe977852011-07-19 14:54:54 -0700528 // See if the component type is already loaded. Array classes are
529 // always associated with the class loader of their underlying
530 // element type -- an array of Strings goes with the loader for
531 // java/lang/String -- so we need to look for it there. (The
532 // caller should have checked for the existence of the class
533 // before calling here, but they did so with *their* class loader,
534 // not the component type's loader.)
535 //
536 // If we find it, the caller adds "loader" to the class' initiating
537 // loader list, which should prevent us from going through this again.
538 //
539 // This call is unnecessary if "loader" and "component_type_->class_loader_"
540 // are the same, because our caller (FindClass) just did the
541 // lookup. (Even if we get this wrong we still have correct behavior,
542 // because we effectively do this lookup again when we add the new
543 // class to the hash table --- necessary because of possible races with
544 // other threads.)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700545 if (class_loader != component_type_->class_loader_) {
546 Class* new_class = LookupClass(descriptor, component_type_->class_loader_);
547 if (new_class != NULL) {
548 return new_class;
549 }
550 }
551
Brian Carlstrombe977852011-07-19 14:54:54 -0700552 // Fill out the fields in the Class.
553 //
554 // It is possible to execute some methods against arrays, because
555 // all arrays are subclasses of java_lang_Object_, so we need to set
556 // up a vtable. We can just point at the one in java_lang_Object_.
557 //
558 // Array classes are simple enough that we don't need to do a full
559 // link step.
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700560
561 Class* new_class;
562 if (descriptor == "[Ljava/lang/Object;") {
563 CHECK(object_array_class_);
564 new_class = object_array_class_;
565 } else {
566 new_class = AllocClass(NULL);
567 if (new_class == NULL) {
568 return NULL;
569 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700570 }
571 new_class->descriptor_alloc_ = new std::string(descriptor.data(),
572 descriptor.size());
573 new_class->descriptor_.set(new_class->descriptor_alloc_->data(),
574 new_class->descriptor_alloc_->size());
575 new_class->super_class_ = java_lang_Object_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700576 new_class->vtable_ = java_lang_Object_->vtable_;
577 new_class->primitive_type_ = Class::kPrimNot;
578 new_class->component_type_ = component_type_;
579 new_class->class_loader_ = component_type_->class_loader_;
580 new_class->array_rank_ = array_rank;
581 new_class->status_ = Class::kStatusInitialized;
Brian Carlstrombe977852011-07-19 14:54:54 -0700582 // don't need to set new_class->object_size_
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700583
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700584
Brian Carlstrombe977852011-07-19 14:54:54 -0700585 // All arrays have java/lang/Cloneable and java/io/Serializable as
586 // interfaces. We need to set that up here, so that stuff like
587 // "instanceof" works right.
588 //
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700589 // Note: The GC could run during the call to FindSystemClass,
Brian Carlstrombe977852011-07-19 14:54:54 -0700590 // so we need to make sure the class object is GC-valid while we're in
591 // there. Do this by clearing the interface list so the GC will just
592 // think that the entries are null.
Brian Carlstrombe977852011-07-19 14:54:54 -0700593
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700594
595 // Use the single, global copies of "interfaces" and "iftable"
596 // (remember not to free them for arrays).
597 DCHECK(array_interfaces_ != NULL);
598 new_class->interfaces_ = array_interfaces_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700599 new_class->iftable_count_ = 2;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700600 DCHECK(array_iftable_ != NULL);
601 new_class->iftable_ = array_iftable_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700602
Brian Carlstrombe977852011-07-19 14:54:54 -0700603 // Inherit access flags from the component type. Arrays can't be
604 // used as a superclass or interface, so we want to add "final"
605 // and remove "interface".
606 //
607 // Don't inherit any non-standard flags (e.g., kAccFinal)
608 // from component_type_. We assume that the array class does not
609 // override finalize().
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700610 new_class->access_flags_ = ((new_class->component_type_->access_flags_ &
611 ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask;
612
613 if (InsertClass(new_class)) {
614 return new_class;
615 }
Brian Carlstrombe977852011-07-19 14:54:54 -0700616 // Another thread must have loaded the class after we
617 // started but before we finished. Abandon what we've
618 // done.
619 //
620 // (Yes, this happens.)
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700621
Brian Carlstrombe977852011-07-19 14:54:54 -0700622 // Grab the winning class.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700623 Class* other_class = LookupClass(descriptor, component_type_->class_loader_);
624 DCHECK(other_class != NULL);
625 return other_class;
626}
627
628Class* ClassLinker::FindPrimitiveClass(char type) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700629 switch (type) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700630 case 'B':
Carl Shapiro565f5072011-07-10 13:39:43 -0700631 CHECK(primitive_byte_ != NULL);
632 return primitive_byte_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700633 case 'C':
Carl Shapiro565f5072011-07-10 13:39:43 -0700634 CHECK(primitive_char_ != NULL);
635 return primitive_char_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700636 case 'D':
Carl Shapiro565f5072011-07-10 13:39:43 -0700637 CHECK(primitive_double_ != NULL);
638 return primitive_double_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700639 case 'F':
Carl Shapiro565f5072011-07-10 13:39:43 -0700640 CHECK(primitive_float_ != NULL);
641 return primitive_float_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700642 case 'I':
Carl Shapiro565f5072011-07-10 13:39:43 -0700643 CHECK(primitive_int_ != NULL);
644 return primitive_int_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700645 case 'J':
Carl Shapiro565f5072011-07-10 13:39:43 -0700646 CHECK(primitive_long_ != NULL);
647 return primitive_long_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700648 case 'S':
Carl Shapiro565f5072011-07-10 13:39:43 -0700649 CHECK(primitive_short_ != NULL);
650 return primitive_short_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700651 case 'Z':
Carl Shapiro565f5072011-07-10 13:39:43 -0700652 CHECK(primitive_boolean_ != NULL);
653 return primitive_boolean_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700654 case 'V':
Carl Shapiro565f5072011-07-10 13:39:43 -0700655 CHECK(primitive_void_ != NULL);
656 return primitive_void_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700657 case 'L':
658 case '[':
659 LOG(ERROR) << "Not a primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700660 default:
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700661 LOG(ERROR) << "Unknown primitive type " << static_cast<int>(type);
Carl Shapiro565f5072011-07-10 13:39:43 -0700662 };
663 return NULL; // Not reachable.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700664}
665
666bool ClassLinker::InsertClass(Class* klass) {
667 // TODO: acquire classes_lock_
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700668 const StringPiece& key = klass->GetDescriptor();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700669 bool success = classes_.insert(std::make_pair(key, klass)).second;
670 // TODO: release classes_lock_
671 return success;
672}
673
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700674Class* ClassLinker::LookupClass(const StringPiece& descriptor, Object* class_loader) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700675 // TODO: acquire classes_lock_
676 Table::iterator it = classes_.find(descriptor);
677 // TODO: release classes_lock_
678 if (it == classes_.end()) {
679 return NULL;
680 } else {
681 return (*it).second;
682 }
683}
684
685bool ClassLinker::InitializeClass(Class* klass) {
686 CHECK(klass->GetStatus() == Class::kStatusResolved ||
687 klass->GetStatus() == Class::kStatusError);
688
Carl Shapirob5573532011-07-12 18:22:59 -0700689 Thread* self = Thread::Current();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700690
691 {
692 ObjectLock lock(klass);
693
694 if (klass->GetStatus() < Class::kStatusVerified) {
695 if (klass->IsErroneous()) {
696 LG << "re-initializing failed class"; // TODO: throw
697 return false;
698 }
699
700 CHECK(klass->GetStatus() == Class::kStatusResolved);
701
702 klass->status_ = Class::kStatusVerifying;
703 if (!DexVerify::VerifyClass(klass)) {
704 LG << "Verification failed"; // TODO: ThrowVerifyError
705 Object* exception = self->GetException();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700706 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
707 klass->SetFieldObject(field_offset, exception->GetClass());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700708 klass->SetStatus(Class::kStatusError);
709 return false;
710 }
711
712 klass->SetStatus(Class::kStatusVerified);
713 }
714
715 if (klass->status_ == Class::kStatusInitialized) {
716 return true;
717 }
718
719 while (klass->status_ == Class::kStatusInitializing) {
720 // we caught somebody else in the act; was it us?
Carl Shapirob5573532011-07-12 18:22:59 -0700721 if (klass->clinit_thread_id_ == self->GetId()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700722 LG << "recursive <clinit>";
723 return true;
724 }
725
726 CHECK(!self->IsExceptionPending());
727
728 lock.Wait(); // TODO: check for interruption
729
730 // When we wake up, repeat the test for init-in-progress. If
731 // there's an exception pending (only possible if
732 // "interruptShouldThrow" was set), bail out.
733 if (self->IsExceptionPending()) {
734 CHECK(false);
735 LG << "Exception in initialization."; // TODO: ExceptionInInitializerError
736 klass->SetStatus(Class::kStatusError);
737 return false;
738 }
739 if (klass->GetStatus() == Class::kStatusInitializing) {
740 continue;
741 }
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700742 DCHECK(klass->GetStatus() == Class::kStatusInitialized ||
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700743 klass->GetStatus() == Class::kStatusError);
744 if (klass->IsErroneous()) {
Brian Carlstrombe977852011-07-19 14:54:54 -0700745 // The caller wants an exception, but it was thrown in a
746 // different thread. Synthesize one here.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700747 LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError
748 return false;
749 }
750 return true; // otherwise, initialized
751 }
752
753 // see if we failed previously
754 if (klass->IsErroneous()) {
755 // might be wise to unlock before throwing; depends on which class
756 // it is that we have locked
757
758 // TODO: throwEarlierClassFailure(klass);
759 return false;
760 }
761
762 if (!ValidateSuperClassDescriptors(klass)) {
763 klass->SetStatus(Class::kStatusError);
764 return false;
765 }
766
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700767 DCHECK(klass->status_ < Class::kStatusInitializing);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700768
Carl Shapirob5573532011-07-12 18:22:59 -0700769 klass->clinit_thread_id_ = self->GetId();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700770 klass->status_ = Class::kStatusInitializing;
771 }
772
773 if (!InitializeSuperClass(klass)) {
774 return false;
775 }
776
777 InitializeStaticFields(klass);
778
779 Method* clinit = klass->FindDirectMethodLocally("<clinit>", "()V");
780 if (clinit != NULL) {
781 } else {
782 // JValue unused;
783 // TODO: dvmCallMethod(self, method, NULL, &unused);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700784 //CHECK(!"unimplemented");
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700785 }
786
787 {
788 ObjectLock lock(klass);
789
790 if (self->IsExceptionPending()) {
791 klass->SetStatus(Class::kStatusError);
792 } else {
793 klass->SetStatus(Class::kStatusInitialized);
794 }
795 lock.NotifyAll();
796 }
797
798 return true;
799}
800
801bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) {
802 if (klass->IsInterface()) {
803 return true;
804 }
805 // begin with the methods local to the superclass
806 if (klass->HasSuperClass() &&
807 klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) {
808 const Class* super = klass->GetSuperClass();
809 for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) {
810 const Method* method = klass->GetVirtualMethod(i);
811 if (method != super->GetVirtualMethod(i) &&
812 !HasSameMethodDescriptorClasses(method, super, klass)) {
813 LG << "Classes resolve differently in superclass";
814 return false;
815 }
816 }
817 }
818 for (size_t i = 0; i < klass->iftable_count_; ++i) {
819 const InterfaceEntry* iftable = &klass->iftable_[i];
820 Class* interface = iftable->GetClass();
821 if (klass->GetClassLoader() != interface->GetClassLoader()) {
822 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
823 uint32_t vtable_index = iftable->method_index_array_[j];
824 const Method* method = klass->GetVirtualMethod(vtable_index);
825 if (!HasSameMethodDescriptorClasses(method, interface,
826 method->GetClass())) {
827 LG << "Classes resolve differently in interface"; // TODO: LinkageError
828 return false;
829 }
830 }
831 }
832 }
833 return true;
834}
835
836bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700837 const Class* klass1,
838 const Class* klass2) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700839 const DexFile* dex_file = FindDexFile(method->GetClass()->GetDexCache());
840 const DexFile::ProtoId& proto_id = dex_file->GetProtoId(method->proto_idx_);
841 DexFile::ParameterIterator *it;
842 for (it = dex_file->GetParameterIterator(proto_id); it->HasNext(); it->Next()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700843 const char* descriptor = it->GetDescriptor();
844 if (descriptor == NULL) {
845 break;
846 }
847 if (descriptor[0] == 'L' || descriptor[0] == '[') {
848 // Found a non-primitive type.
849 if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) {
850 return false;
851 }
852 }
853 }
854 // Check the return type
Brian Carlstromf615a612011-07-23 12:50:34 -0700855 const char* descriptor = dex_file->GetReturnTypeDescriptor(proto_id);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700856 if (descriptor[0] == 'L' || descriptor[0] == '[') {
857 if (HasSameDescriptorClasses(descriptor, klass1, klass2)) {
858 return false;
859 }
860 }
861 return true;
862}
863
864// Returns true if classes referenced by the descriptor are the
865// same classes in klass1 as they are in klass2.
866bool ClassLinker::HasSameDescriptorClasses(const char* descriptor,
Brian Carlstrom934486c2011-07-12 23:42:50 -0700867 const Class* klass1,
868 const Class* klass2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700869 CHECK(descriptor != NULL);
870 CHECK(klass1 != NULL);
871 CHECK(klass2 != NULL);
872#if 0
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700873 Class* found1 = FindClass(descriptor, klass1->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700874 // TODO: found1 == NULL
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700875 Class* found2 = FindClass(descriptor, klass2->GetClassLoader());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700876 // TODO: found2 == NULL
877 // TODO: lookup found1 in initiating loader list
878 if (found1 == NULL || found2 == NULL) {
Carl Shapirob5573532011-07-12 18:22:59 -0700879 Thread::Current()->ClearException();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700880 if (found1 == found2) {
881 return true;
882 } else {
883 return false;
884 }
885 }
886#endif
887 return true;
888}
889
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700890bool ClassLinker::HasSameArgumentTypes(const Method* m1, const Method* m2) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700891 const DexFile* dex1 = FindDexFile(m1->GetClass()->GetDexCache());
892 const DexFile* dex2 = FindDexFile(m2->GetClass()->GetDexCache());
893 const DexFile::ProtoId& proto1 = dex1->GetProtoId(m1->proto_idx_);
894 const DexFile::ProtoId& proto2 = dex2->GetProtoId(m2->proto_idx_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700895
896 // TODO: compare ProtoId objects for equality and exit early
Brian Carlstromf615a612011-07-23 12:50:34 -0700897 const DexFile::TypeList* type_list1 = dex1->GetProtoParameters(proto1);
898 const DexFile::TypeList* type_list2 = dex2->GetProtoParameters(proto2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700899 size_t arity1 = (type_list1 == NULL) ? 0 : type_list1->Size();
900 size_t arity2 = (type_list2 == NULL) ? 0 : type_list2->Size();
901 if (arity1 != arity2) {
902 return false;
903 }
904
905 for (size_t i = 0; i < arity1; ++i) {
906 uint32_t type_idx1 = type_list1->GetTypeItem(i).type_idx_;
907 uint32_t type_idx2 = type_list2->GetTypeItem(i).type_idx_;
Brian Carlstromf615a612011-07-23 12:50:34 -0700908 const char* type1 = dex1->dexStringByTypeIdx(type_idx1);
909 const char* type2 = dex2->dexStringByTypeIdx(type_idx2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700910 if (strcmp(type1, type2) != 0) {
911 return false;
912 }
913 }
914
915 return true;
916}
917
918bool ClassLinker::HasSameReturnType(const Method* m1, const Method* m2) const {
Brian Carlstromf615a612011-07-23 12:50:34 -0700919 const DexFile* dex1 = FindDexFile(m1->GetClass()->GetDexCache());
920 const DexFile* dex2 = FindDexFile(m2->GetClass()->GetDexCache());
921 const DexFile::ProtoId& proto1 = dex1->GetProtoId(m1->proto_idx_);
922 const DexFile::ProtoId& proto2 = dex2->GetProtoId(m2->proto_idx_);
923 const char* type1 = dex1->dexStringByTypeIdx(proto1.return_type_idx_);
924 const char* type2 = dex2->dexStringByTypeIdx(proto2.return_type_idx_);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700925 return (strcmp(type1, type2) == 0);
926}
927
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700928bool ClassLinker::InitializeSuperClass(Class* klass) {
929 CHECK(klass != NULL);
930 // TODO: assert klass lock is acquired
931 if (!klass->IsInterface() && klass->HasSuperClass()) {
932 Class* super_class = klass->GetSuperClass();
933 if (super_class->GetStatus() != Class::kStatusInitialized) {
934 CHECK(!super_class->IsInterface());
935 klass->MonitorExit();
936 bool super_initialized = InitializeClass(super_class);
937 klass->MonitorEnter();
938 // TODO: check for a pending exception
939 if (!super_initialized) {
940 klass->SetStatus(Class::kStatusError);
941 klass->NotifyAll();
942 return false;
943 }
944 }
945 }
946 return true;
947}
948
949void ClassLinker::InitializeStaticFields(Class* klass) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700950 size_t num_static_fields = klass->NumStaticFields();
951 if (num_static_fields == 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700952 return;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700953 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700954 DexCache* dex_cache = klass->GetDexCache();
955 if (dex_cache == NULL) {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700956 return;
957 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700958 const StringPiece& descriptor = klass->GetDescriptor();
Brian Carlstromf615a612011-07-23 12:50:34 -0700959 const DexFile* dex_file = FindDexFile(dex_cache);
960 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor);
961 CHECK(dex_class_def != NULL);
962 const byte* addr = dex_file->GetEncodedArray(*dex_class_def);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700963 size_t array_size = DecodeUnsignedLeb128(&addr);
964 for (size_t i = 0; i < array_size; ++i) {
965 StaticField* field = klass->GetStaticField(i);
966 JValue value;
Brian Carlstromf615a612011-07-23 12:50:34 -0700967 DexFile::ValueType type = dex_file->ReadEncodedValue(&addr, &value);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700968 switch (type) {
Brian Carlstromf615a612011-07-23 12:50:34 -0700969 case DexFile::kByte:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700970 field->SetByte(value.b);
971 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700972 case DexFile::kShort:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700973 field->SetShort(value.s);
974 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700975 case DexFile::kChar:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700976 field->SetChar(value.c);
977 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700978 case DexFile::kInt:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700979 field->SetInt(value.i);
980 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700981 case DexFile::kLong:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700982 field->SetLong(value.j);
983 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700984 case DexFile::kFloat:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700985 field->SetFloat(value.f);
986 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700987 case DexFile::kDouble:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700988 field->SetDouble(value.d);
989 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700990 case DexFile::kString: {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700991 uint32_t string_idx = value.i;
992 String* resolved = ResolveString(klass, string_idx);
993 field->SetObject(resolved);
994 break;
995 }
Brian Carlstromf615a612011-07-23 12:50:34 -0700996 case DexFile::kBoolean:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700997 field->SetBoolean(value.z);
998 break;
Brian Carlstromf615a612011-07-23 12:50:34 -0700999 case DexFile::kNull:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001000 field->SetObject(value.l);
1001 break;
1002 default:
Carl Shapiro606258b2011-07-09 16:09:09 -07001003 LOG(FATAL) << "Unknown type " << static_cast<int>(type);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001004 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001005 }
1006}
1007
Brian Carlstromf615a612011-07-23 12:50:34 -07001008bool ClassLinker::LinkClass(Class* klass, const DexFile* dex_file) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001009 CHECK(klass->status_ == Class::kStatusIdx ||
1010 klass->status_ == Class::kStatusLoaded);
1011 if (klass->status_ == Class::kStatusIdx) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001012 if (!LinkInterfaces(klass, dex_file)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001013 return false;
1014 }
1015 }
1016 if (!LinkSuperClass(klass)) {
1017 return false;
1018 }
1019 if (!LinkMethods(klass)) {
1020 return false;
1021 }
1022 if (!LinkInstanceFields(klass)) {
1023 return false;
1024 }
1025 CreateReferenceOffsets(klass);
1026 CHECK_EQ(klass->status_, Class::kStatusLoaded);
1027 klass->status_ = Class::kStatusResolved;
1028 return true;
1029}
1030
Brian Carlstromf615a612011-07-23 12:50:34 -07001031bool ClassLinker::LinkInterfaces(Class* klass, const DexFile* dex_file) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001032 // Mark the class as loaded.
1033 klass->status_ = Class::kStatusLoaded;
Brian Carlstromf615a612011-07-23 12:50:34 -07001034 if (klass->super_class_idx_ != DexFile::kDexNoIndex) {
1035 Class* super_class = ResolveClass(klass, klass->super_class_idx_, dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001036 if (super_class == NULL) {
1037 LG << "Failed to resolve superclass";
1038 return false;
1039 }
1040 klass->super_class_ = super_class; // TODO: write barrier
1041 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001042 if (klass->NumInterfaces() > 0) {
1043 for (size_t i = 0; i < klass->NumInterfaces(); ++i) {
1044 uint32_t idx = klass->interfaces_idx_[i];
1045 klass->SetInterface(i, ResolveClass(klass, idx, dex_file));
1046 if (klass->GetInterface(i) == NULL) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001047 LG << "Failed to resolve interface";
1048 return false;
1049 }
1050 // Verify
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001051 if (!klass->CanAccess(klass->GetInterface(i))) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001052 LG << "Inaccessible interface";
1053 return false;
1054 }
1055 }
1056 }
1057 return true;
1058}
1059
1060bool ClassLinker::LinkSuperClass(Class* klass) {
1061 CHECK(!klass->IsPrimitive());
1062 const Class* super = klass->GetSuperClass();
1063 if (klass->GetDescriptor() == "Ljava/lang/Object;") {
1064 if (super != NULL) {
1065 LG << "Superclass must not be defined"; // TODO: ClassFormatError
1066 return false;
1067 }
1068 // TODO: clear finalize attribute
1069 return true;
1070 }
1071 if (super == NULL) {
1072 LG << "No superclass defined"; // TODO: LinkageError
1073 return false;
1074 }
1075 // Verify
1076 if (super->IsFinal()) {
1077 LG << "Superclass is declared final"; // TODO: IncompatibleClassChangeError
1078 return false;
1079 }
1080 if (super->IsInterface()) {
1081 LG << "Superclass is an interface"; // TODO: IncompatibleClassChangeError
1082 return false;
1083 }
1084 if (!klass->CanAccess(super)) {
1085 LG << "Superclass is inaccessible"; // TODO: IllegalAccessError
1086 return false;
1087 }
1088 return true;
1089}
1090
1091// Populate the class vtable and itable.
1092bool ClassLinker::LinkMethods(Class* klass) {
1093 if (klass->IsInterface()) {
1094 // No vtable.
1095 size_t count = klass->NumVirtualMethods();
1096 if (!IsUint(16, count)) {
1097 LG << "Too many methods on interface"; // TODO: VirtualMachineError
1098 return false;
1099 }
Carl Shapiro565f5072011-07-10 13:39:43 -07001100 for (size_t i = 0; i < count; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001101 klass->GetVirtualMethod(i)->method_index_ = i;
1102 }
1103 } else {
1104 // Link virtual method tables
1105 LinkVirtualMethods(klass);
1106
1107 // Link interface method tables
1108 LinkInterfaceMethods(klass);
1109
1110 // Insert stubs.
1111 LinkAbstractMethods(klass);
1112 }
1113 return true;
1114}
1115
1116bool ClassLinker::LinkVirtualMethods(Class* klass) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001117 if (klass->HasSuperClass()) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001118 uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength();
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001119 size_t actual_count = klass->GetSuperClass()->vtable_->GetLength();
1120 CHECK_LE(actual_count, max_count);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001121 // TODO: do not assign to the vtable field until it is fully constructed.
1122 klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001123 // See if any of our virtual methods override the superclass.
1124 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1125 Method* local_method = klass->GetVirtualMethod(i);
1126 size_t j = 0;
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001127 for (; j < actual_count; ++j) {
1128 Method* super_method = klass->vtable_->Get(j);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001129 if (HasSameNameAndPrototype(local_method, super_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001130 // Verify
1131 if (super_method->IsFinal()) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001132 LG << "Method overrides final method"; // TODO: VirtualMachineError
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001133 return false;
1134 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001135 klass->vtable_->Set(j, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001136 local_method->method_index_ = j;
1137 break;
1138 }
1139 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001140 if (j == actual_count) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001141 // Not overriding, append.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001142 klass->vtable_->Set(actual_count, local_method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001143 local_method->method_index_ = actual_count;
1144 actual_count += 1;
1145 }
1146 }
1147 if (!IsUint(16, actual_count)) {
1148 LG << "Too many methods defined on class"; // TODO: VirtualMachineError
1149 return false;
1150 }
1151 CHECK_LE(actual_count, max_count);
1152 if (actual_count < max_count) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001153 // TODO: do not assign to the vtable field until it is fully constructed.
1154 klass->vtable_ = klass->vtable_->CopyOf(actual_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001155 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001156 } else {
1157 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001158 uint32_t num_virtual_methods = klass->NumVirtualMethods();
1159 CHECK(klass->GetDescriptor() == "Ljava/lang/Object;");
1160 if (!IsUint(16, num_virtual_methods)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001161 LG << "Too many methods"; // TODO: VirtualMachineError
1162 return false;
1163 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001164 // TODO: do not assign to the vtable field until it is fully constructed.
1165 klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods);
1166 for (size_t i = 0; i < num_virtual_methods; ++i) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001167 klass->vtable_->Set(i, klass->GetVirtualMethod(i));
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001168 klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF;
1169 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001170 }
1171 return true;
1172}
1173
1174bool ClassLinker::LinkInterfaceMethods(Class* klass) {
1175 int pool_offset = 0;
1176 int pool_size = 0;
1177 int miranda_count = 0;
1178 int miranda_alloc = 0;
1179 size_t super_ifcount;
1180 if (klass->HasSuperClass()) {
1181 super_ifcount = klass->GetSuperClass()->iftable_count_;
1182 } else {
1183 super_ifcount = 0;
1184 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001185 size_t ifcount = super_ifcount;
1186 ifcount += klass->NumInterfaces();
1187 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1188 ifcount += klass->GetInterface(i)->iftable_count_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001189 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001190 if (ifcount == 0) {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001191 DCHECK(klass->iftable_count_ == 0);
1192 DCHECK(klass->iftable_ == NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001193 return true;
1194 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001195 klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)];
1196 memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001197 if (super_ifcount != 0) {
1198 memcpy(klass->iftable_, klass->GetSuperClass()->iftable_,
1199 sizeof(InterfaceEntry) * super_ifcount);
1200 }
1201 // Flatten the interface inheritance hierarchy.
1202 size_t idx = super_ifcount;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001203 for (size_t i = 0; i < klass->NumInterfaces(); i++) {
1204 Class* interf = klass->GetInterface(i);
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001205 DCHECK(interf != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001206 if (!interf->IsInterface()) {
1207 LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError
1208 return false;
1209 }
1210 klass->iftable_[idx++].SetClass(interf);
1211 for (size_t j = 0; j < interf->iftable_count_; j++) {
1212 klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass());
1213 }
1214 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001215 CHECK_EQ(idx, ifcount);
1216 klass->iftable_count_ = ifcount;
1217 if (klass->IsInterface() || super_ifcount == ifcount) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001218 return true;
1219 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001220 for (size_t i = super_ifcount; i < ifcount; i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001221 pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods();
1222 }
1223 if (pool_size == 0) {
1224 return true;
1225 }
1226 klass->ifvi_pool_count_ = pool_size;
1227 klass->ifvi_pool_ = new uint32_t[pool_size];
1228 std::vector<Method*> miranda_list;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001229 for (size_t i = super_ifcount; i < ifcount; ++i) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001230 klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset;
1231 Class* interface = klass->iftable_[i].GetClass();
1232 pool_offset += interface->NumVirtualMethods(); // end here
1233 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
1234 Method* interface_method = interface->GetVirtualMethod(j);
1235 int k; // must be signed
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001236 for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) {
1237 if (HasSameNameAndPrototype(interface_method, klass->vtable_->Get(k))) {
1238 if (!klass->vtable_->Get(k)->IsPublic()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001239 LG << "Implementation not public";
1240 return false;
1241 }
1242 klass->iftable_[i].method_index_array_[j] = k;
1243 break;
1244 }
1245 }
1246 if (k < 0) {
1247 if (miranda_count == miranda_alloc) {
1248 miranda_alloc += 8;
1249 if (miranda_list.empty()) {
1250 miranda_list.resize(miranda_alloc);
1251 } else {
1252 miranda_list.resize(miranda_alloc);
1253 }
1254 }
1255 int mir;
1256 for (mir = 0; mir < miranda_count; mir++) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001257 if (HasSameNameAndPrototype(miranda_list[mir], interface_method)) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001258 break;
1259 }
1260 }
1261 // point the interface table at a phantom slot index
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001262 klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001263 if (mir == miranda_count) {
1264 miranda_list[miranda_count++] = interface_method;
1265 }
1266 }
1267 }
1268 }
1269 if (miranda_count != 0) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001270 int old_method_count = klass->NumVirtualMethods();
1271 int new_method_count = old_method_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001272 klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001273
1274 CHECK(klass->vtable_ != NULL);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001275 int old_vtable_count = klass->vtable_->GetLength();
1276 int new_vtable_count = old_vtable_count + miranda_count;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001277 // TODO: do not assign to the vtable field until it is fully constructed.
1278 klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001279
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001280 for (int i = 0; i < miranda_count; i++) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001281 Method* meth = AllocMethod();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001282 memcpy(meth, miranda_list[i], sizeof(Method));
1283 meth->klass_ = klass;
1284 meth->access_flags_ |= kAccMiranda;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001285 meth->method_index_ = 0xFFFF & (old_vtable_count + i);
1286 klass->SetVirtualMethod(old_method_count + i, meth);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001287 klass->vtable_->Set(old_vtable_count + i, meth);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001288 }
1289 }
1290 return true;
1291}
1292
1293void ClassLinker::LinkAbstractMethods(Class* klass) {
1294 for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) {
1295 Method* method = klass->GetVirtualMethod(i);
1296 if (method->IsAbstract()) {
1297 method->insns_ = reinterpret_cast<uint16_t*>(0xFFFFFFFF); // TODO: AbstractMethodError
1298 }
1299 }
1300}
1301
1302bool ClassLinker::LinkInstanceFields(Class* klass) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001303 int field_offset;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001304 if (klass->GetSuperClass() != NULL) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001305 field_offset = klass->GetSuperClass()->object_size_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001306 } else {
Brian Carlstroma0808032011-07-18 00:39:23 -07001307 field_offset = OFFSETOF_MEMBER(DataObject, fields_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001308 }
1309 // Move references to the front.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001310 klass->num_reference_instance_fields_ = 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001311 size_t i = 0;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001312 for ( ; i < klass->NumInstanceFields(); i++) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001313 InstanceField* pField = klass->GetInstanceField(i);
1314 char c = pField->GetType();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001315 if (c != '[' && c != 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001316 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1317 InstanceField* refField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001318 char rc = refField->GetType();
1319 if (rc == '[' || rc == 'L') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001320 klass->SetInstanceField(i, refField);
1321 klass->SetInstanceField(j, pField);
1322 pField = refField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001323 c = rc;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001324 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001325 break;
1326 }
1327 }
1328 } else {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001329 klass->num_reference_instance_fields_++;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001330 }
1331 if (c != '[' && c != 'L') {
1332 break;
1333 }
Brian Carlstroma0808032011-07-18 00:39:23 -07001334 pField->SetOffset(field_offset);
1335 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001336 }
1337
1338 // Now we want to pack all of the double-wide fields together. If
1339 // we're not aligned, though, we want to shuffle one 32-bit field
1340 // into place. If we can't find one, we'll have to pad it.
Brian Carlstroma0808032011-07-18 00:39:23 -07001341 if (i != klass->NumInstanceFields() && (field_offset & 0x04) != 0) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001342 InstanceField* pField = klass->GetInstanceField(i);
1343 char c = pField->GetType();
1344
1345 if (c != 'J' && c != 'D') {
1346 // The field that comes next is 32-bit, so just advance past it.
Brian Carlstrombe977852011-07-19 14:54:54 -07001347 DCHECK(c != '[');
1348 DCHECK(c != 'L');
Brian Carlstroma0808032011-07-18 00:39:23 -07001349 pField->SetOffset(field_offset);
1350 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001351 i++;
1352 } else {
1353 // Next field is 64-bit, so search for a 32-bit field we can
1354 // swap into it.
1355 bool found = false;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001356 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1357 InstanceField* singleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001358 char rc = singleField->GetType();
1359 if (rc != 'J' && rc != 'D') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001360 klass->SetInstanceField(i, singleField);
1361 klass->SetInstanceField(j, pField);
1362 pField = singleField;
Brian Carlstroma0808032011-07-18 00:39:23 -07001363 pField->SetOffset(field_offset);
1364 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001365 found = true;
1366 i++;
1367 break;
1368 }
1369 }
1370 if (!found) {
Brian Carlstroma0808032011-07-18 00:39:23 -07001371 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001372 }
1373 }
1374 }
1375
1376 // Alignment is good, shuffle any double-wide fields forward, and
1377 // finish assigning field offsets to all fields.
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001378 DCHECK(i == klass->NumInstanceFields() || (field_offset & 0x04) == 0);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001379 for ( ; i < klass->NumInstanceFields(); i++) {
1380 InstanceField* pField = klass->GetInstanceField(i);
1381 char c = pField->GetType();
1382 if (c != 'D' && c != 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001383 for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) {
1384 InstanceField* doubleField = klass->GetInstanceField(j);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001385 char rc = doubleField->GetType();
1386 if (rc == 'D' || rc == 'J') {
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001387 klass->SetInstanceField(i, doubleField);
1388 klass->SetInstanceField(j, pField);
1389 pField = doubleField;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001390 c = rc;
1391 break;
1392 }
1393 }
1394 } else {
1395 // This is a double-wide field, leave it be.
1396 }
1397
Brian Carlstroma0808032011-07-18 00:39:23 -07001398 pField->SetOffset(field_offset);
1399 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001400 if (c == 'J' || c == 'D')
Brian Carlstroma0808032011-07-18 00:39:23 -07001401 field_offset += sizeof(uint32_t);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001402 }
1403
1404#ifndef NDEBUG
Brian Carlstrombe977852011-07-19 14:54:54 -07001405 // Make sure that all reference fields appear before
1406 // non-reference fields, and all double-wide fields are aligned.
1407 bool seen_non_ref = false;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001408 for (i = 0; i < klass->NumInstanceFields(); i++) {
Brian Carlstrombe977852011-07-19 14:54:54 -07001409 InstanceField *pField = klass->GetInstanceField(i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001410 char c = pField->GetType();
1411
1412 if (c == 'D' || c == 'J') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001413 DCHECK_EQ(0U, pField->GetOffset() & 0x07);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001414 }
1415
1416 if (c != '[' && c != 'L') {
Brian Carlstrombe977852011-07-19 14:54:54 -07001417 if (!seen_non_ref) {
1418 seen_non_ref = true;
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001419 DCHECK_EQ(klass->NumReferenceInstanceFields(), i);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001420 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001421 } else {
1422 DCHECK(!seen_non_ref);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001423 }
1424 }
Brian Carlstrombe977852011-07-19 14:54:54 -07001425 if (!seen_non_ref) {
Brian Carlstrom07d579f2011-07-27 13:31:51 -07001426 DCHECK_EQ(klass->NumInstanceFields(), klass->NumReferenceInstanceFields());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001427 }
1428#endif
1429
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001430 if (klass->object_size_ == 0) {
1431 // avoid overwriting object_size_ of special crafted classes such as java_lang_*_
1432 klass->object_size_ = field_offset;
1433 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001434 return true;
1435}
1436
1437// Set the bitmap of reference offsets, refOffsets, from the ifields
1438// list.
1439void ClassLinker::CreateReferenceOffsets(Class* klass) {
1440 uint32_t reference_offsets = 0;
1441 if (klass->HasSuperClass()) {
1442 reference_offsets = klass->GetSuperClass()->GetReferenceOffsets();
1443 }
1444 // If our superclass overflowed, we don't stand a chance.
1445 if (reference_offsets != CLASS_WALK_SUPER) {
1446 // All of the fields that contain object references are guaranteed
1447 // to be at the beginning of the ifields list.
1448 for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) {
1449 // Note that, per the comment on struct InstField, f->byteOffset
1450 // is the offset from the beginning of obj, not the offset into
1451 // obj->instanceData.
1452 const InstanceField* field = klass->GetInstanceField(i);
1453 size_t byte_offset = field->GetOffset();
1454 CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001455 CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001456 if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) {
1457 uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset);
Elliott Hughes1f359b02011-07-17 14:27:17 -07001458 CHECK_NE(new_bit, 0U);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001459 reference_offsets |= new_bit;
1460 } else {
1461 reference_offsets = CLASS_WALK_SUPER;
1462 break;
1463 }
1464 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001465 }
Brian Carlstromae3ac012011-07-27 01:30:28 -07001466 klass->SetReferenceOffsets(reference_offsets);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001467}
1468
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001469Class* ClassLinker::ResolveClass(const Class* referrer,
1470 uint32_t class_idx,
Brian Carlstromf615a612011-07-23 12:50:34 -07001471 const DexFile* dex_file) {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001472 DexCache* dex_cache = referrer->GetDexCache();
1473 Class* resolved = dex_cache->GetResolvedClass(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001474 if (resolved != NULL) {
1475 return resolved;
1476 }
Brian Carlstromf615a612011-07-23 12:50:34 -07001477 const char* descriptor = dex_file->dexStringByTypeIdx(class_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001478 if (descriptor[0] != '\0' && descriptor[1] == '\0') {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001479 resolved = FindPrimitiveClass(descriptor[0]);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001480 } else {
Brian Carlstromf615a612011-07-23 12:50:34 -07001481 resolved = FindClass(descriptor, referrer->GetClassLoader(), dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001482 }
1483 if (resolved != NULL) {
1484 Class* check = resolved->IsArray() ? resolved->component_type_ : resolved;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001485 if (referrer->GetDexCache() != check->GetDexCache()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001486 if (check->GetClassLoader() != NULL) {
1487 LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError
1488 return NULL;
1489 }
1490 }
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001491 dex_cache->SetResolvedClass(class_idx, resolved);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001492 } else {
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001493 DCHECK(Thread::Current()->IsExceptionPending());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001494 }
1495 return resolved;
1496}
1497
1498Method* ResolveMethod(const Class* referrer, uint32_t method_idx,
1499 /*MethodType*/ int method_type) {
1500 CHECK(false);
1501 return NULL;
1502}
1503
Carl Shapiro69759ea2011-07-21 18:13:35 -07001504String* ClassLinker::ResolveString(const Class* referring,
1505 uint32_t string_idx) {
Brian Carlstromf615a612011-07-23 12:50:34 -07001506 const DexFile* dex_file = FindDexFile(referring->GetDexCache());
1507 const DexFile::StringId& string_id = dex_file->GetStringId(string_idx);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001508 int32_t utf16_length = dex_file->GetStringLength(string_id);
1509 const char* utf8_data = dex_file->GetStringData(string_id);
1510 String* new_string = AllocStringFromModifiedUtf8(utf16_length, utf8_data);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001511 // TODO: intern the new string
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001512 referring->GetDexCache()->SetResolvedString(string_idx, new_string);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001513 return new_string;
1514}
1515
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001516} // namespace art