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