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