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