Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "class_linker.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 18 | |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 19 | #include <fcntl.h> |
| 20 | #include <sys/file.h> |
| 21 | #include <sys/stat.h> |
Brian Carlstrom | dbf05b7 | 2011-12-15 00:55:24 -0800 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | #include <sys/wait.h> |
| 24 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 25 | #include <deque> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 26 | #include <string> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 27 | #include <utility> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 28 | #include <vector> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 29 | |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 30 | #include "base/casts.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 31 | #include "base/logging.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 32 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 33 | #include "base/unix_file/fd_file.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 34 | #include "class_linker-inl.h" |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 35 | #include "debugger.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 36 | #include "dex_file-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 37 | #include "gc/accounting/card_table-inl.h" |
| 38 | #include "gc/accounting/heap_bitmap.h" |
| 39 | #include "gc/heap.h" |
| 40 | #include "gc/space/image_space.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 41 | #include "intern_table.h" |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 42 | #include "interpreter/interpreter.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 43 | #include "leb128.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 44 | #include "oat.h" |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 45 | #include "oat_file.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 46 | #include "mirror/art_field-inl.h" |
| 47 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 48 | #include "mirror/class.h" |
| 49 | #include "mirror/class-inl.h" |
| 50 | #include "mirror/class_loader.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 51 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 52 | #include "mirror/iftable-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 53 | #include "mirror/object-inl.h" |
| 54 | #include "mirror/object_array-inl.h" |
| 55 | #include "mirror/proxy.h" |
| 56 | #include "mirror/stack_trace_element.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 57 | #include "object_utils.h" |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 58 | #include "os.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 59 | #include "runtime.h" |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 60 | #include "entrypoints/entrypoint_utils.h" |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 61 | #include "ScopedLocalRef.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 62 | #include "scoped_thread_state_change.h" |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 63 | #include "sirt_ref.h" |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 64 | #include "stack_indirect_reference_table.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 65 | #include "thread.h" |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 66 | #include "UniquePtr.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 67 | #include "utils.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 68 | #include "verifier/method_verifier.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 69 | #include "well_known_classes.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 70 | |
| 71 | namespace art { |
| 72 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 73 | static void ThrowNoClassDefFoundError(const char* fmt, ...) |
| 74 | __attribute__((__format__(__printf__, 1, 2))) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 75 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 76 | static void ThrowNoClassDefFoundError(const char* fmt, ...) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 77 | va_list args; |
| 78 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 79 | Thread* self = Thread::Current(); |
| 80 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 81 | self->ThrowNewExceptionV(throw_location, "Ljava/lang/NoClassDefFoundError;", fmt, args); |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 82 | va_end(args); |
| 83 | } |
| 84 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 85 | static void ThrowEarlierClassFailure(mirror::Class* c) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 86 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 87 | // The class failed to initialize on a previous attempt, so we want to throw |
| 88 | // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we |
| 89 | // failed in verification, in which case v2 5.4.1 says we need to re-throw |
| 90 | // the previous error. |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 91 | if (!Runtime::Current()->IsCompiler()) { // Give info if this occurs at runtime. |
| 92 | LOG(INFO) << "Rejecting re-init on previously-failed class " << PrettyClass(c); |
| 93 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 94 | |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 95 | CHECK(c->IsErroneous()) << PrettyClass(c) << " " << c->GetStatus(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 96 | Thread* self = Thread::Current(); |
| 97 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 98 | if (c->GetVerifyErrorClass() != NULL) { |
| 99 | // TODO: change the verifier to store an _instance_, with a useful detail message? |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 100 | ClassHelper ve_ch(c->GetVerifyErrorClass()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 101 | self->ThrowNewException(throw_location, ve_ch.GetDescriptor(), PrettyDescriptor(c).c_str()); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 102 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 103 | self->ThrowNewException(throw_location, "Ljava/lang/NoClassDefFoundError;", |
| 104 | PrettyDescriptor(c).c_str()); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 108 | static void WrapExceptionInInitializer() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 109 | Thread* self = Thread::Current(); |
| 110 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 111 | |
| 112 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
| 113 | CHECK(cause.get() != NULL); |
| 114 | |
| 115 | env->ExceptionClear(); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 116 | bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error); |
| 117 | env->Throw(cause.get()); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 118 | |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 119 | // We only wrap non-Error exceptions; an Error can just be used as-is. |
| 120 | if (!is_error) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 121 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 122 | self->ThrowNewWrappedException(throw_location, "Ljava/lang/ExceptionInInitializerError;", NULL); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 123 | } |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 126 | static size_t Hash(const char* s) { |
| 127 | // This is the java.lang.String hashcode for convenience, not interoperability. |
| 128 | size_t hash = 0; |
| 129 | for (; *s != '\0'; ++s) { |
| 130 | hash = hash * 31 + *s; |
| 131 | } |
| 132 | return hash; |
| 133 | } |
| 134 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 135 | const char* ClassLinker::class_roots_descriptors_[] = { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 136 | "Ljava/lang/Class;", |
| 137 | "Ljava/lang/Object;", |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 138 | "[Ljava/lang/Class;", |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 139 | "[Ljava/lang/Object;", |
| 140 | "Ljava/lang/String;", |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 141 | "Ljava/lang/DexCache;", |
Elliott Hughes | bf61ba3 | 2011-10-11 10:53:09 -0700 | [diff] [blame] | 142 | "Ljava/lang/ref/Reference;", |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 143 | "Ljava/lang/reflect/ArtField;", |
| 144 | "Ljava/lang/reflect/ArtMethod;", |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 145 | "Ljava/lang/reflect/Proxy;", |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 146 | "[Ljava/lang/String;", |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 147 | "[Ljava/lang/reflect/ArtField;", |
| 148 | "[Ljava/lang/reflect/ArtMethod;", |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 149 | "Ljava/lang/ClassLoader;", |
Ian Rogers | 5167c97 | 2012-02-03 10:41:20 -0800 | [diff] [blame] | 150 | "Ljava/lang/Throwable;", |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 151 | "Ljava/lang/ClassNotFoundException;", |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 152 | "Ljava/lang/StackTraceElement;", |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 153 | "Z", |
| 154 | "B", |
| 155 | "C", |
| 156 | "D", |
| 157 | "F", |
| 158 | "I", |
| 159 | "J", |
| 160 | "S", |
| 161 | "V", |
| 162 | "[Z", |
| 163 | "[B", |
| 164 | "[C", |
| 165 | "[D", |
| 166 | "[F", |
| 167 | "[I", |
| 168 | "[J", |
| 169 | "[S", |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 170 | "[Ljava/lang/StackTraceElement;", |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 171 | }; |
| 172 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 173 | ClassLinker::ClassLinker(InternTable* intern_table) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 174 | // dex_lock_ is recursive as it may be used in stack dumping. |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 175 | : dex_lock_("ClassLinker dex lock", kDefaultMutexLevel), |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 176 | dex_cache_image_class_lookup_required_(false), |
| 177 | failed_dex_cache_class_lookups_(0), |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 178 | class_roots_(NULL), |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 179 | array_iftable_(NULL), |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 180 | init_done_(false), |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 181 | dex_caches_dirty_(false), |
| 182 | class_table_dirty_(false), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 183 | intern_table_(intern_table), |
| 184 | portable_resolution_trampoline_(NULL), |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 185 | quick_resolution_trampoline_(NULL), |
| 186 | portable_imt_conflict_trampoline_(NULL), |
| 187 | quick_imt_conflict_trampoline_(NULL) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 188 | CHECK_EQ(arraysize(class_roots_descriptors_), size_t(kClassRootsMax)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 189 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 190 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 191 | void ClassLinker::InitFromCompiler(const std::vector<const DexFile*>& boot_class_path) { |
| 192 | VLOG(startup) << "ClassLinker::Init"; |
| 193 | CHECK(Runtime::Current()->IsCompiler()); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 194 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 195 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 196 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 197 | // java_lang_Class comes first, it's needed for AllocClass |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 198 | Thread* self = Thread::Current(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 199 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 200 | // The GC can't handle an object with a null class since we can't get the size of this object. |
| 201 | heap->IncrementDisableGC(self); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 202 | SirtRef<mirror::Class> java_lang_Class(self, down_cast<mirror::Class*>( |
| 203 | heap->AllocNonMovableObject<true>(self, nullptr, sizeof(mirror::ClassClass)))); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 204 | CHECK(java_lang_Class.get() != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 205 | mirror::Class::SetClassClass(java_lang_Class.get()); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 206 | java_lang_Class->SetClass(java_lang_Class.get()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 207 | java_lang_Class->SetClassSize(sizeof(mirror::ClassClass)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 208 | heap->DecrementDisableGC(self); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 209 | // AllocClass(mirror::Class*) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 210 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 211 | // Class[] is used for reflection support. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 212 | SirtRef<mirror::Class> class_array_class(self, AllocClass(self, java_lang_Class.get(), sizeof(mirror::Class))); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 213 | class_array_class->SetComponentType(java_lang_Class.get()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 214 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 215 | // java_lang_Object comes next so that object_array_class can be created. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 216 | SirtRef<mirror::Class> java_lang_Object(self, AllocClass(self, java_lang_Class.get(), sizeof(mirror::Class))); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 217 | CHECK(java_lang_Object.get() != NULL); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 218 | // backfill Object as the super class of Class. |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 219 | java_lang_Class->SetSuperClass(java_lang_Object.get()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 220 | java_lang_Object->SetStatus(mirror::Class::kStatusLoaded, self); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 221 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 222 | // Object[] next to hold class roots. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 223 | SirtRef<mirror::Class> object_array_class(self, AllocClass(self, java_lang_Class.get(), sizeof(mirror::Class))); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 224 | object_array_class->SetComponentType(java_lang_Object.get()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 225 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 226 | // Setup the char class to be used for char[]. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 227 | SirtRef<mirror::Class> char_class(self, AllocClass(self, java_lang_Class.get(), sizeof(mirror::Class))); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 228 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 229 | // Setup the char[] class to be used for String. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 230 | SirtRef<mirror::Class> char_array_class(self, AllocClass(self, java_lang_Class.get(), sizeof(mirror::Class))); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 231 | char_array_class->SetComponentType(char_class.get()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 232 | mirror::CharArray::SetArrayClass(char_array_class.get()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 233 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 234 | // Setup String. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 235 | SirtRef<mirror::Class> java_lang_String(self, AllocClass(self, java_lang_Class.get(), sizeof(mirror::StringClass))); |
| 236 | mirror::String::SetClass(java_lang_String.get()); |
| 237 | java_lang_String->SetObjectSize(sizeof(mirror::String)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 238 | java_lang_String->SetStatus(mirror::Class::kStatusResolved, self); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 239 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 240 | // Create storage for root classes, save away our work so far (requires descriptors). |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 241 | class_roots_ = mirror::ObjectArray<mirror::Class>::Alloc(self, object_array_class.get(), |
| 242 | kClassRootsMax); |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 243 | CHECK(class_roots_ != NULL); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 244 | SetClassRoot(kJavaLangClass, java_lang_Class.get()); |
| 245 | SetClassRoot(kJavaLangObject, java_lang_Object.get()); |
| 246 | SetClassRoot(kClassArrayClass, class_array_class.get()); |
| 247 | SetClassRoot(kObjectArrayClass, object_array_class.get()); |
| 248 | SetClassRoot(kCharArrayClass, char_array_class.get()); |
| 249 | SetClassRoot(kJavaLangString, java_lang_String.get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 250 | |
| 251 | // Setup the primitive type classes. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 252 | SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass(self, Primitive::kPrimBoolean)); |
| 253 | SetClassRoot(kPrimitiveByte, CreatePrimitiveClass(self, Primitive::kPrimByte)); |
| 254 | SetClassRoot(kPrimitiveShort, CreatePrimitiveClass(self, Primitive::kPrimShort)); |
| 255 | SetClassRoot(kPrimitiveInt, CreatePrimitiveClass(self, Primitive::kPrimInt)); |
| 256 | SetClassRoot(kPrimitiveLong, CreatePrimitiveClass(self, Primitive::kPrimLong)); |
| 257 | SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass(self, Primitive::kPrimFloat)); |
| 258 | SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass(self, Primitive::kPrimDouble)); |
| 259 | SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass(self, Primitive::kPrimVoid)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 260 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 261 | // Create array interface entries to populate once we can load system classes. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 262 | array_iftable_ = AllocIfTable(self, 2); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 263 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 264 | // Create int array type for AllocDexCache (done in AppendToBootClassPath). |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 265 | SirtRef<mirror::Class> int_array_class(self, AllocClass(self, java_lang_Class.get(), sizeof(mirror::Class))); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 266 | int_array_class->SetComponentType(GetClassRoot(kPrimitiveInt)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 267 | mirror::IntArray::SetArrayClass(int_array_class.get()); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 268 | SetClassRoot(kIntArrayClass, int_array_class.get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 269 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 270 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 271 | |
Ian Rogers | 52813c9 | 2012-10-11 11:50:38 -0700 | [diff] [blame] | 272 | // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 273 | SirtRef<mirror::Class> |
| 274 | java_lang_DexCache(self, AllocClass(self, java_lang_Class.get(), sizeof(mirror::DexCacheClass))); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 275 | SetClassRoot(kJavaLangDexCache, java_lang_DexCache.get()); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 276 | java_lang_DexCache->SetObjectSize(sizeof(mirror::DexCache)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 277 | java_lang_DexCache->SetStatus(mirror::Class::kStatusResolved, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 278 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 279 | // Constructor, Field, Method, and AbstractMethod are necessary so that FindClass can link members. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 280 | SirtRef<mirror::Class> java_lang_reflect_ArtField(self, AllocClass(self, java_lang_Class.get(), |
| 281 | sizeof(mirror::ArtFieldClass))); |
| 282 | CHECK(java_lang_reflect_ArtField.get() != NULL); |
| 283 | java_lang_reflect_ArtField->SetObjectSize(sizeof(mirror::ArtField)); |
| 284 | SetClassRoot(kJavaLangReflectArtField, java_lang_reflect_ArtField.get()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 285 | java_lang_reflect_ArtField->SetStatus(mirror::Class::kStatusResolved, self); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 286 | mirror::ArtField::SetClass(java_lang_reflect_ArtField.get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 287 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 288 | SirtRef<mirror::Class> java_lang_reflect_ArtMethod(self, AllocClass(self, java_lang_Class.get(), |
| 289 | sizeof(mirror::ArtMethodClass))); |
| 290 | CHECK(java_lang_reflect_ArtMethod.get() != NULL); |
| 291 | java_lang_reflect_ArtMethod->SetObjectSize(sizeof(mirror::ArtMethod)); |
| 292 | SetClassRoot(kJavaLangReflectArtMethod, java_lang_reflect_ArtMethod.get()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 293 | java_lang_reflect_ArtMethod->SetStatus(mirror::Class::kStatusResolved, self); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 294 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 295 | mirror::ArtMethod::SetClass(java_lang_reflect_ArtMethod.get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 296 | |
| 297 | // Set up array classes for string, field, method |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 298 | SirtRef<mirror::Class> object_array_string(self, AllocClass(self, java_lang_Class.get(), |
| 299 | sizeof(mirror::Class))); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 300 | object_array_string->SetComponentType(java_lang_String.get()); |
| 301 | SetClassRoot(kJavaLangStringArrayClass, object_array_string.get()); |
| 302 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 303 | SirtRef<mirror::Class> object_array_art_method(self, AllocClass(self, java_lang_Class.get(), |
| 304 | sizeof(mirror::Class))); |
| 305 | object_array_art_method->SetComponentType(java_lang_reflect_ArtMethod.get()); |
| 306 | SetClassRoot(kJavaLangReflectArtMethodArrayClass, object_array_art_method.get()); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 307 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 308 | SirtRef<mirror::Class> object_array_art_field(self, AllocClass(self, java_lang_Class.get(), |
| 309 | sizeof(mirror::Class))); |
| 310 | object_array_art_field->SetComponentType(java_lang_reflect_ArtField.get()); |
| 311 | SetClassRoot(kJavaLangReflectArtFieldArrayClass, object_array_art_field.get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 312 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 313 | // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create |
| 314 | // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses |
| 315 | // these roots. |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 316 | CHECK_NE(0U, boot_class_path.size()); |
| 317 | for (size_t i = 0; i != boot_class_path.size(); ++i) { |
| 318 | const DexFile* dex_file = boot_class_path[i]; |
| 319 | CHECK(dex_file != NULL); |
| 320 | AppendToBootClassPath(*dex_file); |
| 321 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 322 | |
| 323 | // now we can use FindSystemClass |
| 324 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 325 | // run char class through InitializePrimitiveClass to finish init |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 326 | InitializePrimitiveClass(char_class.get(), Primitive::kPrimChar); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 327 | SetClassRoot(kPrimitiveChar, char_class.get()); // needs descriptor |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 328 | |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 329 | // Create runtime resolution and imt conflict methods. Also setup the default imt. |
| 330 | Runtime* runtime = Runtime::Current(); |
| 331 | runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); |
| 332 | runtime->SetImtConflictMethod(runtime->CreateImtConflictMethod()); |
| 333 | runtime->SetDefaultImt(runtime->CreateDefaultImt(this)); |
| 334 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 335 | // Object, String and DexCache need to be rerun through FindSystemClass to finish init |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 336 | java_lang_Object->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 337 | mirror::Class* Object_class = FindSystemClass("Ljava/lang/Object;"); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 338 | CHECK_EQ(java_lang_Object.get(), Object_class); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 339 | CHECK_EQ(java_lang_Object->GetObjectSize(), sizeof(mirror::Object)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 340 | java_lang_String->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 341 | mirror::Class* String_class = FindSystemClass("Ljava/lang/String;"); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 342 | CHECK_EQ(java_lang_String.get(), String_class); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 343 | CHECK_EQ(java_lang_String->GetObjectSize(), sizeof(mirror::String)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 344 | java_lang_DexCache->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 345 | mirror::Class* DexCache_class = FindSystemClass("Ljava/lang/DexCache;"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 346 | CHECK_EQ(java_lang_String.get(), String_class); |
| 347 | CHECK_EQ(java_lang_DexCache.get(), DexCache_class); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 348 | CHECK_EQ(java_lang_DexCache->GetObjectSize(), sizeof(mirror::DexCache)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 349 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 350 | // Setup the primitive array type classes - can't be done until Object has a vtable. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 351 | SetClassRoot(kBooleanArrayClass, FindSystemClass("[Z")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 352 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 353 | |
| 354 | SetClassRoot(kByteArrayClass, FindSystemClass("[B")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 355 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 356 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 357 | mirror::Class* found_char_array_class = FindSystemClass("[C"); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 358 | CHECK_EQ(char_array_class.get(), found_char_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 359 | |
| 360 | SetClassRoot(kShortArrayClass, FindSystemClass("[S")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 361 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 362 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 363 | mirror::Class* found_int_array_class = FindSystemClass("[I"); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 364 | CHECK_EQ(int_array_class.get(), found_int_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 365 | |
| 366 | SetClassRoot(kLongArrayClass, FindSystemClass("[J")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 367 | mirror::LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 368 | |
| 369 | SetClassRoot(kFloatArrayClass, FindSystemClass("[F")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 370 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 371 | |
| 372 | SetClassRoot(kDoubleArrayClass, FindSystemClass("[D")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 373 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 374 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 375 | mirror::Class* found_class_array_class = FindSystemClass("[Ljava/lang/Class;"); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 376 | CHECK_EQ(class_array_class.get(), found_class_array_class); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 377 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 378 | mirror::Class* found_object_array_class = FindSystemClass("[Ljava/lang/Object;"); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 379 | CHECK_EQ(object_array_class.get(), found_object_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 380 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 381 | // Setup the single, global copy of "iftable". |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 382 | mirror::Class* java_lang_Cloneable = FindSystemClass("Ljava/lang/Cloneable;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 383 | CHECK(java_lang_Cloneable != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 384 | mirror::Class* java_io_Serializable = FindSystemClass("Ljava/io/Serializable;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 385 | CHECK(java_io_Serializable != NULL); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 386 | // We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to |
| 387 | // crawl up and explicitly list all of the supers as well. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 388 | array_iftable_->SetInterface(0, java_lang_Cloneable); |
| 389 | array_iftable_->SetInterface(1, java_io_Serializable); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 390 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 391 | // Sanity check Class[] and Object[]'s interfaces. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 392 | ClassHelper kh(class_array_class.get()); |
Ian Rogers | d24e264 | 2012-06-06 21:21:43 -0700 | [diff] [blame] | 393 | CHECK_EQ(java_lang_Cloneable, kh.GetDirectInterface(0)); |
| 394 | CHECK_EQ(java_io_Serializable, kh.GetDirectInterface(1)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 395 | kh.ChangeClass(object_array_class.get()); |
Ian Rogers | d24e264 | 2012-06-06 21:21:43 -0700 | [diff] [blame] | 396 | CHECK_EQ(java_lang_Cloneable, kh.GetDirectInterface(0)); |
| 397 | CHECK_EQ(java_io_Serializable, kh.GetDirectInterface(1)); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 398 | // Run Class, ArtField, and ArtMethod through FindSystemClass. This initializes their |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 399 | // dex_cache_ fields and register them in class_table_. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 400 | mirror::Class* Class_class = FindSystemClass("Ljava/lang/Class;"); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 401 | CHECK_EQ(java_lang_Class.get(), Class_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 402 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 403 | java_lang_reflect_ArtMethod->SetStatus(mirror::Class::kStatusNotReady, self); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 404 | mirror::Class* Art_method_class = FindSystemClass("Ljava/lang/reflect/ArtMethod;"); |
| 405 | CHECK_EQ(java_lang_reflect_ArtMethod.get(), Art_method_class); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 406 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 407 | java_lang_reflect_ArtField->SetStatus(mirror::Class::kStatusNotReady, self); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 408 | mirror::Class* Art_field_class = FindSystemClass("Ljava/lang/reflect/ArtField;"); |
| 409 | CHECK_EQ(java_lang_reflect_ArtField.get(), Art_field_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 410 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 411 | mirror::Class* String_array_class = FindSystemClass(class_roots_descriptors_[kJavaLangStringArrayClass]); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 412 | CHECK_EQ(object_array_string.get(), String_array_class); |
| 413 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 414 | mirror::Class* Art_method_array_class = |
| 415 | FindSystemClass(class_roots_descriptors_[kJavaLangReflectArtMethodArrayClass]); |
| 416 | CHECK_EQ(object_array_art_method.get(), Art_method_array_class); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 417 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 418 | mirror::Class* Art_field_array_class = |
| 419 | FindSystemClass(class_roots_descriptors_[kJavaLangReflectArtFieldArrayClass]); |
| 420 | CHECK_EQ(object_array_art_field.get(), Art_field_array_class); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 421 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 422 | // End of special init trickery, subsequent classes may be loaded via FindSystemClass. |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 423 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 424 | // Create java.lang.reflect.Proxy root. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 425 | mirror::Class* java_lang_reflect_Proxy = FindSystemClass("Ljava/lang/reflect/Proxy;"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 426 | SetClassRoot(kJavaLangReflectProxy, java_lang_reflect_Proxy); |
| 427 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 428 | // java.lang.ref classes need to be specially flagged, but otherwise are normal classes |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 429 | mirror::Class* java_lang_ref_Reference = FindSystemClass("Ljava/lang/ref/Reference;"); |
Elliott Hughes | bf61ba3 | 2011-10-11 10:53:09 -0700 | [diff] [blame] | 430 | SetClassRoot(kJavaLangRefReference, java_lang_ref_Reference); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 431 | mirror::Class* java_lang_ref_FinalizerReference = FindSystemClass("Ljava/lang/ref/FinalizerReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 432 | java_lang_ref_FinalizerReference->SetAccessFlags( |
| 433 | java_lang_ref_FinalizerReference->GetAccessFlags() | |
| 434 | kAccClassIsReference | kAccClassIsFinalizerReference); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 435 | mirror::Class* java_lang_ref_PhantomReference = FindSystemClass("Ljava/lang/ref/PhantomReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 436 | java_lang_ref_PhantomReference->SetAccessFlags( |
| 437 | java_lang_ref_PhantomReference->GetAccessFlags() | |
| 438 | kAccClassIsReference | kAccClassIsPhantomReference); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 439 | mirror::Class* java_lang_ref_SoftReference = FindSystemClass("Ljava/lang/ref/SoftReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 440 | java_lang_ref_SoftReference->SetAccessFlags( |
| 441 | java_lang_ref_SoftReference->GetAccessFlags() | kAccClassIsReference); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 442 | mirror::Class* java_lang_ref_WeakReference = FindSystemClass("Ljava/lang/ref/WeakReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 443 | java_lang_ref_WeakReference->SetAccessFlags( |
| 444 | java_lang_ref_WeakReference->GetAccessFlags() | |
| 445 | kAccClassIsReference | kAccClassIsWeakReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 446 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 447 | // Setup the ClassLoader, verifying the object_size_. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 448 | mirror::Class* java_lang_ClassLoader = FindSystemClass("Ljava/lang/ClassLoader;"); |
| 449 | CHECK_EQ(java_lang_ClassLoader->GetObjectSize(), sizeof(mirror::ClassLoader)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 450 | SetClassRoot(kJavaLangClassLoader, java_lang_ClassLoader); |
| 451 | |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 452 | // Set up java.lang.Throwable, java.lang.ClassNotFoundException, and |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 453 | // java.lang.StackTraceElement as a convenience. |
Ian Rogers | 5167c97 | 2012-02-03 10:41:20 -0800 | [diff] [blame] | 454 | SetClassRoot(kJavaLangThrowable, FindSystemClass("Ljava/lang/Throwable;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 455 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 456 | SetClassRoot(kJavaLangClassNotFoundException, FindSystemClass("Ljava/lang/ClassNotFoundException;")); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 457 | SetClassRoot(kJavaLangStackTraceElement, FindSystemClass("Ljava/lang/StackTraceElement;")); |
| 458 | SetClassRoot(kJavaLangStackTraceElementArrayClass, FindSystemClass("[Ljava/lang/StackTraceElement;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 459 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 460 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 461 | FinishInit(); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 462 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 463 | VLOG(startup) << "ClassLinker::InitFromCompiler exiting"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | void ClassLinker::FinishInit() { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 467 | VLOG(startup) << "ClassLinker::FinishInit entering"; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 468 | |
| 469 | // Let the heap know some key offsets into java.lang.ref instances |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 470 | // Note: we hard code the field indexes here rather than using FindInstanceField |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 471 | // as the types of the field can't be resolved prior to the runtime being |
| 472 | // fully initialized |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 473 | mirror::Class* java_lang_ref_Reference = GetClassRoot(kJavaLangRefReference); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 474 | mirror::Class* java_lang_ref_FinalizerReference = |
| 475 | FindSystemClass("Ljava/lang/ref/FinalizerReference;"); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 476 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 477 | mirror::ArtField* pendingNext = java_lang_ref_Reference->GetInstanceField(0); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 478 | FieldHelper fh(pendingNext); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 479 | CHECK_STREQ(fh.GetName(), "pendingNext"); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 480 | CHECK_STREQ(fh.GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 481 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 482 | mirror::ArtField* queue = java_lang_ref_Reference->GetInstanceField(1); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 483 | fh.ChangeField(queue); |
| 484 | CHECK_STREQ(fh.GetName(), "queue"); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 485 | CHECK_STREQ(fh.GetTypeDescriptor(), "Ljava/lang/ref/ReferenceQueue;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 486 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 487 | mirror::ArtField* queueNext = java_lang_ref_Reference->GetInstanceField(2); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 488 | fh.ChangeField(queueNext); |
| 489 | CHECK_STREQ(fh.GetName(), "queueNext"); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 490 | CHECK_STREQ(fh.GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 491 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 492 | mirror::ArtField* referent = java_lang_ref_Reference->GetInstanceField(3); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 493 | fh.ChangeField(referent); |
| 494 | CHECK_STREQ(fh.GetName(), "referent"); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 495 | CHECK_STREQ(fh.GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 496 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 497 | mirror::ArtField* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 498 | fh.ChangeField(zombie); |
| 499 | CHECK_STREQ(fh.GetName(), "zombie"); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 500 | CHECK_STREQ(fh.GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 501 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 502 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 503 | heap->SetReferenceOffsets(referent->GetOffset(), |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 504 | queue->GetOffset(), |
| 505 | queueNext->GetOffset(), |
| 506 | pendingNext->GetOffset(), |
| 507 | zombie->GetOffset()); |
| 508 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 509 | // ensure all class_roots_ are initialized |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 510 | for (size_t i = 0; i < kClassRootsMax; i++) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 511 | ClassRoot class_root = static_cast<ClassRoot>(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 512 | mirror::Class* klass = GetClassRoot(class_root); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 513 | CHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 514 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != NULL); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 515 | // note SetClassRoot does additional validation. |
| 516 | // if possible add new checks there to catch errors early |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 519 | CHECK(array_iftable_ != NULL); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 520 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 521 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 522 | // that Object, Class, and Object[] are setup |
| 523 | init_done_ = true; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 524 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 525 | VLOG(startup) << "ClassLinker::FinishInit exiting"; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 528 | void ClassLinker::RunRootClinits() { |
| 529 | Thread* self = Thread::Current(); |
| 530 | for (size_t i = 0; i < ClassLinker::kClassRootsMax; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 531 | mirror::Class* c = GetClassRoot(ClassRoot(i)); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 532 | if (!c->IsArrayClass() && !c->IsPrimitive()) { |
Ian Rogers | 0045a29 | 2012-03-31 21:08:41 -0700 | [diff] [blame] | 533 | EnsureInitialized(GetClassRoot(ClassRoot(i)), true, true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 534 | self->AssertNoPendingException(); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 539 | bool ClassLinker::GenerateOatFile(const char* dex_filename, |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 540 | int oat_fd, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 541 | const char* oat_cache_filename) { |
| 542 | Locks::mutator_lock_->AssertNotHeld(Thread::Current()); // Avoid starving GC. |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 543 | std::string dex2oat_string(GetAndroidRoot()); |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 544 | dex2oat_string += (kIsDebugBuild ? "/bin/dex2oatd" : "/bin/dex2oat"); |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 545 | const char* dex2oat = dex2oat_string.c_str(); |
| 546 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 547 | const char* class_path = Runtime::Current()->GetClassPathString().c_str(); |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 548 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 549 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 550 | std::string boot_image_option_string("--boot-image="); |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 551 | boot_image_option_string += heap->GetImageSpace()->GetImageFilename(); |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 552 | const char* boot_image_option = boot_image_option_string.c_str(); |
| 553 | |
| 554 | std::string dex_file_option_string("--dex-file="); |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 555 | dex_file_option_string += dex_filename; |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 556 | const char* dex_file_option = dex_file_option_string.c_str(); |
| 557 | |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 558 | std::string oat_fd_option_string("--oat-fd="); |
Brian Carlstrom | 866c862 | 2012-01-06 16:35:13 -0800 | [diff] [blame] | 559 | StringAppendF(&oat_fd_option_string, "%d", oat_fd); |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 560 | const char* oat_fd_option = oat_fd_option_string.c_str(); |
| 561 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 562 | std::string oat_location_option_string("--oat-location="); |
| 563 | oat_location_option_string += oat_cache_filename; |
| 564 | const char* oat_location_option = oat_location_option_string.c_str(); |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 565 | |
buzbee | a024a06 | 2013-07-31 10:47:37 -0700 | [diff] [blame] | 566 | std::string oat_compiler_filter_string("-compiler-filter:"); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 567 | Runtime::CompilerFilter filter = Runtime::Current()->GetCompilerFilter(); |
| 568 | switch (filter) { |
buzbee | a024a06 | 2013-07-31 10:47:37 -0700 | [diff] [blame] | 569 | case Runtime::kInterpretOnly: |
| 570 | oat_compiler_filter_string += "interpret-only"; |
| 571 | break; |
buzbee | a024a06 | 2013-07-31 10:47:37 -0700 | [diff] [blame] | 572 | case Runtime::kSpace: |
| 573 | oat_compiler_filter_string += "space"; |
| 574 | break; |
| 575 | case Runtime::kBalanced: |
| 576 | oat_compiler_filter_string += "balanced"; |
| 577 | break; |
| 578 | case Runtime::kSpeed: |
| 579 | oat_compiler_filter_string += "speed"; |
| 580 | break; |
buzbee | fe9ca40 | 2013-08-21 09:48:11 -0700 | [diff] [blame] | 581 | case Runtime::kEverything: |
| 582 | oat_compiler_filter_string += "everything"; |
| 583 | break; |
buzbee | a024a06 | 2013-07-31 10:47:37 -0700 | [diff] [blame] | 584 | default: |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 585 | LOG(FATAL) << "Unexpected case: " << filter; |
buzbee | a024a06 | 2013-07-31 10:47:37 -0700 | [diff] [blame] | 586 | } |
| 587 | const char* oat_compiler_filter_option = oat_compiler_filter_string.c_str(); |
| 588 | |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 589 | // fork and exec dex2oat |
| 590 | pid_t pid = fork(); |
| 591 | if (pid == 0) { |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 592 | // no allocation allowed between fork and exec |
Ian Rogers | 725aee5 | 2012-01-11 11:56:56 -0800 | [diff] [blame] | 593 | |
| 594 | // change process groups, so we don't get reaped by ProcessManager |
| 595 | setpgid(0, 0); |
| 596 | |
Brian Carlstrom | b00309f | 2013-09-30 14:23:30 -0700 | [diff] [blame] | 597 | // gLogVerbosity.class_linker = true; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 598 | VLOG(class_linker) << dex2oat |
| 599 | << " --runtime-arg -Xms64m" |
| 600 | << " --runtime-arg -Xmx64m" |
| 601 | << " --runtime-arg -classpath" |
| 602 | << " --runtime-arg " << class_path |
buzbee | a024a06 | 2013-07-31 10:47:37 -0700 | [diff] [blame] | 603 | << " --runtime-arg " << oat_compiler_filter_option |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 604 | #if !defined(ART_TARGET) |
| 605 | << " --host" |
| 606 | #endif |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 607 | << " " << boot_image_option |
| 608 | << " " << dex_file_option |
| 609 | << " " << oat_fd_option |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 610 | << " " << oat_location_option; |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 611 | |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 612 | execl(dex2oat, dex2oat, |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 613 | "--runtime-arg", "-Xms64m", |
| 614 | "--runtime-arg", "-Xmx64m", |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 615 | "--runtime-arg", "-classpath", |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 616 | "--runtime-arg", class_path, |
buzbee | a024a06 | 2013-07-31 10:47:37 -0700 | [diff] [blame] | 617 | "--runtime-arg", oat_compiler_filter_option, |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 618 | #if !defined(ART_TARGET) |
| 619 | "--host", |
| 620 | #endif |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 621 | boot_image_option, |
| 622 | dex_file_option, |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 623 | oat_fd_option, |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 624 | oat_location_option, |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 625 | NULL); |
| 626 | |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 627 | PLOG(FATAL) << "execl(" << dex2oat << ") failed"; |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 628 | return false; |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 629 | } else { |
| 630 | // wait for dex2oat to finish |
| 631 | int status; |
| 632 | pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 633 | if (got_pid != pid) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 634 | ScopedObjectAccess soa(Thread::Current()); |
| 635 | ThrowIOException("Failed to create oat file. Waitpid failed: wanted %d, got %d", pid, |
| 636 | got_pid); |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 637 | return false; |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 638 | } |
| 639 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 640 | ScopedObjectAccess soa(Thread::Current()); |
| 641 | ThrowIOException("Failed to create oat file. %s failed with dex-file '%s'", dex2oat, |
| 642 | dex_filename); |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 643 | return false; |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 644 | } |
| 645 | } |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 646 | return true; |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 647 | } |
| 648 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 649 | const OatFile* ClassLinker::RegisterOatFile(const OatFile* oat_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 650 | WriterMutexLock mu(Thread::Current(), dex_lock_); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 651 | for (size_t i = 0; i < oat_files_.size(); ++i) { |
| 652 | if (UNLIKELY(oat_file->GetLocation() == oat_files_[i]->GetLocation())) { |
| 653 | VLOG(class_linker) << "Attempt to register oat file that's already registered: " |
| 654 | << oat_file->GetLocation(); |
| 655 | for (size_t j = i; j < oat_files_.size(); ++j) { |
| 656 | CHECK_NE(oat_file, oat_files_[j]) << "Attempt to re-register dex file."; |
| 657 | } |
| 658 | delete oat_file; |
| 659 | return oat_files_[i]; |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 660 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 661 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 662 | VLOG(class_linker) << "Registering " << oat_file->GetLocation(); |
| 663 | oat_files_.push_back(oat_file); |
| 664 | return oat_file; |
Brian Carlstrom | 866c862 | 2012-01-06 16:35:13 -0800 | [diff] [blame] | 665 | } |
| 666 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 667 | OatFile& ClassLinker::GetImageOatFile(gc::space::ImageSpace* space) { |
| 668 | VLOG(startup) << "ClassLinker::GetImageOatFile entering"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 669 | OatFile* oat_file = space->ReleaseOatFile(); |
| 670 | CHECK_EQ(RegisterOatFile(oat_file), oat_file); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 671 | VLOG(startup) << "ClassLinker::GetImageOatFile exiting"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 672 | return *oat_file; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 675 | const OatFile* ClassLinker::FindOpenedOatFileForDexFile(const DexFile& dex_file) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 676 | return FindOpenedOatFileFromDexLocation(dex_file.GetLocation().c_str(), |
| 677 | dex_file.GetLocationChecksum()); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 678 | } |
| 679 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 680 | const OatFile* ClassLinker::FindOpenedOatFileFromDexLocation(const char* dex_location, |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 681 | uint32_t dex_location_checksum) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 682 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 683 | for (size_t i = 0; i < oat_files_.size(); i++) { |
| 684 | const OatFile* oat_file = oat_files_[i]; |
| 685 | DCHECK(oat_file != NULL); |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 686 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, |
| 687 | &dex_location_checksum, |
| 688 | false); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 689 | if (oat_dex_file != NULL) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 690 | return oat_file; |
| 691 | } |
| 692 | } |
| 693 | return NULL; |
| 694 | } |
| 695 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 696 | const DexFile* ClassLinker::FindDexFileInOatLocation(const char* dex_location, |
Ian Rogers | e3359f7 | 2013-06-11 15:14:11 -0700 | [diff] [blame] | 697 | uint32_t dex_location_checksum, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 698 | const char* oat_location, |
| 699 | std::string* error_msg) { |
Brian Carlstrom | f1d3455 | 2013-07-12 20:22:23 -0700 | [diff] [blame] | 700 | UniquePtr<OatFile> oat_file(OatFile::Open(oat_location, oat_location, NULL, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 701 | !Runtime::Current()->IsCompiler(), |
| 702 | error_msg)); |
| 703 | if (oat_file.get() == nullptr) { |
| 704 | *error_msg = StringPrintf("Failed to find existing oat file at %s: %s", oat_location, |
| 705 | error_msg->c_str()); |
| 706 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 707 | } |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 708 | Runtime* runtime = Runtime::Current(); |
| 709 | const ImageHeader& image_header = runtime->GetHeap()->GetImageSpace()->GetImageHeader(); |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 710 | uint32_t expected_image_oat_checksum = image_header.GetOatChecksum(); |
| 711 | uint32_t actual_image_oat_checksum = oat_file->GetOatHeader().GetImageFileLocationOatChecksum(); |
| 712 | if (expected_image_oat_checksum != actual_image_oat_checksum) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 713 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected image oat checksum of " |
| 714 | "0x%x, found 0x%x", oat_location, expected_image_oat_checksum, |
| 715 | actual_image_oat_checksum); |
| 716 | return nullptr; |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 717 | } |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 718 | |
| 719 | uint32_t expected_image_oat_offset = reinterpret_cast<uint32_t>(image_header.GetOatDataBegin()); |
| 720 | uint32_t actual_image_oat_offset = oat_file->GetOatHeader().GetImageFileLocationOatDataBegin(); |
| 721 | if (expected_image_oat_offset != actual_image_oat_offset) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 722 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected image oat offset %ud, " |
| 723 | "found %ud", oat_location, expected_image_oat_offset, |
| 724 | actual_image_oat_offset); |
| 725 | return nullptr; |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 726 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 727 | // TODO: this registers the oat file now as we may use the oat_dex_file later and we want the |
| 728 | // intern behavior of RegisterOatFile. However, if we take an early return we could remove |
| 729 | // the oat file. |
| 730 | const OatFile* opened_oat_file = RegisterOatFile(oat_file.release()); |
| 731 | const OatFile::OatDexFile* oat_dex_file = opened_oat_file->GetOatDexFile(dex_location, |
| 732 | &dex_location_checksum); |
| 733 | if (oat_dex_file == nullptr) { |
| 734 | *error_msg = StringPrintf("Failed to find oat file at '%s' containing '%s'", oat_location, |
| 735 | dex_location); |
| 736 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 737 | } |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 738 | uint32_t expected_dex_checksum = dex_location_checksum; |
| 739 | uint32_t actual_dex_checksum = oat_dex_file->GetDexFileLocationChecksum(); |
| 740 | if (expected_dex_checksum != actual_dex_checksum) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 741 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected dex checksum of 0x%x, " |
| 742 | "found 0x%x", oat_location, expected_dex_checksum, |
| 743 | actual_dex_checksum); |
| 744 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 745 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 746 | return oat_dex_file->OpenDexFile(error_msg); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 749 | class ScopedFlock { |
| 750 | public: |
| 751 | ScopedFlock() {} |
| 752 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 753 | bool Init(const char* filename, std::string* error_msg) { |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 754 | while (true) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 755 | file_.reset(OS::OpenFileWithFlags(filename, O_CREAT | O_RDWR)); |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 756 | if (file_.get() == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 757 | *error_msg = StringPrintf("Failed to open file '%s'", filename); |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 758 | return false; |
| 759 | } |
| 760 | int flock_result = TEMP_FAILURE_RETRY(flock(file_->Fd(), LOCK_EX)); |
| 761 | if (flock_result != 0) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 762 | *error_msg = StringPrintf("Failed to lock file '%s': %s", filename, strerror(errno)); |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 763 | return false; |
| 764 | } |
| 765 | struct stat fstat_stat; |
| 766 | int fstat_result = TEMP_FAILURE_RETRY(fstat(file_->Fd(), &fstat_stat)); |
| 767 | if (fstat_result != 0) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 768 | *error_msg = StringPrintf("Failed to fstat file '%s': %s", filename, strerror(errno)); |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 769 | return false; |
| 770 | } |
| 771 | struct stat stat_stat; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 772 | int stat_result = TEMP_FAILURE_RETRY(stat(filename, &stat_stat)); |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 773 | if (stat_result != 0) { |
| 774 | PLOG(WARNING) << "Failed to stat, will retry: " << filename; |
| 775 | // ENOENT can happen if someone racing with us unlinks the file we created so just retry. |
| 776 | continue; |
| 777 | } |
| 778 | if (fstat_stat.st_dev != stat_stat.st_dev || fstat_stat.st_ino != stat_stat.st_ino) { |
| 779 | LOG(WARNING) << "File changed while locking, will retry: " << filename; |
| 780 | continue; |
| 781 | } |
| 782 | return true; |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | File& GetFile() { |
| 787 | return *file_; |
| 788 | } |
| 789 | |
| 790 | ~ScopedFlock() { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 791 | if (file_.get() != NULL) { |
| 792 | int flock_result = TEMP_FAILURE_RETRY(flock(file_->Fd(), LOCK_UN)); |
| 793 | CHECK_EQ(0, flock_result); |
| 794 | } |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | private: |
| 798 | UniquePtr<File> file_; |
| 799 | |
| 800 | DISALLOW_COPY_AND_ASSIGN(ScopedFlock); |
| 801 | }; |
| 802 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 803 | const DexFile* ClassLinker::FindOrCreateOatFileForDexLocation(const char* dex_location, |
| 804 | uint32_t dex_location_checksum, |
| 805 | const char* oat_location, |
| 806 | std::string* error_msg) { |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 807 | // We play a locking game here so that if two different processes |
| 808 | // race to generate (or worse, one tries to open a partial generated |
| 809 | // file) we will be okay. This is actually common with apps that use |
| 810 | // DexClassLoader to work around the dex method reference limit and |
| 811 | // that have a background service running in a separate process. |
| 812 | ScopedFlock scoped_flock; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 813 | if (!scoped_flock.Init(oat_location, error_msg)) { |
| 814 | return nullptr; |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 817 | // Check if we already have an up-to-date output file |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 818 | const DexFile* dex_file = FindDexFileInOatLocation(dex_location, dex_location_checksum, |
| 819 | oat_location, error_msg); |
| 820 | if (dex_file != nullptr) { |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 821 | return dex_file; |
| 822 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 823 | VLOG(class_linker) << "Failed to find dex file '" << dex_location << "' in oat location '" |
| 824 | << oat_location << "': " << *error_msg; |
| 825 | error_msg->clear(); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 826 | |
| 827 | // Generate the output oat file for the dex file |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 828 | VLOG(class_linker) << "Generating oat file " << oat_location << " for " << dex_location; |
| 829 | if (!GenerateOatFile(dex_location, scoped_flock.GetFile().Fd(), oat_location)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 830 | CHECK(Thread::Current()->IsExceptionPending()); |
| 831 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 832 | } |
Brian Carlstrom | f1d3455 | 2013-07-12 20:22:23 -0700 | [diff] [blame] | 833 | const OatFile* oat_file = OatFile::Open(oat_location, oat_location, NULL, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 834 | !Runtime::Current()->IsCompiler(), |
| 835 | error_msg); |
| 836 | if (oat_file == nullptr) { |
| 837 | *error_msg = StringPrintf("Failed to open generated oat file '%s': %s", |
| 838 | oat_location, error_msg->c_str()); |
| 839 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 840 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 841 | oat_file = RegisterOatFile(oat_file); |
| 842 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, |
| 843 | &dex_location_checksum); |
| 844 | if (oat_dex_file == nullptr) { |
| 845 | *error_msg = StringPrintf("Failed to find dex file '%s' (checksum 0x%x) in generated out file " |
| 846 | "'%s'", dex_location, dex_location_checksum, oat_location); |
| 847 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 848 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 849 | const DexFile* result = oat_dex_file->OpenDexFile(error_msg); |
| 850 | CHECK(result != nullptr) << *error_msg; |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 851 | CHECK_EQ(dex_location_checksum, result->GetLocationChecksum()) |
| 852 | << "dex_location=" << dex_location << " oat_location=" << oat_location << std::hex |
| 853 | << " dex_location_checksum=" << dex_location_checksum |
Brian Carlstrom | b00309f | 2013-09-30 14:23:30 -0700 | [diff] [blame] | 854 | << " DexFile::GetLocationChecksum()=" << result->GetLocationChecksum(); |
| 855 | return result; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 856 | } |
| 857 | |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 858 | bool ClassLinker::VerifyOatFileChecksums(const OatFile* oat_file, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 859 | const char* dex_location, |
| 860 | uint32_t dex_location_checksum, |
| 861 | std::string* error_msg) { |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 862 | Runtime* runtime = Runtime::Current(); |
| 863 | const ImageHeader& image_header = runtime->GetHeap()->GetImageSpace()->GetImageHeader(); |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 864 | uint32_t image_oat_checksum = image_header.GetOatChecksum(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 865 | uint32_t image_oat_data_begin = reinterpret_cast<uint32_t>(image_header.GetOatDataBegin()); |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 866 | bool image_check = ((oat_file->GetOatHeader().GetImageFileLocationOatChecksum() == image_oat_checksum) |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 867 | && (oat_file->GetOatHeader().GetImageFileLocationOatDataBegin() == image_oat_data_begin)); |
Brian Carlstrom | 46b8a62 | 2012-06-19 23:13:22 -0700 | [diff] [blame] | 868 | |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 869 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, &dex_location_checksum); |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 870 | if (oat_dex_file == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 871 | *error_msg = StringPrintf("oat file '%s' does not contain contents for '%s' with checksum 0x%x", |
| 872 | oat_file->GetLocation().c_str(), dex_location, dex_location_checksum); |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 873 | std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file->GetOatDexFiles(); |
| 874 | for (size_t i = 0; i < oat_dex_files.size(); i++) { |
| 875 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i]; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 876 | *error_msg += StringPrintf("\noat file '%s' contains contents for '%s'", |
| 877 | oat_file->GetLocation().c_str(), |
| 878 | oat_dex_file->GetDexFileLocation().c_str()); |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 879 | } |
| 880 | return false; |
| 881 | } |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 882 | bool dex_check = dex_location_checksum == oat_dex_file->GetDexFileLocationChecksum(); |
Brian Carlstrom | 46b8a62 | 2012-06-19 23:13:22 -0700 | [diff] [blame] | 883 | |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 884 | if (image_check && dex_check) { |
| 885 | return true; |
| 886 | } |
Brian Carlstrom | 46b8a62 | 2012-06-19 23:13:22 -0700 | [diff] [blame] | 887 | |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 888 | if (!image_check) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 889 | ScopedObjectAccess soa(Thread::Current()); |
| 890 | mirror::String* oat_location = image_header.GetImageRoot(ImageHeader::kOatLocation)->AsString(); |
| 891 | std::string image_file(oat_location->ToModifiedUtf8()); |
| 892 | *error_msg = StringPrintf("oat file '%s' mismatch (0x%x, %d) with '%s' (0x%x, %d)", |
| 893 | oat_file->GetLocation().c_str(), |
| 894 | oat_file->GetOatHeader().GetImageFileLocationOatChecksum(), |
| 895 | oat_file->GetOatHeader().GetImageFileLocationOatDataBegin(), |
| 896 | image_file.c_str(), image_oat_checksum, image_oat_data_begin); |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 897 | } |
| 898 | if (!dex_check) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 899 | *error_msg = StringPrintf("oat file '%s' mismatch (0x%x) with '%s' (0x%x)", |
| 900 | oat_file->GetLocation().c_str(), |
| 901 | oat_dex_file->GetDexFileLocationChecksum(), |
| 902 | dex_location, dex_location_checksum); |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 903 | } |
| 904 | return false; |
| 905 | } |
| 906 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 907 | const DexFile* ClassLinker::VerifyAndOpenDexFileFromOatFile(const std::string& oat_file_location, |
| 908 | const char* dex_location, |
| 909 | std::string* error_msg, |
| 910 | bool* open_failed) { |
| 911 | UniquePtr<const OatFile> oat_file(FindOatFileFromOatLocation(oat_file_location, error_msg)); |
| 912 | if (oat_file.get() == nullptr) { |
| 913 | *open_failed = true; |
| 914 | return nullptr; |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 915 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 916 | *open_failed = false; |
| 917 | uint32_t dex_location_checksum; |
| 918 | if (!DexFile::GetChecksum(dex_location, &dex_location_checksum, error_msg)) { |
| 919 | // If no classes.dex found in dex_location, it has been stripped or is corrupt, assume oat is |
| 920 | // up-to-date. This is the common case in user builds for jar's and apk's in the /system |
| 921 | // directory. |
| 922 | const OatFile* opened_oat_file = oat_file.release(); |
| 923 | opened_oat_file = RegisterOatFile(opened_oat_file); |
| 924 | const OatFile::OatDexFile* oat_dex_file = opened_oat_file->GetOatDexFile(dex_location, NULL); |
| 925 | if (oat_dex_file == nullptr) { |
| 926 | *error_msg = StringPrintf("Dex checksum mismatch for location '%s' and failed to find oat " |
| 927 | "dex file '%s': %s", oat_file_location.c_str(), dex_location, |
| 928 | error_msg->c_str()); |
| 929 | return nullptr; |
| 930 | } |
| 931 | return oat_dex_file->OpenDexFile(error_msg); |
| 932 | } |
| 933 | |
| 934 | bool verified = VerifyOatFileChecksums(oat_file.get(), dex_location, dex_location_checksum, |
| 935 | error_msg); |
| 936 | if (!verified) { |
| 937 | return nullptr; |
| 938 | } |
| 939 | const OatFile* opened_oat_file = oat_file.release(); |
| 940 | opened_oat_file = RegisterOatFile(opened_oat_file); |
| 941 | return opened_oat_file->GetOatDexFile(dex_location, |
| 942 | &dex_location_checksum)->OpenDexFile(error_msg); |
Brian Carlstrom | 46b8a62 | 2012-06-19 23:13:22 -0700 | [diff] [blame] | 943 | } |
| 944 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 945 | const DexFile* ClassLinker::FindDexFileInOatFileFromDexLocation(const char* dex_location, |
| 946 | uint32_t dex_location_checksum, |
| 947 | std::string* error_msg) { |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 948 | const OatFile* open_oat_file = FindOpenedOatFileFromDexLocation(dex_location, |
| 949 | dex_location_checksum); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 950 | if (open_oat_file != nullptr) { |
| 951 | const OatFile::OatDexFile* oat_dex_file = open_oat_file->GetOatDexFile(dex_location, |
| 952 | &dex_location_checksum); |
| 953 | return oat_dex_file->OpenDexFile(error_msg); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 954 | } |
| 955 | |
Brian Carlstrom | 46b8a62 | 2012-06-19 23:13:22 -0700 | [diff] [blame] | 956 | // Look for an existing file next to dex. for example, for |
Brian Carlstrom | 30e2ea4 | 2013-06-19 23:25:37 -0700 | [diff] [blame] | 957 | // /foo/bar/baz.jar, look for /foo/bar/baz.odex. |
| 958 | std::string odex_filename(OatFile::DexFilenameToOdexFilename(dex_location)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 959 | bool open_failed; |
| 960 | const DexFile* dex_file = VerifyAndOpenDexFileFromOatFile(odex_filename, dex_location, |
| 961 | error_msg, &open_failed); |
| 962 | if (dex_file != nullptr) { |
| 963 | return dex_file; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 964 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 965 | std::string cache_error_msg; |
Brian Carlstrom | 30e2ea4 | 2013-06-19 23:25:37 -0700 | [diff] [blame] | 966 | std::string cache_location(GetDalvikCacheFilenameOrDie(dex_location)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 967 | dex_file = VerifyAndOpenDexFileFromOatFile(cache_location, dex_location, &cache_error_msg, |
| 968 | &open_failed); |
| 969 | if (dex_file != nullptr) { |
| 970 | return dex_file; |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 971 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 972 | if (!open_failed && TEMP_FAILURE_RETRY(unlink(cache_location.c_str())) != 0) { |
| 973 | PLOG(FATAL) << "Failed to remove obsolete oat file from " << cache_location; |
| 974 | } |
| 975 | VLOG(class_linker) << "Failed to open oat file from " << odex_filename |
| 976 | << " (error '" << *error_msg << "') or " << cache_location |
| 977 | << " (error '" << cache_error_msg << "')."; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 978 | |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 979 | // Try to generate oat file if it wasn't found or was obsolete. |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 980 | error_msg->clear(); |
| 981 | return FindOrCreateOatFileForDexLocation(dex_location, dex_location_checksum, |
| 982 | cache_location.c_str(), error_msg); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 983 | } |
| 984 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 985 | const OatFile* ClassLinker::FindOpenedOatFileFromOatLocation(const std::string& oat_location) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 986 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 987 | for (size_t i = 0; i < oat_files_.size(); i++) { |
| 988 | const OatFile* oat_file = oat_files_[i]; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 989 | DCHECK(oat_file != nullptr); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 990 | if (oat_file->GetLocation() == oat_location) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 991 | return oat_file; |
| 992 | } |
| 993 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 994 | return nullptr; |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 995 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 996 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 997 | const OatFile* ClassLinker::FindOatFileFromOatLocation(const std::string& oat_location, |
| 998 | std::string* error_msg) { |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 999 | const OatFile* oat_file = FindOpenedOatFileFromOatLocation(oat_location); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1000 | if (oat_file != nullptr) { |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 1001 | return oat_file; |
| 1002 | } |
| 1003 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1004 | oat_file = OatFile::Open(oat_location, oat_location, NULL, !Runtime::Current()->IsCompiler(), |
| 1005 | error_msg); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1006 | if (oat_file == NULL) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 1007 | return NULL; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1008 | } |
| 1009 | return oat_file; |
| 1010 | } |
| 1011 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1012 | static void InitFromImageInterpretOnlyCallback(mirror::Object* obj, void* arg) |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1013 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1014 | ClassLinker* class_linker = reinterpret_cast<ClassLinker*>(arg); |
| 1015 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1016 | DCHECK(obj != NULL); |
| 1017 | DCHECK(class_linker != NULL); |
| 1018 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1019 | if (obj->IsArtMethod()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1020 | mirror::ArtMethod* method = obj->AsArtMethod(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1021 | if (!method->IsNative()) { |
| 1022 | method->SetEntryPointFromInterpreter(interpreter::artInterpreterToInterpreterBridge); |
| 1023 | if (method != Runtime::Current()->GetResolutionMethod()) { |
| 1024 | method->SetEntryPointFromCompiledCode(GetCompiledCodeToInterpreterBridge()); |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1030 | void ClassLinker::InitFromImage() { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1031 | VLOG(startup) << "ClassLinker::InitFromImage entering"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1032 | CHECK(!init_done_); |
| 1033 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1034 | Thread* self = Thread::Current(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1035 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 1036 | gc::space::ImageSpace* space = heap->GetImageSpace(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1037 | dex_cache_image_class_lookup_required_ = true; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1038 | CHECK(space != NULL); |
| 1039 | OatFile& oat_file = GetImageOatFile(space); |
| 1040 | CHECK_EQ(oat_file.GetOatHeader().GetImageFileLocationOatChecksum(), 0U); |
| 1041 | CHECK_EQ(oat_file.GetOatHeader().GetImageFileLocationOatDataBegin(), 0U); |
| 1042 | CHECK(oat_file.GetOatHeader().GetImageFileLocation().empty()); |
| 1043 | portable_resolution_trampoline_ = oat_file.GetOatHeader().GetPortableResolutionTrampoline(); |
| 1044 | quick_resolution_trampoline_ = oat_file.GetOatHeader().GetQuickResolutionTrampoline(); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1045 | portable_imt_conflict_trampoline_ = oat_file.GetOatHeader().GetPortableImtConflictTrampoline(); |
| 1046 | quick_imt_conflict_trampoline_ = oat_file.GetOatHeader().GetQuickImtConflictTrampoline(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1047 | mirror::Object* dex_caches_object = space->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
| 1048 | mirror::ObjectArray<mirror::DexCache>* dex_caches = |
| 1049 | dex_caches_object->AsObjectArray<mirror::DexCache>(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1050 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1051 | SirtRef<mirror::ObjectArray<mirror::Class> > class_roots( |
| 1052 | self, |
| 1053 | space->GetImageHeader().GetImageRoot(ImageHeader::kClassRoots)->AsObjectArray<mirror::Class>()); |
| 1054 | class_roots_ = class_roots.get(); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1055 | |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1056 | // Special case of setting up the String class early so that we can test arbitrary objects |
| 1057 | // as being Strings or not |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 1058 | mirror::String::SetClass(GetClassRoot(kJavaLangString)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1059 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1060 | CHECK_EQ(oat_file.GetOatHeader().GetDexFileCount(), |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1061 | static_cast<uint32_t>(dex_caches->GetLength())); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1062 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1063 | SirtRef<mirror::DexCache> dex_cache(self, dex_caches->Get(i)); |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1064 | const std::string& dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1065 | const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(dex_file_location.c_str(), |
| 1066 | nullptr); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1067 | CHECK(oat_dex_file != NULL) << oat_file.GetLocation() << " " << dex_file_location; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1068 | std::string error_msg; |
| 1069 | const DexFile* dex_file = oat_dex_file->OpenDexFile(&error_msg); |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1070 | if (dex_file == NULL) { |
| 1071 | LOG(FATAL) << "Failed to open dex file " << dex_file_location |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1072 | << " from within oat file " << oat_file.GetLocation() |
| 1073 | << " error '" << error_msg << "'"; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1074 | } |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1075 | |
| 1076 | CHECK_EQ(dex_file->GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum()); |
| 1077 | |
| 1078 | AppendToBootClassPath(*dex_file, dex_cache); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 1081 | // Set classes on AbstractMethod early so that IsMethod tests can be performed during the live |
| 1082 | // bitmap walk. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1083 | mirror::ArtMethod::SetClass(GetClassRoot(kJavaLangReflectArtMethod)); |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 1084 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1085 | // Set entry point to interpreter if in InterpretOnly mode. |
| 1086 | if (Runtime::Current()->GetInstrumentation()->InterpretOnly()) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1087 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1088 | heap->VisitObjects(InitFromImageInterpretOnlyCallback, this); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1089 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1090 | |
| 1091 | // reinit class_roots_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1092 | mirror::Class::SetClassClass(class_roots->Get(kJavaLangClass)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1093 | class_roots_ = class_roots.get(); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1094 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1095 | // reinit array_iftable_ from any array class instance, they should be == |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 1096 | array_iftable_ = GetClassRoot(kObjectArrayClass)->GetIfTable(); |
| 1097 | DCHECK(array_iftable_ == GetClassRoot(kBooleanArrayClass)->GetIfTable()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1098 | // String class root was set above |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1099 | mirror::ArtField::SetClass(GetClassRoot(kJavaLangReflectArtField)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1100 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
| 1101 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
| 1102 | mirror::CharArray::SetArrayClass(GetClassRoot(kCharArrayClass)); |
| 1103 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
| 1104 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
| 1105 | mirror::IntArray::SetArrayClass(GetClassRoot(kIntArrayClass)); |
| 1106 | mirror::LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
| 1107 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
| 1108 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
| 1109 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1110 | |
| 1111 | FinishInit(); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1112 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1113 | VLOG(startup) << "ClassLinker::InitFromImage exiting"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1116 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 1117 | // reinit references to when reinitializing a ClassLinker from a |
| 1118 | // mapped image. |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 1119 | void ClassLinker::VisitRoots(RootVisitor* visitor, void* arg, bool only_dirty, bool clean_dirty) { |
Mathieu Chartier | b307052 | 2013-09-17 14:18:21 -0700 | [diff] [blame] | 1120 | class_roots_ = down_cast<mirror::ObjectArray<mirror::Class>*>(visitor(class_roots_, arg)); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1121 | Thread* self = Thread::Current(); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1122 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 1123 | ReaderMutexLock mu(self, dex_lock_); |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 1124 | if (!only_dirty || dex_caches_dirty_) { |
Mathieu Chartier | b307052 | 2013-09-17 14:18:21 -0700 | [diff] [blame] | 1125 | for (mirror::DexCache*& dex_cache : dex_caches_) { |
| 1126 | dex_cache = down_cast<mirror::DexCache*>(visitor(dex_cache, arg)); |
| 1127 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 1128 | } |
| 1129 | if (clean_dirty) { |
| 1130 | dex_caches_dirty_ = false; |
| 1131 | } |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1132 | } |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 1135 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 1136 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 1137 | if (!only_dirty || class_table_dirty_) { |
Mathieu Chartier | b307052 | 2013-09-17 14:18:21 -0700 | [diff] [blame] | 1138 | for (std::pair<const size_t, mirror::Class*>& it : class_table_) { |
| 1139 | it.second = down_cast<mirror::Class*>(visitor(it.second, arg)); |
| 1140 | DCHECK(it.second != nullptr); |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 1141 | } |
| 1142 | if (clean_dirty) { |
| 1143 | class_table_dirty_ = false; |
| 1144 | } |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 1145 | } |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 1146 | |
| 1147 | // We deliberately ignore the class roots in the image since we |
| 1148 | // handle image roots by using the MS/CMS rescanning of dirty cards. |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1149 | } |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 1150 | |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 1151 | array_iftable_ = reinterpret_cast<mirror::IfTable*>(visitor(array_iftable_, arg)); |
| 1152 | DCHECK(array_iftable_ != nullptr); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1155 | void ClassLinker::VisitClasses(ClassVisitor* visitor, void* arg) { |
| 1156 | if (dex_cache_image_class_lookup_required_) { |
| 1157 | MoveImageClassesToClassTable(); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1158 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1159 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 1160 | for (const std::pair<size_t, mirror::Class*>& it : class_table_) { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1161 | if (!visitor(it.second, arg)) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1162 | return; |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1167 | static bool GetClassesVisitor(mirror::Class* c, void* arg) { |
| 1168 | std::set<mirror::Class*>* classes = reinterpret_cast<std::set<mirror::Class*>*>(arg); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1169 | classes->insert(c); |
| 1170 | return true; |
| 1171 | } |
| 1172 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1173 | void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor, void* arg) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1174 | std::set<mirror::Class*> classes; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1175 | VisitClasses(GetClassesVisitor, &classes); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1176 | for (mirror::Class* klass : classes) { |
| 1177 | if (!visitor(klass, arg)) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1178 | return; |
| 1179 | } |
| 1180 | } |
| 1181 | } |
| 1182 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1183 | ClassLinker::~ClassLinker() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1184 | mirror::Class::ResetClass(); |
| 1185 | mirror::String::ResetClass(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1186 | mirror::ArtField::ResetClass(); |
| 1187 | mirror::ArtMethod::ResetClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1188 | mirror::BooleanArray::ResetArrayClass(); |
| 1189 | mirror::ByteArray::ResetArrayClass(); |
| 1190 | mirror::CharArray::ResetArrayClass(); |
| 1191 | mirror::DoubleArray::ResetArrayClass(); |
| 1192 | mirror::FloatArray::ResetArrayClass(); |
| 1193 | mirror::IntArray::ResetArrayClass(); |
| 1194 | mirror::LongArray::ResetArrayClass(); |
| 1195 | mirror::ShortArray::ResetArrayClass(); |
| 1196 | mirror::Throwable::ResetClass(); |
| 1197 | mirror::StackTraceElement::ResetClass(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1198 | STLDeleteElements(&boot_class_path_); |
| 1199 | STLDeleteElements(&oat_files_); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1202 | mirror::DexCache* ClassLinker::AllocDexCache(Thread* self, const DexFile& dex_file) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1203 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1204 | SirtRef<mirror::Class> dex_cache_class(self, GetClassRoot(kJavaLangDexCache)); |
| 1205 | SirtRef<mirror::DexCache> dex_cache( |
| 1206 | self, down_cast<mirror::DexCache*>( |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1207 | heap->AllocObject<true>(self, dex_cache_class.get(), dex_cache_class->GetObjectSize()))); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1208 | if (dex_cache.get() == NULL) { |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1209 | return NULL; |
| 1210 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1211 | SirtRef<mirror::String> |
| 1212 | location(self, intern_table_->InternStrong(dex_file.GetLocation().c_str())); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1213 | if (location.get() == NULL) { |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1214 | return NULL; |
| 1215 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1216 | SirtRef<mirror::ObjectArray<mirror::String> > |
| 1217 | strings(self, AllocStringArray(self, dex_file.NumStringIds())); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1218 | if (strings.get() == NULL) { |
| 1219 | return NULL; |
| 1220 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1221 | SirtRef<mirror::ObjectArray<mirror::Class> > |
| 1222 | types(self, AllocClassArray(self, dex_file.NumTypeIds())); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1223 | if (types.get() == NULL) { |
| 1224 | return NULL; |
| 1225 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1226 | SirtRef<mirror::ObjectArray<mirror::ArtMethod> > |
| 1227 | methods(self, AllocArtMethodArray(self, dex_file.NumMethodIds())); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1228 | if (methods.get() == NULL) { |
| 1229 | return NULL; |
| 1230 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1231 | SirtRef<mirror::ObjectArray<mirror::ArtField> > |
| 1232 | fields(self, AllocArtFieldArray(self, dex_file.NumFieldIds())); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1233 | if (fields.get() == NULL) { |
| 1234 | return NULL; |
| 1235 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1236 | SirtRef<mirror::ObjectArray<mirror::StaticStorageBase> > |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1237 | initialized_static_storage(self, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1238 | AllocObjectArray<mirror::StaticStorageBase>(self, dex_file.NumTypeIds())); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1239 | if (initialized_static_storage.get() == NULL) { |
| 1240 | return NULL; |
| 1241 | } |
| 1242 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1243 | dex_cache->Init(&dex_file, location.get(), strings.get(), types.get(), methods.get(), |
| 1244 | fields.get(), initialized_static_storage.get()); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1245 | return dex_cache.get(); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1246 | } |
| 1247 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1248 | mirror::Class* ClassLinker::AllocClass(Thread* self, mirror::Class* java_lang_Class, |
| 1249 | size_t class_size) { |
| 1250 | DCHECK_GE(class_size, sizeof(mirror::Class)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1251 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1252 | mirror::Object* k = heap->AllocNonMovableObject<true>(self, java_lang_Class, class_size); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1253 | if (UNLIKELY(k == NULL)) { |
| 1254 | CHECK(self->IsExceptionPending()); // OOME. |
| 1255 | return NULL; |
| 1256 | } |
| 1257 | mirror::Class* klass = k->AsClass(); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 1258 | klass->SetPrimitiveType(Primitive::kPrimNot); // Default to not being primitive. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1259 | klass->SetClassSize(class_size); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 1260 | klass->SetDexClassDefIndex(DexFile::kDexNoIndex16); // Default to no valid class def index. |
| 1261 | klass->SetDexTypeIndex(DexFile::kDexNoIndex16); // Default to no valid type index. |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1262 | return klass; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1265 | mirror::Class* ClassLinker::AllocClass(Thread* self, size_t class_size) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1266 | return AllocClass(self, GetClassRoot(kJavaLangClass), class_size); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1267 | } |
| 1268 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1269 | mirror::ArtField* ClassLinker::AllocArtField(Thread* self) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1270 | return down_cast<mirror::ArtField*>( |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1271 | GetClassRoot(kJavaLangReflectArtField)->AllocNonMovableObject(self)); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1272 | } |
| 1273 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1274 | mirror::ArtMethod* ClassLinker::AllocArtMethod(Thread* self) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1275 | return down_cast<mirror::ArtMethod*>( |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1276 | GetClassRoot(kJavaLangReflectArtMethod)->AllocNonMovableObject(self)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1279 | mirror::ObjectArray<mirror::StackTraceElement>* ClassLinker::AllocStackTraceElementArray( |
| 1280 | Thread* self, size_t length) { |
| 1281 | return mirror::ObjectArray<mirror::StackTraceElement>::Alloc( |
| 1282 | self, GetClassRoot(kJavaLangStackTraceElementArrayClass), length); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1285 | static mirror::Class* EnsureResolved(Thread* self, mirror::Class* klass) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1286 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1287 | DCHECK(klass != NULL); |
| 1288 | // Wait for the class if it has not already been linked. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 1289 | if (!klass->IsResolved() && !klass->IsErroneous()) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 1290 | ObjectLock lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1291 | // Check for circular dependencies between classes. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 1292 | if (!klass->IsResolved() && klass->GetClinitThreadId() == self->GetTid()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1293 | ThrowClassCircularityError(klass); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1294 | klass->SetStatus(mirror::Class::kStatusError, self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1295 | return NULL; |
| 1296 | } |
| 1297 | // Wait for the pending initialization to complete. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 1298 | while (!klass->IsResolved() && !klass->IsErroneous()) { |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 1299 | lock.WaitIgnoringInterrupts(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1300 | } |
| 1301 | } |
| 1302 | if (klass->IsErroneous()) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 1303 | ThrowEarlierClassFailure(klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1304 | return NULL; |
| 1305 | } |
| 1306 | // Return the loaded class. No exceptions should be pending. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1307 | CHECK(klass->IsResolved()) << PrettyClass(klass); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1308 | self->AssertNoPendingException(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1309 | return klass; |
| 1310 | } |
| 1311 | |
Ian Rogers | be7149f | 2013-08-20 09:29:39 -0700 | [diff] [blame] | 1312 | bool ClassLinker::IsInBootClassPath(const char* descriptor) { |
| 1313 | DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, boot_class_path_); |
| 1314 | return pair.second != NULL; |
| 1315 | } |
| 1316 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1317 | mirror::Class* ClassLinker::FindSystemClass(const char* descriptor) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1318 | SirtRef<mirror::ClassLoader> class_loader(Thread::Current(), nullptr); |
| 1319 | return FindClass(descriptor, class_loader); |
Elliott Hughes | db7d5e9 | 2011-12-16 18:47:37 -0800 | [diff] [blame] | 1320 | } |
| 1321 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1322 | mirror::Class* ClassLinker::FindClass(const char* descriptor, |
| 1323 | SirtRef<mirror::ClassLoader>& class_loader) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 1324 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1325 | Thread* self = Thread::Current(); |
| 1326 | DCHECK(self != NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1327 | self->AssertNoPendingException(); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 1328 | if (descriptor[1] == '\0') { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1329 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 1330 | // for primitive classes that aren't backed by dex files. |
| 1331 | return FindPrimitiveClass(descriptor[0]); |
| 1332 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1333 | // Find the class in the loaded classes table. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1334 | mirror::Class* klass = LookupClass(descriptor, class_loader.get()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1335 | if (klass != NULL) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1336 | return EnsureResolved(self, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1337 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1338 | // Class is not yet loaded. |
| 1339 | if (descriptor[0] == '[') { |
| 1340 | return CreateArrayClass(descriptor, class_loader); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1341 | } else if (class_loader.get() == nullptr) { |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1342 | DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, boot_class_path_); |
| 1343 | if (pair.second != NULL) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1344 | SirtRef<mirror::ClassLoader> class_loader(self, nullptr); |
| 1345 | return DefineClass(descriptor, class_loader, *pair.first, *pair.second); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1346 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1347 | } else if (Runtime::Current()->UseCompileTimeClassPath()) { |
Ian Rogers | be7149f | 2013-08-20 09:29:39 -0700 | [diff] [blame] | 1348 | // First try the boot class path, we check the descriptor first to avoid an unnecessary |
| 1349 | // throw of a NoClassDefFoundError. |
| 1350 | if (IsInBootClassPath(descriptor)) { |
| 1351 | mirror::Class* system_class = FindSystemClass(descriptor); |
| 1352 | CHECK(system_class != NULL); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1353 | return system_class; |
| 1354 | } |
Ian Rogers | be7149f | 2013-08-20 09:29:39 -0700 | [diff] [blame] | 1355 | // Next try the compile time class path. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1356 | const std::vector<const DexFile*>* class_path; |
| 1357 | { |
Ian Rogers | be7149f | 2013-08-20 09:29:39 -0700 | [diff] [blame] | 1358 | ScopedObjectAccessUnchecked soa(self); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1359 | ScopedLocalRef<jobject> jclass_loader(soa.Env(), |
| 1360 | soa.AddLocalReference<jobject>(class_loader.get())); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1361 | class_path = &Runtime::Current()->GetCompileTimeClassPath(jclass_loader.get()); |
| 1362 | } |
| 1363 | |
| 1364 | DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, *class_path); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1365 | if (pair.second != NULL) { |
| 1366 | return DefineClass(descriptor, class_loader, *pair.first, *pair.second); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1367 | } |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1368 | |
| 1369 | } else { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1370 | ScopedObjectAccessUnchecked soa(self->GetJniEnv()); |
| 1371 | ScopedLocalRef<jobject> class_loader_object(soa.Env(), |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1372 | soa.AddLocalReference<jobject>(class_loader.get())); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 1373 | std::string class_name_string(DescriptorToDot(descriptor)); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1374 | ScopedLocalRef<jobject> result(soa.Env(), NULL); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 1375 | { |
| 1376 | ScopedThreadStateChange tsc(self, kNative); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1377 | ScopedLocalRef<jobject> class_name_object(soa.Env(), |
| 1378 | soa.Env()->NewStringUTF(class_name_string.c_str())); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 1379 | if (class_name_object.get() == NULL) { |
| 1380 | return NULL; |
| 1381 | } |
| 1382 | CHECK(class_loader_object.get() != NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1383 | result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(), |
| 1384 | WellKnownClasses::java_lang_ClassLoader_loadClass, |
| 1385 | class_name_object.get())); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1386 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1387 | if (soa.Self()->IsExceptionPending()) { |
Elliott Hughes | 748382f | 2012-01-26 18:07:38 -0800 | [diff] [blame] | 1388 | // If the ClassLoader threw, pass that exception up. |
| 1389 | return NULL; |
Ian Rogers | 761bfa8 | 2012-01-11 10:14:05 -0800 | [diff] [blame] | 1390 | } else if (result.get() == NULL) { |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 1391 | // broken loader - throw NPE to be compatible with Dalvik |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1392 | ThrowNullPointerException(NULL, StringPrintf("ClassLoader.loadClass returned null for %s", |
| 1393 | class_name_string.c_str()).c_str()); |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 1394 | return NULL; |
Ian Rogers | 761bfa8 | 2012-01-11 10:14:05 -0800 | [diff] [blame] | 1395 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1396 | // success, return mirror::Class* |
| 1397 | return soa.Decode<mirror::Class*>(result.get()); |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 1398 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1399 | } |
| 1400 | |
Elliott Hughes | 82914b6 | 2012-04-09 15:56:29 -0700 | [diff] [blame] | 1401 | ThrowNoClassDefFoundError("Class %s not found", PrintableString(descriptor).c_str()); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1402 | return NULL; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1405 | mirror::Class* ClassLinker::DefineClass(const char* descriptor, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1406 | SirtRef<mirror::ClassLoader>& class_loader, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1407 | const DexFile& dex_file, |
| 1408 | const DexFile::ClassDef& dex_class_def) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 1409 | Thread* self = Thread::Current(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1410 | SirtRef<mirror::Class> klass(self, NULL); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1411 | // Load the class from the dex file. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1412 | if (UNLIKELY(!init_done_)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1413 | // finish up init of hand crafted class_roots_ |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1414 | if (strcmp(descriptor, "Ljava/lang/Object;") == 0) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1415 | klass.reset(GetClassRoot(kJavaLangObject)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1416 | } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1417 | klass.reset(GetClassRoot(kJavaLangClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1418 | } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1419 | klass.reset(GetClassRoot(kJavaLangString)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1420 | } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 1421 | klass.reset(GetClassRoot(kJavaLangDexCache)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1422 | } else if (strcmp(descriptor, "Ljava/lang/reflect/ArtField;") == 0) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1423 | klass.reset(GetClassRoot(kJavaLangReflectArtField)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1424 | } else if (strcmp(descriptor, "Ljava/lang/reflect/ArtMethod;") == 0) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1425 | klass.reset(GetClassRoot(kJavaLangReflectArtMethod)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1426 | } else { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1427 | klass.reset(AllocClass(self, SizeOfClass(dex_file, dex_class_def))); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1428 | } |
| 1429 | } else { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1430 | klass.reset(AllocClass(self, SizeOfClass(dex_file, dex_class_def))); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1431 | } |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1432 | if (UNLIKELY(klass.get() == NULL)) { |
| 1433 | CHECK(self->IsExceptionPending()); // Expect an OOME. |
| 1434 | return NULL; |
| 1435 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1436 | klass->SetDexCache(FindDexCache(dex_file)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1437 | LoadClass(dex_file, dex_class_def, klass, class_loader.get()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1438 | // Check for a pending exception during load |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1439 | if (self->IsExceptionPending()) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1440 | klass->SetStatus(mirror::Class::kStatusError, self); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1441 | return NULL; |
| 1442 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 1443 | ObjectLock lock(self, klass.get()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1444 | klass->SetClinitThreadId(self->GetTid()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1445 | // Add the newly loaded class to the loaded classes table. |
| 1446 | mirror::Class* existing = InsertClass(descriptor, klass.get(), Hash(descriptor)); |
| 1447 | if (existing != NULL) { |
| 1448 | // We failed to insert because we raced with another thread. Calling EnsureResolved may cause |
| 1449 | // this thread to block. |
| 1450 | return EnsureResolved(self, existing); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1451 | } |
| 1452 | // Finish loading (if necessary) by finding parents |
| 1453 | CHECK(!klass->IsLoaded()); |
| 1454 | if (!LoadSuperAndInterfaces(klass, dex_file)) { |
| 1455 | // Loading failed. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1456 | klass->SetStatus(mirror::Class::kStatusError, self); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1457 | return NULL; |
| 1458 | } |
| 1459 | CHECK(klass->IsLoaded()); |
| 1460 | // Link the class (if necessary) |
| 1461 | CHECK(!klass->IsResolved()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1462 | // TODO: Use fast jobjects? |
| 1463 | SirtRef<mirror::ObjectArray<mirror::Class> > interfaces(self, nullptr); |
| 1464 | if (!LinkClass(self, klass, interfaces)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1465 | // Linking failed. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1466 | klass->SetStatus(mirror::Class::kStatusError, self); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1467 | return NULL; |
| 1468 | } |
| 1469 | CHECK(klass->IsResolved()); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1470 | |
| 1471 | /* |
| 1472 | * We send CLASS_PREPARE events to the debugger from here. The |
| 1473 | * definition of "preparation" is creating the static fields for a |
| 1474 | * class and initializing them to the standard default values, but not |
| 1475 | * executing any code (that comes later, during "initialization"). |
| 1476 | * |
| 1477 | * We did the static preparation in LinkClass. |
| 1478 | * |
| 1479 | * The class has been prepared and resolved but possibly not yet verified |
| 1480 | * at this point. |
| 1481 | */ |
| 1482 | Dbg::PostClassPrepare(klass.get()); |
| 1483 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1484 | return klass.get(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1487 | // Precomputes size that will be needed for Class, matching LinkStaticFields |
| 1488 | size_t ClassLinker::SizeOfClass(const DexFile& dex_file, |
| 1489 | const DexFile::ClassDef& dex_class_def) { |
| 1490 | const byte* class_data = dex_file.GetClassData(dex_class_def); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1491 | size_t num_ref = 0; |
| 1492 | size_t num_32 = 0; |
| 1493 | size_t num_64 = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1494 | if (class_data != NULL) { |
| 1495 | for (ClassDataItemIterator it(dex_file, class_data); it.HasNextStaticField(); it.Next()) { |
| 1496 | const DexFile::FieldId& field_id = dex_file.GetFieldId(it.GetMemberIndex()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1497 | const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1498 | char c = descriptor[0]; |
| 1499 | if (c == 'L' || c == '[') { |
| 1500 | num_ref++; |
| 1501 | } else if (c == 'J' || c == 'D') { |
| 1502 | num_64++; |
| 1503 | } else { |
| 1504 | num_32++; |
| 1505 | } |
| 1506 | } |
| 1507 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1508 | // start with generic class data |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1509 | size_t size = sizeof(mirror::Class); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1510 | // follow with reference fields which must be contiguous at start |
| 1511 | size += (num_ref * sizeof(uint32_t)); |
| 1512 | // if there are 64-bit fields to add, make sure they are aligned |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1513 | if (num_64 != 0 && size != RoundUp(size, 8)) { // for 64-bit alignment |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 1514 | if (num_32 != 0) { |
| 1515 | // use an available 32-bit field for padding |
| 1516 | num_32--; |
| 1517 | } |
| 1518 | size += sizeof(uint32_t); // either way, we are adding a word |
| 1519 | DCHECK_EQ(size, RoundUp(size, 8)); |
| 1520 | } |
| 1521 | // tack on any 64-bit fields now that alignment is assured |
| 1522 | size += (num_64 * sizeof(uint64_t)); |
| 1523 | // tack on any remaining 32-bit fields |
| 1524 | size += (num_32 * sizeof(uint32_t)); |
| 1525 | return size; |
| 1526 | } |
| 1527 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 1528 | const OatFile::OatClass* ClassLinker::GetOatClass(const DexFile& dex_file, uint16_t class_def_idx) { |
| 1529 | DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1530 | const OatFile* oat_file = FindOpenedOatFileForDexFile(dex_file); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 1531 | CHECK(oat_file != NULL) << dex_file.GetLocation(); |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 1532 | uint dex_location_checksum = dex_file.GetLocationChecksum(); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1533 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation().c_str(), |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 1534 | &dex_location_checksum); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 1535 | CHECK(oat_dex_file != NULL) << dex_file.GetLocation(); |
| 1536 | const OatFile::OatClass* oat_class = oat_dex_file->GetOatClass(class_def_idx); |
| 1537 | CHECK(oat_class != NULL) << dex_file.GetLocation() << " " << class_def_idx; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1538 | return oat_class; |
| 1539 | } |
| 1540 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 1541 | static uint32_t GetOatMethodIndexFromMethodIndex(const DexFile& dex_file, uint16_t class_def_idx, |
| 1542 | uint32_t method_idx) { |
| 1543 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_idx); |
| 1544 | const byte* class_data = dex_file.GetClassData(class_def); |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 1545 | CHECK(class_data != NULL); |
| 1546 | ClassDataItemIterator it(dex_file, class_data); |
| 1547 | // Skip fields |
| 1548 | while (it.HasNextStaticField()) { |
| 1549 | it.Next(); |
| 1550 | } |
| 1551 | while (it.HasNextInstanceField()) { |
| 1552 | it.Next(); |
| 1553 | } |
| 1554 | // Process methods |
| 1555 | size_t class_def_method_index = 0; |
| 1556 | while (it.HasNextDirectMethod()) { |
| 1557 | if (it.GetMemberIndex() == method_idx) { |
| 1558 | return class_def_method_index; |
| 1559 | } |
| 1560 | class_def_method_index++; |
| 1561 | it.Next(); |
| 1562 | } |
| 1563 | while (it.HasNextVirtualMethod()) { |
| 1564 | if (it.GetMemberIndex() == method_idx) { |
| 1565 | return class_def_method_index; |
| 1566 | } |
| 1567 | class_def_method_index++; |
| 1568 | it.Next(); |
| 1569 | } |
| 1570 | DCHECK(!it.HasNext()); |
| 1571 | LOG(FATAL) << "Failed to find method index " << method_idx << " in " << dex_file.GetLocation(); |
| 1572 | return 0; |
| 1573 | } |
| 1574 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1575 | const OatFile::OatMethod ClassLinker::GetOatMethodFor(const mirror::ArtMethod* method) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1576 | // Although we overwrite the trampoline of non-static methods, we may get here via the resolution |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 1577 | // method for direct methods (or virtual methods made direct). |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1578 | mirror::Class* declaring_class = method->GetDeclaringClass(); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 1579 | size_t oat_method_index; |
| 1580 | if (method->IsStatic() || method->IsDirect()) { |
| 1581 | // Simple case where the oat method index was stashed at load time. |
| 1582 | oat_method_index = method->GetMethodIndex(); |
| 1583 | } else { |
| 1584 | // We're invoking a virtual method directly (thanks to sharpening), compute the oat_method_index |
| 1585 | // by search for its position in the declared virtual methods. |
| 1586 | oat_method_index = declaring_class->NumDirectMethods(); |
| 1587 | size_t end = declaring_class->NumVirtualMethods(); |
| 1588 | bool found = false; |
| 1589 | for (size_t i = 0; i < end; i++) { |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 1590 | if (declaring_class->GetVirtualMethod(i) == method) { |
| 1591 | found = true; |
| 1592 | break; |
| 1593 | } |
Ian Rogers | f320b63 | 2012-03-13 18:47:47 -0700 | [diff] [blame] | 1594 | oat_method_index++; |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 1595 | } |
| 1596 | CHECK(found) << "Didn't find oat method index for virtual method: " << PrettyMethod(method); |
| 1597 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 1598 | UniquePtr<const OatFile::OatClass> |
| 1599 | oat_class(GetOatClass(*declaring_class->GetDexCache()->GetDexFile(), |
| 1600 | declaring_class->GetDexClassDefIndex())); |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 1601 | CHECK(oat_class.get() != NULL); |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 1602 | DCHECK_EQ(oat_method_index, |
| 1603 | GetOatMethodIndexFromMethodIndex(*declaring_class->GetDexCache()->GetDexFile(), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 1604 | method->GetDeclaringClass()->GetDexClassDefIndex(), |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 1605 | method->GetDexMethodIndex())); |
| 1606 | |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 1607 | return oat_class->GetOatMethod(oat_method_index); |
| 1608 | } |
| 1609 | |
| 1610 | // Special case to get oat code without overwriting a trampoline. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1611 | const void* ClassLinker::GetOatCodeFor(const mirror::ArtMethod* method) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1612 | CHECK(!method->IsAbstract()) << PrettyMethod(method); |
Jeff Hao | 8df6cea | 2013-07-29 13:54:48 -0700 | [diff] [blame] | 1613 | if (method->IsProxyMethod()) { |
| 1614 | #if !defined(ART_USE_PORTABLE_COMPILER) |
| 1615 | return reinterpret_cast<void*>(art_quick_proxy_invoke_handler); |
| 1616 | #else |
| 1617 | return reinterpret_cast<void*>(art_portable_proxy_invoke_handler); |
| 1618 | #endif |
| 1619 | } |
Ian Rogers | f3e9855 | 2013-03-20 15:49:49 -0700 | [diff] [blame] | 1620 | const void* result = GetOatMethodFor(method).GetCode(); |
| 1621 | if (result == NULL) { |
| 1622 | // No code? You must mean to go into the interpreter. |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1623 | result = GetCompiledCodeToInterpreterBridge(); |
Ian Rogers | f3e9855 | 2013-03-20 15:49:49 -0700 | [diff] [blame] | 1624 | } |
| 1625 | return result; |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 1626 | } |
| 1627 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 1628 | const void* ClassLinker::GetOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, |
| 1629 | uint32_t method_idx) { |
| 1630 | UniquePtr<const OatFile::OatClass> oat_class(GetOatClass(dex_file, class_def_idx)); |
| 1631 | CHECK(oat_class.get() != nullptr); |
| 1632 | uint32_t oat_method_idx = GetOatMethodIndexFromMethodIndex(dex_file, class_def_idx, method_idx); |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 1633 | return oat_class->GetOatMethod(oat_method_idx).GetCode(); |
| 1634 | } |
| 1635 | |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 1636 | // Returns true if the method must run with interpreter, false otherwise. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1637 | static bool NeedsInterpreter(const mirror::ArtMethod* method, const void* code) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 1638 | if (code == NULL) { |
| 1639 | // No code: need interpreter. |
| 1640 | return true; |
| 1641 | } |
Dragos Sbirlea | 90af14d | 2013-08-15 17:50:16 -0700 | [diff] [blame] | 1642 | #ifdef ART_SEA_IR_MODE |
| 1643 | ScopedObjectAccess soa(Thread::Current()); |
| 1644 | if (std::string::npos != PrettyMethod(method).find("fibonacci")) { |
| 1645 | LOG(INFO) << "Found " << PrettyMethod(method); |
| 1646 | return false; |
| 1647 | } |
| 1648 | #endif |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 1649 | // If interpreter mode is enabled, every method (except native and proxy) must |
| 1650 | // be run with interpreter. |
| 1651 | return Runtime::Current()->GetInstrumentation()->InterpretOnly() && |
| 1652 | !method->IsNative() && !method->IsProxyMethod(); |
| 1653 | } |
| 1654 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1655 | void ClassLinker::FixupStaticTrampolines(mirror::Class* klass) { |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 1656 | if (klass->NumDirectMethods() == 0) { |
| 1657 | return; // No direct methods => no static methods. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1658 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1659 | Runtime* runtime = Runtime::Current(); |
| 1660 | if (!runtime->IsStarted() || runtime->UseCompileTimeClassPath()) { |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 1661 | return; // OAT file unavailable. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1662 | } |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 1663 | ClassHelper kh(klass); |
| 1664 | const DexFile& dex_file = kh.GetDexFile(); |
| 1665 | const DexFile::ClassDef* dex_class_def = kh.GetClassDef(); |
| 1666 | CHECK(dex_class_def != nullptr); |
| 1667 | const byte* class_data = dex_file.GetClassData(*dex_class_def); |
| 1668 | // There should always be class data if there were direct methods. |
| 1669 | CHECK(class_data != nullptr) << PrettyDescriptor(klass); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 1670 | UniquePtr<const OatFile::OatClass> oat_class(GetOatClass(dex_file, klass->GetDexClassDefIndex())); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 1671 | CHECK(oat_class.get() != nullptr); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1672 | ClassDataItemIterator it(dex_file, class_data); |
| 1673 | // Skip fields |
| 1674 | while (it.HasNextStaticField()) { |
| 1675 | it.Next(); |
| 1676 | } |
| 1677 | while (it.HasNextInstanceField()) { |
| 1678 | it.Next(); |
| 1679 | } |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 1680 | // Link the code of methods skipped by LinkCode. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 1681 | for (size_t method_index = 0; it.HasNextDirectMethod(); ++method_index, it.Next()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1682 | mirror::ArtMethod* method = klass->GetDirectMethod(method_index); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 1683 | if (!method->IsStatic()) { |
| 1684 | // Only update static methods. |
| 1685 | continue; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1686 | } |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 1687 | const void* code = oat_class->GetOatMethod(method_index).GetCode(); |
| 1688 | const bool enter_interpreter = NeedsInterpreter(method, code); |
| 1689 | if (enter_interpreter) { |
| 1690 | // Use interpreter entry point. |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1691 | code = GetCompiledCodeToInterpreterBridge(); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 1692 | } |
| 1693 | runtime->GetInstrumentation()->UpdateMethodsCode(method, code); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1694 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1695 | // Ignore virtual methods on the iterator. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1696 | } |
| 1697 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1698 | static void LinkCode(SirtRef<mirror::ArtMethod>& method, const OatFile::OatClass* oat_class, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1699 | uint32_t method_index) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1700 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1701 | // Method shouldn't have already been linked. |
Jeff Hao | aa4a793 | 2013-05-13 11:28:27 -0700 | [diff] [blame] | 1702 | DCHECK(method->GetEntryPointFromCompiledCode() == NULL); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 1703 | // Every kind of method should at least get an invoke stub from the oat_method. |
| 1704 | // non-abstract methods also get their code pointers. |
| 1705 | const OatFile::OatMethod oat_method = oat_class->GetOatMethod(method_index); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1706 | oat_method.LinkMethod(method.get()); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 1707 | |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 1708 | // Install entry point from interpreter. |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 1709 | Runtime* runtime = Runtime::Current(); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 1710 | bool enter_interpreter = NeedsInterpreter(method.get(), method->GetEntryPointFromCompiledCode()); |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1711 | if (enter_interpreter) { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1712 | method->SetEntryPointFromInterpreter(interpreter::artInterpreterToInterpreterBridge); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 1713 | } else { |
Dragos Sbirlea | 08bf196 | 2013-08-12 08:53:04 -0700 | [diff] [blame] | 1714 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 1715 | } |
| 1716 | |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 1717 | if (method->IsAbstract()) { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1718 | method->SetEntryPointFromCompiledCode(GetCompiledCodeToInterpreterBridge()); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 1719 | return; |
| 1720 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1721 | |
| 1722 | if (method->IsStatic() && !method->IsConstructor()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1723 | // For static methods excluding the class initializer, install the trampoline. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 1724 | // It will be replaced by the proper entry point by ClassLinker::FixupStaticTrampolines |
| 1725 | // after initializing class (see ClassLinker::InitializeClass method). |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 1726 | method->SetEntryPointFromCompiledCode(GetResolutionTrampoline(runtime->GetClassLinker())); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 1727 | } else if (enter_interpreter) { |
| 1728 | // Set entry point from compiled code if there's no code or in interpreter only mode. |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1729 | method->SetEntryPointFromCompiledCode(GetCompiledCodeToInterpreterBridge()); |
Ian Rogers | 0d6de04 | 2012-02-29 08:50:26 -0800 | [diff] [blame] | 1730 | } |
jeffhao | 26c0a1a | 2012-01-17 16:28:33 -0800 | [diff] [blame] | 1731 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1732 | if (method->IsNative()) { |
| 1733 | // Unregistering restores the dlsym lookup stub. |
| 1734 | method->UnregisterNative(Thread::Current()); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 1735 | } |
Ian Rogers | f3e9855 | 2013-03-20 15:49:49 -0700 | [diff] [blame] | 1736 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1737 | // Allow instrumentation its chance to hijack code. |
Jeff Hao | aa4a793 | 2013-05-13 11:28:27 -0700 | [diff] [blame] | 1738 | runtime->GetInstrumentation()->UpdateMethodsCode(method.get(), |
| 1739 | method->GetEntryPointFromCompiledCode()); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 1740 | } |
| 1741 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1742 | void ClassLinker::LoadClass(const DexFile& dex_file, |
| 1743 | const DexFile::ClassDef& dex_class_def, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1744 | SirtRef<mirror::Class>& klass, |
| 1745 | mirror::ClassLoader* class_loader) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1746 | CHECK(klass.get() != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1747 | CHECK(klass->GetDexCache() != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1748 | CHECK_EQ(mirror::Class::kStatusNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 1749 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1750 | CHECK(descriptor != NULL); |
| 1751 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1752 | klass->SetClass(GetClassRoot(kJavaLangClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1753 | uint32_t access_flags = dex_class_def.access_flags_; |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame] | 1754 | // Make sure that none of our runtime-only flags are set. |
Brian Carlstrom | 8e3fb14 | 2013-10-09 21:00:27 -0700 | [diff] [blame] | 1755 | CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1756 | klass->SetAccessFlags(access_flags); |
| 1757 | klass->SetClassLoader(class_loader); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 1758 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1759 | klass->SetStatus(mirror::Class::kStatusIdx, NULL); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1760 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 1761 | klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1762 | klass->SetDexTypeIndex(dex_class_def.class_idx_); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1763 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1764 | // Load fields fields. |
| 1765 | const byte* class_data = dex_file.GetClassData(dex_class_def); |
| 1766 | if (class_data == NULL) { |
| 1767 | return; // no fields or methods - for example a marker interface |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1768 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1769 | ClassDataItemIterator it(dex_file, class_data); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1770 | Thread* self = Thread::Current(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1771 | if (it.NumStaticFields() != 0) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1772 | mirror::ObjectArray<mirror::ArtField>* statics = AllocArtFieldArray(self, it.NumStaticFields()); |
| 1773 | if (UNLIKELY(statics == NULL)) { |
| 1774 | CHECK(self->IsExceptionPending()); // OOME. |
| 1775 | return; |
| 1776 | } |
| 1777 | klass->SetSFields(statics); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1778 | } |
| 1779 | if (it.NumInstanceFields() != 0) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1780 | mirror::ObjectArray<mirror::ArtField>* fields = |
| 1781 | AllocArtFieldArray(self, it.NumInstanceFields()); |
| 1782 | if (UNLIKELY(fields == NULL)) { |
| 1783 | CHECK(self->IsExceptionPending()); // OOME. |
| 1784 | return; |
| 1785 | } |
| 1786 | klass->SetIFields(fields); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1787 | } |
| 1788 | for (size_t i = 0; it.HasNextStaticField(); i++, it.Next()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1789 | SirtRef<mirror::ArtField> sfield(self, AllocArtField(self)); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1790 | if (UNLIKELY(sfield.get() == NULL)) { |
| 1791 | CHECK(self->IsExceptionPending()); // OOME. |
| 1792 | return; |
| 1793 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1794 | klass->SetStaticField(i, sfield.get()); |
| 1795 | LoadField(dex_file, it, klass, sfield); |
| 1796 | } |
| 1797 | for (size_t i = 0; it.HasNextInstanceField(); i++, it.Next()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1798 | SirtRef<mirror::ArtField> ifield(self, AllocArtField(self)); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1799 | if (UNLIKELY(ifield.get() == NULL)) { |
| 1800 | CHECK(self->IsExceptionPending()); // OOME. |
| 1801 | return; |
| 1802 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1803 | klass->SetInstanceField(i, ifield.get()); |
| 1804 | LoadField(dex_file, it, klass, ifield); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1805 | } |
| 1806 | |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 1807 | UniquePtr<const OatFile::OatClass> oat_class; |
| 1808 | if (Runtime::Current()->IsStarted() && !Runtime::Current()->UseCompileTimeClassPath()) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 1809 | oat_class.reset(GetOatClass(dex_file, klass->GetDexClassDefIndex())); |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 1810 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1811 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1812 | // Load methods. |
| 1813 | if (it.NumDirectMethods() != 0) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1814 | // TODO: append direct methods to class object |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1815 | mirror::ObjectArray<mirror::ArtMethod>* directs = |
| 1816 | AllocArtMethodArray(self, it.NumDirectMethods()); |
| 1817 | if (UNLIKELY(directs == NULL)) { |
| 1818 | CHECK(self->IsExceptionPending()); // OOME. |
| 1819 | return; |
| 1820 | } |
| 1821 | klass->SetDirectMethods(directs); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1822 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1823 | if (it.NumVirtualMethods() != 0) { |
| 1824 | // TODO: append direct methods to class object |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1825 | mirror::ObjectArray<mirror::ArtMethod>* virtuals = |
| 1826 | AllocArtMethodArray(self, it.NumVirtualMethods()); |
| 1827 | if (UNLIKELY(virtuals == NULL)) { |
| 1828 | CHECK(self->IsExceptionPending()); // OOME. |
| 1829 | return; |
| 1830 | } |
| 1831 | klass->SetVirtualMethods(virtuals); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1832 | } |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 1833 | size_t class_def_method_index = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1834 | for (size_t i = 0; it.HasNextDirectMethod(); i++, it.Next()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1835 | SirtRef<mirror::ArtMethod> method(self, LoadMethod(self, dex_file, it, klass)); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1836 | if (UNLIKELY(method.get() == NULL)) { |
| 1837 | CHECK(self->IsExceptionPending()); // OOME. |
| 1838 | return; |
| 1839 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1840 | klass->SetDirectMethod(i, method.get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1841 | if (oat_class.get() != NULL) { |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 1842 | LinkCode(method, oat_class.get(), class_def_method_index); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1843 | } |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 1844 | method->SetMethodIndex(class_def_method_index); |
| 1845 | class_def_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1846 | } |
| 1847 | for (size_t i = 0; it.HasNextVirtualMethod(); i++, it.Next()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1848 | SirtRef<mirror::ArtMethod> method(self, LoadMethod(self, dex_file, it, klass)); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1849 | if (UNLIKELY(method.get() == NULL)) { |
| 1850 | CHECK(self->IsExceptionPending()); // OOME. |
| 1851 | return; |
| 1852 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1853 | klass->SetVirtualMethod(i, method.get()); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 1854 | DCHECK_EQ(class_def_method_index, it.NumDirectMethods() + i); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1855 | if (oat_class.get() != NULL) { |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 1856 | LinkCode(method, oat_class.get(), class_def_method_index); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1857 | } |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 1858 | class_def_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1859 | } |
| 1860 | DCHECK(!it.HasNext()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1861 | } |
| 1862 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1863 | void ClassLinker::LoadField(const DexFile& /*dex_file*/, const ClassDataItemIterator& it, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1864 | SirtRef<mirror::Class>& klass, SirtRef<mirror::ArtField>& dst) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1865 | uint32_t field_idx = it.GetMemberIndex(); |
| 1866 | dst->SetDexFieldIndex(field_idx); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1867 | dst->SetDeclaringClass(klass.get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1868 | dst->SetAccessFlags(it.GetMemberAccessFlags()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1869 | } |
| 1870 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1871 | mirror::ArtMethod* ClassLinker::LoadMethod(Thread* self, const DexFile& dex_file, |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1872 | const ClassDataItemIterator& it, |
| 1873 | SirtRef<mirror::Class>& klass) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1874 | uint32_t dex_method_idx = it.GetMemberIndex(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1875 | const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 1876 | const char* method_name = dex_file.StringDataByIdx(method_id.name_idx_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 1877 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1878 | mirror::ArtMethod* dst = AllocArtMethod(self); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1879 | if (UNLIKELY(dst == NULL)) { |
| 1880 | CHECK(self->IsExceptionPending()); // OOME. |
| 1881 | return NULL; |
| 1882 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1883 | DCHECK(dst->IsArtMethod()) << PrettyDescriptor(dst->GetClass()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 1884 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1885 | const char* old_cause = self->StartAssertNoThreadSuspension("LoadMethod"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 1886 | dst->SetDexMethodIndex(dex_method_idx); |
| 1887 | dst->SetDeclaringClass(klass.get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 1888 | dst->SetCodeItemOffset(it.GetMethodCodeItemOffset()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1889 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1890 | dst->SetDexCacheStrings(klass->GetDexCache()->GetStrings()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1891 | dst->SetDexCacheResolvedMethods(klass->GetDexCache()->GetResolvedMethods()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1892 | dst->SetDexCacheResolvedTypes(klass->GetDexCache()->GetResolvedTypes()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1893 | dst->SetDexCacheInitializedStaticStorage(klass->GetDexCache()->GetInitializedStaticStorage()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 1894 | |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 1895 | uint32_t access_flags = it.GetMemberAccessFlags(); |
| 1896 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 1897 | if (UNLIKELY(strcmp("finalize", method_name) == 0)) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 1898 | // Set finalizable flag on declaring class. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 1899 | if (strcmp("V", dex_file.GetShorty(method_id.proto_idx_)) == 0) { |
| 1900 | // Void return type. |
| 1901 | if (klass->GetClassLoader() != NULL) { // All non-boot finalizer methods are flagged |
| 1902 | klass->SetFinalizable(); |
| 1903 | } else { |
| 1904 | ClassHelper kh(klass.get()); |
| 1905 | const char* klass_descriptor = kh.GetDescriptor(); |
| 1906 | // The Enum class declares a "final" finalize() method to prevent subclasses from |
| 1907 | // introducing a finalizer. We don't want to set the finalizable flag for Enum or its |
| 1908 | // subclasses, so we exclude it here. |
| 1909 | // We also want to avoid setting the flag on Object, where we know that finalize() is |
| 1910 | // empty. |
| 1911 | if ((strcmp("Ljava/lang/Object;", klass_descriptor) != 0) && |
| 1912 | (strcmp("Ljava/lang/Enum;", klass_descriptor) != 0)) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 1913 | klass->SetFinalizable(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 1914 | } |
| 1915 | } |
| 1916 | } |
| 1917 | } else if (method_name[0] == '<') { |
| 1918 | // Fix broken access flags for initializers. Bug 11157540. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 1919 | bool is_init = (strcmp("<init>", method_name) == 0); |
| 1920 | bool is_clinit = !is_init && (strcmp("<clinit>", method_name) == 0); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 1921 | if (UNLIKELY(!is_init && !is_clinit)) { |
| 1922 | LOG(WARNING) << "Unexpected '<' at start of method name " << method_name; |
| 1923 | } else { |
| 1924 | if (UNLIKELY((access_flags & kAccConstructor) == 0)) { |
| 1925 | LOG(WARNING) << method_name << " didn't have expected constructor access flag in class " |
| 1926 | << PrettyDescriptor(klass.get()) << " in dex file " << dex_file.GetLocation(); |
| 1927 | access_flags |= kAccConstructor; |
| 1928 | } |
| 1929 | } |
| 1930 | } |
| 1931 | dst->SetAccessFlags(access_flags); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 1932 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1933 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 1934 | return dst; |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 1935 | } |
| 1936 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1937 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1938 | Thread* self = Thread::Current(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1939 | SirtRef<mirror::DexCache> dex_cache(self, AllocDexCache(self, dex_file)); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1940 | CHECK(dex_cache.get() != NULL) << "Failed to allocate dex cache for " << dex_file.GetLocation(); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1941 | AppendToBootClassPath(dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1942 | } |
| 1943 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1944 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, SirtRef<mirror::DexCache>& dex_cache) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1945 | CHECK(dex_cache.get() != NULL) << dex_file.GetLocation(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1946 | boot_class_path_.push_back(&dex_file); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1947 | RegisterDexFile(dex_file, dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1948 | } |
| 1949 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1950 | bool ClassLinker::IsDexFileRegisteredLocked(const DexFile& dex_file) const { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 1951 | dex_lock_.AssertSharedHeld(Thread::Current()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 1952 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
| 1953 | if (dex_caches_[i]->GetDexFile() == &dex_file) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1954 | return true; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1955 | } |
| 1956 | } |
| 1957 | return false; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1958 | } |
| 1959 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1960 | bool ClassLinker::IsDexFileRegistered(const DexFile& dex_file) const { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 1961 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | 0691851 | 2011-10-16 23:39:12 -0700 | [diff] [blame] | 1962 | return IsDexFileRegisteredLocked(dex_file); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1963 | } |
| 1964 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1965 | void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, SirtRef<mirror::DexCache>& dex_cache) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 1966 | dex_lock_.AssertExclusiveHeld(Thread::Current()); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1967 | CHECK(dex_cache.get() != NULL) << dex_file.GetLocation(); |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 1968 | CHECK(dex_cache->GetLocation()->Equals(dex_file.GetLocation())) |
| 1969 | << dex_cache->GetLocation()->ToModifiedUtf8() << " " << dex_file.GetLocation(); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1970 | dex_caches_.push_back(dex_cache.get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 1971 | dex_cache->SetDexFile(&dex_file); |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 1972 | dex_caches_dirty_ = true; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1973 | } |
| 1974 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1975 | void ClassLinker::RegisterDexFile(const DexFile& dex_file) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 1976 | Thread* self = Thread::Current(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 1977 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 1978 | ReaderMutexLock mu(self, dex_lock_); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 1979 | if (IsDexFileRegisteredLocked(dex_file)) { |
| 1980 | return; |
| 1981 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1982 | } |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 1983 | // Don't alloc while holding the lock, since allocation may need to |
| 1984 | // suspend all threads and another thread may need the dex_lock_ to |
| 1985 | // get to a suspend point. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1986 | SirtRef<mirror::DexCache> dex_cache(self, AllocDexCache(self, dex_file)); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1987 | CHECK(dex_cache.get() != NULL) << "Failed to allocate dex cache for " << dex_file.GetLocation(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 1988 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 1989 | WriterMutexLock mu(self, dex_lock_); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 1990 | if (IsDexFileRegisteredLocked(dex_file)) { |
| 1991 | return; |
| 1992 | } |
| 1993 | RegisterDexFileLocked(dex_file, dex_cache); |
| 1994 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1995 | } |
| 1996 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1997 | void ClassLinker::RegisterDexFile(const DexFile& dex_file, SirtRef<mirror::DexCache>& dex_cache) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 1998 | WriterMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1999 | RegisterDexFileLocked(dex_file, dex_cache); |
| 2000 | } |
| 2001 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2002 | mirror::DexCache* ClassLinker::FindDexCache(const DexFile& dex_file) const { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2003 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2004 | // Search assuming unique-ness of dex file. |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2005 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2006 | mirror::DexCache* dex_cache = dex_caches_[i]; |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2007 | if (dex_cache->GetDexFile() == &dex_file) { |
| 2008 | return dex_cache; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2009 | } |
| 2010 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2011 | // Search matching by location name. |
| 2012 | std::string location(dex_file.GetLocation()); |
| 2013 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2014 | mirror::DexCache* dex_cache = dex_caches_[i]; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2015 | if (dex_cache->GetDexFile()->GetLocation() == location) { |
| 2016 | return dex_cache; |
| 2017 | } |
| 2018 | } |
| 2019 | // Failure, dump diagnostic and abort. |
| 2020 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2021 | mirror::DexCache* dex_cache = dex_caches_[i]; |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2022 | LOG(ERROR) << "Registered dex file " << i << " = " << dex_cache->GetDexFile()->GetLocation(); |
| 2023 | } |
| 2024 | LOG(FATAL) << "Failed to find DexCache for DexFile " << location; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2025 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2026 | } |
| 2027 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2028 | void ClassLinker::FixupDexCaches(mirror::ArtMethod* resolution_method) const { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2029 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2030 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
| 2031 | dex_caches_[i]->Fixup(resolution_method); |
| 2032 | } |
| 2033 | } |
| 2034 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2035 | mirror::Class* ClassLinker::CreatePrimitiveClass(Thread* self, Primitive::Type type) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2036 | mirror::Class* klass = AllocClass(self, sizeof(mirror::Class)); |
| 2037 | if (UNLIKELY(klass == NULL)) { |
| 2038 | return NULL; |
| 2039 | } |
| 2040 | return InitializePrimitiveClass(klass, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | mirror::Class* ClassLinker::InitializePrimitiveClass(mirror::Class* primitive_class, Primitive::Type type) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2044 | CHECK(primitive_class != NULL); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 2045 | // Must hold lock on object when initializing. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2046 | Thread* self = Thread::Current(); |
| 2047 | ObjectLock lock(self, primitive_class); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2048 | primitive_class->SetAccessFlags(kAccPublic | kAccFinal | kAccAbstract); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2049 | primitive_class->SetPrimitiveType(type); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2050 | primitive_class->SetStatus(mirror::Class::kStatusInitialized, self); |
| 2051 | const char* descriptor = Primitive::Descriptor(type); |
| 2052 | mirror::Class* existing = InsertClass(descriptor, primitive_class, Hash(descriptor)); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 2053 | CHECK(existing == NULL) << "InitPrimitiveClass(" << type << ") failed"; |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2054 | return primitive_class; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 2055 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2056 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 2057 | // Create an array class (i.e. the class object for the array, not the |
| 2058 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 2059 | // "[Ljava/lang/String;". |
| 2060 | // |
| 2061 | // If "descriptor" refers to an array of primitives, look up the |
| 2062 | // primitive type's internally-generated class object. |
| 2063 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2064 | // "class_loader" is the class loader of the class that's referring to |
| 2065 | // us. It's used to ensure that we're looking for the element type in |
| 2066 | // the right context. It does NOT become the class loader for the |
| 2067 | // array class; that always comes from the base element class. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 2068 | // |
| 2069 | // Returns NULL with an exception raised on failure. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2070 | mirror::Class* ClassLinker::CreateArrayClass(const char* descriptor, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2071 | SirtRef<mirror::ClassLoader>& class_loader) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2072 | // Identify the underlying component type |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2073 | CHECK_EQ('[', descriptor[0]); |
| 2074 | mirror::Class* component_type = FindClass(descriptor + 1, class_loader); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2075 | if (component_type == NULL) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2076 | DCHECK(Thread::Current()->IsExceptionPending()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2077 | return NULL; |
| 2078 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 2079 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2080 | // See if the component type is already loaded. Array classes are |
| 2081 | // always associated with the class loader of their underlying |
| 2082 | // element type -- an array of Strings goes with the loader for |
| 2083 | // java/lang/String -- so we need to look for it there. (The |
| 2084 | // caller should have checked for the existence of the class |
| 2085 | // before calling here, but they did so with *their* class loader, |
| 2086 | // not the component type's loader.) |
| 2087 | // |
| 2088 | // If we find it, the caller adds "loader" to the class' initiating |
| 2089 | // loader list, which should prevent us from going through this again. |
| 2090 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2091 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2092 | // are the same, because our caller (FindClass) just did the |
| 2093 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 2094 | // because we effectively do this lookup again when we add the new |
| 2095 | // class to the hash table --- necessary because of possible races with |
| 2096 | // other threads.) |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2097 | if (class_loader.get() != component_type->GetClassLoader()) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2098 | mirror::Class* new_class = LookupClass(descriptor, component_type->GetClassLoader()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2099 | if (new_class != NULL) { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 2100 | return new_class; |
| 2101 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2102 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 2103 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2104 | // Fill out the fields in the Class. |
| 2105 | // |
| 2106 | // It is possible to execute some methods against arrays, because |
| 2107 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 2108 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 2109 | // |
| 2110 | // Array classes are simple enough that we don't need to do a full |
| 2111 | // link step. |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 2112 | Thread* self = Thread::Current(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2113 | SirtRef<mirror::Class> new_class(self, NULL); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2114 | if (UNLIKELY(!init_done_)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2115 | // Classes that were hand created, ie not by FindSystemClass |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2116 | if (strcmp(descriptor, "[Ljava/lang/Class;") == 0) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2117 | new_class.reset(GetClassRoot(kClassArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2118 | } else if (strcmp(descriptor, "[Ljava/lang/Object;") == 0) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2119 | new_class.reset(GetClassRoot(kObjectArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2120 | } else if (strcmp(descriptor, class_roots_descriptors_[kJavaLangStringArrayClass]) == 0) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2121 | new_class.reset(GetClassRoot(kJavaLangStringArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2122 | } else if (strcmp(descriptor, |
| 2123 | class_roots_descriptors_[kJavaLangReflectArtMethodArrayClass]) == 0) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2124 | new_class.reset(GetClassRoot(kJavaLangReflectArtMethodArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2125 | } else if (strcmp(descriptor, |
| 2126 | class_roots_descriptors_[kJavaLangReflectArtFieldArrayClass]) == 0) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2127 | new_class.reset(GetClassRoot(kJavaLangReflectArtFieldArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2128 | } else if (strcmp(descriptor, "[C") == 0) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2129 | new_class.reset(GetClassRoot(kCharArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2130 | } else if (strcmp(descriptor, "[I") == 0) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2131 | new_class.reset(GetClassRoot(kIntArrayClass)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2132 | } |
| 2133 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2134 | if (new_class.get() == NULL) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2135 | new_class.reset(AllocClass(self, sizeof(mirror::Class))); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2136 | if (new_class.get() == NULL) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2137 | return NULL; |
| 2138 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2139 | new_class->SetComponentType(component_type); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2140 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 2141 | ObjectLock lock(self, new_class.get()); // Must hold lock on object when initializing. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2142 | DCHECK(new_class->GetComponentType() != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2143 | mirror::Class* java_lang_Object = GetClassRoot(kJavaLangObject); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2144 | new_class->SetSuperClass(java_lang_Object); |
| 2145 | new_class->SetVTable(java_lang_Object->GetVTable()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2146 | new_class->SetPrimitiveType(Primitive::kPrimNot); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2147 | new_class->SetClassLoader(component_type->GetClassLoader()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2148 | new_class->SetStatus(mirror::Class::kStatusInitialized, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2149 | // don't need to set new_class->SetObjectSize(..) |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 2150 | // because Object::SizeOf delegates to Array::SizeOf |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2151 | |
| 2152 | |
| 2153 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 2154 | // interfaces. We need to set that up here, so that stuff like |
| 2155 | // "instanceof" works right. |
| 2156 | // |
| 2157 | // Note: The GC could run during the call to FindSystemClass, |
| 2158 | // so we need to make sure the class object is GC-valid while we're in |
| 2159 | // there. Do this by clearing the interface list so the GC will just |
| 2160 | // think that the entries are null. |
| 2161 | |
| 2162 | |
| 2163 | // Use the single, global copies of "interfaces" and "iftable" |
| 2164 | // (remember not to free them for arrays). |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 2165 | CHECK(array_iftable_ != NULL); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 2166 | new_class->SetIfTable(array_iftable_); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2167 | |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 2168 | // Inherit access flags from the component type. |
| 2169 | int access_flags = new_class->GetComponentType()->GetAccessFlags(); |
| 2170 | // Lose any implementation detail flags; in particular, arrays aren't finalizable. |
| 2171 | access_flags &= kAccJavaFlagsMask; |
| 2172 | // Arrays can't be used as a superclass or interface, so we want to add "abstract final" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2173 | // and remove "interface". |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 2174 | access_flags |= kAccAbstract | kAccFinal; |
| 2175 | access_flags &= ~kAccInterface; |
| 2176 | |
| 2177 | new_class->SetAccessFlags(access_flags); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2178 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2179 | mirror::Class* existing = InsertClass(descriptor, new_class.get(), Hash(descriptor)); |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 2180 | if (existing == NULL) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2181 | return new_class.get(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2182 | } |
| 2183 | // Another thread must have loaded the class after we |
| 2184 | // started but before we finished. Abandon what we've |
| 2185 | // done. |
| 2186 | // |
| 2187 | // (Yes, this happens.) |
| 2188 | |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 2189 | return existing; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 2190 | } |
| 2191 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2192 | mirror::Class* ClassLinker::FindPrimitiveClass(char type) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2193 | switch (Primitive::GetType(type)) { |
| 2194 | case Primitive::kPrimByte: |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2195 | return GetClassRoot(kPrimitiveByte); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2196 | case Primitive::kPrimChar: |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2197 | return GetClassRoot(kPrimitiveChar); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2198 | case Primitive::kPrimDouble: |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2199 | return GetClassRoot(kPrimitiveDouble); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2200 | case Primitive::kPrimFloat: |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2201 | return GetClassRoot(kPrimitiveFloat); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2202 | case Primitive::kPrimInt: |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2203 | return GetClassRoot(kPrimitiveInt); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2204 | case Primitive::kPrimLong: |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2205 | return GetClassRoot(kPrimitiveLong); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2206 | case Primitive::kPrimShort: |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2207 | return GetClassRoot(kPrimitiveShort); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2208 | case Primitive::kPrimBoolean: |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2209 | return GetClassRoot(kPrimitiveBoolean); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2210 | case Primitive::kPrimVoid: |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2211 | return GetClassRoot(kPrimitiveVoid); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2212 | case Primitive::kPrimNot: |
| 2213 | break; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 2214 | } |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 2215 | std::string printable_type(PrintableChar(type)); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2216 | ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str()); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 2217 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2218 | } |
| 2219 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2220 | mirror::Class* ClassLinker::InsertClass(const char* descriptor, mirror::Class* klass, |
| 2221 | size_t hash) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2222 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2223 | mirror::DexCache* dex_cache = klass->GetDexCache(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 2224 | std::string source; |
| 2225 | if (dex_cache != NULL) { |
| 2226 | source += " from "; |
| 2227 | source += dex_cache->GetLocation()->ToModifiedUtf8(); |
| 2228 | } |
| 2229 | LOG(INFO) << "Loaded class " << descriptor << source; |
| 2230 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2231 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2232 | mirror::Class* existing = |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2233 | LookupClassFromTableLocked(descriptor, klass->GetClassLoader(), hash); |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 2234 | if (existing != NULL) { |
| 2235 | return existing; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 2236 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2237 | if (kIsDebugBuild && klass->GetClassLoader() == NULL && dex_cache_image_class_lookup_required_) { |
| 2238 | // Check a class loaded with the system class loader matches one in the image if the class |
| 2239 | // is in the image. |
| 2240 | existing = LookupClassFromImage(descriptor); |
| 2241 | if (existing != NULL) { |
| 2242 | CHECK(klass == existing); |
| 2243 | } |
| 2244 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2245 | Runtime::Current()->GetHeap()->VerifyObject(klass); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2246 | class_table_.insert(std::make_pair(hash, klass)); |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 2247 | class_table_dirty_ = true; |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 2248 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2249 | } |
| 2250 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2251 | bool ClassLinker::RemoveClass(const char* descriptor, const mirror::ClassLoader* class_loader) { |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 2252 | size_t hash = Hash(descriptor); |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2253 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2254 | for (auto it = class_table_.lower_bound(hash), end = class_table_.end(); it != end && it->first == hash; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2255 | ++it) { |
| 2256 | mirror::Class* klass = it->second; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2257 | ClassHelper kh(klass); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2258 | if ((klass->GetClassLoader() == class_loader) && |
| 2259 | (strcmp(descriptor, kh.GetDescriptor()) == 0)) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2260 | class_table_.erase(it); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 2261 | return true; |
| 2262 | } |
| 2263 | } |
| 2264 | return false; |
| 2265 | } |
| 2266 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2267 | mirror::Class* ClassLinker::LookupClass(const char* descriptor, |
| 2268 | const mirror::ClassLoader* class_loader) { |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 2269 | size_t hash = Hash(descriptor); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2270 | { |
| 2271 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 2272 | mirror::Class* result = LookupClassFromTableLocked(descriptor, class_loader, hash); |
| 2273 | if (result != NULL) { |
| 2274 | return result; |
| 2275 | } |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 2276 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2277 | if (class_loader != NULL || !dex_cache_image_class_lookup_required_) { |
| 2278 | return NULL; |
| 2279 | } else { |
| 2280 | // Lookup failed but need to search dex_caches_. |
| 2281 | mirror::Class* result = LookupClassFromImage(descriptor); |
| 2282 | if (result != NULL) { |
| 2283 | InsertClass(descriptor, result, hash); |
| 2284 | } else { |
| 2285 | // Searching the image dex files/caches failed, we don't want to get into this situation |
| 2286 | // often as map searches are faster, so after kMaxFailedDexCacheLookups move all image |
| 2287 | // classes into the class table. |
| 2288 | const int32_t kMaxFailedDexCacheLookups = 1000; |
| 2289 | if (++failed_dex_cache_class_lookups_ > kMaxFailedDexCacheLookups) { |
| 2290 | MoveImageClassesToClassTable(); |
| 2291 | } |
| 2292 | } |
| 2293 | return result; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2294 | } |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 2295 | } |
| 2296 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2297 | mirror::Class* ClassLinker::LookupClassFromTableLocked(const char* descriptor, |
| 2298 | const mirror::ClassLoader* class_loader, |
| 2299 | size_t hash) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2300 | auto end = class_table_.end(); |
| 2301 | for (auto it = class_table_.lower_bound(hash); it != end && it->first == hash; ++it) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2302 | mirror::Class* klass = it->second; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2303 | ClassHelper kh(klass); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2304 | if ((klass->GetClassLoader() == class_loader) && |
| 2305 | (strcmp(descriptor, kh.GetDescriptor()) == 0)) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2306 | if (kIsDebugBuild) { |
| 2307 | // Check for duplicates in the table. |
| 2308 | for (++it; it != end && it->first == hash; ++it) { |
| 2309 | mirror::Class* klass2 = it->second; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2310 | ClassHelper kh(klass2); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2311 | CHECK(!((klass2->GetClassLoader() == class_loader) && |
| 2312 | (strcmp(descriptor, kh.GetDescriptor()) == 0))) |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2313 | << PrettyClass(klass) << " " << klass << " " << klass->GetClassLoader() << " " |
| 2314 | << PrettyClass(klass2) << " " << klass2 << " " << klass2->GetClassLoader(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2315 | } |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 2316 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 2317 | return klass; |
| 2318 | } |
| 2319 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2320 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2321 | } |
| 2322 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2323 | static mirror::ObjectArray<mirror::DexCache>* GetImageDexCaches() |
| 2324 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2325 | gc::space::ImageSpace* image = Runtime::Current()->GetHeap()->GetImageSpace(); |
| 2326 | CHECK(image != NULL); |
| 2327 | mirror::Object* root = image->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
| 2328 | return root->AsObjectArray<mirror::DexCache>(); |
| 2329 | } |
| 2330 | |
| 2331 | void ClassLinker::MoveImageClassesToClassTable() { |
| 2332 | Thread* self = Thread::Current(); |
| 2333 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2334 | if (!dex_cache_image_class_lookup_required_) { |
| 2335 | return; // All dex cache classes are already in the class table. |
| 2336 | } |
| 2337 | const char* old_no_suspend_cause = |
| 2338 | self->StartAssertNoThreadSuspension("Moving image classes to class table"); |
| 2339 | mirror::ObjectArray<mirror::DexCache>* dex_caches = GetImageDexCaches(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2340 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 2341 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 2342 | mirror::ObjectArray<mirror::Class>* types = dex_cache->GetResolvedTypes(); |
| 2343 | for (int32_t j = 0; j < types->GetLength(); j++) { |
| 2344 | mirror::Class* klass = types->Get(j); |
| 2345 | if (klass != NULL) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2346 | ClassHelper kh(klass); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2347 | DCHECK(klass->GetClassLoader() == NULL); |
| 2348 | const char* descriptor = kh.GetDescriptor(); |
| 2349 | size_t hash = Hash(descriptor); |
| 2350 | mirror::Class* existing = LookupClassFromTableLocked(descriptor, NULL, hash); |
| 2351 | if (existing != NULL) { |
| 2352 | CHECK(existing == klass) << PrettyClassAndClassLoader(existing) << " != " |
| 2353 | << PrettyClassAndClassLoader(klass); |
| 2354 | } else { |
| 2355 | class_table_.insert(std::make_pair(hash, klass)); |
| 2356 | } |
| 2357 | } |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 2358 | } |
| 2359 | } |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 2360 | class_table_dirty_ = true; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2361 | dex_cache_image_class_lookup_required_ = false; |
| 2362 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
| 2363 | } |
| 2364 | |
| 2365 | mirror::Class* ClassLinker::LookupClassFromImage(const char* descriptor) { |
| 2366 | Thread* self = Thread::Current(); |
| 2367 | const char* old_no_suspend_cause = |
| 2368 | self->StartAssertNoThreadSuspension("Image class lookup"); |
| 2369 | mirror::ObjectArray<mirror::DexCache>* dex_caches = GetImageDexCaches(); |
| 2370 | for (int32_t i = 0; i < dex_caches->GetLength(); ++i) { |
| 2371 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 2372 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 2373 | // First search using the class def map, but don't bother for non-class types. |
| 2374 | if (descriptor[0] == 'L') { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2375 | const DexFile::StringId* descriptor_string_id = dex_file->FindStringId(descriptor); |
| 2376 | if (descriptor_string_id != NULL) { |
| 2377 | const DexFile::TypeId* type_id = |
| 2378 | dex_file->FindTypeId(dex_file->GetIndexForStringId(*descriptor_string_id)); |
| 2379 | if (type_id != NULL) { |
| 2380 | mirror::Class* klass = dex_cache->GetResolvedType(dex_file->GetIndexForTypeId(*type_id)); |
| 2381 | if (klass != NULL) { |
| 2382 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
| 2383 | return klass; |
| 2384 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2385 | } |
| 2386 | } |
| 2387 | } |
| 2388 | // Now try binary searching the string/type index. |
| 2389 | const DexFile::StringId* string_id = dex_file->FindStringId(descriptor); |
| 2390 | if (string_id != NULL) { |
| 2391 | const DexFile::TypeId* type_id = |
| 2392 | dex_file->FindTypeId(dex_file->GetIndexForStringId(*string_id)); |
| 2393 | if (type_id != NULL) { |
| 2394 | uint16_t type_idx = dex_file->GetIndexForTypeId(*type_id); |
| 2395 | mirror::Class* klass = dex_cache->GetResolvedType(type_idx); |
| 2396 | if (klass != NULL) { |
| 2397 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
| 2398 | return klass; |
| 2399 | } |
| 2400 | } |
| 2401 | } |
| 2402 | } |
| 2403 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
| 2404 | return NULL; |
| 2405 | } |
| 2406 | |
| 2407 | void ClassLinker::LookupClasses(const char* descriptor, std::vector<mirror::Class*>& result) { |
| 2408 | result.clear(); |
| 2409 | if (dex_cache_image_class_lookup_required_) { |
| 2410 | MoveImageClassesToClassTable(); |
| 2411 | } |
| 2412 | size_t hash = Hash(descriptor); |
| 2413 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2414 | for (auto it = class_table_.lower_bound(hash), end = class_table_.end(); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 2415 | it != end && it->first == hash; ++it) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2416 | mirror::Class* klass = it->second; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2417 | ClassHelper kh(klass); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2418 | if (strcmp(descriptor, kh.GetDescriptor()) == 0) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2419 | result.push_back(klass); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 2420 | } |
| 2421 | } |
| 2422 | } |
| 2423 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2424 | void ClassLinker::VerifyClass(mirror::Class* klass) { |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 2425 | // TODO: assert that the monitor on the Class is held |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 2426 | Thread* self = Thread::Current(); |
| 2427 | ObjectLock lock(self, klass); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 2428 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 2429 | // Don't attempt to re-verify if already sufficiently verified. |
| 2430 | if (klass->IsVerified() || |
| 2431 | (klass->IsCompileTimeVerified() && Runtime::Current()->IsCompiler())) { |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 2432 | return; |
| 2433 | } |
| 2434 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 2435 | // The class might already be erroneous, for example at compile time if we attempted to verify |
| 2436 | // this class as a parent to another. |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 2437 | if (klass->IsErroneous()) { |
| 2438 | ThrowEarlierClassFailure(klass); |
| 2439 | return; |
| 2440 | } |
| 2441 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2442 | if (klass->GetStatus() == mirror::Class::kStatusResolved) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2443 | klass->SetStatus(mirror::Class::kStatusVerifying, self); |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 2444 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2445 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime) |
| 2446 | << PrettyClass(klass); |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 2447 | CHECK(!Runtime::Current()->IsCompiler()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2448 | klass->SetStatus(mirror::Class::kStatusVerifyingAtRuntime, self); |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 2449 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 2450 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 2451 | // Verify super class. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 2452 | SirtRef<mirror::Class> super(self, klass->GetSuperClass()); |
| 2453 | if (super.get() != NULL) { |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 2454 | // Acquire lock to prevent races on verifying the super class. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 2455 | ObjectLock lock(self, super.get()); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 2456 | |
| 2457 | if (!super->IsVerified() && !super->IsErroneous()) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 2458 | VerifyClass(super.get()); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 2459 | } |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 2460 | if (!super->IsCompileTimeVerified()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2461 | std::string error_msg(StringPrintf("Rejecting class %s that attempts to sub-class erroneous class %s", |
| 2462 | PrettyDescriptor(klass).c_str(), |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 2463 | PrettyDescriptor(super.get()).c_str())); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 2464 | LOG(ERROR) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2465 | SirtRef<mirror::Throwable> cause(self, self->GetException(NULL)); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 2466 | if (cause.get() != NULL) { |
| 2467 | self->ClearException(); |
| 2468 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2469 | ThrowVerifyError(klass, "%s", error_msg.c_str()); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 2470 | if (cause.get() != NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2471 | self->GetException(NULL)->SetCause(cause.get()); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 2472 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2473 | klass->SetStatus(mirror::Class::kStatusError, self); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 2474 | return; |
| 2475 | } |
| 2476 | } |
| 2477 | |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 2478 | // Try to use verification information from the oat file, otherwise do runtime verification. |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 2479 | const DexFile& dex_file = *klass->GetDexCache()->GetDexFile(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2480 | mirror::Class::Status oat_file_class_status(mirror::Class::kStatusNotReady); |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 2481 | bool preverified = VerifyClassUsingOatFile(dex_file, klass, oat_file_class_status); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2482 | if (oat_file_class_status == mirror::Class::kStatusError) { |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 2483 | VLOG(class_linker) << "Skipping runtime verification of erroneous class " |
| 2484 | << PrettyDescriptor(klass) << " in " |
| 2485 | << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2486 | ThrowVerifyError(klass, "Rejecting class %s because it failed compile-time verification", |
| 2487 | PrettyDescriptor(klass).c_str()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2488 | klass->SetStatus(mirror::Class::kStatusError, self); |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 2489 | return; |
| 2490 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 2491 | verifier::MethodVerifier::FailureKind verifier_failure = verifier::MethodVerifier::kNoFailure; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2492 | std::string error_msg; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 2493 | if (!preverified) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2494 | verifier_failure = verifier::MethodVerifier::VerifyClass(klass, |
| 2495 | Runtime::Current()->IsCompiler(), |
| 2496 | &error_msg); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 2497 | } |
| 2498 | if (preverified || verifier_failure != verifier::MethodVerifier::kHardFailure) { |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 2499 | if (!preverified && verifier_failure != verifier::MethodVerifier::kNoFailure) { |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 2500 | VLOG(class_linker) << "Soft verification failure in class " << PrettyDescriptor(klass) |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 2501 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 2502 | << " because: " << error_msg; |
| 2503 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 2504 | self->AssertNoPendingException(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 2505 | // Make sure all classes referenced by catch blocks are resolved. |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 2506 | ResolveClassExceptionHandlerTypes(dex_file, klass); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 2507 | if (verifier_failure == verifier::MethodVerifier::kNoFailure) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 2508 | // Even though there were no verifier failures we need to respect whether the super-class |
| 2509 | // was verified or requiring runtime reverification. |
| 2510 | if (super.get() == NULL || super->IsVerified()) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2511 | klass->SetStatus(mirror::Class::kStatusVerified, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 2512 | } else { |
| 2513 | CHECK_EQ(super->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2514 | klass->SetStatus(mirror::Class::kStatusRetryVerificationAtRuntime, self); |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 2515 | // Pretend a soft failure occured so that we don't consider the class verified below. |
| 2516 | verifier_failure = verifier::MethodVerifier::kSoftFailure; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 2517 | } |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 2518 | } else { |
| 2519 | CHECK_EQ(verifier_failure, verifier::MethodVerifier::kSoftFailure); |
| 2520 | // Soft failures at compile time should be retried at runtime. Soft |
| 2521 | // failures at runtime will be handled by slow paths in the generated |
| 2522 | // code. Set status accordingly. |
| 2523 | if (Runtime::Current()->IsCompiler()) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2524 | klass->SetStatus(mirror::Class::kStatusRetryVerificationAtRuntime, self); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 2525 | } else { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2526 | klass->SetStatus(mirror::Class::kStatusVerified, self); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 2527 | } |
| 2528 | } |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 2529 | } else { |
Ian Rogers | 09f6b56 | 2012-01-31 21:58:52 -0800 | [diff] [blame] | 2530 | LOG(ERROR) << "Verification failed on class " << PrettyDescriptor(klass) |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 2531 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 2532 | << " because: " << error_msg; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2533 | self->AssertNoPendingException(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2534 | ThrowVerifyError(klass, "%s", error_msg.c_str()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2535 | klass->SetStatus(mirror::Class::kStatusError, self); |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 2536 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 2537 | if (preverified || verifier_failure == verifier::MethodVerifier::kNoFailure) { |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 2538 | // Class is verified so we don't need to do any access check on its methods. |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 2539 | // Let the interpreter know it by setting the kAccPreverified flag onto each |
| 2540 | // method. |
| 2541 | // Note: we're going here during compilation and at runtime. When we set the |
| 2542 | // kAccPreverified flag when compiling image classes, the flag is recorded |
| 2543 | // in the image and is set when loading the image. |
| 2544 | klass->SetPreverifiedFlagOnAllMethods(); |
| 2545 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 2546 | } |
| 2547 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2548 | bool ClassLinker::VerifyClassUsingOatFile(const DexFile& dex_file, mirror::Class* klass, |
| 2549 | mirror::Class::Status& oat_file_class_status) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 2550 | // If we're compiling, we can only verify the class using the oat file if |
| 2551 | // we are not compiling the image or if the class we're verifying is not part of |
| 2552 | // the app. In other words, we will only check for preverification of bootclasspath |
| 2553 | // classes. |
| 2554 | if (Runtime::Current()->IsCompiler()) { |
| 2555 | // Are we compiling the bootclasspath? |
| 2556 | if (!Runtime::Current()->UseCompileTimeClassPath()) { |
| 2557 | return false; |
| 2558 | } |
| 2559 | // We are compiling an app (not the image). |
| 2560 | |
| 2561 | // Is this an app class? (I.e. not a bootclasspath class) |
| 2562 | if (klass->GetClassLoader() != NULL) { |
| 2563 | return false; |
| 2564 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 2565 | } |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 2566 | |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 2567 | const OatFile* oat_file = FindOpenedOatFileForDexFile(dex_file); |
Anwar Ghuloum | ad256bb | 2013-07-18 14:58:55 -0700 | [diff] [blame] | 2568 | // Make this work with gtests, which do not set up the image properly. |
| 2569 | // TODO: we should clean up gtests to set up the image path properly. |
| 2570 | if (Runtime::Current()->IsCompiler() && (oat_file == NULL)) { |
| 2571 | return false; |
| 2572 | } |
| 2573 | |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 2574 | CHECK(oat_file != NULL) << dex_file.GetLocation() << " " << PrettyClass(klass); |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 2575 | uint dex_location_checksum = dex_file.GetLocationChecksum(); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 2576 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation().c_str(), |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 2577 | &dex_location_checksum); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 2578 | CHECK(oat_dex_file != NULL) << dex_file.GetLocation() << " " << PrettyClass(klass); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2579 | uint16_t class_def_index = klass->GetDexClassDefIndex(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 2580 | UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index)); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 2581 | CHECK(oat_class.get() != NULL) |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2582 | << dex_file.GetLocation() << " " << PrettyClass(klass) << " " |
| 2583 | << ClassHelper(klass).GetDescriptor(); |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 2584 | oat_file_class_status = oat_class->GetStatus(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2585 | if (oat_file_class_status == mirror::Class::kStatusVerified || |
| 2586 | oat_file_class_status == mirror::Class::kStatusInitialized) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 2587 | return true; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 2588 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2589 | if (oat_file_class_status == mirror::Class::kStatusRetryVerificationAtRuntime) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 2590 | // Compile time verification failed with a soft error. Compile time verification can fail |
| 2591 | // because we have incomplete type information. Consider the following: |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 2592 | // class ... { |
| 2593 | // Foo x; |
| 2594 | // .... () { |
| 2595 | // if (...) { |
| 2596 | // v1 gets assigned a type of resolved class Foo |
| 2597 | // } else { |
| 2598 | // v1 gets assigned a type of unresolved class Bar |
| 2599 | // } |
| 2600 | // iput x = v1 |
| 2601 | // } } |
| 2602 | // when we merge v1 following the if-the-else it results in Conflict |
| 2603 | // (see verifier::RegType::Merge) as we can't know the type of Bar and we could possibly be |
| 2604 | // allowing an unsafe assignment to the field x in the iput (javac may have compiled this as |
| 2605 | // it knew Bar was a sub-class of Foo, but for us this may have been moved into a separate apk |
| 2606 | // at compile time). |
| 2607 | return false; |
| 2608 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2609 | if (oat_file_class_status == mirror::Class::kStatusError) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 2610 | // Compile time verification failed with a hard error. This is caused by invalid instructions |
| 2611 | // in the class. These errors are unrecoverable. |
| 2612 | return false; |
| 2613 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2614 | if (oat_file_class_status == mirror::Class::kStatusNotReady) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 2615 | // Status is uninitialized if we couldn't determine the status at compile time, for example, |
| 2616 | // not loading the class. |
| 2617 | // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy |
| 2618 | // isn't a problem and this case shouldn't occur |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 2619 | return false; |
| 2620 | } |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 2621 | LOG(FATAL) << "Unexpected class status: " << oat_file_class_status |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2622 | << " " << dex_file.GetLocation() << " " << PrettyClass(klass) << " " |
| 2623 | << ClassHelper(klass).GetDescriptor(); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 2624 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 2625 | return false; |
| 2626 | } |
| 2627 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2628 | void ClassLinker::ResolveClassExceptionHandlerTypes(const DexFile& dex_file, mirror::Class* klass) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 2629 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
| 2630 | ResolveMethodExceptionHandlerTypes(dex_file, klass->GetDirectMethod(i)); |
| 2631 | } |
| 2632 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
| 2633 | ResolveMethodExceptionHandlerTypes(dex_file, klass->GetVirtualMethod(i)); |
| 2634 | } |
| 2635 | } |
| 2636 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2637 | void ClassLinker::ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2638 | mirror::ArtMethod* method) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 2639 | // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod. |
| 2640 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset()); |
| 2641 | if (code_item == NULL) { |
| 2642 | return; // native or abstract method |
| 2643 | } |
| 2644 | if (code_item->tries_size_ == 0) { |
| 2645 | return; // nothing to process |
| 2646 | } |
| 2647 | const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item, 0); |
| 2648 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 2649 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 2650 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 2651 | CatchHandlerIterator iterator(handlers_ptr); |
| 2652 | for (; iterator.HasNext(); iterator.Next()) { |
| 2653 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 2654 | // unresolved exception types will be ignored by exception delivery |
| 2655 | if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2656 | mirror::Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 2657 | if (exception_type == NULL) { |
| 2658 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 2659 | Thread::Current()->ClearException(); |
| 2660 | } |
| 2661 | } |
| 2662 | } |
| 2663 | handlers_ptr = iterator.EndDataPointer(); |
| 2664 | } |
| 2665 | } |
| 2666 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2667 | static void CheckProxyConstructor(mirror::ArtMethod* constructor); |
| 2668 | static void CheckProxyMethod(mirror::ArtMethod* method, |
| 2669 | SirtRef<mirror::ArtMethod>& prototype); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2670 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2671 | mirror::Class* ClassLinker::CreateProxyClass(ScopedObjectAccess& soa, jstring name, |
| 2672 | jobjectArray interfaces, jobject loader, |
| 2673 | jobjectArray methods, jobjectArray throws) { |
| 2674 | Thread* self = soa.Self(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2675 | SirtRef<mirror::Class> klass(self, AllocClass(self, GetClassRoot(kJavaLangClass), |
| 2676 | sizeof(mirror::SynthesizedProxyClass))); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2677 | if (klass.get() == NULL) { |
| 2678 | CHECK(self->IsExceptionPending()); // OOME. |
| 2679 | return NULL; |
| 2680 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2681 | DCHECK(klass->GetClass() != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2682 | klass->SetObjectSize(sizeof(mirror::Proxy)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2683 | klass->SetAccessFlags(kAccClassIsProxy | kAccPublic | kAccFinal); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2684 | klass->SetClassLoader(soa.Decode<mirror::ClassLoader*>(loader)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2685 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2686 | klass->SetName(soa.Decode<mirror::String*>(name)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2687 | mirror::Class* proxy_class = GetClassRoot(kJavaLangReflectProxy); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2688 | klass->SetDexCache(proxy_class->GetDexCache()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2689 | klass->SetStatus(mirror::Class::kStatusIdx, self); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2690 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 2691 | // Instance fields are inherited, but we add a couple of static fields... |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2692 | { |
| 2693 | mirror::ObjectArray<mirror::ArtField>* sfields = AllocArtFieldArray(self, 2); |
| 2694 | if (UNLIKELY(sfields == NULL)) { |
| 2695 | CHECK(self->IsExceptionPending()); // OOME. |
| 2696 | return NULL; |
| 2697 | } |
| 2698 | klass->SetSFields(sfields); |
| 2699 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 2700 | // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by |
| 2701 | // our proxy, so Class.getInterfaces doesn't return the flattened set. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2702 | SirtRef<mirror::ArtField> interfaces_sfield(self, AllocArtField(self)); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2703 | if (UNLIKELY(interfaces_sfield.get() == NULL)) { |
| 2704 | CHECK(self->IsExceptionPending()); // OOME. |
| 2705 | return NULL; |
| 2706 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 2707 | klass->SetStaticField(0, interfaces_sfield.get()); |
| 2708 | interfaces_sfield->SetDexFieldIndex(0); |
| 2709 | interfaces_sfield->SetDeclaringClass(klass.get()); |
| 2710 | interfaces_sfield->SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
| 2711 | // 2. Create a static field 'throws' that holds exceptions thrown by our methods. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2712 | SirtRef<mirror::ArtField> throws_sfield(self, AllocArtField(self)); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2713 | if (UNLIKELY(throws_sfield.get() == NULL)) { |
| 2714 | CHECK(self->IsExceptionPending()); // OOME. |
| 2715 | return NULL; |
| 2716 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 2717 | klass->SetStaticField(1, throws_sfield.get()); |
| 2718 | throws_sfield->SetDexFieldIndex(1); |
| 2719 | throws_sfield->SetDeclaringClass(klass.get()); |
| 2720 | throws_sfield->SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2721 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2722 | // Proxies have 1 direct method, the constructor |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2723 | { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2724 | mirror::ObjectArray<mirror::ArtMethod>* directs = AllocArtMethodArray(self, 1); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2725 | if (UNLIKELY(directs == NULL)) { |
| 2726 | CHECK(self->IsExceptionPending()); // OOME. |
| 2727 | return NULL; |
| 2728 | } |
| 2729 | klass->SetDirectMethods(directs); |
| 2730 | mirror::ArtMethod* constructor = CreateProxyConstructor(self, klass, proxy_class); |
| 2731 | if (UNLIKELY(constructor == NULL)) { |
| 2732 | CHECK(self->IsExceptionPending()); // OOME. |
| 2733 | return NULL; |
| 2734 | } |
| 2735 | klass->SetDirectMethod(0, constructor); |
| 2736 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2737 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2738 | // Create virtual method using specified prototypes. |
| 2739 | size_t num_virtual_methods = |
| 2740 | soa.Decode<mirror::ObjectArray<mirror::ArtMethod>*>(methods)->GetLength(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2741 | { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2742 | mirror::ObjectArray<mirror::ArtMethod>* virtuals = AllocArtMethodArray(self, num_virtual_methods); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2743 | if (UNLIKELY(virtuals == NULL)) { |
| 2744 | CHECK(self->IsExceptionPending()); // OOME. |
| 2745 | return NULL; |
| 2746 | } |
| 2747 | klass->SetVirtualMethods(virtuals); |
| 2748 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2749 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2750 | mirror::ObjectArray<mirror::ArtMethod>* decoded_methods = |
| 2751 | soa.Decode<mirror::ObjectArray<mirror::ArtMethod>*>(methods); |
| 2752 | SirtRef<mirror::ArtMethod> prototype(self, decoded_methods->Get(i)); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2753 | mirror::ArtMethod* clone = CreateProxyMethod(self, klass, prototype); |
| 2754 | if (UNLIKELY(clone == NULL)) { |
| 2755 | CHECK(self->IsExceptionPending()); // OOME. |
| 2756 | return NULL; |
| 2757 | } |
| 2758 | klass->SetVirtualMethod(i, clone); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2759 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2760 | |
| 2761 | klass->SetSuperClass(proxy_class); // The super class is java.lang.reflect.Proxy |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2762 | klass->SetStatus(mirror::Class::kStatusLoaded, self); // Class is now effectively in the loaded state |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2763 | self->AssertNoPendingException(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2764 | |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 2765 | { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2766 | ObjectLock lock(self, klass.get()); // Must hold lock on object when resolved. |
| 2767 | // Link the fields and virtual methods, creating vtable and iftables |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2768 | SirtRef<mirror::ObjectArray<mirror::Class> > sirt_interfaces( |
| 2769 | self, soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces)); |
| 2770 | if (!LinkClass(self, klass, sirt_interfaces)) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2771 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 2772 | return NULL; |
| 2773 | } |
| 2774 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2775 | interfaces_sfield->SetObject(klass.get(), soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces)); |
| 2776 | throws_sfield->SetObject(klass.get(), soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class> >*>(throws)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2777 | klass->SetStatus(mirror::Class::kStatusInitialized, self); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 2778 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2779 | |
| 2780 | // sanity checks |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 2781 | if (kIsDebugBuild) { |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2782 | CHECK(klass->GetIFields() == NULL); |
| 2783 | CheckProxyConstructor(klass->GetDirectMethod(0)); |
| 2784 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2785 | mirror::ObjectArray<mirror::ArtMethod>* decoded_methods = |
| 2786 | soa.Decode<mirror::ObjectArray<mirror::ArtMethod>*>(methods); |
| 2787 | SirtRef<mirror::ArtMethod> prototype(self, decoded_methods->Get(i)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2788 | CheckProxyMethod(klass->GetVirtualMethod(i), prototype); |
| 2789 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 2790 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2791 | mirror::String* decoded_name = soa.Decode<mirror::String*>(name); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 2792 | std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2793 | decoded_name->ToModifiedUtf8().c_str())); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 2794 | CHECK_EQ(PrettyField(klass->GetStaticField(0)), interfaces_field_name); |
| 2795 | |
| 2796 | std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2797 | decoded_name->ToModifiedUtf8().c_str())); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 2798 | CHECK_EQ(PrettyField(klass->GetStaticField(1)), throws_field_name); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2799 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2800 | mirror::SynthesizedProxyClass* synth_proxy_class = |
| 2801 | down_cast<mirror::SynthesizedProxyClass*>(klass.get()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2802 | CHECK_EQ(synth_proxy_class->GetInterfaces(), soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces)); |
| 2803 | CHECK_EQ(synth_proxy_class->GetThrows(), soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class> >*>(throws)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2804 | } |
Mathieu Chartier | 31b9d66 | 2013-10-14 11:53:12 -0700 | [diff] [blame] | 2805 | std::string descriptor(GetDescriptorForProxy(klass.get())); |
| 2806 | mirror::Class* existing = InsertClass(descriptor.c_str(), klass.get(), Hash(descriptor.c_str())); |
| 2807 | CHECK(existing == nullptr); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2808 | return klass.get(); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2809 | } |
| 2810 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2811 | std::string ClassLinker::GetDescriptorForProxy(const mirror::Class* proxy_class) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2812 | DCHECK(proxy_class->IsProxyClass()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2813 | mirror::String* name = proxy_class->GetName(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2814 | DCHECK(name != NULL); |
| 2815 | return DotToDescriptor(name->ToModifiedUtf8().c_str()); |
| 2816 | } |
| 2817 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2818 | mirror::ArtMethod* ClassLinker::FindMethodForProxy(const mirror::Class* proxy_class, |
| 2819 | const mirror::ArtMethod* proxy_method) { |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 2820 | DCHECK(proxy_class->IsProxyClass()); |
| 2821 | DCHECK(proxy_method->IsProxyMethod()); |
| 2822 | // Locate the dex cache of the original interface/Object |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2823 | mirror::DexCache* dex_cache = NULL; |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 2824 | { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2825 | mirror::ObjectArray<mirror::Class>* resolved_types = proxy_method->GetDexCacheResolvedTypes(); |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2826 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 2827 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
| 2828 | if (dex_caches_[i]->GetResolvedTypes() == resolved_types) { |
| 2829 | dex_cache = dex_caches_[i]; |
| 2830 | break; |
| 2831 | } |
| 2832 | } |
| 2833 | } |
| 2834 | CHECK(dex_cache != NULL); |
| 2835 | uint32_t method_idx = proxy_method->GetDexMethodIndex(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2836 | mirror::ArtMethod* resolved_method = dex_cache->GetResolvedMethod(method_idx); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 2837 | CHECK(resolved_method != NULL); |
| 2838 | return resolved_method; |
| 2839 | } |
| 2840 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2841 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2842 | mirror::ArtMethod* ClassLinker::CreateProxyConstructor(Thread* self, |
| 2843 | SirtRef<mirror::Class>& klass, |
| 2844 | mirror::Class* proxy_class) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2845 | // Create constructor for Proxy that must initialize h |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2846 | mirror::ObjectArray<mirror::ArtMethod>* proxy_direct_methods = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2847 | proxy_class->GetDirectMethods(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2848 | CHECK_EQ(proxy_direct_methods->GetLength(), 16); |
| 2849 | mirror::ArtMethod* proxy_constructor = proxy_direct_methods->Get(2); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2850 | // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its |
| 2851 | // code_ too) |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2852 | mirror::ArtMethod* constructor = |
| 2853 | down_cast<mirror::ArtMethod*>(proxy_constructor->Clone(self)); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2854 | if (constructor == NULL) { |
| 2855 | CHECK(self->IsExceptionPending()); // OOME. |
| 2856 | return NULL; |
| 2857 | } |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2858 | // Make this constructor public and fix the class to be our Proxy version |
| 2859 | constructor->SetAccessFlags((constructor->GetAccessFlags() & ~kAccProtected) | kAccPublic); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2860 | constructor->SetDeclaringClass(klass.get()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2861 | return constructor; |
| 2862 | } |
| 2863 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2864 | static void CheckProxyConstructor(mirror::ArtMethod* constructor) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 2865 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2866 | CHECK(constructor->IsConstructor()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2867 | MethodHelper mh(constructor); |
| 2868 | CHECK_STREQ(mh.GetName(), "<init>"); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 2869 | CHECK_STREQ(mh.GetSignature().ToString().c_str(), "(Ljava/lang/reflect/InvocationHandler;)V"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2870 | DCHECK(constructor->IsPublic()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2871 | } |
| 2872 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2873 | mirror::ArtMethod* ClassLinker::CreateProxyMethod(Thread* self, SirtRef<mirror::Class>& klass, |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 2874 | SirtRef<mirror::ArtMethod>& prototype) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2875 | // Ensure prototype is in dex cache so that we can use the dex cache to look up the overridden |
| 2876 | // prototype method |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 2877 | prototype->GetDeclaringClass()->GetDexCache()->SetResolvedMethod(prototype->GetDexMethodIndex(), |
| 2878 | prototype.get()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2879 | // We steal everything from the prototype (such as DexCache, invoke stub, etc.) then specialize |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2880 | // as necessary |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2881 | mirror::ArtMethod* method = down_cast<mirror::ArtMethod*>(prototype->Clone(self)); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2882 | if (UNLIKELY(method == NULL)) { |
| 2883 | CHECK(self->IsExceptionPending()); // OOME. |
| 2884 | return NULL; |
| 2885 | } |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2886 | |
| 2887 | // Set class to be the concrete proxy class and clear the abstract flag, modify exceptions to |
| 2888 | // the intersection of throw exceptions as defined in Proxy |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2889 | method->SetDeclaringClass(klass.get()); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2890 | method->SetAccessFlags((method->GetAccessFlags() & ~kAccAbstract) | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2891 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2892 | // At runtime the method looks like a reference and argument saving method, clone the code |
| 2893 | // related parameters from this method. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2894 | mirror::ArtMethod* refs_and_args = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2895 | Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2896 | method->SetCoreSpillMask(refs_and_args->GetCoreSpillMask()); |
| 2897 | method->SetFpSpillMask(refs_and_args->GetFpSpillMask()); |
| 2898 | method->SetFrameSizeInBytes(refs_and_args->GetFrameSizeInBytes()); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2899 | method->SetEntryPointFromCompiledCode(GetProxyInvokeHandler()); |
Dragos Sbirlea | 08bf196 | 2013-08-12 08:53:04 -0700 | [diff] [blame] | 2900 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 2901 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2902 | return method; |
| 2903 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2904 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2905 | static void CheckProxyMethod(mirror::ArtMethod* method, |
| 2906 | SirtRef<mirror::ArtMethod>& prototype) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 2907 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2908 | // Basic sanity |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2909 | CHECK(!prototype->IsFinal()); |
| 2910 | CHECK(method->IsFinal()); |
| 2911 | CHECK(!method->IsAbstract()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2912 | |
| 2913 | // The proxy method doesn't have its own dex cache or dex file and so it steals those of its |
| 2914 | // interface prototype. The exception to this are Constructors and the Class of the Proxy itself. |
| 2915 | CHECK_EQ(prototype->GetDexCacheStrings(), method->GetDexCacheStrings()); |
| 2916 | CHECK_EQ(prototype->GetDexCacheResolvedMethods(), method->GetDexCacheResolvedMethods()); |
| 2917 | CHECK_EQ(prototype->GetDexCacheResolvedTypes(), method->GetDexCacheResolvedTypes()); |
| 2918 | CHECK_EQ(prototype->GetDexCacheInitializedStaticStorage(), |
| 2919 | method->GetDexCacheInitializedStaticStorage()); |
| 2920 | CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex()); |
| 2921 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2922 | MethodHelper mh(method); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2923 | MethodHelper mh2(prototype.get()); |
| 2924 | CHECK_STREQ(mh.GetName(), mh2.GetName()); |
| 2925 | CHECK_STREQ(mh.GetShorty(), mh2.GetShorty()); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 2926 | // More complex sanity - via dex cache |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2927 | CHECK_EQ(mh.GetReturnType(), mh2.GetReturnType()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 2928 | } |
| 2929 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 2930 | static bool CanWeInitializeClass(mirror::Class* klass, bool can_init_statics, |
| 2931 | bool can_init_parents) |
| 2932 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 2933 | if (can_init_statics && can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 2934 | return true; |
| 2935 | } |
| 2936 | if (!can_init_statics) { |
| 2937 | // Check if there's a class initializer. |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 2938 | mirror::ArtMethod* clinit = klass->FindClassInitializer(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 2939 | if (clinit != NULL) { |
| 2940 | return false; |
| 2941 | } |
| 2942 | // Check if there are encoded static values needing initialization. |
| 2943 | if (klass->NumStaticFields() != 0) { |
| 2944 | ClassHelper kh(klass); |
| 2945 | const DexFile::ClassDef* dex_class_def = kh.GetClassDef(); |
| 2946 | DCHECK(dex_class_def != NULL); |
| 2947 | if (dex_class_def->static_values_off_ != 0) { |
| 2948 | return false; |
| 2949 | } |
| 2950 | } |
| 2951 | } |
| 2952 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
| 2953 | mirror::Class* super_class = klass->GetSuperClass(); |
| 2954 | if (!can_init_parents && !super_class->IsInitialized()) { |
| 2955 | return false; |
| 2956 | } else { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 2957 | if (!CanWeInitializeClass(super_class, can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 2958 | return false; |
| 2959 | } |
| 2960 | } |
| 2961 | } |
| 2962 | return true; |
| 2963 | } |
| 2964 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2965 | bool ClassLinker::IsInitialized() const { |
| 2966 | return init_done_; |
| 2967 | } |
| 2968 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 2969 | bool ClassLinker::InitializeClass(mirror::Class* klass, bool can_init_statics, |
| 2970 | bool can_init_parents) { |
| 2971 | // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol |
| 2972 | |
| 2973 | // Are we already initialized and therefore done? |
| 2974 | // Note: we differ from the JLS here as we don't do this under the lock, this is benign as |
| 2975 | // an initialized class will never change its state. |
| 2976 | if (klass->IsInitialized()) { |
| 2977 | return true; |
| 2978 | } |
| 2979 | |
| 2980 | // Fast fail if initialization requires a full runtime. Not part of the JLS. |
| 2981 | if (!CanWeInitializeClass(klass, can_init_statics, can_init_parents)) { |
| 2982 | return false; |
| 2983 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2984 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 2985 | Thread* self = Thread::Current(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 2986 | uint64_t t0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2987 | { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 2988 | ObjectLock lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2989 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 2990 | // Re-check under the lock in case another thread initialized ahead of us. |
| 2991 | if (klass->IsInitialized()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 2992 | return true; |
| 2993 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2994 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 2995 | // Was the class already found to be erroneous? Done under the lock to match the JLS. |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 2996 | if (klass->IsErroneous()) { |
| 2997 | ThrowEarlierClassFailure(klass); |
| 2998 | return false; |
| 2999 | } |
| 3000 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3001 | CHECK(klass->IsResolved()) << PrettyClass(klass) << ": state=" << klass->GetStatus(); |
| 3002 | |
| 3003 | if (!klass->IsVerified()) { |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3004 | VerifyClass(klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3005 | if (!klass->IsVerified()) { |
| 3006 | // We failed to verify, expect either the klass to be erroneous or verification failed at |
| 3007 | // compile time. |
| 3008 | if (klass->IsErroneous()) { |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 3009 | CHECK(self->IsExceptionPending()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3010 | } else { |
| 3011 | CHECK(Runtime::Current()->IsCompiler()); |
| 3012 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 3013 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3014 | return false; |
| 3015 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3016 | } |
| 3017 | |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3018 | // If the class is kStatusInitializing, either this thread is |
| 3019 | // initializing higher up the stack or another thread has beat us |
| 3020 | // to initializing and we need to wait. Either way, this |
| 3021 | // invocation of InitializeClass will not be responsible for |
| 3022 | // running <clinit> and will return. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3023 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 3024 | // We caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 3025 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3026 | // Yes. That's fine. Return so we can continue initializing. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3027 | return true; |
| 3028 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3029 | // No. That's fine. Wait for another thread to finish initializing. |
| 3030 | return WaitForInitializeClass(klass, self, lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3031 | } |
| 3032 | |
| 3033 | if (!ValidateSuperClassDescriptors(klass)) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3034 | klass->SetStatus(mirror::Class::kStatusError, self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3035 | return false; |
| 3036 | } |
| 3037 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3038 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusVerified) << PrettyClass(klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3039 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3040 | // From here out other threads may observe that we're initializing and so changes of state |
| 3041 | // require the a notification. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 3042 | klass->SetClinitThreadId(self->GetTid()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3043 | klass->SetStatus(mirror::Class::kStatusInitializing, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3044 | |
| 3045 | t0 = NanoTime(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3046 | } |
| 3047 | |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 3048 | // Initialize super classes, must be done while initializing for the JLS. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3049 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
| 3050 | mirror::Class* super_class = klass->GetSuperClass(); |
| 3051 | if (!super_class->IsInitialized()) { |
| 3052 | CHECK(!super_class->IsInterface()); |
| 3053 | CHECK(can_init_parents); |
| 3054 | bool super_initialized = InitializeClass(super_class, can_init_statics, true); |
| 3055 | if (!super_initialized) { |
| 3056 | // The super class was verified ahead of entering initializing, we should only be here if |
| 3057 | // the super class became erroneous due to initialization. |
| 3058 | CHECK(super_class->IsErroneous() && self->IsExceptionPending()) |
| 3059 | << "Super class initialization failed for " << PrettyDescriptor(super_class) |
| 3060 | << " that has unexpected status " << super_class->GetStatus() |
| 3061 | << "\nPending exception:\n" |
| 3062 | << (self->GetException(NULL) != NULL ? self->GetException(NULL)->Dump() : ""); |
| 3063 | ObjectLock lock(self, klass); |
| 3064 | // Initialization failed because the super-class is erroneous. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3065 | klass->SetStatus(mirror::Class::kStatusError, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3066 | return false; |
| 3067 | } |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 3068 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3069 | } |
| 3070 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3071 | if (klass->NumStaticFields() > 0) { |
| 3072 | ClassHelper kh(klass); |
| 3073 | const DexFile::ClassDef* dex_class_def = kh.GetClassDef(); |
| 3074 | CHECK(dex_class_def != NULL); |
| 3075 | const DexFile& dex_file = kh.GetDexFile(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3076 | SirtRef<mirror::ClassLoader> class_loader(self, klass->GetClassLoader()); |
| 3077 | SirtRef<mirror::DexCache> dex_cache(self, kh.GetDexCache()); |
| 3078 | EncodedStaticFieldValueIterator it(dex_file, &dex_cache, &class_loader, |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3079 | this, *dex_class_def); |
| 3080 | if (it.HasNext()) { |
| 3081 | CHECK(can_init_statics); |
| 3082 | // We reordered the fields, so we need to be able to map the field indexes to the right fields. |
| 3083 | SafeMap<uint32_t, mirror::ArtField*> field_map; |
| 3084 | ConstructFieldMap(dex_file, *dex_class_def, klass, field_map); |
| 3085 | for (size_t i = 0; it.HasNext(); i++, it.Next()) { |
| 3086 | it.ReadValueToField(field_map.Get(i)); |
| 3087 | } |
| 3088 | } |
| 3089 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3090 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 3091 | mirror::ArtMethod* clinit = klass->FindClassInitializer(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3092 | if (clinit != NULL) { |
| 3093 | CHECK(can_init_statics); |
| 3094 | if (LIKELY(Runtime::Current()->IsStarted())) { |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 3095 | JValue result; |
Jeff Hao | 6474d19 | 2013-03-26 14:08:09 -0700 | [diff] [blame] | 3096 | clinit->Invoke(self, NULL, 0, &result, 'V'); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 3097 | } else { |
Jeff Hao | 6474d19 | 2013-03-26 14:08:09 -0700 | [diff] [blame] | 3098 | art::interpreter::EnterInterpreterFromInvoke(self, clinit, NULL, NULL, NULL); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 3099 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3100 | } |
| 3101 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3102 | // Opportunistically set static method trampolines to their destination. |
| 3103 | FixupStaticTrampolines(klass); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3104 | |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 3105 | uint64_t t1 = NanoTime(); |
| 3106 | |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 3107 | bool success = true; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3108 | { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3109 | ObjectLock lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3110 | |
| 3111 | if (self->IsExceptionPending()) { |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 3112 | WrapExceptionInInitializer(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3113 | klass->SetStatus(mirror::Class::kStatusError, self); |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 3114 | success = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3115 | } else { |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 3116 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 3117 | RuntimeStats* thread_stats = self->GetStats(); |
| 3118 | ++global_stats->class_init_count; |
| 3119 | ++thread_stats->class_init_count; |
| 3120 | global_stats->class_init_time_ns += (t1 - t0); |
| 3121 | thread_stats->class_init_time_ns += (t1 - t0); |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 3122 | // Set the class as initialized except if failed to initialize static fields. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3123 | klass->SetStatus(mirror::Class::kStatusInitialized, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3124 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3125 | ClassHelper kh(klass); |
| 3126 | LOG(INFO) << "Initialized class " << kh.GetDescriptor() << " from " << kh.GetLocation(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3127 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3128 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3129 | } |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 3130 | return success; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3131 | } |
| 3132 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3133 | bool ClassLinker::WaitForInitializeClass(mirror::Class* klass, Thread* self, ObjectLock& lock) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 3134 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3135 | while (true) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3136 | self->AssertNoPendingException(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3137 | CHECK(!klass->IsInitialized()); |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 3138 | lock.WaitIgnoringInterrupts(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3139 | |
| 3140 | // When we wake up, repeat the test for init-in-progress. If |
| 3141 | // there's an exception pending (only possible if |
| 3142 | // "interruptShouldThrow" was set), bail out. |
| 3143 | if (self->IsExceptionPending()) { |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 3144 | WrapExceptionInInitializer(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3145 | klass->SetStatus(mirror::Class::kStatusError, self); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3146 | return false; |
| 3147 | } |
| 3148 | // Spurious wakeup? Go back to waiting. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3149 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3150 | continue; |
| 3151 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3152 | if (klass->GetStatus() == mirror::Class::kStatusVerified && Runtime::Current()->IsCompiler()) { |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 3153 | // Compile time initialization failed. |
| 3154 | return false; |
| 3155 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3156 | if (klass->IsErroneous()) { |
| 3157 | // The caller wants an exception, but it was thrown in a |
| 3158 | // different thread. Synthesize one here. |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 3159 | ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3160 | PrettyDescriptor(klass).c_str()); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3161 | return false; |
| 3162 | } |
| 3163 | if (klass->IsInitialized()) { |
| 3164 | return true; |
| 3165 | } |
| 3166 | LOG(FATAL) << "Unexpected class status. " << PrettyClass(klass) << " is " << klass->GetStatus(); |
| 3167 | } |
| 3168 | LOG(FATAL) << "Not Reached" << PrettyClass(klass); |
| 3169 | } |
| 3170 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3171 | bool ClassLinker::ValidateSuperClassDescriptors(const mirror::Class* klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3172 | if (klass->IsInterface()) { |
| 3173 | return true; |
| 3174 | } |
| 3175 | // begin with the methods local to the superclass |
| 3176 | if (klass->HasSuperClass() && |
| 3177 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3178 | const mirror::Class* super = klass->GetSuperClass(); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 3179 | for (int i = super->GetVTable()->GetLength() - 1; i >= 0; --i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3180 | const mirror::ArtMethod* method = klass->GetVTable()->Get(i); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 3181 | if (method != super->GetVTable()->Get(i) && |
| 3182 | !IsSameMethodSignatureInDifferentClassContexts(method, super, klass)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3183 | ThrowLinkageError(klass, "Class %s method %s resolves differently in superclass %s", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3184 | PrettyDescriptor(klass).c_str(), PrettyMethod(method).c_str(), |
| 3185 | PrettyDescriptor(super).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3186 | return false; |
| 3187 | } |
| 3188 | } |
| 3189 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 3190 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3191 | mirror::Class* interface = klass->GetIfTable()->GetInterface(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3192 | if (klass->GetClassLoader() != interface->GetClassLoader()) { |
| 3193 | for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3194 | const mirror::ArtMethod* method = klass->GetIfTable()->GetMethodArray(i)->Get(j); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 3195 | if (!IsSameMethodSignatureInDifferentClassContexts(method, interface, |
| 3196 | method->GetDeclaringClass())) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3197 | ThrowLinkageError(klass, "Class %s method %s resolves differently in interface %s", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3198 | PrettyDescriptor(method->GetDeclaringClass()).c_str(), |
| 3199 | PrettyMethod(method).c_str(), |
| 3200 | PrettyDescriptor(interface).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3201 | return false; |
| 3202 | } |
| 3203 | } |
| 3204 | } |
| 3205 | } |
| 3206 | return true; |
| 3207 | } |
| 3208 | |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 3209 | // Returns true if classes referenced by the signature of the method are the |
| 3210 | // same classes in klass1 as they are in klass2. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3211 | bool ClassLinker::IsSameMethodSignatureInDifferentClassContexts(const mirror::ArtMethod* method, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3212 | const mirror::Class* klass1, |
| 3213 | const mirror::Class* klass2) { |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 3214 | if (klass1 == klass2) { |
| 3215 | return true; |
Brian Carlstrom | e10b697 | 2011-09-26 13:49:03 -0700 | [diff] [blame] | 3216 | } |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 3217 | const DexFile& dex_file = *method->GetDeclaringClass()->GetDexCache()->GetDexFile(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3218 | const DexFile::ProtoId& proto_id = |
| 3219 | dex_file.GetMethodPrototype(dex_file.GetMethodId(method->GetDexMethodIndex())); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3220 | for (DexFileParameterIterator it(dex_file, proto_id); it.HasNext(); it.Next()) { |
| 3221 | const char* descriptor = it.GetDescriptor(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3222 | if (descriptor == NULL) { |
| 3223 | break; |
| 3224 | } |
| 3225 | if (descriptor[0] == 'L' || descriptor[0] == '[') { |
| 3226 | // Found a non-primitive type. |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 3227 | if (!IsSameDescriptorInDifferentClassContexts(descriptor, klass1, klass2)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3228 | return false; |
| 3229 | } |
| 3230 | } |
| 3231 | } |
| 3232 | // Check the return type |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3233 | const char* descriptor = dex_file.GetReturnTypeDescriptor(proto_id); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3234 | if (descriptor[0] == 'L' || descriptor[0] == '[') { |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 3235 | if (!IsSameDescriptorInDifferentClassContexts(descriptor, klass1, klass2)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3236 | return false; |
| 3237 | } |
| 3238 | } |
| 3239 | return true; |
| 3240 | } |
| 3241 | |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 3242 | // Returns true if the descriptor resolves to the same class in the context of klass1 and klass2. |
| 3243 | bool ClassLinker::IsSameDescriptorInDifferentClassContexts(const char* descriptor, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3244 | const mirror::Class* klass1, |
| 3245 | const mirror::Class* klass2) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3246 | CHECK(descriptor != NULL); |
| 3247 | CHECK(klass1 != NULL); |
| 3248 | CHECK(klass2 != NULL); |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 3249 | if (klass1 == klass2) { |
| 3250 | return true; |
| 3251 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3252 | Thread* self = Thread::Current(); |
| 3253 | SirtRef<mirror::ClassLoader> class_loader1(self, klass1->GetClassLoader()); |
| 3254 | mirror::Class* found1 = FindClass(descriptor, class_loader1); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 3255 | if (found1 == NULL) { |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 3256 | Thread::Current()->ClearException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3257 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3258 | SirtRef<mirror::ClassLoader> class_loader2(self, klass2->GetClassLoader()); |
| 3259 | mirror::Class* found2 = FindClass(descriptor, class_loader2); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 3260 | if (found2 == NULL) { |
| 3261 | Thread::Current()->ClearException(); |
| 3262 | } |
| 3263 | return found1 == found2; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3264 | } |
| 3265 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3266 | bool ClassLinker::EnsureInitialized(mirror::Class* c, bool can_init_fields, bool can_init_parents) { |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 3267 | DCHECK(c != NULL); |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 3268 | if (c->IsInitialized()) { |
| 3269 | return true; |
| 3270 | } |
| 3271 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3272 | bool success = InitializeClass(c, can_init_fields, can_init_parents); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 3273 | if (!success) { |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 3274 | Thread* self = Thread::Current(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3275 | CHECK(self->IsExceptionPending() || !can_init_fields || !can_init_parents) << PrettyClass(c); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 3276 | } |
| 3277 | return success; |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 3278 | } |
| 3279 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 3280 | void ClassLinker::ConstructFieldMap(const DexFile& dex_file, const DexFile::ClassDef& dex_class_def, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3281 | mirror::Class* c, |
| 3282 | SafeMap<uint32_t, mirror::ArtField*>& field_map) { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 3283 | const byte* class_data = dex_file.GetClassData(dex_class_def); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3284 | ClassDataItemIterator it(dex_file, class_data); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3285 | Thread* self = Thread::Current(); |
| 3286 | SirtRef<mirror::DexCache> dex_cache(self, c->GetDexCache()); |
| 3287 | SirtRef<mirror::ClassLoader> class_loader(self, c->GetClassLoader()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3288 | for (size_t i = 0; it.HasNextStaticField(); i++, it.Next()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3289 | field_map.Put(i, ResolveField(dex_file, it.GetMemberIndex(), dex_cache, class_loader, true)); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 3290 | } |
| 3291 | } |
| 3292 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3293 | bool ClassLinker::LinkClass(Thread* self, SirtRef<mirror::Class>& klass, |
| 3294 | SirtRef<mirror::ObjectArray<mirror::Class> >& interfaces) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3295 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3296 | if (!LinkSuperClass(klass)) { |
| 3297 | return false; |
| 3298 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3299 | if (!LinkMethods(klass, interfaces)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3300 | return false; |
| 3301 | } |
| 3302 | if (!LinkInstanceFields(klass)) { |
| 3303 | return false; |
| 3304 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3305 | if (!LinkStaticFields(klass)) { |
| 3306 | return false; |
| 3307 | } |
| 3308 | CreateReferenceInstanceOffsets(klass); |
| 3309 | CreateReferenceStaticOffsets(klass); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3310 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3311 | klass->SetStatus(mirror::Class::kStatusResolved, self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3312 | return true; |
| 3313 | } |
| 3314 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3315 | bool ClassLinker::LoadSuperAndInterfaces(SirtRef<mirror::Class>& klass, const DexFile& dex_file) { |
| 3316 | CHECK_EQ(mirror::Class::kStatusIdx, klass->GetStatus()); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3317 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
| 3318 | uint16_t super_class_idx = class_def.superclass_idx_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3319 | if (super_class_idx != DexFile::kDexNoIndex16) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3320 | mirror::Class* super_class = ResolveType(dex_file, super_class_idx, klass.get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3321 | if (super_class == NULL) { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 3322 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3323 | return false; |
| 3324 | } |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 3325 | // Verify |
| 3326 | if (!klass->CanAccess(super_class)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3327 | ThrowIllegalAccessError(klass.get(), "Class %s extended by class %s is inaccessible", |
| 3328 | PrettyDescriptor(super_class).c_str(), |
| 3329 | PrettyDescriptor(klass.get()).c_str()); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 3330 | return false; |
| 3331 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3332 | klass->SetSuperClass(super_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3333 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3334 | const DexFile::TypeList* interfaces = dex_file.GetInterfacesList(class_def); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3335 | if (interfaces != NULL) { |
| 3336 | for (size_t i = 0; i < interfaces->Size(); i++) { |
| 3337 | uint16_t idx = interfaces->GetTypeItem(i).type_idx_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3338 | mirror::Class* interface = ResolveType(dex_file, idx, klass.get()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3339 | if (interface == NULL) { |
| 3340 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 3341 | return false; |
| 3342 | } |
| 3343 | // Verify |
| 3344 | if (!klass->CanAccess(interface)) { |
| 3345 | // TODO: the RI seemed to ignore this in my testing. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3346 | ThrowIllegalAccessError(klass.get(), "Interface %s implemented by class %s is inaccessible", |
| 3347 | PrettyDescriptor(interface).c_str(), |
| 3348 | PrettyDescriptor(klass.get()).c_str()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3349 | return false; |
| 3350 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3351 | } |
| 3352 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3353 | // Mark the class as loaded. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3354 | klass->SetStatus(mirror::Class::kStatusLoaded, NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3355 | return true; |
| 3356 | } |
| 3357 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3358 | bool ClassLinker::LinkSuperClass(SirtRef<mirror::Class>& klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3359 | CHECK(!klass->IsPrimitive()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3360 | mirror::Class* super = klass->GetSuperClass(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3361 | if (klass.get() == GetClassRoot(kJavaLangObject)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3362 | if (super != NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3363 | ThrowClassFormatError(klass.get(), "java.lang.Object must not have a superclass"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3364 | return false; |
| 3365 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3366 | return true; |
| 3367 | } |
| 3368 | if (super == NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3369 | ThrowLinkageError(klass.get(), "No superclass defined for class %s", |
| 3370 | PrettyDescriptor(klass.get()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3371 | return false; |
| 3372 | } |
| 3373 | // Verify |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 3374 | if (super->IsFinal() || super->IsInterface()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3375 | ThrowIncompatibleClassChangeError(klass.get(), "Superclass %s of %s is %s", |
| 3376 | PrettyDescriptor(super).c_str(), |
| 3377 | PrettyDescriptor(klass.get()).c_str(), |
| 3378 | super->IsFinal() ? "declared final" : "an interface"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3379 | return false; |
| 3380 | } |
| 3381 | if (!klass->CanAccess(super)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3382 | ThrowIllegalAccessError(klass.get(), "Superclass %s is inaccessible to class %s", |
| 3383 | PrettyDescriptor(super).c_str(), |
| 3384 | PrettyDescriptor(klass.get()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3385 | return false; |
| 3386 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 3387 | |
| 3388 | // Inherit kAccClassIsFinalizable from the superclass in case this class doesn't override finalize. |
| 3389 | if (super->IsFinalizable()) { |
| 3390 | klass->SetFinalizable(); |
| 3391 | } |
| 3392 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 3393 | // Inherit reference flags (if any) from the superclass. |
| 3394 | int reference_flags = (super->GetAccessFlags() & kAccReferenceFlagsMask); |
| 3395 | if (reference_flags != 0) { |
| 3396 | klass->SetAccessFlags(klass->GetAccessFlags() | reference_flags); |
| 3397 | } |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 3398 | // Disallow custom direct subclasses of java.lang.ref.Reference. |
Elliott Hughes | bf61ba3 | 2011-10-11 10:53:09 -0700 | [diff] [blame] | 3399 | if (init_done_ && super == GetClassRoot(kJavaLangRefReference)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3400 | ThrowLinkageError(klass.get(), |
| 3401 | "Class %s attempts to subclass java.lang.ref.Reference, which is not allowed", |
| 3402 | PrettyDescriptor(klass.get()).c_str()); |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 3403 | return false; |
| 3404 | } |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 3405 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3406 | if (kIsDebugBuild) { |
| 3407 | // Ensure super classes are fully resolved prior to resolving fields.. |
| 3408 | while (super != NULL) { |
| 3409 | CHECK(super->IsResolved()); |
| 3410 | super = super->GetSuperClass(); |
| 3411 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3412 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3413 | return true; |
| 3414 | } |
| 3415 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3416 | // Populate the class vtable and itable. Compute return type indices. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3417 | bool ClassLinker::LinkMethods(SirtRef<mirror::Class>& klass, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3418 | SirtRef<mirror::ObjectArray<mirror::Class> >& interfaces) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3419 | if (klass->IsInterface()) { |
| 3420 | // No vtable. |
| 3421 | size_t count = klass->NumVirtualMethods(); |
| 3422 | if (!IsUint(16, count)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3423 | ThrowClassFormatError(klass.get(), "Too many methods on interface: %zd", count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3424 | return false; |
| 3425 | } |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 3426 | for (size_t i = 0; i < count; ++i) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3427 | klass->GetVirtualMethodDuringLinking(i)->SetMethodIndex(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3428 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 3429 | // Link interface method tables |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3430 | return LinkInterfaceMethods(klass, interfaces); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3431 | } else { |
Elliott Hughes | bc258fa | 2011-10-06 14:45:21 -0700 | [diff] [blame] | 3432 | // Link virtual and interface method tables |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3433 | return LinkVirtualMethods(klass) && LinkInterfaceMethods(klass, interfaces); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3434 | } |
| 3435 | return true; |
| 3436 | } |
| 3437 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3438 | bool ClassLinker::LinkVirtualMethods(SirtRef<mirror::Class>& klass) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3439 | Thread* self = Thread::Current(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3440 | if (klass->HasSuperClass()) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3441 | uint32_t max_count = klass->NumVirtualMethods() + klass->GetSuperClass()->GetVTable()->GetLength(); |
| 3442 | size_t actual_count = klass->GetSuperClass()->GetVTable()->GetLength(); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 3443 | CHECK_LE(actual_count, max_count); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 3444 | // TODO: do not assign to the vtable field until it is fully constructed. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3445 | SirtRef<mirror::ObjectArray<mirror::ArtMethod> > |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 3446 | vtable(self, klass->GetSuperClass()->GetVTable()->CopyOf(self, max_count)); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3447 | if (UNLIKELY(vtable.get() == NULL)) { |
| 3448 | CHECK(self->IsExceptionPending()); // OOME. |
| 3449 | return false; |
| 3450 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3451 | // See if any of our virtual methods override the superclass. |
| 3452 | for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3453 | mirror::ArtMethod* local_method = klass->GetVirtualMethodDuringLinking(i); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3454 | MethodHelper local_mh(local_method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3455 | size_t j = 0; |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 3456 | for (; j < actual_count; ++j) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3457 | mirror::ArtMethod* super_method = vtable->Get(j); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3458 | MethodHelper super_mh(super_method); |
Elliott Hughes | 3971737 | 2012-07-13 16:21:23 -0700 | [diff] [blame] | 3459 | if (local_mh.HasSameNameAndSignature(&super_mh)) { |
| 3460 | if (klass->CanAccessMember(super_method->GetDeclaringClass(), super_method->GetAccessFlags())) { |
| 3461 | if (super_method->IsFinal()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3462 | ThrowLinkageError(klass.get(), "Method %s overrides final method in class %s", |
Elliott Hughes | 3971737 | 2012-07-13 16:21:23 -0700 | [diff] [blame] | 3463 | PrettyMethod(local_method).c_str(), |
| 3464 | super_mh.GetDeclaringClassDescriptor()); |
| 3465 | return false; |
| 3466 | } |
| 3467 | vtable->Set(j, local_method); |
| 3468 | local_method->SetMethodIndex(j); |
| 3469 | break; |
| 3470 | } else { |
| 3471 | LOG(WARNING) << "Before Android 4.1, method " << PrettyMethod(local_method) |
| 3472 | << " would have incorrectly overridden the package-private method in " |
| 3473 | << PrettyDescriptor(super_mh.GetDeclaringClassDescriptor()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3474 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3475 | } |
| 3476 | } |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 3477 | if (j == actual_count) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3478 | // Not overriding, append. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3479 | vtable->Set(actual_count, local_method); |
| 3480 | local_method->SetMethodIndex(actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3481 | actual_count += 1; |
| 3482 | } |
| 3483 | } |
| 3484 | if (!IsUint(16, actual_count)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3485 | ThrowClassFormatError(klass.get(), "Too many methods defined on class: %zd", actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3486 | return false; |
| 3487 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3488 | // Shrink vtable if possible |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3489 | CHECK_LE(actual_count, max_count); |
| 3490 | if (actual_count < max_count) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 3491 | vtable.reset(vtable->CopyOf(self, actual_count)); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3492 | if (UNLIKELY(vtable.get() == NULL)) { |
| 3493 | CHECK(self->IsExceptionPending()); // OOME. |
| 3494 | return false; |
| 3495 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3496 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3497 | klass->SetVTable(vtable.get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3498 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3499 | CHECK(klass.get() == GetClassRoot(kJavaLangObject)); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 3500 | uint32_t num_virtual_methods = klass->NumVirtualMethods(); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 3501 | if (!IsUint(16, num_virtual_methods)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3502 | ThrowClassFormatError(klass.get(), "Too many methods: %d", num_virtual_methods); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3503 | return false; |
| 3504 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3505 | SirtRef<mirror::ObjectArray<mirror::ArtMethod> > |
| 3506 | vtable(self, AllocArtMethodArray(self, num_virtual_methods)); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3507 | if (UNLIKELY(vtable.get() == NULL)) { |
| 3508 | CHECK(self->IsExceptionPending()); // OOME. |
| 3509 | return false; |
| 3510 | } |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 3511 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3512 | mirror::ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3513 | vtable->Set(i, virtual_method); |
| 3514 | virtual_method->SetMethodIndex(i & 0xFFFF); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3515 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 3516 | klass->SetVTable(vtable.get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3517 | } |
| 3518 | return true; |
| 3519 | } |
| 3520 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3521 | bool ClassLinker::LinkInterfaceMethods(SirtRef<mirror::Class>& klass, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3522 | SirtRef<mirror::ObjectArray<mirror::Class> >& interfaces) { |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 3523 | // Set the imt table to be all conflicts by default. |
| 3524 | klass->SetImTable(Runtime::Current()->GetDefaultImt()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3525 | size_t super_ifcount; |
| 3526 | if (klass->HasSuperClass()) { |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 3527 | super_ifcount = klass->GetSuperClass()->GetIfTableCount(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3528 | } else { |
| 3529 | super_ifcount = 0; |
| 3530 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 3531 | size_t ifcount = super_ifcount; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3532 | ClassHelper kh(klass.get()); |
| 3533 | uint32_t num_interfaces = |
| 3534 | interfaces.get() == nullptr ? kh.NumDirectInterfaces() : interfaces->GetLength(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3535 | ifcount += num_interfaces; |
| 3536 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3537 | mirror::Class* interface = |
| 3538 | interfaces.get() == nullptr ? kh.GetDirectInterface(i) : interfaces->Get(i); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3539 | ifcount += interface->GetIfTableCount(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3540 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 3541 | if (ifcount == 0) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 3542 | // Class implements no interfaces. |
| 3543 | DCHECK_EQ(klass->GetIfTableCount(), 0); |
| 3544 | DCHECK(klass->GetIfTable() == NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3545 | return true; |
| 3546 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 3547 | if (ifcount == super_ifcount) { |
| 3548 | // Class implements same interfaces as parent, are any of these not marker interfaces? |
| 3549 | bool has_non_marker_interface = false; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3550 | mirror::IfTable* super_iftable = klass->GetSuperClass()->GetIfTable(); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 3551 | for (size_t i = 0; i < ifcount; ++i) { |
| 3552 | if (super_iftable->GetMethodArrayCount(i) > 0) { |
| 3553 | has_non_marker_interface = true; |
| 3554 | break; |
| 3555 | } |
| 3556 | } |
| 3557 | if (!has_non_marker_interface) { |
| 3558 | // Class just inherits marker interfaces from parent so recycle parent's iftable. |
| 3559 | klass->SetIfTable(super_iftable); |
| 3560 | return true; |
| 3561 | } |
| 3562 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3563 | Thread* self = Thread::Current(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3564 | SirtRef<mirror::IfTable> iftable(self, AllocIfTable(self, ifcount)); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3565 | if (UNLIKELY(iftable.get() == NULL)) { |
| 3566 | CHECK(self->IsExceptionPending()); // OOME. |
| 3567 | return false; |
| 3568 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3569 | if (super_ifcount != 0) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3570 | mirror::IfTable* super_iftable = klass->GetSuperClass()->GetIfTable(); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 3571 | for (size_t i = 0; i < super_ifcount; i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3572 | mirror::Class* super_interface = super_iftable->GetInterface(i); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 3573 | iftable->SetInterface(i, super_interface); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 3574 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3575 | } |
| 3576 | // Flatten the interface inheritance hierarchy. |
| 3577 | size_t idx = super_ifcount; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3578 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3579 | mirror::Class* interface = |
| 3580 | interfaces.get() == nullptr ? kh.GetDirectInterface(i) : interfaces->Get(i); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 3581 | DCHECK(interface != NULL); |
| 3582 | if (!interface->IsInterface()) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3583 | ClassHelper ih(interface); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3584 | ThrowIncompatibleClassChangeError(klass.get(), "Class %s implements non-interface class %s", |
| 3585 | PrettyDescriptor(klass.get()).c_str(), |
| 3586 | PrettyDescriptor(ih.GetDescriptor()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3587 | return false; |
| 3588 | } |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 3589 | // Check if interface is already in iftable |
| 3590 | bool duplicate = false; |
| 3591 | for (size_t j = 0; j < idx; j++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3592 | mirror::Class* existing_interface = iftable->GetInterface(j); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 3593 | if (existing_interface == interface) { |
| 3594 | duplicate = true; |
| 3595 | break; |
| 3596 | } |
| 3597 | } |
| 3598 | if (!duplicate) { |
| 3599 | // Add this non-duplicate interface. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 3600 | iftable->SetInterface(idx++, interface); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 3601 | // Add this interface's non-duplicate super-interfaces. |
| 3602 | for (int32_t j = 0; j < interface->GetIfTableCount(); j++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3603 | mirror::Class* super_interface = interface->GetIfTable()->GetInterface(j); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 3604 | bool super_duplicate = false; |
| 3605 | for (size_t k = 0; k < idx; k++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3606 | mirror::Class* existing_interface = iftable->GetInterface(k); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 3607 | if (existing_interface == super_interface) { |
| 3608 | super_duplicate = true; |
| 3609 | break; |
| 3610 | } |
| 3611 | } |
| 3612 | if (!super_duplicate) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 3613 | iftable->SetInterface(idx++, super_interface); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 3614 | } |
| 3615 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3616 | } |
| 3617 | } |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 3618 | // Shrink iftable in case duplicates were found |
| 3619 | if (idx < ifcount) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3620 | iftable.reset(down_cast<mirror::IfTable*>(iftable->CopyOf(self, idx * mirror::IfTable::kMax))); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3621 | if (UNLIKELY(iftable.get() == NULL)) { |
| 3622 | CHECK(self->IsExceptionPending()); // OOME. |
| 3623 | return false; |
| 3624 | } |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 3625 | ifcount = idx; |
| 3626 | } else { |
| 3627 | CHECK_EQ(idx, ifcount); |
| 3628 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 3629 | klass->SetIfTable(iftable.get()); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 3630 | |
| 3631 | // If we're an interface, we don't need the vtable pointers, so we're done. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 3632 | if (klass->IsInterface()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3633 | return true; |
| 3634 | } |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 3635 | // Allocate imtable |
| 3636 | bool imtable_changed = false; |
| 3637 | SirtRef<mirror::ObjectArray<mirror::ArtMethod> > imtable(self, AllocArtMethodArray(self, kImtSize)); |
| 3638 | if (UNLIKELY(imtable.get() == NULL)) { |
| 3639 | CHECK(self->IsExceptionPending()); // OOME. |
| 3640 | return false; |
| 3641 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3642 | std::vector<mirror::ArtMethod*> miranda_list; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3643 | MethodHelper vtable_mh(NULL); |
| 3644 | MethodHelper interface_mh(NULL); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 3645 | for (size_t i = 0; i < ifcount; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3646 | mirror::Class* interface = iftable->GetInterface(i); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 3647 | size_t num_methods = interface->NumVirtualMethods(); |
| 3648 | if (num_methods > 0) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3649 | SirtRef<mirror::ObjectArray<mirror::ArtMethod> > |
| 3650 | method_array(self, AllocArtMethodArray(self, num_methods)); |
| 3651 | if (UNLIKELY(method_array.get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3652 | CHECK(self->IsExceptionPending()); // OOME. |
| 3653 | return false; |
| 3654 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3655 | iftable->SetMethodArray(i, method_array.get()); |
| 3656 | SirtRef<mirror::ObjectArray<mirror::ArtMethod> > vtable(self, |
| 3657 | klass->GetVTableDuringLinking()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3658 | for (size_t j = 0; j < num_methods; ++j) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3659 | mirror::ArtMethod* interface_method = interface->GetVirtualMethod(j); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 3660 | interface_mh.ChangeMethod(interface_method); |
| 3661 | int32_t k; |
| 3662 | // For each method listed in the interface's method list, find the |
| 3663 | // matching method in our class's method list. We want to favor the |
| 3664 | // subclass over the superclass, which just requires walking |
| 3665 | // back from the end of the vtable. (This only matters if the |
| 3666 | // superclass defines a private method and this class redefines |
| 3667 | // it -- otherwise it would use the same vtable slot. In .dex files |
| 3668 | // those don't end up in the virtual method table, so it shouldn't |
| 3669 | // matter which direction we go. We walk it backward anyway.) |
| 3670 | for (k = vtable->GetLength() - 1; k >= 0; --k) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3671 | mirror::ArtMethod* vtable_method = vtable->Get(k); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 3672 | vtable_mh.ChangeMethod(vtable_method); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3673 | if (interface_mh.HasSameNameAndSignature(&vtable_mh)) { |
Elliott Hughes | 6824361 | 2013-02-22 17:16:07 -0800 | [diff] [blame] | 3674 | if (!vtable_method->IsAbstract() && !vtable_method->IsPublic()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3675 | ThrowIllegalAccessError(klass.get(), |
| 3676 | "Method '%s' implementing interface method '%s' is not public", |
| 3677 | PrettyMethod(vtable_method).c_str(), |
| 3678 | PrettyMethod(interface_method).c_str()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 3679 | return false; |
| 3680 | } |
| 3681 | method_array->Set(j, vtable_method); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 3682 | // Place method in imt if entry is empty, place conflict otherwise. |
| 3683 | uint32_t imt_index = interface_method->GetDexMethodIndex() % kImtSize; |
| 3684 | if (imtable->Get(imt_index) == NULL) { |
| 3685 | imtable->Set(imt_index, vtable_method); |
| 3686 | imtable_changed = true; |
| 3687 | } else { |
| 3688 | imtable->Set(imt_index, Runtime::Current()->GetImtConflictMethod()); |
| 3689 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3690 | break; |
| 3691 | } |
| 3692 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 3693 | if (k < 0) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3694 | SirtRef<mirror::ArtMethod> miranda_method(self, NULL); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 3695 | for (size_t mir = 0; mir < miranda_list.size(); mir++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3696 | mirror::ArtMethod* mir_method = miranda_list[mir]; |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 3697 | vtable_mh.ChangeMethod(mir_method); |
| 3698 | if (interface_mh.HasSameNameAndSignature(&vtable_mh)) { |
| 3699 | miranda_method.reset(miranda_list[mir]); |
| 3700 | break; |
| 3701 | } |
| 3702 | } |
| 3703 | if (miranda_method.get() == NULL) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3704 | // Point the interface table at a phantom slot. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3705 | miranda_method.reset(down_cast<mirror::ArtMethod*>(interface_method->Clone(self))); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3706 | if (UNLIKELY(miranda_method.get() == NULL)) { |
| 3707 | CHECK(self->IsExceptionPending()); // OOME. |
| 3708 | return false; |
| 3709 | } |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3710 | // TODO: If a methods move then the miranda_list may hold stale references. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 3711 | miranda_list.push_back(miranda_method.get()); |
| 3712 | } |
| 3713 | method_array->Set(j, miranda_method.get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3714 | } |
| 3715 | } |
| 3716 | } |
| 3717 | } |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 3718 | if (imtable_changed) { |
| 3719 | // Fill in empty entries in interface method table with conflict. |
| 3720 | mirror::ArtMethod* imt_conflict_method = Runtime::Current()->GetImtConflictMethod(); |
| 3721 | for (size_t i = 0; i < kImtSize; i++) { |
| 3722 | if (imtable->Get(i) == NULL) { |
| 3723 | imtable->Set(i, imt_conflict_method); |
| 3724 | } |
| 3725 | } |
| 3726 | klass->SetImTable(imtable.get()); |
| 3727 | } |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 3728 | if (!miranda_list.empty()) { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 3729 | int old_method_count = klass->NumVirtualMethods(); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 3730 | int new_method_count = old_method_count + miranda_list.size(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3731 | mirror::ObjectArray<mirror::ArtMethod>* virtuals; |
| 3732 | if (old_method_count == 0) { |
| 3733 | virtuals = AllocArtMethodArray(self, new_method_count); |
| 3734 | } else { |
| 3735 | virtuals = klass->GetVirtualMethods()->CopyOf(self, new_method_count); |
| 3736 | } |
| 3737 | if (UNLIKELY(virtuals == NULL)) { |
| 3738 | CHECK(self->IsExceptionPending()); // OOME. |
| 3739 | return false; |
| 3740 | } |
| 3741 | klass->SetVirtualMethods(virtuals); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3742 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3743 | SirtRef<mirror::ObjectArray<mirror::ArtMethod> > |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3744 | vtable(self, klass->GetVTableDuringLinking()); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3745 | CHECK(vtable.get() != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3746 | int old_vtable_count = vtable->GetLength(); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 3747 | int new_vtable_count = old_vtable_count + miranda_list.size(); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 3748 | vtable.reset(vtable->CopyOf(self, new_vtable_count)); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3749 | if (UNLIKELY(vtable.get() == NULL)) { |
| 3750 | CHECK(self->IsExceptionPending()); // OOME. |
| 3751 | return false; |
| 3752 | } |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 3753 | for (size_t i = 0; i < miranda_list.size(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3754 | mirror::ArtMethod* method = miranda_list[i]; |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 3755 | // Leave the declaring class alone as type indices are relative to it |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3756 | method->SetAccessFlags(method->GetAccessFlags() | kAccMiranda); |
| 3757 | method->SetMethodIndex(0xFFFF & (old_vtable_count + i)); |
| 3758 | klass->SetVirtualMethod(old_method_count + i, method); |
| 3759 | vtable->Set(old_vtable_count + i, method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3760 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3761 | // TODO: do not assign to the vtable field until it is fully constructed. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 3762 | klass->SetVTable(vtable.get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3763 | } |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 3764 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3765 | mirror::ObjectArray<mirror::ArtMethod>* vtable = klass->GetVTableDuringLinking(); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 3766 | for (int i = 0; i < vtable->GetLength(); ++i) { |
| 3767 | CHECK(vtable->Get(i) != NULL); |
| 3768 | } |
| 3769 | |
| 3770 | // klass->DumpClass(std::cerr, Class::kDumpClassFullDetail); |
| 3771 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3772 | return true; |
| 3773 | } |
| 3774 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3775 | bool ClassLinker::LinkInstanceFields(SirtRef<mirror::Class>& klass) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 3776 | CHECK(klass.get() != NULL); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 3777 | return LinkFields(klass, false); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3778 | } |
| 3779 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3780 | bool ClassLinker::LinkStaticFields(SirtRef<mirror::Class>& klass) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 3781 | CHECK(klass.get() != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3782 | size_t allocated_class_size = klass->GetClassSize(); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 3783 | bool success = LinkFields(klass, true); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3784 | CHECK_EQ(allocated_class_size, klass->GetClassSize()); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3785 | return success; |
| 3786 | } |
| 3787 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3788 | struct LinkFieldsComparator { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3789 | explicit LinkFieldsComparator() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 3790 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3791 | // No thread safety analysis as will be called from STL. Checked lock held in constructor. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3792 | bool operator()(const mirror::ArtField* field1, const mirror::ArtField* field2) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3793 | NO_THREAD_SAFETY_ANALYSIS { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3794 | // First come reference fields, then 64-bit, and finally 32-bit |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3795 | FieldHelper fh1(field1); |
| 3796 | Primitive::Type type1 = fh1.GetTypeAsPrimitiveType(); |
| 3797 | FieldHelper fh2(field2); |
| 3798 | Primitive::Type type2 = fh2.GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3799 | bool isPrimitive1 = type1 != Primitive::kPrimNot; |
| 3800 | bool isPrimitive2 = type2 != Primitive::kPrimNot; |
| 3801 | bool is64bit1 = isPrimitive1 && (type1 == Primitive::kPrimLong || type1 == Primitive::kPrimDouble); |
| 3802 | bool is64bit2 = isPrimitive2 && (type2 == Primitive::kPrimLong || type2 == Primitive::kPrimDouble); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3803 | int order1 = (!isPrimitive1 ? 0 : (is64bit1 ? 1 : 2)); |
| 3804 | int order2 = (!isPrimitive2 ? 0 : (is64bit2 ? 1 : 2)); |
| 3805 | if (order1 != order2) { |
| 3806 | return order1 < order2; |
| 3807 | } |
| 3808 | |
| 3809 | // same basic group? then sort by string. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3810 | const char* name1 = fh1.GetName(); |
| 3811 | const char* name2 = fh2.GetName(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3812 | return strcmp(name1, name2) < 0; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3813 | } |
| 3814 | }; |
| 3815 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3816 | bool ClassLinker::LinkFields(SirtRef<mirror::Class>& klass, bool is_static) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3817 | size_t num_fields = |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 3818 | is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3819 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3820 | mirror::ObjectArray<mirror::ArtField>* fields = |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 3821 | is_static ? klass->GetSFields() : klass->GetIFields(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3822 | |
| 3823 | // Initialize size and field_offset |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 3824 | size_t size; |
| 3825 | MemberOffset field_offset(0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 3826 | if (is_static) { |
| 3827 | size = klass->GetClassSize(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3828 | field_offset = mirror::Class::FieldsOffset(); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 3829 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3830 | mirror::Class* super_class = klass->GetSuperClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3831 | if (super_class != NULL) { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 3832 | CHECK(super_class->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3833 | field_offset = MemberOffset(super_class->GetObjectSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3834 | } |
| 3835 | size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3836 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3837 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3838 | CHECK_EQ(num_fields == 0, fields == NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3839 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3840 | // we want a relatively stable order so that adding new fields |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 3841 | // minimizes disruption of C++ version such as Class and Method. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3842 | std::deque<mirror::ArtField*> grouped_and_sorted_fields; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3843 | for (size_t i = 0; i < num_fields; i++) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3844 | mirror::ArtField* f = fields->Get(i); |
| 3845 | CHECK(f != NULL); |
| 3846 | grouped_and_sorted_fields.push_back(f); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3847 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3848 | std::sort(grouped_and_sorted_fields.begin(), grouped_and_sorted_fields.end(), |
| 3849 | LinkFieldsComparator()); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3850 | |
| 3851 | // References should be at the front. |
| 3852 | size_t current_field = 0; |
| 3853 | size_t num_reference_fields = 0; |
| 3854 | for (; current_field < num_fields; current_field++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3855 | mirror::ArtField* field = grouped_and_sorted_fields.front(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3856 | FieldHelper fh(field); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3857 | Primitive::Type type = fh.GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3858 | bool isPrimitive = type != Primitive::kPrimNot; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3859 | if (isPrimitive) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 3860 | break; // past last reference, move on to the next phase |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3861 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3862 | grouped_and_sorted_fields.pop_front(); |
| 3863 | num_reference_fields++; |
| 3864 | fields->Set(current_field, field); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3865 | field->SetOffset(field_offset); |
| 3866 | field_offset = MemberOffset(field_offset.Uint32Value() + sizeof(uint32_t)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3867 | } |
| 3868 | |
| 3869 | // Now we want to pack all of the double-wide fields together. If |
| 3870 | // we're not aligned, though, we want to shuffle one 32-bit field |
| 3871 | // into place. If we can't find one, we'll have to pad it. |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 3872 | if (current_field != num_fields && !IsAligned<8>(field_offset.Uint32Value())) { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3873 | for (size_t i = 0; i < grouped_and_sorted_fields.size(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3874 | mirror::ArtField* field = grouped_and_sorted_fields[i]; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3875 | FieldHelper fh(field); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3876 | Primitive::Type type = fh.GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3877 | CHECK(type != Primitive::kPrimNot); // should only be working on primitive types |
| 3878 | if (type == Primitive::kPrimLong || type == Primitive::kPrimDouble) { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3879 | continue; |
| 3880 | } |
| 3881 | fields->Set(current_field++, field); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3882 | field->SetOffset(field_offset); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3883 | // drop the consumed field |
| 3884 | grouped_and_sorted_fields.erase(grouped_and_sorted_fields.begin() + i); |
| 3885 | break; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3886 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3887 | // whether we found a 32-bit field for padding or not, we advance |
| 3888 | field_offset = MemberOffset(field_offset.Uint32Value() + sizeof(uint32_t)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3889 | } |
| 3890 | |
| 3891 | // Alignment is good, shuffle any double-wide fields forward, and |
| 3892 | // finish assigning field offsets to all fields. |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 3893 | DCHECK(current_field == num_fields || IsAligned<8>(field_offset.Uint32Value())); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3894 | while (!grouped_and_sorted_fields.empty()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3895 | mirror::ArtField* field = grouped_and_sorted_fields.front(); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3896 | grouped_and_sorted_fields.pop_front(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3897 | FieldHelper fh(field); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3898 | Primitive::Type type = fh.GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3899 | CHECK(type != Primitive::kPrimNot); // should only be working on primitive types |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3900 | fields->Set(current_field, field); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3901 | field->SetOffset(field_offset); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3902 | field_offset = MemberOffset(field_offset.Uint32Value() + |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3903 | ((type == Primitive::kPrimLong || type == Primitive::kPrimDouble) |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3904 | ? sizeof(uint64_t) |
| 3905 | : sizeof(uint32_t))); |
| 3906 | current_field++; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3907 | } |
| 3908 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 3909 | // We lie to the GC about the java.lang.ref.Reference.referent field, so it doesn't scan it. |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 3910 | if (!is_static && |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3911 | (strcmp("Ljava/lang/ref/Reference;", ClassHelper(klass.get()).GetDescriptor()) == 0)) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 3912 | // We know there are no non-reference fields in the Reference classes, and we know |
| 3913 | // that 'referent' is alphabetically last, so this is easy... |
| 3914 | CHECK_EQ(num_reference_fields, num_fields); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3915 | FieldHelper fh(fields->Get(num_fields - 1)); |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 3916 | CHECK_STREQ(fh.GetName(), "referent"); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 3917 | --num_reference_fields; |
| 3918 | } |
| 3919 | |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3920 | if (kIsDebugBuild) { |
| 3921 | // Make sure that all reference fields appear before |
| 3922 | // non-reference fields, and all double-wide fields are aligned. |
| 3923 | bool seen_non_ref = false; |
| 3924 | for (size_t i = 0; i < num_fields; i++) { |
| 3925 | mirror::ArtField* field = fields->Get(i); |
| 3926 | if (false) { // enable to debug field layout |
| 3927 | LOG(INFO) << "LinkFields: " << (is_static ? "static" : "instance") |
| 3928 | << " class=" << PrettyClass(klass.get()) |
| 3929 | << " field=" << PrettyField(field) |
| 3930 | << " offset=" << field->GetField32(MemberOffset(mirror::ArtField::OffsetOffset()), |
| 3931 | false); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3932 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3933 | FieldHelper fh(field); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3934 | Primitive::Type type = fh.GetTypeAsPrimitiveType(); |
| 3935 | bool is_primitive = type != Primitive::kPrimNot; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3936 | if ((strcmp("Ljava/lang/ref/Reference;", ClassHelper(klass.get()).GetDescriptor()) == 0) |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3937 | && (strcmp("referent", fh.GetName()) == 0)) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3938 | is_primitive = true; // We lied above, so we have to expect a lie here. |
| 3939 | } |
| 3940 | if (is_primitive) { |
| 3941 | if (!seen_non_ref) { |
| 3942 | seen_non_ref = true; |
| 3943 | DCHECK_EQ(num_reference_fields, i); |
| 3944 | } |
| 3945 | } else { |
| 3946 | DCHECK(!seen_non_ref); |
| 3947 | } |
| 3948 | } |
| 3949 | if (!seen_non_ref) { |
| 3950 | DCHECK_EQ(num_fields, num_reference_fields); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3951 | } |
| 3952 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3953 | size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3954 | // Update klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 3955 | if (is_static) { |
| 3956 | klass->SetNumReferenceStaticFields(num_reference_fields); |
| 3957 | klass->SetClassSize(size); |
| 3958 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3959 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 3960 | if (!klass->IsVariableSize()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3961 | DCHECK_GE(size, sizeof(mirror::Object)) << ClassHelper(klass.get()).GetDescriptor(); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 3962 | size_t previous_size = klass->GetObjectSize(); |
| 3963 | if (previous_size != 0) { |
| 3964 | // Make sure that we didn't originally have an incorrect size. |
| 3965 | CHECK_EQ(previous_size, size); |
| 3966 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3967 | klass->SetObjectSize(size); |
| 3968 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3969 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3970 | return true; |
| 3971 | } |
| 3972 | |
| 3973 | // Set the bitmap of reference offsets, refOffsets, from the ifields |
| 3974 | // list. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3975 | void ClassLinker::CreateReferenceInstanceOffsets(SirtRef<mirror::Class>& klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3976 | uint32_t reference_offsets = 0; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3977 | mirror::Class* super_class = klass->GetSuperClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3978 | if (super_class != NULL) { |
| 3979 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3980 | // If our superclass overflowed, we don't stand a chance. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3981 | if (reference_offsets == CLASS_WALK_SUPER) { |
| 3982 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3983 | return; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3984 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3985 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 3986 | CreateReferenceOffsets(klass, false, reference_offsets); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3987 | } |
| 3988 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3989 | void ClassLinker::CreateReferenceStaticOffsets(SirtRef<mirror::Class>& klass) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 3990 | CreateReferenceOffsets(klass, true, 0); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3991 | } |
| 3992 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3993 | void ClassLinker::CreateReferenceOffsets(SirtRef<mirror::Class>& klass, bool is_static, |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3994 | uint32_t reference_offsets) { |
| 3995 | size_t num_reference_fields = |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 3996 | is_static ? klass->NumReferenceStaticFieldsDuringLinking() |
| 3997 | : klass->NumReferenceInstanceFieldsDuringLinking(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3998 | const mirror::ObjectArray<mirror::ArtField>* fields = |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 3999 | is_static ? klass->GetSFields() : klass->GetIFields(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 4000 | // All of the fields that contain object references are guaranteed |
| 4001 | // to be at the beginning of the fields list. |
| 4002 | for (size_t i = 0; i < num_reference_fields; ++i) { |
| 4003 | // Note that byte_offset is the offset from the beginning of |
| 4004 | // object, not the offset into instance data |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4005 | const mirror::ArtField* field = fields->Get(i); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4006 | MemberOffset byte_offset = field->GetOffsetDuringLinking(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4007 | CHECK_EQ(byte_offset.Uint32Value() & (CLASS_OFFSET_ALIGNMENT - 1), 0U); |
| 4008 | if (CLASS_CAN_ENCODE_OFFSET(byte_offset.Uint32Value())) { |
| 4009 | uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset.Uint32Value()); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 4010 | CHECK_NE(new_bit, 0U); |
| 4011 | reference_offsets |= new_bit; |
| 4012 | } else { |
| 4013 | reference_offsets = CLASS_WALK_SUPER; |
| 4014 | break; |
| 4015 | } |
| 4016 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4017 | // Update fields in klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 4018 | if (is_static) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4019 | klass->SetReferenceStaticOffsets(reference_offsets); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 4020 | } else { |
| 4021 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4022 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4023 | } |
| 4024 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4025 | mirror::String* ClassLinker::ResolveString(const DexFile& dex_file, uint32_t string_idx, |
| 4026 | SirtRef<mirror::DexCache>& dex_cache) { |
| 4027 | DCHECK(dex_cache.get() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4028 | mirror::String* resolved = dex_cache->GetResolvedString(string_idx); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4029 | if (resolved != NULL) { |
| 4030 | return resolved; |
| 4031 | } |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 4032 | uint32_t utf16_length; |
| 4033 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4034 | mirror::String* string = intern_table_->InternStrong(utf16_length, utf8_data); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4035 | dex_cache->SetResolvedString(string_idx, string); |
| 4036 | return string; |
| 4037 | } |
| 4038 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4039 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, uint16_t type_idx, |
| 4040 | const mirror::Class* referrer) { |
| 4041 | Thread* self = Thread::Current(); |
| 4042 | SirtRef<mirror::DexCache> dex_cache(self, referrer->GetDexCache()); |
| 4043 | SirtRef<mirror::ClassLoader> class_loader(self, referrer->GetClassLoader()); |
| 4044 | return ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 4045 | } |
| 4046 | |
| 4047 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, uint16_t type_idx, |
| 4048 | SirtRef<mirror::DexCache>& dex_cache, |
| 4049 | SirtRef<mirror::ClassLoader>& class_loader) { |
| 4050 | DCHECK(dex_cache.get() != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4051 | mirror::Class* resolved = dex_cache->GetResolvedType(type_idx); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4052 | if (resolved == NULL) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 4053 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 4054 | resolved = FindClass(descriptor, class_loader); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4055 | if (resolved != NULL) { |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 4056 | // TODO: we used to throw here if resolved's class loader was not the |
| 4057 | // boot class loader. This was to permit different classes with the |
| 4058 | // same name to be loaded simultaneously by different loaders |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4059 | dex_cache->SetResolvedType(type_idx, resolved); |
| 4060 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4061 | Thread* self = Thread::Current(); |
| 4062 | CHECK(self->IsExceptionPending()) |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 4063 | << "Expected pending exception for failed resolution of: " << descriptor; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4064 | // Convert a ClassNotFoundException to a NoClassDefFoundError. |
| 4065 | SirtRef<mirror::Throwable> cause(self, self->GetException(NULL)); |
| 4066 | if (cause->InstanceOf(GetClassRoot(kJavaLangClassNotFoundException))) { |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4067 | Thread::Current()->ClearException(); |
| 4068 | ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4069 | self->GetException(NULL)->SetCause(cause.get()); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4070 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4071 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4072 | } |
Brian Carlstrom | abcf7ae | 2013-09-23 22:19:52 -0700 | [diff] [blame] | 4073 | DCHECK((resolved == NULL) || resolved->IsResolved() || resolved->IsErroneous()) |
| 4074 | << PrettyDescriptor(resolved) << " " << resolved->GetStatus(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4075 | return resolved; |
| 4076 | } |
| 4077 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4078 | mirror::ArtMethod* ClassLinker::ResolveMethod(const DexFile& dex_file, |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 4079 | uint32_t method_idx, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4080 | SirtRef<mirror::DexCache>& dex_cache, |
| 4081 | SirtRef<mirror::ClassLoader>& class_loader, |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 4082 | const mirror::ArtMethod* referrer, |
| 4083 | InvokeType type) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4084 | DCHECK(dex_cache.get() != NULL); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4085 | // Check for hit in the dex cache. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4086 | mirror::ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 4087 | if (resolved != NULL && !resolved->IsRuntimeMethod()) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4088 | return resolved; |
| 4089 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4090 | // Fail, get the declaring class. |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4091 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4092 | mirror::Class* klass = ResolveType(dex_file, method_id.class_idx_, dex_cache, class_loader); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4093 | if (klass == NULL) { |
Elliott Hughes | cc5f9a9 | 2011-09-28 19:17:29 -0700 | [diff] [blame] | 4094 | DCHECK(Thread::Current()->IsExceptionPending()); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4095 | return NULL; |
| 4096 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4097 | // Scan using method_idx, this saves string compares but will only hit for matching dex |
| 4098 | // caches/files. |
| 4099 | switch (type) { |
| 4100 | case kDirect: // Fall-through. |
| 4101 | case kStatic: |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4102 | resolved = klass->FindDirectMethod(dex_cache.get(), method_idx); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4103 | break; |
| 4104 | case kInterface: |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4105 | resolved = klass->FindInterfaceMethod(dex_cache.get(), method_idx); |
Ian Rogers | 4c5b265 | 2012-12-15 21:36:16 -0800 | [diff] [blame] | 4106 | DCHECK(resolved == NULL || resolved->GetDeclaringClass()->IsInterface()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4107 | break; |
| 4108 | case kSuper: // Fall-through. |
| 4109 | case kVirtual: |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4110 | resolved = klass->FindVirtualMethod(dex_cache.get(), method_idx); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4111 | break; |
| 4112 | default: |
| 4113 | LOG(FATAL) << "Unreachable - invocation type: " << type; |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 4114 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4115 | if (resolved == NULL) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4116 | // Search by name, which works across dex files. |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4117 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 4118 | const Signature signature = dex_file.GetMethodSignature(method_id); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4119 | switch (type) { |
| 4120 | case kDirect: // Fall-through. |
| 4121 | case kStatic: |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4122 | resolved = klass->FindDirectMethod(name, signature); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4123 | break; |
| 4124 | case kInterface: |
| 4125 | resolved = klass->FindInterfaceMethod(name, signature); |
Ian Rogers | 4c5b265 | 2012-12-15 21:36:16 -0800 | [diff] [blame] | 4126 | DCHECK(resolved == NULL || resolved->GetDeclaringClass()->IsInterface()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4127 | break; |
| 4128 | case kSuper: // Fall-through. |
| 4129 | case kVirtual: |
| 4130 | resolved = klass->FindVirtualMethod(name, signature); |
| 4131 | break; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4132 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4133 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4134 | if (resolved != NULL) { |
| 4135 | // We found a method, check for incompatible class changes. |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 4136 | if (resolved->CheckIncompatibleClassChange(type)) { |
| 4137 | resolved = NULL; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4138 | } |
| 4139 | } |
| 4140 | if (resolved != NULL) { |
| 4141 | // Be a good citizen and update the dex cache to speed subsequent calls. |
| 4142 | dex_cache->SetResolvedMethod(method_idx, resolved); |
| 4143 | return resolved; |
| 4144 | } else { |
jeffhao | c0228b8 | 2012-08-29 18:15:05 -0700 | [diff] [blame] | 4145 | // We failed to find the method which means either an access error, an incompatible class |
| 4146 | // change, or no such method. First try to find the method among direct and virtual methods. |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4147 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 4148 | const Signature signature = dex_file.GetMethodSignature(method_id); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4149 | switch (type) { |
| 4150 | case kDirect: |
| 4151 | case kStatic: |
| 4152 | resolved = klass->FindVirtualMethod(name, signature); |
jeffhao | c0228b8 | 2012-08-29 18:15:05 -0700 | [diff] [blame] | 4153 | break; |
| 4154 | case kInterface: |
| 4155 | case kVirtual: |
| 4156 | case kSuper: |
| 4157 | resolved = klass->FindDirectMethod(name, signature); |
| 4158 | break; |
| 4159 | } |
| 4160 | |
| 4161 | // If we found something, check that it can be accessed by the referrer. |
| 4162 | if (resolved != NULL && referrer != NULL) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4163 | mirror::Class* methods_class = resolved->GetDeclaringClass(); |
| 4164 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
jeffhao | c0228b8 | 2012-08-29 18:15:05 -0700 | [diff] [blame] | 4165 | if (!referring_class->CanAccess(methods_class)) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 4166 | ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, methods_class, |
| 4167 | referrer, resolved, type); |
jeffhao | c0228b8 | 2012-08-29 18:15:05 -0700 | [diff] [blame] | 4168 | return NULL; |
| 4169 | } else if (!referring_class->CanAccessMember(methods_class, |
| 4170 | resolved->GetAccessFlags())) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 4171 | ThrowIllegalAccessErrorMethod(referring_class, resolved); |
jeffhao | c0228b8 | 2012-08-29 18:15:05 -0700 | [diff] [blame] | 4172 | return NULL; |
| 4173 | } |
| 4174 | } |
| 4175 | |
| 4176 | // Otherwise, throw an IncompatibleClassChangeError if we found something, and check interface |
| 4177 | // methods and throw if we find the method there. If we find nothing, throw a NoSuchMethodError. |
| 4178 | switch (type) { |
| 4179 | case kDirect: |
| 4180 | case kStatic: |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4181 | if (resolved != NULL) { |
Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 4182 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4183 | } else { |
| 4184 | resolved = klass->FindInterfaceMethod(name, signature); |
| 4185 | if (resolved != NULL) { |
Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 4186 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4187 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4188 | ThrowNoSuchMethodError(type, klass, name, signature); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4189 | } |
| 4190 | } |
| 4191 | break; |
| 4192 | case kInterface: |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4193 | if (resolved != NULL) { |
Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 4194 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4195 | } else { |
| 4196 | resolved = klass->FindVirtualMethod(name, signature); |
| 4197 | if (resolved != NULL) { |
Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 4198 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4199 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4200 | ThrowNoSuchMethodError(type, klass, name, signature); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4201 | } |
| 4202 | } |
| 4203 | break; |
| 4204 | case kSuper: |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4205 | ThrowNoSuchMethodError(type, klass, name, signature); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4206 | break; |
| 4207 | case kVirtual: |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4208 | if (resolved != NULL) { |
Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 4209 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4210 | } else { |
| 4211 | resolved = klass->FindInterfaceMethod(name, signature); |
| 4212 | if (resolved != NULL) { |
Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 4213 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4214 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4215 | ThrowNoSuchMethodError(type, klass, name, signature); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4216 | } |
| 4217 | } |
| 4218 | break; |
| 4219 | } |
| 4220 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4221 | return NULL; |
| 4222 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4223 | } |
| 4224 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4225 | mirror::ArtField* ClassLinker::ResolveField(const DexFile& dex_file, uint32_t field_idx, |
| 4226 | SirtRef<mirror::DexCache>& dex_cache, |
| 4227 | SirtRef<mirror::ClassLoader>& class_loader, |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 4228 | bool is_static) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4229 | DCHECK(dex_cache.get() != nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4230 | mirror::ArtField* resolved = dex_cache->GetResolvedField(field_idx); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4231 | if (resolved != NULL) { |
| 4232 | return resolved; |
| 4233 | } |
| 4234 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4235 | mirror::Class* klass = ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4236 | if (klass == NULL) { |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 4237 | DCHECK(Thread::Current()->IsExceptionPending()); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4238 | return NULL; |
| 4239 | } |
| 4240 | |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 4241 | if (is_static) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4242 | resolved = klass->FindStaticField(dex_cache.get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 4243 | } else { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4244 | resolved = klass->FindInstanceField(dex_cache.get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 4245 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4246 | |
| 4247 | if (resolved == NULL) { |
| 4248 | const char* name = dex_file.GetFieldName(field_id); |
| 4249 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 4250 | if (is_static) { |
| 4251 | resolved = klass->FindStaticField(name, type); |
| 4252 | } else { |
| 4253 | resolved = klass->FindInstanceField(name, type); |
| 4254 | } |
| 4255 | if (resolved == NULL) { |
| 4256 | ThrowNoSuchFieldError(is_static ? "static " : "instance ", klass, type, name); |
| 4257 | return NULL; |
| 4258 | } |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 4259 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4260 | dex_cache->SetResolvedField(field_idx, resolved); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 4261 | return resolved; |
| 4262 | } |
| 4263 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4264 | mirror::ArtField* ClassLinker::ResolveFieldJLS(const DexFile& dex_file, |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4265 | uint32_t field_idx, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4266 | SirtRef<mirror::DexCache>& dex_cache, |
| 4267 | SirtRef<mirror::ClassLoader>& class_loader) { |
| 4268 | DCHECK(dex_cache.get() != nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4269 | mirror::ArtField* resolved = dex_cache->GetResolvedField(field_idx); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 4270 | if (resolved != NULL) { |
| 4271 | return resolved; |
| 4272 | } |
| 4273 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4274 | mirror::Class* klass = ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 4275 | if (klass == NULL) { |
| 4276 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4277 | return NULL; |
| 4278 | } |
| 4279 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 4280 | StringPiece name(dex_file.StringDataByIdx(field_id.name_idx_)); |
| 4281 | StringPiece type(dex_file.StringDataByIdx( |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4282 | dex_file.GetTypeId(field_id.type_idx_).descriptor_idx_)); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 4283 | resolved = klass->FindField(name, type); |
| 4284 | if (resolved != NULL) { |
| 4285 | dex_cache->SetResolvedField(field_idx, resolved); |
| 4286 | } else { |
| 4287 | ThrowNoSuchFieldError("", klass, type, name); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4288 | } |
| 4289 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 4290 | } |
| 4291 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4292 | const char* ClassLinker::MethodShorty(uint32_t method_idx, mirror::ArtMethod* referrer, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4293 | uint32_t* length) { |
| 4294 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
| 4295 | mirror::DexCache* dex_cache = declaring_class->GetDexCache(); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 4296 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 4297 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 4298 | return dex_file.GetMethodShorty(method_id, length); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 4299 | } |
| 4300 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4301 | void ClassLinker::DumpAllClasses(int flags) { |
| 4302 | if (dex_cache_image_class_lookup_required_) { |
| 4303 | MoveImageClassesToClassTable(); |
| 4304 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 4305 | // TODO: at the time this was written, it wasn't safe to call PrettyField with the ClassLinker |
| 4306 | // lock held, because it might need to resolve a field's type, which would try to take the lock. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4307 | std::vector<mirror::Class*> all_classes; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 4308 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 4309 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4310 | for (const std::pair<size_t, mirror::Class*>& it : class_table_) { |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 4311 | all_classes.push_back(it.second); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 4312 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 4313 | } |
| 4314 | |
| 4315 | for (size_t i = 0; i < all_classes.size(); ++i) { |
| 4316 | all_classes[i]->DumpClass(std::cerr, flags); |
| 4317 | } |
| 4318 | } |
| 4319 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4320 | void ClassLinker::DumpForSigQuit(std::ostream& os) { |
| 4321 | if (dex_cache_image_class_lookup_required_) { |
| 4322 | MoveImageClassesToClassTable(); |
| 4323 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 4324 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4325 | os << "Loaded classes: " << class_table_.size() << " allocated classes\n"; |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 4326 | } |
| 4327 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4328 | size_t ClassLinker::NumLoadedClasses() { |
| 4329 | if (dex_cache_image_class_lookup_required_) { |
| 4330 | MoveImageClassesToClassTable(); |
| 4331 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 4332 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4333 | return class_table_.size(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 4334 | } |
| 4335 | |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 4336 | pid_t ClassLinker::GetClassesLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4337 | return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 4338 | } |
| 4339 | |
| 4340 | pid_t ClassLinker::GetDexLockOwner() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4341 | return dex_lock_.GetExclusiveOwnerTid(); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 4342 | } |
| 4343 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4344 | void ClassLinker::SetClassRoot(ClassRoot class_root, mirror::Class* klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4345 | DCHECK(!init_done_); |
| 4346 | |
| 4347 | DCHECK(klass != NULL); |
| 4348 | DCHECK(klass->GetClassLoader() == NULL); |
| 4349 | |
| 4350 | DCHECK(class_roots_ != NULL); |
| 4351 | DCHECK(class_roots_->Get(class_root) == NULL); |
| 4352 | class_roots_->Set(class_root, klass); |
| 4353 | } |
| 4354 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4355 | } // namespace art |