Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | // Author: cshapiro@google.com (Carl Shapiro) |
| 3 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 4 | #include "class_linker.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5 | |
| 6 | #include <vector> |
| 7 | #include <utility> |
| 8 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 9 | #include "casts.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 10 | #include "dex_cache.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 11 | #include "dex_verifier.h" |
| 12 | #include "heap.h" |
| 13 | #include "logging.h" |
| 14 | #include "monitor.h" |
| 15 | #include "object.h" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 16 | #include "dex_file.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "scoped_ptr.h" |
| 18 | #include "thread.h" |
| 19 | #include "utils.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 20 | |
| 21 | namespace art { |
| 22 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 23 | ClassLinker* ClassLinker::Create(const std::vector<DexFile*>& boot_class_path) { |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 24 | scoped_ptr<ClassLinker> class_linker(new ClassLinker); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 25 | class_linker->Init(boot_class_path); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 26 | // TODO: check for failure during initialization |
| 27 | return class_linker.release(); |
| 28 | } |
| 29 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 30 | void ClassLinker::Init(const std::vector<DexFile*>& boot_class_path) { |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 31 | init_done_ = false; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 32 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 33 | // java_lang_Class comes first, its needed for AllocClass |
| 34 | Class* java_lang_Class = down_cast<Class*>(Heap::AllocObject(NULL, sizeof(Class))); |
| 35 | CHECK(java_lang_Class != NULL); |
| 36 | java_lang_Class->descriptor_ = "Ljava/lang/Class;"; |
| 37 | java_lang_Class->object_size_ = sizeof(Class); |
| 38 | java_lang_Class->klass_ = java_lang_Class; |
| 39 | // AllocClass(Class*) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 40 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 41 | // java_lang_Object comes next so that object_array_class can be created |
| 42 | Class* java_lang_Object = AllocClass(java_lang_Class); |
| 43 | CHECK(java_lang_Object != NULL); |
| 44 | java_lang_Object->descriptor_ = "Ljava/lang/Object;"; |
| 45 | // backfill Object as the super class of Class |
| 46 | java_lang_Class->super_class_ = java_lang_Object; |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 47 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 48 | // object_array_class is for root_classes to provide the storage for these classes |
| 49 | Class* object_array_class = AllocClass(java_lang_Class); |
| 50 | CHECK(object_array_class != NULL); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 51 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 52 | // String and char[] are necessary so that FindClass can assign names to members |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 53 | Class* java_lang_String = AllocClass(java_lang_Class); |
| 54 | CHECK(java_lang_String != NULL); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 55 | java_lang_String->descriptor_ = "Ljava/lang/String;"; |
| 56 | CHECK_LT(java_lang_String->object_size_, sizeof(String)); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 57 | java_lang_String->object_size_ = sizeof(String); |
Jesse Wilson | 8989d99 | 2011-08-02 13:39:42 -0700 | [diff] [blame] | 58 | Class* char_array_class = AllocClass(java_lang_Class); |
| 59 | CHECK(char_array_class != NULL); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 60 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 61 | // Field and Method are necessary so that FindClass can link members |
| 62 | Class* java_lang_reflect_Field = AllocClass(java_lang_Class); |
| 63 | CHECK(java_lang_reflect_Field != NULL); |
| 64 | java_lang_reflect_Field->descriptor_ = "Ljava/lang/reflect/Field;"; |
| 65 | CHECK_LT(java_lang_reflect_Field->object_size_, std::max(sizeof(StaticField), sizeof(InstanceField))); |
| 66 | java_lang_reflect_Field->object_size_ = std::max(sizeof(StaticField), sizeof(InstanceField)); |
| 67 | Class* java_lang_reflect_Method = AllocClass(java_lang_Class); |
| 68 | java_lang_reflect_Method->descriptor_ = "Ljava/lang/reflect/Method;"; |
| 69 | CHECK(java_lang_reflect_Method != NULL); |
| 70 | CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method)); |
| 71 | java_lang_reflect_Method->object_size_ = sizeof(Method); |
| 72 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 73 | // create storage for root classes, save away our work so far |
| 74 | class_roots_ = ObjectArray<Class>::Alloc(object_array_class, kClassRootsMax); |
| 75 | class_roots_->Set(kJavaLangClass, java_lang_Class); |
| 76 | class_roots_->Set(kJavaLangObject, java_lang_Object); |
| 77 | class_roots_->Set(kObjectArrayClass, object_array_class); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 78 | class_roots_->Set(kJavaLangString, java_lang_String); |
Jesse Wilson | 8989d99 | 2011-08-02 13:39:42 -0700 | [diff] [blame] | 79 | class_roots_->Set(kCharArrayClass, char_array_class); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 80 | class_roots_->Set(kJavaLangReflectField, java_lang_reflect_Field); |
| 81 | class_roots_->Set(kJavaLangReflectMethod, java_lang_reflect_Method); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 82 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 83 | |
Jesse Wilson | 8989d99 | 2011-08-02 13:39:42 -0700 | [diff] [blame] | 84 | String::InitClasses(java_lang_String, char_array_class); |
| 85 | // Now AllocString* can be used |
| 86 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 87 | // setup boot_class_path_ now that we can use AllocObjectArray to |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 88 | // create DexCache instances |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 89 | for (size_t i = 0; i != boot_class_path.size(); ++i) { |
| 90 | AppendToBootClassPath(boot_class_path[i]); |
| 91 | } |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 92 | // now we can use FindSystemClass, at least for non-arrays classes. |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 93 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 94 | // run Class, Field, and Method through FindSystemClass. |
| 95 | // this initializes their dex_cache_ fields and register them in classes_. |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 96 | // we also override their object_size_ values to accommodate the extra C++ fields. |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 97 | Class* Class_class = FindSystemClass(java_lang_Class->GetDescriptor()); |
| 98 | CHECK_EQ(java_lang_Class, Class_class); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 99 | CHECK_LT(java_lang_Class->object_size_, sizeof(Class)); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 100 | java_lang_Class->object_size_ = sizeof(Class); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 101 | Class* Field_class = FindSystemClass(java_lang_reflect_Field->GetDescriptor()); |
| 102 | CHECK_EQ(java_lang_reflect_Field, Field_class); |
| 103 | CHECK_LT(java_lang_reflect_Field->object_size_, std::max(sizeof(StaticField), sizeof(InstanceField))); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 104 | java_lang_reflect_Field->object_size_ = std::max(sizeof(StaticField), sizeof(InstanceField)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 105 | Class* Method_class = FindSystemClass(java_lang_reflect_Method->GetDescriptor()); |
| 106 | CHECK_EQ(java_lang_reflect_Method, Method_class); |
| 107 | CHECK_LT(java_lang_reflect_Method->object_size_, sizeof(Method)); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 108 | java_lang_reflect_Method->object_size_ = sizeof(Method); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 109 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 110 | // Object and String just need more minimal setup, since they do not have extra C++ fields. |
| 111 | Class* Object_class = FindSystemClass(java_lang_Object->GetDescriptor()); |
| 112 | CHECK_EQ(java_lang_Object, Object_class); |
| 113 | CHECK_EQ(java_lang_Object->object_size_, sizeof(Object)); |
| 114 | Class* String_class = FindSystemClass(java_lang_String->GetDescriptor()); |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 115 | CHECK_EQ(java_lang_String, String_class); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 116 | CHECK_EQ(java_lang_String->object_size_, sizeof(String)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 117 | |
| 118 | // Setup the ClassLoaders, adjusting the object_size_ as necessary |
| 119 | Class* java_lang_ClassLoader = FindSystemClass("Ljava/lang/ClassLoader;"); |
| 120 | CHECK(java_lang_ClassLoader != NULL); |
| 121 | CHECK_LT(java_lang_ClassLoader->object_size_, sizeof(ClassLoader)); |
| 122 | java_lang_ClassLoader->object_size_ = sizeof(ClassLoader); |
| 123 | class_roots_->Set(kJavaLangClassLoader, java_lang_ClassLoader); |
| 124 | Class* dalvik_system_BaseDexClassLoader = FindSystemClass("Ldalvik/system/BaseDexClassLoader;"); |
| 125 | CHECK(dalvik_system_BaseDexClassLoader != NULL); |
| 126 | CHECK_EQ(dalvik_system_BaseDexClassLoader->object_size_, sizeof(BaseDexClassLoader)); |
| 127 | class_roots_->Set(kDalvikSystemBaseDexClassLoader, dalvik_system_BaseDexClassLoader); |
| 128 | Class* dalvik_system_PathClassLoader = FindSystemClass("Ldalvik/system/PathClassLoader;"); |
| 129 | CHECK(dalvik_system_PathClassLoader != NULL); |
| 130 | CHECK_EQ(dalvik_system_PathClassLoader->object_size_, sizeof(PathClassLoader)); |
| 131 | class_roots_->Set(kDalvikSystemPathClassLoader, dalvik_system_PathClassLoader); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 132 | |
| 133 | // Setup a single, global copy of "interfaces" and "iftable" for |
| 134 | // reuse across array classes |
| 135 | Class* java_lang_Cloneable = AllocClass(); |
| 136 | CHECK(java_lang_Cloneable != NULL); |
| 137 | java_lang_Cloneable->descriptor_ = "Ljava/lang/Cloneable;"; |
| 138 | |
| 139 | Class* java_io_Serializable = AllocClass(); |
| 140 | CHECK(java_io_Serializable != NULL); |
| 141 | java_io_Serializable->descriptor_ = "Ljava/io/Serializable;"; |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 142 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 143 | array_interfaces_ = AllocObjectArray<Class>(2); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 144 | CHECK(array_interfaces_ != NULL); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 145 | array_interfaces_->Set(0, java_lang_Cloneable); |
| 146 | array_interfaces_->Set(1, java_io_Serializable); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 147 | |
| 148 | // We assume that Cloneable/Serializable don't have superinterfaces -- |
| 149 | // normally we'd have to crawl up and explicitly list all of the |
| 150 | // supers as well. These interfaces don't have any methods, so we |
| 151 | // don't have to worry about the ifviPool either. |
| 152 | array_iftable_ = new InterfaceEntry[2]; |
| 153 | CHECK(array_iftable_ != NULL); |
| 154 | memset(array_iftable_, 0, sizeof(InterfaceEntry) * 2); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 155 | array_iftable_[0].SetClass(array_interfaces_->Get(0)); |
| 156 | array_iftable_[1].SetClass(array_interfaces_->Get(1)); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 157 | // now FindClass can be used for non-primitive array classes |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 158 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 159 | // run Object[] through FindClass to complete initialization |
Jesse Wilson | 8989d99 | 2011-08-02 13:39:42 -0700 | [diff] [blame] | 160 | Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;"); |
| 161 | CHECK_EQ(object_array_class, found_object_array_class); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 162 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 163 | // Setup the primitive type classes. |
| 164 | class_roots_->Set(kPrimitiveByte, CreatePrimitiveClass("B")); |
| 165 | class_roots_->Set(kPrimitiveChar, CreatePrimitiveClass("C")); |
| 166 | class_roots_->Set(kPrimitiveDouble, CreatePrimitiveClass("D")); |
| 167 | class_roots_->Set(kPrimitiveFloat, CreatePrimitiveClass("F")); |
| 168 | class_roots_->Set(kPrimitiveInt, CreatePrimitiveClass("I")); |
| 169 | class_roots_->Set(kPrimitiveLong, CreatePrimitiveClass("J")); |
| 170 | class_roots_->Set(kPrimitiveShort, CreatePrimitiveClass("S")); |
| 171 | class_roots_->Set(kPrimitiveBoolean, CreatePrimitiveClass("Z")); |
| 172 | class_roots_->Set(kPrimitiveVoid, CreatePrimitiveClass("V")); |
| 173 | // now we can use FindSystemClass for anything, including for "[C" |
| 174 | |
Jesse Wilson | 8989d99 | 2011-08-02 13:39:42 -0700 | [diff] [blame] | 175 | // run char[] through FindClass to complete initialization |
| 176 | Class* found_char_array_class = FindSystemClass("[C"); |
| 177 | CHECK_EQ(char_array_class, found_char_array_class); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 178 | |
| 179 | // ensure all class_roots_ were initialized |
| 180 | for (size_t i = 0; i < kClassRootsMax; i++) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 181 | CHECK(GetClassRoot(static_cast<ClassRoot>(i))); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 185 | // that Object, Class, and Object[] are setup |
| 186 | init_done_ = true; |
| 187 | } |
| 188 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame^] | 189 | void ClassLinker::VisitRoots(Heap::RootVistor* root_visitor, void* arg) { |
Brian Carlstrom | b88e944 | 2011-07-28 15:15:51 -0700 | [diff] [blame] | 190 | root_visitor(class_roots_, arg); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 191 | |
| 192 | for (size_t i = 0; i < dex_caches_.size(); i++) { |
Brian Carlstrom | b88e944 | 2011-07-28 15:15:51 -0700 | [diff] [blame] | 193 | root_visitor(dex_caches_[i], arg); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame^] | 196 | { |
| 197 | MutexLock mu(classes_lock_); |
| 198 | typedef Table::const_iterator It; // TODO: C++0x auto |
| 199 | for (It it = classes_.begin(), end = classes_.end(); it != end; ++it) { |
| 200 | root_visitor(it->second, arg); |
| 201 | } |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 202 | } |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame^] | 203 | |
| 204 | intern_table_.VisitRoots(root_visitor, arg); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 205 | |
Brian Carlstrom | b88e944 | 2011-07-28 15:15:51 -0700 | [diff] [blame] | 206 | root_visitor(array_interfaces_, arg); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 209 | DexCache* ClassLinker::AllocDexCache() { |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 210 | return down_cast<DexCache*>(AllocObjectArray<Object>(DexCache::kMax)); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 213 | Class* ClassLinker::AllocClass(Class* java_lang_Class) { |
| 214 | return down_cast<Class*>(Object::Alloc(java_lang_Class)); |
| 215 | } |
| 216 | |
| 217 | Class* ClassLinker::AllocClass() { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 218 | return AllocClass(GetClassRoot(kJavaLangClass)); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | StaticField* ClassLinker::AllocStaticField() { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 222 | return down_cast<StaticField*>(Object::Alloc(GetClassRoot(kJavaLangReflectField))); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | InstanceField* ClassLinker::AllocInstanceField() { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 226 | return down_cast<InstanceField*>(Object::Alloc(GetClassRoot(kJavaLangReflectField))); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | Method* ClassLinker::AllocMethod() { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 230 | return down_cast<Method*>(Object::Alloc(GetClassRoot(kJavaLangReflectMethod))); |
| 231 | } |
| 232 | |
| 233 | // TODO remove once we can use java.lang.Class.getSystemClassLoader |
| 234 | PathClassLoader* ClassLinker::AllocPathClassLoader(std::vector<const DexFile*> dex_files) { |
| 235 | PathClassLoader* cl = down_cast<PathClassLoader*>(Object::Alloc(GetClassRoot(kDalvikSystemPathClassLoader))); |
| 236 | cl->SetClassPath(dex_files); |
| 237 | return cl; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 240 | Class* ClassLinker::FindClass(const StringPiece& descriptor, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 241 | ClassLoader* class_loader) { |
| 242 | // TODO remove this contrived parent class loader check when we have a real ClassLoader. |
| 243 | if (class_loader != NULL) { |
| 244 | Class* klass = FindClass(descriptor, NULL); |
| 245 | if (klass != NULL) { |
| 246 | return klass; |
| 247 | } |
| 248 | } |
| 249 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 250 | Thread* self = Thread::Current(); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 251 | DCHECK(self != NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 252 | CHECK(!self->IsExceptionPending()); |
| 253 | // Find the class in the loaded classes table. |
| 254 | Class* klass = LookupClass(descriptor, class_loader); |
| 255 | if (klass == NULL) { |
| 256 | // Class is not yet loaded. |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 257 | if (descriptor[0] == '[') { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 258 | return CreateArrayClass(descriptor, class_loader); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 259 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 260 | DexFile::ClassPath& class_path = ((class_loader != NULL) ? class_loader->GetClassPath() : boot_class_path_); |
| 261 | DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, class_path); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 262 | if (pair.second == NULL) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 263 | LG << "Class " << descriptor << " not found in class loader " << class_loader; // TODO: NoClassDefFoundError |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 264 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 265 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 266 | const DexFile& dex_file = *pair.first; |
| 267 | const DexFile::ClassDef& dex_class_def = *pair.second; |
| 268 | DexCache* dex_cache = FindDexCache(pair.first); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 269 | // Load the class from the dex file. |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 270 | if (!init_done_) { |
| 271 | // finish up init of hand crafted class_roots_ |
| 272 | if (descriptor == "Ljava/lang/Object;") { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 273 | klass = GetClassRoot(kJavaLangObject); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 274 | } else if (descriptor == "Ljava/lang/Class;") { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 275 | klass = GetClassRoot(kJavaLangClass); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 276 | } else if (descriptor == "Ljava/lang/String;") { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 277 | klass = GetClassRoot(kJavaLangString); |
| 278 | } else if (descriptor == "Ljava/lang/reflect/Field;") { |
| 279 | klass = GetClassRoot(kJavaLangReflectField); |
| 280 | } else if (descriptor == "Ljava/lang/reflect/Method;") { |
| 281 | klass = GetClassRoot(kJavaLangReflectMethod); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 282 | } else { |
| 283 | klass = AllocClass(); |
| 284 | } |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 285 | } else { |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 286 | klass = AllocClass(); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 287 | } |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 288 | klass->dex_cache_ = dex_cache; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 289 | LoadClass(dex_file, dex_class_def, klass, class_loader); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 290 | // Check for a pending exception during load |
| 291 | if (self->IsExceptionPending()) { |
| 292 | // TODO: free native allocations in klass |
| 293 | return NULL; |
| 294 | } |
| 295 | { |
| 296 | ObjectLock lock(klass); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 297 | klass->clinit_thread_id_ = self->GetId(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 298 | // Add the newly loaded class to the loaded classes table. |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 299 | bool success = InsertClass(klass); // TODO just return collision |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 300 | if (!success) { |
| 301 | // We may fail to insert if we raced with another thread. |
| 302 | klass->clinit_thread_id_ = 0; |
| 303 | // TODO: free native allocations in klass |
| 304 | klass = LookupClass(descriptor, class_loader); |
| 305 | CHECK(klass != NULL); |
| 306 | } else { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 307 | // Finish loading (if necessary) by finding parents |
| 308 | if (!klass->IsLoaded() && !LoadSuperAndInterfaces(klass, dex_file)) { |
| 309 | // Loading failed. |
| 310 | // TODO: CHECK(self->IsExceptionPending()); |
| 311 | lock.NotifyAll(); |
| 312 | return NULL; |
| 313 | } |
| 314 | CHECK(klass->IsLoaded()); |
| 315 | // Link the class (if necessary) |
| 316 | if (!klass->IsLinked() && !LinkClass(klass, dex_file)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 317 | // Linking failed. |
| 318 | // TODO: CHECK(self->IsExceptionPending()); |
| 319 | lock.NotifyAll(); |
| 320 | return NULL; |
| 321 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 322 | CHECK(klass->IsLinked()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | } |
| 326 | // Link the class if it has not already been linked. |
| 327 | if (!klass->IsLinked() && !klass->IsErroneous()) { |
| 328 | ObjectLock lock(klass); |
| 329 | // Check for circular dependencies between classes. |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 330 | if (!klass->IsLinked() && klass->clinit_thread_id_ == self->GetId()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 331 | LG << "Recursive link"; // TODO: ClassCircularityError |
| 332 | return NULL; |
| 333 | } |
| 334 | // Wait for the pending initialization to complete. |
| 335 | while (!klass->IsLinked() && !klass->IsErroneous()) { |
| 336 | lock.Wait(); |
| 337 | } |
| 338 | } |
| 339 | if (klass->IsErroneous()) { |
| 340 | LG << "EarlierClassFailure"; // TODO: EarlierClassFailure |
| 341 | return NULL; |
| 342 | } |
| 343 | // Return the loaded class. No exceptions should be pending. |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 344 | CHECK(klass->IsLinked()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 345 | CHECK(!self->IsExceptionPending()); |
| 346 | return klass; |
| 347 | } |
| 348 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 349 | void ClassLinker::LoadClass(const DexFile& dex_file, |
| 350 | const DexFile::ClassDef& dex_class_def, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 351 | Class* klass, |
| 352 | ClassLoader* class_loader) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 353 | CHECK(klass != NULL); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 354 | CHECK(klass->dex_cache_ != NULL); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 355 | CHECK_EQ(Class::kStatusNotReady, klass->status_); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 356 | const byte* class_data = dex_file.GetClassData(dex_class_def); |
| 357 | DexFile::ClassDataHeader header = dex_file.ReadClassDataHeader(&class_data); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 358 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 359 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 360 | CHECK(descriptor != NULL); |
| 361 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 362 | klass->klass_ = GetClassRoot(kJavaLangClass); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 363 | klass->descriptor_.set(descriptor); |
| 364 | klass->descriptor_alloc_ = NULL; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 365 | klass->access_flags_ = dex_class_def.access_flags_; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 366 | klass->class_loader_ = class_loader; |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 367 | klass->primitive_type_ = Class::kPrimNot; |
| 368 | klass->status_ = Class::kStatusIdx; |
| 369 | |
| 370 | klass->super_class_ = NULL; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 371 | klass->super_class_idx_ = dex_class_def.superclass_idx_; |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 372 | |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 373 | size_t num_static_fields = header.static_fields_size_; |
| 374 | size_t num_instance_fields = header.instance_fields_size_; |
| 375 | size_t num_direct_methods = header.direct_methods_size_; |
| 376 | size_t num_virtual_methods = header.virtual_methods_size_; |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 377 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 378 | klass->source_file_ = dex_file.dexGetSourceFile(dex_class_def); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 379 | |
| 380 | // Load class interfaces. |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 381 | LoadInterfaces(dex_file, dex_class_def, klass); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 382 | |
| 383 | // Load static fields. |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 384 | DCHECK(klass->sfields_ == NULL); |
| 385 | if (num_static_fields != 0) { |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 386 | klass->sfields_ = AllocObjectArray<StaticField>(num_static_fields); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 387 | uint32_t last_idx = 0; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 388 | for (size_t i = 0; i < klass->NumStaticFields(); ++i) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 389 | DexFile::Field dex_field; |
| 390 | dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 391 | StaticField* sfield = AllocStaticField(); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 392 | klass->SetStaticField(i, sfield); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 393 | LoadField(dex_file, dex_field, klass, sfield); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
| 397 | // Load instance fields. |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 398 | DCHECK(klass->ifields_ == NULL); |
| 399 | if (num_instance_fields != 0) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 400 | // TODO: allocate on the object heap. |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 401 | klass->ifields_ = AllocObjectArray<InstanceField>(num_instance_fields); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 402 | uint32_t last_idx = 0; |
| 403 | for (size_t i = 0; i < klass->NumInstanceFields(); ++i) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 404 | DexFile::Field dex_field; |
| 405 | dex_file.dexReadClassDataField(&class_data, &dex_field, &last_idx); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 406 | InstanceField* ifield = AllocInstanceField(); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 407 | klass->SetInstanceField(i, ifield); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 408 | LoadField(dex_file, dex_field, klass, ifield); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
| 412 | // Load direct methods. |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 413 | DCHECK(klass->direct_methods_ == NULL); |
| 414 | if (num_direct_methods != 0) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 415 | // TODO: append direct methods to class object |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 416 | klass->direct_methods_ = AllocObjectArray<Method>(num_direct_methods); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 417 | uint32_t last_idx = 0; |
| 418 | for (size_t i = 0; i < klass->NumDirectMethods(); ++i) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 419 | DexFile::Method dex_method; |
| 420 | dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 421 | Method* meth = AllocMethod(); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 422 | klass->SetDirectMethod(i, meth); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 423 | LoadMethod(dex_file, dex_method, klass, meth); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 424 | // TODO: register maps |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | // Load virtual methods. |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 429 | DCHECK(klass->virtual_methods_ == NULL); |
| 430 | if (num_virtual_methods != 0) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 431 | // TODO: append virtual methods to class object |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 432 | klass->virtual_methods_ = AllocObjectArray<Method>(num_virtual_methods); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 433 | uint32_t last_idx = 0; |
| 434 | for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 435 | DexFile::Method dex_method; |
| 436 | dex_file.dexReadClassDataMethod(&class_data, &dex_method, &last_idx); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 437 | Method* meth = AllocMethod(); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 438 | klass->SetVirtualMethod(i, meth); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 439 | LoadMethod(dex_file, dex_method, klass, meth); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 440 | // TODO: register maps |
| 441 | } |
| 442 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 445 | void ClassLinker::LoadInterfaces(const DexFile& dex_file, |
| 446 | const DexFile::ClassDef& dex_class_def, |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 447 | Class* klass) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 448 | const DexFile::TypeList* list = dex_file.GetInterfacesList(dex_class_def); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 449 | if (list != NULL) { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 450 | DCHECK(klass->interfaces_ == NULL); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 451 | klass->interfaces_ = AllocObjectArray<Class>(list->Size()); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 452 | DCHECK(klass->interfaces_idx_ == NULL); |
| 453 | klass->interfaces_idx_ = new uint32_t[list->Size()]; |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 454 | for (size_t i = 0; i < list->Size(); ++i) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 455 | const DexFile::TypeItem& type_item = list->GetTypeItem(i); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 456 | klass->interfaces_idx_[i] = type_item.type_idx_; |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 461 | void ClassLinker::LoadField(const DexFile& dex_file, |
| 462 | const DexFile::Field& src, |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 463 | Class* klass, |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 464 | Field* dst) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 465 | const DexFile::FieldId& field_id = dex_file.GetFieldId(src.field_idx_); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 466 | dst->klass_ = klass; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 467 | dst->java_name_ = ResolveString(klass, field_id.name_idx_, dex_file); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 468 | dst->descriptor_.set(dex_file.dexStringByTypeIdx(field_id.type_idx_)); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 469 | dst->access_flags_ = src.access_flags_; |
| 470 | } |
| 471 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 472 | void ClassLinker::LoadMethod(const DexFile& dex_file, |
| 473 | const DexFile::Method& src, |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 474 | Class* klass, |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 475 | Method* dst) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 476 | const DexFile::MethodId& method_id = dex_file.GetMethodId(src.method_idx_); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 477 | dst->klass_ = klass; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 478 | dst->java_name_ = ResolveString(klass, method_id.name_idx_, dex_file); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 479 | { |
| 480 | int32_t utf16_length; |
| 481 | scoped_ptr<char> utf8(dex_file.CreateMethodDescriptor(method_id.proto_idx_, |
| 482 | &utf16_length)); |
| 483 | dst->descriptor_ = String::AllocFromModifiedUtf8(utf16_length, utf8.get()); |
| 484 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 485 | dst->proto_idx_ = method_id.proto_idx_; |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 486 | dst->shorty_ = dex_file.GetShorty(method_id.proto_idx_); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 487 | dst->access_flags_ = src.access_flags_; |
| 488 | |
| 489 | // TODO: check for finalize method |
| 490 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 491 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(src); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 492 | if (code_item != NULL) { |
| 493 | dst->num_registers_ = code_item->registers_size_; |
| 494 | dst->num_ins_ = code_item->ins_size_; |
| 495 | dst->num_outs_ = code_item->outs_size_; |
| 496 | dst->insns_ = code_item->insns_; |
| 497 | } else { |
| 498 | uint16_t num_args = dst->NumArgRegisters(); |
| 499 | if (!dst->IsStatic()) { |
| 500 | ++num_args; |
| 501 | } |
| 502 | dst->num_registers_ = dst->num_ins_ + num_args; |
| 503 | // TODO: native methods |
| 504 | } |
| 505 | } |
| 506 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 507 | void ClassLinker::AppendToBootClassPath(DexFile* dex_file) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 508 | CHECK(dex_file != NULL); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 509 | boot_class_path_.push_back(dex_file); |
| 510 | RegisterDexFile(dex_file); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 513 | void ClassLinker::RegisterDexFile(const DexFile* dex_file) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 514 | CHECK(dex_file != NULL); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 515 | dex_files_.push_back(dex_file); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 516 | DexCache* dex_cache = AllocDexCache(); |
| 517 | CHECK(dex_cache != NULL); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 518 | dex_cache->Init(AllocObjectArray<String>(dex_file->NumStringIds()), |
| 519 | AllocObjectArray<Class>(dex_file->NumTypeIds()), |
| 520 | AllocObjectArray<Method>(dex_file->NumMethodIds()), |
| 521 | AllocObjectArray<Field>(dex_file->NumFieldIds())); |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 522 | dex_caches_.push_back(dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 525 | const DexFile& ClassLinker::FindDexFile(const DexCache* dex_cache) const { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 526 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
| 527 | if (dex_caches_[i] == dex_cache) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 528 | return *dex_files_[i]; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 529 | } |
| 530 | } |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 531 | CHECK(false) << "Could not find DexFile"; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 532 | return *dex_files_[-1]; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 535 | DexCache* ClassLinker::FindDexCache(const DexFile* dex_file) const { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 536 | for (size_t i = 0; i != dex_files_.size(); ++i) { |
| 537 | if (dex_files_[i] == dex_file) { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 538 | return dex_caches_[i]; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 541 | CHECK(false) << "Could not find DexCache"; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 542 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 545 | Class* ClassLinker::CreatePrimitiveClass(const StringPiece& descriptor) { |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 546 | Class* klass = AllocClass(); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 547 | CHECK(klass != NULL); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 548 | klass->super_class_ = NULL; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 549 | klass->access_flags_ = kAccPublic | kAccFinal | kAccAbstract; |
| 550 | klass->descriptor_ = descriptor; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 551 | klass->descriptor_alloc_ = NULL; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 552 | klass->status_ = Class::kStatusInitialized; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 553 | bool success = InsertClass(klass); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 554 | CHECK(success) << "CreatePrimitiveClass(" << descriptor << ") failed"; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 555 | return klass; |
| 556 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 557 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 558 | // Create an array class (i.e. the class object for the array, not the |
| 559 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 560 | // "[Ljava/lang/String;". |
| 561 | // |
| 562 | // If "descriptor" refers to an array of primitives, look up the |
| 563 | // primitive type's internally-generated class object. |
| 564 | // |
| 565 | // "loader" is the class loader of the class that's referring to us. It's |
| 566 | // used to ensure that we're looking for the element type in the right |
| 567 | // context. It does NOT become the class loader for the array class; that |
| 568 | // always comes from the base element class. |
| 569 | // |
| 570 | // Returns NULL with an exception raised on failure. |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 571 | Class* ClassLinker::CreateArrayClass(const StringPiece& descriptor, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 572 | ClassLoader* class_loader) |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 573 | { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 574 | CHECK(descriptor[0] == '['); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 575 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 576 | // Identify the underlying element class and the array dimension depth. |
| 577 | Class* component_type_ = NULL; |
| 578 | int array_rank; |
| 579 | if (descriptor[1] == '[') { |
| 580 | // array of arrays; keep descriptor and grab stuff from parent |
| 581 | Class* outer = FindClass(descriptor.substr(1), class_loader); |
| 582 | if (outer != NULL) { |
| 583 | // want the base class, not "outer", in our component_type_ |
| 584 | component_type_ = outer->component_type_; |
| 585 | array_rank = outer->array_rank_ + 1; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 586 | } else { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 587 | DCHECK(component_type_ == NULL); // make sure we fail |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 588 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 589 | } else { |
| 590 | array_rank = 1; |
| 591 | if (descriptor[1] == 'L') { |
| 592 | // array of objects; strip off "[" and look up descriptor. |
| 593 | const StringPiece subDescriptor = descriptor.substr(1); |
| 594 | component_type_ = FindClass(subDescriptor, class_loader); |
| 595 | } else { |
| 596 | // array of a primitive type |
| 597 | component_type_ = FindPrimitiveClass(descriptor[1]); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 598 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 599 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 600 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 601 | if (component_type_ == NULL) { |
| 602 | // failed |
| 603 | // DCHECK(Thread::Current()->IsExceptionPending()); // TODO |
| 604 | return NULL; |
| 605 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 606 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 607 | // See if the component type is already loaded. Array classes are |
| 608 | // always associated with the class loader of their underlying |
| 609 | // element type -- an array of Strings goes with the loader for |
| 610 | // java/lang/String -- so we need to look for it there. (The |
| 611 | // caller should have checked for the existence of the class |
| 612 | // before calling here, but they did so with *their* class loader, |
| 613 | // not the component type's loader.) |
| 614 | // |
| 615 | // If we find it, the caller adds "loader" to the class' initiating |
| 616 | // loader list, which should prevent us from going through this again. |
| 617 | // |
| 618 | // This call is unnecessary if "loader" and "component_type_->class_loader_" |
| 619 | // are the same, because our caller (FindClass) just did the |
| 620 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 621 | // because we effectively do this lookup again when we add the new |
| 622 | // class to the hash table --- necessary because of possible races with |
| 623 | // other threads.) |
| 624 | if (class_loader != component_type_->class_loader_) { |
| 625 | Class* new_class = LookupClass(descriptor, component_type_->class_loader_); |
| 626 | if (new_class != NULL) { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 627 | return new_class; |
| 628 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 629 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 630 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 631 | // Fill out the fields in the Class. |
| 632 | // |
| 633 | // It is possible to execute some methods against arrays, because |
| 634 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 635 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 636 | // |
| 637 | // Array classes are simple enough that we don't need to do a full |
| 638 | // link step. |
| 639 | |
| 640 | Class* new_class = NULL; |
| 641 | if (!init_done_) { |
| 642 | if (descriptor == "[Ljava/lang/Object;") { |
| 643 | new_class = GetClassRoot(kObjectArrayClass); |
| 644 | } else if (descriptor == "[C") { |
| 645 | new_class = GetClassRoot(kCharArrayClass); |
| 646 | } |
| 647 | } |
| 648 | if (new_class == NULL) { |
| 649 | new_class = AllocClass(); |
| 650 | if (new_class == NULL) { |
| 651 | return NULL; |
| 652 | } |
| 653 | } |
| 654 | new_class->descriptor_alloc_ = new std::string(descriptor.data(), |
| 655 | descriptor.size()); |
| 656 | new_class->descriptor_.set(new_class->descriptor_alloc_->data(), |
| 657 | new_class->descriptor_alloc_->size()); |
| 658 | Class* java_lang_Object = GetClassRoot(kJavaLangObject); |
| 659 | new_class->super_class_ = java_lang_Object; |
| 660 | new_class->vtable_ = java_lang_Object->vtable_; |
| 661 | new_class->primitive_type_ = Class::kPrimNot; |
| 662 | new_class->component_type_ = component_type_; |
| 663 | new_class->class_loader_ = component_type_->class_loader_; |
| 664 | new_class->array_rank_ = array_rank; |
| 665 | new_class->status_ = Class::kStatusInitialized; |
| 666 | // don't need to set new_class->object_size_ |
| 667 | |
| 668 | |
| 669 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 670 | // interfaces. We need to set that up here, so that stuff like |
| 671 | // "instanceof" works right. |
| 672 | // |
| 673 | // Note: The GC could run during the call to FindSystemClass, |
| 674 | // so we need to make sure the class object is GC-valid while we're in |
| 675 | // there. Do this by clearing the interface list so the GC will just |
| 676 | // think that the entries are null. |
| 677 | |
| 678 | |
| 679 | // Use the single, global copies of "interfaces" and "iftable" |
| 680 | // (remember not to free them for arrays). |
| 681 | DCHECK(array_interfaces_ != NULL); |
| 682 | new_class->interfaces_ = array_interfaces_; |
| 683 | new_class->iftable_count_ = 2; |
| 684 | DCHECK(array_iftable_ != NULL); |
| 685 | new_class->iftable_ = array_iftable_; |
| 686 | |
| 687 | // Inherit access flags from the component type. Arrays can't be |
| 688 | // used as a superclass or interface, so we want to add "final" |
| 689 | // and remove "interface". |
| 690 | // |
| 691 | // Don't inherit any non-standard flags (e.g., kAccFinal) |
| 692 | // from component_type_. We assume that the array class does not |
| 693 | // override finalize(). |
| 694 | new_class->access_flags_ = ((new_class->component_type_->access_flags_ & |
| 695 | ~kAccInterface) | kAccFinal) & kAccJavaFlagsMask; |
| 696 | |
| 697 | if (InsertClass(new_class)) { |
| 698 | return new_class; |
| 699 | } |
| 700 | // Another thread must have loaded the class after we |
| 701 | // started but before we finished. Abandon what we've |
| 702 | // done. |
| 703 | // |
| 704 | // (Yes, this happens.) |
| 705 | |
| 706 | // Grab the winning class. |
| 707 | Class* other_class = LookupClass(descriptor, component_type_->class_loader_); |
| 708 | DCHECK(other_class != NULL); |
| 709 | return other_class; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | Class* ClassLinker::FindPrimitiveClass(char type) { |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 713 | switch (type) { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 714 | case 'B': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 715 | return GetClassRoot(kPrimitiveByte); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 716 | case 'C': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 717 | return GetClassRoot(kPrimitiveChar); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 718 | case 'D': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 719 | return GetClassRoot(kPrimitiveDouble); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 720 | case 'F': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 721 | return GetClassRoot(kPrimitiveFloat); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 722 | case 'I': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 723 | return GetClassRoot(kPrimitiveInt); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 724 | case 'J': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 725 | return GetClassRoot(kPrimitiveLong); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 726 | case 'S': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 727 | return GetClassRoot(kPrimitiveShort); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 728 | case 'Z': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 729 | return GetClassRoot(kPrimitiveBoolean); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 730 | case 'V': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 731 | return GetClassRoot(kPrimitiveVoid); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 732 | case 'L': |
| 733 | case '[': |
| 734 | LOG(ERROR) << "Not a primitive type " << static_cast<int>(type); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 735 | default: |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 736 | LOG(ERROR) << "Unknown primitive type " << static_cast<int>(type); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 737 | }; |
| 738 | return NULL; // Not reachable. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | bool ClassLinker::InsertClass(Class* klass) { |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame^] | 742 | MutexLock mu(classes_lock_); |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 743 | const StringPiece& key = klass->GetDescriptor(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 744 | Table::iterator it = classes_.insert(std::make_pair(key, klass)); |
| 745 | return ((*it).second == klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 748 | Class* ClassLinker::LookupClass(const StringPiece& descriptor, ClassLoader* class_loader) { |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame^] | 749 | MutexLock mu(classes_lock_); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 750 | typedef Table::const_iterator It; // TODO: C++0x auto |
| 751 | for (It it = classes_.find(descriptor), end = classes_.end(); it != end; ++it) { |
| 752 | Class* klass = it->second; |
| 753 | if (klass->descriptor_ == descriptor && klass->class_loader_ == class_loader) { |
| 754 | return klass; |
| 755 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 756 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 757 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | bool ClassLinker::InitializeClass(Class* klass) { |
| 761 | CHECK(klass->GetStatus() == Class::kStatusResolved || |
| 762 | klass->GetStatus() == Class::kStatusError); |
| 763 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 764 | Thread* self = Thread::Current(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 765 | |
| 766 | { |
| 767 | ObjectLock lock(klass); |
| 768 | |
| 769 | if (klass->GetStatus() < Class::kStatusVerified) { |
| 770 | if (klass->IsErroneous()) { |
| 771 | LG << "re-initializing failed class"; // TODO: throw |
| 772 | return false; |
| 773 | } |
| 774 | |
| 775 | CHECK(klass->GetStatus() == Class::kStatusResolved); |
| 776 | |
| 777 | klass->status_ = Class::kStatusVerifying; |
| 778 | if (!DexVerify::VerifyClass(klass)) { |
| 779 | LG << "Verification failed"; // TODO: ThrowVerifyError |
| 780 | Object* exception = self->GetException(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 781 | size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_); |
| 782 | klass->SetFieldObject(field_offset, exception->GetClass()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 783 | klass->SetStatus(Class::kStatusError); |
| 784 | return false; |
| 785 | } |
| 786 | |
| 787 | klass->SetStatus(Class::kStatusVerified); |
| 788 | } |
| 789 | |
| 790 | if (klass->status_ == Class::kStatusInitialized) { |
| 791 | return true; |
| 792 | } |
| 793 | |
| 794 | while (klass->status_ == Class::kStatusInitializing) { |
| 795 | // we caught somebody else in the act; was it us? |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 796 | if (klass->clinit_thread_id_ == self->GetId()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 797 | LG << "recursive <clinit>"; |
| 798 | return true; |
| 799 | } |
| 800 | |
| 801 | CHECK(!self->IsExceptionPending()); |
| 802 | |
| 803 | lock.Wait(); // TODO: check for interruption |
| 804 | |
| 805 | // When we wake up, repeat the test for init-in-progress. If |
| 806 | // there's an exception pending (only possible if |
| 807 | // "interruptShouldThrow" was set), bail out. |
| 808 | if (self->IsExceptionPending()) { |
| 809 | CHECK(false); |
| 810 | LG << "Exception in initialization."; // TODO: ExceptionInInitializerError |
| 811 | klass->SetStatus(Class::kStatusError); |
| 812 | return false; |
| 813 | } |
| 814 | if (klass->GetStatus() == Class::kStatusInitializing) { |
| 815 | continue; |
| 816 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 817 | DCHECK(klass->GetStatus() == Class::kStatusInitialized || |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 818 | klass->GetStatus() == Class::kStatusError); |
| 819 | if (klass->IsErroneous()) { |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 820 | // The caller wants an exception, but it was thrown in a |
| 821 | // different thread. Synthesize one here. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 822 | LG << "<clinit> failed"; // TODO: throw UnsatisfiedLinkError |
| 823 | return false; |
| 824 | } |
| 825 | return true; // otherwise, initialized |
| 826 | } |
| 827 | |
| 828 | // see if we failed previously |
| 829 | if (klass->IsErroneous()) { |
| 830 | // might be wise to unlock before throwing; depends on which class |
| 831 | // it is that we have locked |
| 832 | |
| 833 | // TODO: throwEarlierClassFailure(klass); |
| 834 | return false; |
| 835 | } |
| 836 | |
| 837 | if (!ValidateSuperClassDescriptors(klass)) { |
| 838 | klass->SetStatus(Class::kStatusError); |
| 839 | return false; |
| 840 | } |
| 841 | |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 842 | DCHECK(klass->status_ < Class::kStatusInitializing); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 843 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 844 | klass->clinit_thread_id_ = self->GetId(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 845 | klass->status_ = Class::kStatusInitializing; |
| 846 | } |
| 847 | |
| 848 | if (!InitializeSuperClass(klass)) { |
| 849 | return false; |
| 850 | } |
| 851 | |
| 852 | InitializeStaticFields(klass); |
| 853 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 854 | Method* clinit = klass->FindDeclaredDirectMethod("<clinit>", "()V"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 855 | if (clinit != NULL) { |
| 856 | } else { |
| 857 | // JValue unused; |
| 858 | // TODO: dvmCallMethod(self, method, NULL, &unused); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 859 | //CHECK(!"unimplemented"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | { |
| 863 | ObjectLock lock(klass); |
| 864 | |
| 865 | if (self->IsExceptionPending()) { |
| 866 | klass->SetStatus(Class::kStatusError); |
| 867 | } else { |
| 868 | klass->SetStatus(Class::kStatusInitialized); |
| 869 | } |
| 870 | lock.NotifyAll(); |
| 871 | } |
| 872 | |
| 873 | return true; |
| 874 | } |
| 875 | |
| 876 | bool ClassLinker::ValidateSuperClassDescriptors(const Class* klass) { |
| 877 | if (klass->IsInterface()) { |
| 878 | return true; |
| 879 | } |
| 880 | // begin with the methods local to the superclass |
| 881 | if (klass->HasSuperClass() && |
| 882 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
| 883 | const Class* super = klass->GetSuperClass(); |
| 884 | for (int i = super->NumVirtualMethods() - 1; i >= 0; --i) { |
| 885 | const Method* method = klass->GetVirtualMethod(i); |
| 886 | if (method != super->GetVirtualMethod(i) && |
| 887 | !HasSameMethodDescriptorClasses(method, super, klass)) { |
| 888 | LG << "Classes resolve differently in superclass"; |
| 889 | return false; |
| 890 | } |
| 891 | } |
| 892 | } |
| 893 | for (size_t i = 0; i < klass->iftable_count_; ++i) { |
| 894 | const InterfaceEntry* iftable = &klass->iftable_[i]; |
| 895 | Class* interface = iftable->GetClass(); |
| 896 | if (klass->GetClassLoader() != interface->GetClassLoader()) { |
| 897 | for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) { |
| 898 | uint32_t vtable_index = iftable->method_index_array_[j]; |
| 899 | const Method* method = klass->GetVirtualMethod(vtable_index); |
| 900 | if (!HasSameMethodDescriptorClasses(method, interface, |
| 901 | method->GetClass())) { |
| 902 | LG << "Classes resolve differently in interface"; // TODO: LinkageError |
| 903 | return false; |
| 904 | } |
| 905 | } |
| 906 | } |
| 907 | } |
| 908 | return true; |
| 909 | } |
| 910 | |
| 911 | bool ClassLinker::HasSameMethodDescriptorClasses(const Method* method, |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 912 | const Class* klass1, |
| 913 | const Class* klass2) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 914 | const DexFile& dex_file = FindDexFile(method->GetClass()->GetDexCache()); |
| 915 | const DexFile::ProtoId& proto_id = dex_file.GetProtoId(method->proto_idx_); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 916 | DexFile::ParameterIterator *it; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 917 | for (it = dex_file.GetParameterIterator(proto_id); it->HasNext(); it->Next()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 918 | const char* descriptor = it->GetDescriptor(); |
| 919 | if (descriptor == NULL) { |
| 920 | break; |
| 921 | } |
| 922 | if (descriptor[0] == 'L' || descriptor[0] == '[') { |
| 923 | // Found a non-primitive type. |
| 924 | if (!HasSameDescriptorClasses(descriptor, klass1, klass2)) { |
| 925 | return false; |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | // Check the return type |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 930 | const char* descriptor = dex_file.GetReturnTypeDescriptor(proto_id); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 931 | if (descriptor[0] == 'L' || descriptor[0] == '[') { |
| 932 | if (HasSameDescriptorClasses(descriptor, klass1, klass2)) { |
| 933 | return false; |
| 934 | } |
| 935 | } |
| 936 | return true; |
| 937 | } |
| 938 | |
| 939 | // Returns true if classes referenced by the descriptor are the |
| 940 | // same classes in klass1 as they are in klass2. |
| 941 | bool ClassLinker::HasSameDescriptorClasses(const char* descriptor, |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 942 | const Class* klass1, |
| 943 | const Class* klass2) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 944 | CHECK(descriptor != NULL); |
| 945 | CHECK(klass1 != NULL); |
| 946 | CHECK(klass2 != NULL); |
| 947 | #if 0 |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 948 | Class* found1 = FindClass(descriptor, klass1->GetClassLoader()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 949 | // TODO: found1 == NULL |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 950 | Class* found2 = FindClass(descriptor, klass2->GetClassLoader()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 951 | // TODO: found2 == NULL |
| 952 | // TODO: lookup found1 in initiating loader list |
| 953 | if (found1 == NULL || found2 == NULL) { |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 954 | Thread::Current()->ClearException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 955 | if (found1 == found2) { |
| 956 | return true; |
| 957 | } else { |
| 958 | return false; |
| 959 | } |
| 960 | } |
| 961 | #endif |
| 962 | return true; |
| 963 | } |
| 964 | |
| 965 | bool ClassLinker::InitializeSuperClass(Class* klass) { |
| 966 | CHECK(klass != NULL); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame^] | 967 | MutexLock mu(classes_lock_); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 968 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
| 969 | Class* super_class = klass->GetSuperClass(); |
| 970 | if (super_class->GetStatus() != Class::kStatusInitialized) { |
| 971 | CHECK(!super_class->IsInterface()); |
| 972 | klass->MonitorExit(); |
| 973 | bool super_initialized = InitializeClass(super_class); |
| 974 | klass->MonitorEnter(); |
| 975 | // TODO: check for a pending exception |
| 976 | if (!super_initialized) { |
| 977 | klass->SetStatus(Class::kStatusError); |
| 978 | klass->NotifyAll(); |
| 979 | return false; |
| 980 | } |
| 981 | } |
| 982 | } |
| 983 | return true; |
| 984 | } |
| 985 | |
| 986 | void ClassLinker::InitializeStaticFields(Class* klass) { |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 987 | size_t num_static_fields = klass->NumStaticFields(); |
| 988 | if (num_static_fields == 0) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 989 | return; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 990 | } |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 991 | DexCache* dex_cache = klass->GetDexCache(); |
| 992 | if (dex_cache == NULL) { |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 993 | return; |
| 994 | } |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 995 | const StringPiece& descriptor = klass->GetDescriptor(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 996 | const DexFile& dex_file = FindDexFile(dex_cache); |
| 997 | const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 998 | CHECK(dex_class_def != NULL); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 999 | const byte* addr = dex_file.GetEncodedArray(*dex_class_def); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1000 | size_t array_size = DecodeUnsignedLeb128(&addr); |
| 1001 | for (size_t i = 0; i < array_size; ++i) { |
| 1002 | StaticField* field = klass->GetStaticField(i); |
| 1003 | JValue value; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1004 | DexFile::ValueType type = dex_file.ReadEncodedValue(&addr, &value); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1005 | switch (type) { |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1006 | case DexFile::kByte: |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1007 | field->SetByte(value.b); |
| 1008 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1009 | case DexFile::kShort: |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1010 | field->SetShort(value.s); |
| 1011 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1012 | case DexFile::kChar: |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1013 | field->SetChar(value.c); |
| 1014 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1015 | case DexFile::kInt: |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1016 | field->SetInt(value.i); |
| 1017 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1018 | case DexFile::kLong: |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1019 | field->SetLong(value.j); |
| 1020 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1021 | case DexFile::kFloat: |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1022 | field->SetFloat(value.f); |
| 1023 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1024 | case DexFile::kDouble: |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1025 | field->SetDouble(value.d); |
| 1026 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1027 | case DexFile::kString: { |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1028 | uint32_t string_idx = value.i; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1029 | String* resolved = ResolveString(klass, string_idx, dex_file); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1030 | field->SetObject(resolved); |
| 1031 | break; |
| 1032 | } |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1033 | case DexFile::kBoolean: |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1034 | field->SetBoolean(value.z); |
| 1035 | break; |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1036 | case DexFile::kNull: |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1037 | field->SetObject(value.l); |
| 1038 | break; |
| 1039 | default: |
Carl Shapiro | 606258b | 2011-07-09 16:09:09 -0700 | [diff] [blame] | 1040 | LOG(FATAL) << "Unknown type " << static_cast<int>(type); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1041 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1042 | } |
| 1043 | } |
| 1044 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1045 | bool ClassLinker::LinkClass(Class* klass, const DexFile& dex_file) { |
| 1046 | CHECK_EQ(Class::kStatusLoaded, klass->status_); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1047 | if (!LinkSuperClass(klass)) { |
| 1048 | return false; |
| 1049 | } |
| 1050 | if (!LinkMethods(klass)) { |
| 1051 | return false; |
| 1052 | } |
| 1053 | if (!LinkInstanceFields(klass)) { |
| 1054 | return false; |
| 1055 | } |
| 1056 | CreateReferenceOffsets(klass); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1057 | CHECK_EQ(Class::kStatusLoaded, klass->status_); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1058 | klass->status_ = Class::kStatusResolved; |
| 1059 | return true; |
| 1060 | } |
| 1061 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1062 | bool ClassLinker::LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file) { |
| 1063 | CHECK_EQ(Class::kStatusIdx, klass->status_); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1064 | if (klass->super_class_idx_ != DexFile::kDexNoIndex) { |
| 1065 | Class* super_class = ResolveClass(klass, klass->super_class_idx_, dex_file); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1066 | if (super_class == NULL) { |
| 1067 | LG << "Failed to resolve superclass"; |
| 1068 | return false; |
| 1069 | } |
| 1070 | klass->super_class_ = super_class; // TODO: write barrier |
| 1071 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1072 | if (klass->NumInterfaces() > 0) { |
| 1073 | for (size_t i = 0; i < klass->NumInterfaces(); ++i) { |
| 1074 | uint32_t idx = klass->interfaces_idx_[i]; |
| 1075 | klass->SetInterface(i, ResolveClass(klass, idx, dex_file)); |
| 1076 | if (klass->GetInterface(i) == NULL) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1077 | LG << "Failed to resolve interface"; |
| 1078 | return false; |
| 1079 | } |
| 1080 | // Verify |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1081 | if (!klass->CanAccess(klass->GetInterface(i))) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1082 | LG << "Inaccessible interface"; |
| 1083 | return false; |
| 1084 | } |
| 1085 | } |
| 1086 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1087 | // Mark the class as loaded. |
| 1088 | klass->status_ = Class::kStatusLoaded; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1089 | return true; |
| 1090 | } |
| 1091 | |
| 1092 | bool ClassLinker::LinkSuperClass(Class* klass) { |
| 1093 | CHECK(!klass->IsPrimitive()); |
| 1094 | const Class* super = klass->GetSuperClass(); |
| 1095 | if (klass->GetDescriptor() == "Ljava/lang/Object;") { |
| 1096 | if (super != NULL) { |
| 1097 | LG << "Superclass must not be defined"; // TODO: ClassFormatError |
| 1098 | return false; |
| 1099 | } |
| 1100 | // TODO: clear finalize attribute |
| 1101 | return true; |
| 1102 | } |
| 1103 | if (super == NULL) { |
| 1104 | LG << "No superclass defined"; // TODO: LinkageError |
| 1105 | return false; |
| 1106 | } |
| 1107 | // Verify |
| 1108 | if (super->IsFinal()) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1109 | LG << "Superclass " << super->descriptor_ << " is declared final"; // TODO: IncompatibleClassChangeError |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1110 | return false; |
| 1111 | } |
| 1112 | if (super->IsInterface()) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1113 | LG << "Superclass " << super->descriptor_ << " is an interface"; // TODO: IncompatibleClassChangeError |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1114 | return false; |
| 1115 | } |
| 1116 | if (!klass->CanAccess(super)) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1117 | LG << "Superclass " << super->descriptor_ << " is inaccessible"; // TODO: IllegalAccessError |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1118 | return false; |
| 1119 | } |
| 1120 | return true; |
| 1121 | } |
| 1122 | |
| 1123 | // Populate the class vtable and itable. |
| 1124 | bool ClassLinker::LinkMethods(Class* klass) { |
| 1125 | if (klass->IsInterface()) { |
| 1126 | // No vtable. |
| 1127 | size_t count = klass->NumVirtualMethods(); |
| 1128 | if (!IsUint(16, count)) { |
| 1129 | LG << "Too many methods on interface"; // TODO: VirtualMachineError |
| 1130 | return false; |
| 1131 | } |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 1132 | for (size_t i = 0; i < count; ++i) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1133 | klass->GetVirtualMethod(i)->method_index_ = i; |
| 1134 | } |
| 1135 | } else { |
| 1136 | // Link virtual method tables |
| 1137 | LinkVirtualMethods(klass); |
| 1138 | |
| 1139 | // Link interface method tables |
| 1140 | LinkInterfaceMethods(klass); |
| 1141 | |
| 1142 | // Insert stubs. |
| 1143 | LinkAbstractMethods(klass); |
| 1144 | } |
| 1145 | return true; |
| 1146 | } |
| 1147 | |
| 1148 | bool ClassLinker::LinkVirtualMethods(Class* klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1149 | if (klass->HasSuperClass()) { |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1150 | uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->vtable_->GetLength(); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1151 | size_t actual_count = klass->GetSuperClass()->vtable_->GetLength(); |
| 1152 | CHECK_LE(actual_count, max_count); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1153 | // TODO: do not assign to the vtable field until it is fully constructed. |
| 1154 | klass->vtable_ = klass->GetSuperClass()->vtable_->CopyOf(max_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1155 | // See if any of our virtual methods override the superclass. |
| 1156 | for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) { |
| 1157 | Method* local_method = klass->GetVirtualMethod(i); |
| 1158 | size_t j = 0; |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1159 | for (; j < actual_count; ++j) { |
| 1160 | Method* super_method = klass->vtable_->Get(j); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1161 | if (local_method->HasSameNameAndDescriptor(super_method)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1162 | // Verify |
| 1163 | if (super_method->IsFinal()) { |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1164 | LG << "Method overrides final method"; // TODO: VirtualMachineError |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1165 | return false; |
| 1166 | } |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1167 | klass->vtable_->Set(j, local_method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1168 | local_method->method_index_ = j; |
| 1169 | break; |
| 1170 | } |
| 1171 | } |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1172 | if (j == actual_count) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1173 | // Not overriding, append. |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1174 | klass->vtable_->Set(actual_count, local_method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1175 | local_method->method_index_ = actual_count; |
| 1176 | actual_count += 1; |
| 1177 | } |
| 1178 | } |
| 1179 | if (!IsUint(16, actual_count)) { |
| 1180 | LG << "Too many methods defined on class"; // TODO: VirtualMachineError |
| 1181 | return false; |
| 1182 | } |
| 1183 | CHECK_LE(actual_count, max_count); |
| 1184 | if (actual_count < max_count) { |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1185 | // TODO: do not assign to the vtable field until it is fully constructed. |
| 1186 | klass->vtable_ = klass->vtable_->CopyOf(actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1187 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1188 | } else { |
| 1189 | CHECK(klass->GetDescriptor() == "Ljava/lang/Object;"); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1190 | uint32_t num_virtual_methods = klass->NumVirtualMethods(); |
| 1191 | CHECK(klass->GetDescriptor() == "Ljava/lang/Object;"); |
| 1192 | if (!IsUint(16, num_virtual_methods)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1193 | LG << "Too many methods"; // TODO: VirtualMachineError |
| 1194 | return false; |
| 1195 | } |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1196 | // TODO: do not assign to the vtable field until it is fully constructed. |
| 1197 | klass->vtable_ = AllocObjectArray<Method>(num_virtual_methods); |
| 1198 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1199 | klass->vtable_->Set(i, klass->GetVirtualMethod(i)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1200 | klass->GetVirtualMethod(i)->method_index_ = i & 0xFFFF; |
| 1201 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1202 | } |
| 1203 | return true; |
| 1204 | } |
| 1205 | |
| 1206 | bool ClassLinker::LinkInterfaceMethods(Class* klass) { |
| 1207 | int pool_offset = 0; |
| 1208 | int pool_size = 0; |
| 1209 | int miranda_count = 0; |
| 1210 | int miranda_alloc = 0; |
| 1211 | size_t super_ifcount; |
| 1212 | if (klass->HasSuperClass()) { |
| 1213 | super_ifcount = klass->GetSuperClass()->iftable_count_; |
| 1214 | } else { |
| 1215 | super_ifcount = 0; |
| 1216 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1217 | size_t ifcount = super_ifcount; |
| 1218 | ifcount += klass->NumInterfaces(); |
| 1219 | for (size_t i = 0; i < klass->NumInterfaces(); i++) { |
| 1220 | ifcount += klass->GetInterface(i)->iftable_count_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1221 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1222 | if (ifcount == 0) { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1223 | DCHECK(klass->iftable_count_ == 0); |
| 1224 | DCHECK(klass->iftable_ == NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1225 | return true; |
| 1226 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1227 | klass->iftable_ = new InterfaceEntry[ifcount * sizeof(InterfaceEntry)]; |
| 1228 | memset(klass->iftable_, 0x00, sizeof(InterfaceEntry) * ifcount); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1229 | if (super_ifcount != 0) { |
| 1230 | memcpy(klass->iftable_, klass->GetSuperClass()->iftable_, |
| 1231 | sizeof(InterfaceEntry) * super_ifcount); |
| 1232 | } |
| 1233 | // Flatten the interface inheritance hierarchy. |
| 1234 | size_t idx = super_ifcount; |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1235 | for (size_t i = 0; i < klass->NumInterfaces(); i++) { |
| 1236 | Class* interf = klass->GetInterface(i); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1237 | DCHECK(interf != NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1238 | if (!interf->IsInterface()) { |
| 1239 | LG << "Class implements non-interface class"; // TODO: IncompatibleClassChangeError |
| 1240 | return false; |
| 1241 | } |
| 1242 | klass->iftable_[idx++].SetClass(interf); |
| 1243 | for (size_t j = 0; j < interf->iftable_count_; j++) { |
| 1244 | klass->iftable_[idx++].SetClass(interf->iftable_[j].GetClass()); |
| 1245 | } |
| 1246 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1247 | CHECK_EQ(idx, ifcount); |
| 1248 | klass->iftable_count_ = ifcount; |
| 1249 | if (klass->IsInterface() || super_ifcount == ifcount) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1250 | return true; |
| 1251 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1252 | for (size_t i = super_ifcount; i < ifcount; i++) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1253 | pool_size += klass->iftable_[i].GetClass()->NumVirtualMethods(); |
| 1254 | } |
| 1255 | if (pool_size == 0) { |
| 1256 | return true; |
| 1257 | } |
| 1258 | klass->ifvi_pool_count_ = pool_size; |
| 1259 | klass->ifvi_pool_ = new uint32_t[pool_size]; |
| 1260 | std::vector<Method*> miranda_list; |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1261 | for (size_t i = super_ifcount; i < ifcount; ++i) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1262 | klass->iftable_[i].method_index_array_ = klass->ifvi_pool_ + pool_offset; |
| 1263 | Class* interface = klass->iftable_[i].GetClass(); |
| 1264 | pool_offset += interface->NumVirtualMethods(); // end here |
| 1265 | for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) { |
| 1266 | Method* interface_method = interface->GetVirtualMethod(j); |
| 1267 | int k; // must be signed |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1268 | for (k = klass->vtable_->GetLength() - 1; k >= 0; --k) { |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1269 | Method* vtable_method = klass->vtable_->Get(k); |
| 1270 | if (interface_method->HasSameNameAndDescriptor(vtable_method)) { |
| 1271 | if (!vtable_method->IsPublic()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1272 | LG << "Implementation not public"; |
| 1273 | return false; |
| 1274 | } |
| 1275 | klass->iftable_[i].method_index_array_[j] = k; |
| 1276 | break; |
| 1277 | } |
| 1278 | } |
| 1279 | if (k < 0) { |
| 1280 | if (miranda_count == miranda_alloc) { |
| 1281 | miranda_alloc += 8; |
| 1282 | if (miranda_list.empty()) { |
| 1283 | miranda_list.resize(miranda_alloc); |
| 1284 | } else { |
| 1285 | miranda_list.resize(miranda_alloc); |
| 1286 | } |
| 1287 | } |
| 1288 | int mir; |
| 1289 | for (mir = 0; mir < miranda_count; mir++) { |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1290 | Method* miranda_method = miranda_list[mir]; |
| 1291 | if (miranda_method->HasSameNameAndDescriptor(interface_method)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1292 | break; |
| 1293 | } |
| 1294 | } |
| 1295 | // point the interface table at a phantom slot index |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1296 | klass->iftable_[i].method_index_array_[j] = klass->vtable_->GetLength() + mir; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1297 | if (mir == miranda_count) { |
| 1298 | miranda_list[miranda_count++] = interface_method; |
| 1299 | } |
| 1300 | } |
| 1301 | } |
| 1302 | } |
| 1303 | if (miranda_count != 0) { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1304 | int old_method_count = klass->NumVirtualMethods(); |
| 1305 | int new_method_count = old_method_count + miranda_count; |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1306 | klass->virtual_methods_ = klass->virtual_methods_->CopyOf(new_method_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1307 | |
| 1308 | CHECK(klass->vtable_ != NULL); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1309 | int old_vtable_count = klass->vtable_->GetLength(); |
| 1310 | int new_vtable_count = old_vtable_count + miranda_count; |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1311 | // TODO: do not assign to the vtable field until it is fully constructed. |
| 1312 | klass->vtable_ = klass->vtable_->CopyOf(new_vtable_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1313 | |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1314 | for (int i = 0; i < miranda_count; i++) { |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1315 | Method* meth = AllocMethod(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1316 | memcpy(meth, miranda_list[i], sizeof(Method)); |
| 1317 | meth->klass_ = klass; |
| 1318 | meth->access_flags_ |= kAccMiranda; |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1319 | meth->method_index_ = 0xFFFF & (old_vtable_count + i); |
| 1320 | klass->SetVirtualMethod(old_method_count + i, meth); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1321 | klass->vtable_->Set(old_vtable_count + i, meth); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1322 | } |
| 1323 | } |
| 1324 | return true; |
| 1325 | } |
| 1326 | |
| 1327 | void ClassLinker::LinkAbstractMethods(Class* klass) { |
| 1328 | for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) { |
| 1329 | Method* method = klass->GetVirtualMethod(i); |
| 1330 | if (method->IsAbstract()) { |
| 1331 | method->insns_ = reinterpret_cast<uint16_t*>(0xFFFFFFFF); // TODO: AbstractMethodError |
| 1332 | } |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | bool ClassLinker::LinkInstanceFields(Class* klass) { |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1337 | int field_offset; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1338 | if (klass->GetSuperClass() != NULL) { |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1339 | field_offset = klass->GetSuperClass()->object_size_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1340 | } else { |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1341 | field_offset = OFFSETOF_MEMBER(DataObject, fields_); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1342 | } |
| 1343 | // Move references to the front. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1344 | klass->num_reference_instance_fields_ = 0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1345 | size_t i = 0; |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1346 | for ( ; i < klass->NumInstanceFields(); i++) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1347 | InstanceField* pField = klass->GetInstanceField(i); |
| 1348 | char c = pField->GetType(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1349 | if (c != '[' && c != 'L') { |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1350 | for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) { |
| 1351 | InstanceField* refField = klass->GetInstanceField(j); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1352 | char rc = refField->GetType(); |
| 1353 | if (rc == '[' || rc == 'L') { |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1354 | klass->SetInstanceField(i, refField); |
| 1355 | klass->SetInstanceField(j, pField); |
| 1356 | pField = refField; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1357 | c = rc; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1358 | klass->num_reference_instance_fields_++; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1359 | break; |
| 1360 | } |
| 1361 | } |
| 1362 | } else { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1363 | klass->num_reference_instance_fields_++; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1364 | } |
| 1365 | if (c != '[' && c != 'L') { |
| 1366 | break; |
| 1367 | } |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1368 | pField->SetOffset(field_offset); |
| 1369 | field_offset += sizeof(uint32_t); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | // Now we want to pack all of the double-wide fields together. If |
| 1373 | // we're not aligned, though, we want to shuffle one 32-bit field |
| 1374 | // into place. If we can't find one, we'll have to pad it. |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1375 | if (i != klass->NumInstanceFields() && (field_offset & 0x04) != 0) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1376 | InstanceField* pField = klass->GetInstanceField(i); |
| 1377 | char c = pField->GetType(); |
| 1378 | |
| 1379 | if (c != 'J' && c != 'D') { |
| 1380 | // The field that comes next is 32-bit, so just advance past it. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1381 | DCHECK(c != '['); |
| 1382 | DCHECK(c != 'L'); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1383 | pField->SetOffset(field_offset); |
| 1384 | field_offset += sizeof(uint32_t); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1385 | i++; |
| 1386 | } else { |
| 1387 | // Next field is 64-bit, so search for a 32-bit field we can |
| 1388 | // swap into it. |
| 1389 | bool found = false; |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1390 | for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) { |
| 1391 | InstanceField* singleField = klass->GetInstanceField(j); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1392 | char rc = singleField->GetType(); |
| 1393 | if (rc != 'J' && rc != 'D') { |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1394 | klass->SetInstanceField(i, singleField); |
| 1395 | klass->SetInstanceField(j, pField); |
| 1396 | pField = singleField; |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1397 | pField->SetOffset(field_offset); |
| 1398 | field_offset += sizeof(uint32_t); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1399 | found = true; |
| 1400 | i++; |
| 1401 | break; |
| 1402 | } |
| 1403 | } |
| 1404 | if (!found) { |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1405 | field_offset += sizeof(uint32_t); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1406 | } |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | // Alignment is good, shuffle any double-wide fields forward, and |
| 1411 | // finish assigning field offsets to all fields. |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1412 | DCHECK(i == klass->NumInstanceFields() || (field_offset & 0x04) == 0); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1413 | for ( ; i < klass->NumInstanceFields(); i++) { |
| 1414 | InstanceField* pField = klass->GetInstanceField(i); |
| 1415 | char c = pField->GetType(); |
| 1416 | if (c != 'D' && c != 'J') { |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1417 | for (size_t j = klass->NumInstanceFields() - 1; j > i; j--) { |
| 1418 | InstanceField* doubleField = klass->GetInstanceField(j); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1419 | char rc = doubleField->GetType(); |
| 1420 | if (rc == 'D' || rc == 'J') { |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1421 | klass->SetInstanceField(i, doubleField); |
| 1422 | klass->SetInstanceField(j, pField); |
| 1423 | pField = doubleField; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1424 | c = rc; |
| 1425 | break; |
| 1426 | } |
| 1427 | } |
| 1428 | } else { |
| 1429 | // This is a double-wide field, leave it be. |
| 1430 | } |
| 1431 | |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1432 | pField->SetOffset(field_offset); |
| 1433 | field_offset += sizeof(uint32_t); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1434 | if (c == 'J' || c == 'D') |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1435 | field_offset += sizeof(uint32_t); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | #ifndef NDEBUG |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1439 | // Make sure that all reference fields appear before |
| 1440 | // non-reference fields, and all double-wide fields are aligned. |
| 1441 | bool seen_non_ref = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1442 | for (i = 0; i < klass->NumInstanceFields(); i++) { |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1443 | InstanceField *pField = klass->GetInstanceField(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1444 | char c = pField->GetType(); |
| 1445 | |
| 1446 | if (c == 'D' || c == 'J') { |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1447 | DCHECK_EQ(0U, pField->GetOffset() & 0x07); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | if (c != '[' && c != 'L') { |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1451 | if (!seen_non_ref) { |
| 1452 | seen_non_ref = true; |
Brian Carlstrom | 07d579f | 2011-07-27 13:31:51 -0700 | [diff] [blame] | 1453 | DCHECK_EQ(klass->NumReferenceInstanceFields(), i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1454 | } |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1455 | } else { |
| 1456 | DCHECK(!seen_non_ref); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1457 | } |
| 1458 | } |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 1459 | if (!seen_non_ref) { |
Brian Carlstrom | 07d579f | 2011-07-27 13:31:51 -0700 | [diff] [blame] | 1460 | DCHECK_EQ(klass->NumInstanceFields(), klass->NumReferenceInstanceFields()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1461 | } |
| 1462 | #endif |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1463 | klass->object_size_ = field_offset; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1464 | return true; |
| 1465 | } |
| 1466 | |
| 1467 | // Set the bitmap of reference offsets, refOffsets, from the ifields |
| 1468 | // list. |
| 1469 | void ClassLinker::CreateReferenceOffsets(Class* klass) { |
| 1470 | uint32_t reference_offsets = 0; |
| 1471 | if (klass->HasSuperClass()) { |
| 1472 | reference_offsets = klass->GetSuperClass()->GetReferenceOffsets(); |
| 1473 | } |
| 1474 | // If our superclass overflowed, we don't stand a chance. |
| 1475 | if (reference_offsets != CLASS_WALK_SUPER) { |
| 1476 | // All of the fields that contain object references are guaranteed |
| 1477 | // to be at the beginning of the ifields list. |
| 1478 | for (size_t i = 0; i < klass->NumReferenceInstanceFields(); ++i) { |
| 1479 | // Note that, per the comment on struct InstField, f->byteOffset |
| 1480 | // is the offset from the beginning of obj, not the offset into |
| 1481 | // obj->instanceData. |
| 1482 | const InstanceField* field = klass->GetInstanceField(i); |
| 1483 | size_t byte_offset = field->GetOffset(); |
| 1484 | CHECK_GE(byte_offset, CLASS_SMALLEST_OFFSET); |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 1485 | CHECK_EQ(byte_offset & (CLASS_OFFSET_ALIGNMENT - 1), 0U); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1486 | if (CLASS_CAN_ENCODE_OFFSET(byte_offset)) { |
| 1487 | uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset); |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 1488 | CHECK_NE(new_bit, 0U); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1489 | reference_offsets |= new_bit; |
| 1490 | } else { |
| 1491 | reference_offsets = CLASS_WALK_SUPER; |
| 1492 | break; |
| 1493 | } |
| 1494 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1495 | } |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 1496 | klass->SetReferenceOffsets(reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1497 | } |
| 1498 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1499 | Class* ClassLinker::ResolveClass(const Class* referrer, |
| 1500 | uint32_t class_idx, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1501 | const DexFile& dex_file) { |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1502 | DexCache* dex_cache = referrer->GetDexCache(); |
| 1503 | Class* resolved = dex_cache->GetResolvedClass(class_idx); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1504 | if (resolved != NULL) { |
| 1505 | return resolved; |
| 1506 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1507 | const char* descriptor = dex_file.dexStringByTypeIdx(class_idx); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1508 | if (descriptor[0] != '\0' && descriptor[1] == '\0') { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1509 | resolved = FindPrimitiveClass(descriptor[0]); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1510 | } else { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1511 | resolved = FindClass(descriptor, referrer->GetClassLoader()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1512 | } |
| 1513 | if (resolved != NULL) { |
| 1514 | Class* check = resolved->IsArray() ? resolved->component_type_ : resolved; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1515 | if (referrer->GetDexCache() != check->GetDexCache()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1516 | if (check->GetClassLoader() != NULL) { |
| 1517 | LG << "Class resolved by unexpected DEX"; // TODO: IllegalAccessError |
| 1518 | return NULL; |
| 1519 | } |
| 1520 | } |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1521 | dex_cache->SetResolvedClass(class_idx, resolved); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1522 | } else { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1523 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1524 | } |
| 1525 | return resolved; |
| 1526 | } |
| 1527 | |
| 1528 | Method* ResolveMethod(const Class* referrer, uint32_t method_idx, |
| 1529 | /*MethodType*/ int method_type) { |
| 1530 | CHECK(false); |
| 1531 | return NULL; |
| 1532 | } |
| 1533 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1534 | String* ClassLinker::ResolveString(const Class* referring, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1535 | uint32_t string_idx, |
| 1536 | const DexFile& dex_file) { |
| 1537 | const DexFile::StringId& string_id = dex_file.GetStringId(string_idx); |
| 1538 | int32_t utf16_length = dex_file.GetStringLength(string_id); |
| 1539 | const char* utf8_data = dex_file.GetStringData(string_id); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame^] | 1540 | String* string = intern_table_.Intern(utf16_length, utf8_data); |
| 1541 | referring->GetDexCache()->SetResolvedString(string_idx, string); |
| 1542 | return string; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1543 | } |
| 1544 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1545 | } // namespace art |