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