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 | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 22 | #include <deque> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 23 | #include <memory> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 24 | #include <string> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 25 | #include <utility> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 26 | #include <vector> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 27 | |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 28 | #include "base/casts.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 29 | #include "base/logging.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 30 | #include "base/scoped_flock.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 31 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 32 | #include "base/unix_file/fd_file.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | #include "class_linker-inl.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 34 | #include "compiler_callbacks.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" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 37 | #include "gc_root-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 38 | #include "gc/accounting/card_table-inl.h" |
| 39 | #include "gc/accounting/heap_bitmap.h" |
| 40 | #include "gc/heap.h" |
| 41 | #include "gc/space/image_space.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 42 | #include "handle_scope.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 43 | #include "intern_table.h" |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 44 | #include "interpreter/interpreter.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 45 | #include "leb128.h" |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 46 | #include "method_helper.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 47 | #include "oat.h" |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 48 | #include "oat_file.h" |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 49 | #include "object_lock.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 50 | #include "mirror/art_field-inl.h" |
| 51 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 52 | #include "mirror/class.h" |
| 53 | #include "mirror/class-inl.h" |
| 54 | #include "mirror/class_loader.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 55 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 56 | #include "mirror/iftable-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 57 | #include "mirror/object-inl.h" |
| 58 | #include "mirror/object_array-inl.h" |
| 59 | #include "mirror/proxy.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 60 | #include "mirror/reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 61 | #include "mirror/stack_trace_element.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 62 | #include "mirror/string-inl.h" |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 63 | #include "os.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 64 | #include "runtime.h" |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 65 | #include "entrypoints/entrypoint_utils.h" |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 66 | #include "ScopedLocalRef.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 67 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 68 | #include "handle_scope-inl.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 69 | #include "thread.h" |
| 70 | #include "utils.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 71 | #include "verifier/method_verifier.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 72 | #include "well_known_classes.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 73 | |
| 74 | namespace art { |
| 75 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 76 | static void ThrowNoClassDefFoundError(const char* fmt, ...) |
| 77 | __attribute__((__format__(__printf__, 1, 2))) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 78 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 79 | static void ThrowNoClassDefFoundError(const char* fmt, ...) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 80 | va_list args; |
| 81 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 82 | Thread* self = Thread::Current(); |
| 83 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 84 | self->ThrowNewExceptionV(throw_location, "Ljava/lang/NoClassDefFoundError;", fmt, args); |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 85 | va_end(args); |
| 86 | } |
| 87 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 88 | static void ThrowEarlierClassFailure(mirror::Class* c) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 89 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 90 | // The class failed to initialize on a previous attempt, so we want to throw |
| 91 | // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we |
| 92 | // failed in verification, in which case v2 5.4.1 says we need to re-throw |
| 93 | // the previous error. |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 94 | if (!Runtime::Current()->IsCompiler()) { // Give info if this occurs at runtime. |
| 95 | LOG(INFO) << "Rejecting re-init on previously-failed class " << PrettyClass(c); |
| 96 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 97 | |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 98 | CHECK(c->IsErroneous()) << PrettyClass(c) << " " << c->GetStatus(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 99 | Thread* self = Thread::Current(); |
| 100 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 101 | if (c->GetVerifyErrorClass() != NULL) { |
| 102 | // TODO: change the verifier to store an _instance_, with a useful detail message? |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 103 | self->ThrowNewException(throw_location, c->GetVerifyErrorClass()->GetDescriptor().c_str(), |
| 104 | PrettyDescriptor(c).c_str()); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 105 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 106 | self->ThrowNewException(throw_location, "Ljava/lang/NoClassDefFoundError;", |
| 107 | PrettyDescriptor(c).c_str()); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 111 | static void WrapExceptionInInitializer() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 112 | Thread* self = Thread::Current(); |
| 113 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 114 | |
| 115 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
| 116 | CHECK(cause.get() != NULL); |
| 117 | |
| 118 | env->ExceptionClear(); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 119 | bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error); |
| 120 | env->Throw(cause.get()); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 121 | |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 122 | // We only wrap non-Error exceptions; an Error can just be used as-is. |
| 123 | if (!is_error) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 124 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 125 | self->ThrowNewWrappedException(throw_location, "Ljava/lang/ExceptionInInitializerError;", NULL); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 126 | } |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 129 | static size_t Hash(const char* s) { |
| 130 | // This is the java.lang.String hashcode for convenience, not interoperability. |
| 131 | size_t hash = 0; |
| 132 | for (; *s != '\0'; ++s) { |
| 133 | hash = hash * 31 + *s; |
| 134 | } |
| 135 | return hash; |
| 136 | } |
| 137 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 138 | const char* ClassLinker::class_roots_descriptors_[] = { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 139 | "Ljava/lang/Class;", |
| 140 | "Ljava/lang/Object;", |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 141 | "[Ljava/lang/Class;", |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 142 | "[Ljava/lang/Object;", |
| 143 | "Ljava/lang/String;", |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 144 | "Ljava/lang/DexCache;", |
Elliott Hughes | bf61ba3 | 2011-10-11 10:53:09 -0700 | [diff] [blame] | 145 | "Ljava/lang/ref/Reference;", |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 146 | "Ljava/lang/reflect/ArtField;", |
| 147 | "Ljava/lang/reflect/ArtMethod;", |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 148 | "Ljava/lang/reflect/Proxy;", |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 149 | "[Ljava/lang/String;", |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 150 | "[Ljava/lang/reflect/ArtField;", |
| 151 | "[Ljava/lang/reflect/ArtMethod;", |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 152 | "Ljava/lang/ClassLoader;", |
Ian Rogers | 5167c97 | 2012-02-03 10:41:20 -0800 | [diff] [blame] | 153 | "Ljava/lang/Throwable;", |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 154 | "Ljava/lang/ClassNotFoundException;", |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 155 | "Ljava/lang/StackTraceElement;", |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 156 | "Z", |
| 157 | "B", |
| 158 | "C", |
| 159 | "D", |
| 160 | "F", |
| 161 | "I", |
| 162 | "J", |
| 163 | "S", |
| 164 | "V", |
| 165 | "[Z", |
| 166 | "[B", |
| 167 | "[C", |
| 168 | "[D", |
| 169 | "[F", |
| 170 | "[I", |
| 171 | "[J", |
| 172 | "[S", |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 173 | "[Ljava/lang/StackTraceElement;", |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 176 | ClassLinker::ClassLinker(InternTable* intern_table) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 177 | // dex_lock_ is recursive as it may be used in stack dumping. |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 178 | : dex_lock_("ClassLinker dex lock", kDefaultMutexLevel), |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 179 | dex_cache_image_class_lookup_required_(false), |
| 180 | failed_dex_cache_class_lookups_(0), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 181 | class_roots_(nullptr), |
| 182 | array_iftable_(nullptr), |
| 183 | find_array_class_cache_next_victim_(0), |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 184 | init_done_(false), |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 185 | log_new_dex_caches_roots_(false), |
| 186 | log_new_class_table_roots_(false), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 187 | intern_table_(intern_table), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 188 | portable_resolution_trampoline_(nullptr), |
| 189 | quick_resolution_trampoline_(nullptr), |
| 190 | portable_imt_conflict_trampoline_(nullptr), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 191 | quick_imt_conflict_trampoline_(nullptr), |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 192 | quick_generic_jni_trampoline_(nullptr), |
| 193 | quick_to_interpreter_bridge_trampoline_(nullptr) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 194 | CHECK_EQ(arraysize(class_roots_descriptors_), size_t(kClassRootsMax)); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 195 | memset(find_array_class_cache_, 0, kFindArrayCacheSize * sizeof(mirror::Class*)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 196 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 197 | |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 198 | // To set a value for generic JNI. May be necessary in compiler tests. |
| 199 | extern "C" void art_quick_generic_jni_trampoline(mirror::ArtMethod*); |
| 200 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 201 | void ClassLinker::InitFromCompiler(const std::vector<const DexFile*>& boot_class_path) { |
| 202 | VLOG(startup) << "ClassLinker::Init"; |
| 203 | CHECK(Runtime::Current()->IsCompiler()); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 204 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 205 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 206 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 207 | // java_lang_Class comes first, it's needed for AllocClass |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 208 | Thread* self = Thread::Current(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 209 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 210 | // The GC can't handle an object with a null class since we can't get the size of this object. |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 211 | heap->IncrementDisableMovingGC(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 212 | StackHandleScope<64> hs(self); // 64 is picked arbitrarily. |
| 213 | Handle<mirror::Class> java_lang_Class(hs.NewHandle(down_cast<mirror::Class*>( |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 214 | heap->AllocNonMovableObject<true>(self, nullptr, |
| 215 | mirror::Class::ClassClassSize(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 216 | VoidFunctor())))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 217 | CHECK(java_lang_Class.Get() != NULL); |
| 218 | mirror::Class::SetClassClass(java_lang_Class.Get()); |
| 219 | java_lang_Class->SetClass(java_lang_Class.Get()); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 220 | if (kUseBakerOrBrooksReadBarrier) { |
| 221 | java_lang_Class->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 222 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 223 | java_lang_Class->SetClassSize(mirror::Class::ClassClassSize()); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 224 | heap->DecrementDisableMovingGC(self); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 225 | // AllocClass(mirror::Class*) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 226 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 227 | // Class[] is used for reflection support. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 228 | Handle<mirror::Class> class_array_class(hs.NewHandle( |
| 229 | AllocClass(self, java_lang_Class.Get(), mirror::ObjectArray<mirror::Class>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 230 | class_array_class->SetComponentType(java_lang_Class.Get()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 231 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 232 | // java_lang_Object comes next so that object_array_class can be created. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 233 | Handle<mirror::Class> java_lang_Object(hs.NewHandle( |
| 234 | AllocClass(self, java_lang_Class.Get(), mirror::Object::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 235 | CHECK(java_lang_Object.Get() != NULL); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 236 | // backfill Object as the super class of Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 237 | java_lang_Class->SetSuperClass(java_lang_Object.Get()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 238 | java_lang_Object->SetStatus(mirror::Class::kStatusLoaded, self); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 239 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 240 | // Object[] next to hold class roots. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 241 | Handle<mirror::Class> object_array_class(hs.NewHandle( |
| 242 | AllocClass(self, java_lang_Class.Get(), mirror::ObjectArray<mirror::Object>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 243 | object_array_class->SetComponentType(java_lang_Object.Get()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 244 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 245 | // Setup the char (primitive) class to be used for char[]. |
| 246 | Handle<mirror::Class> char_class(hs.NewHandle( |
| 247 | AllocClass(self, java_lang_Class.Get(), mirror::Class::PrimitiveClassSize()))); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 248 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 249 | // Setup the char[] class to be used for String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 250 | Handle<mirror::Class> char_array_class(hs.NewHandle( |
| 251 | AllocClass(self, java_lang_Class.Get(), |
| 252 | mirror::Array::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 253 | char_array_class->SetComponentType(char_class.Get()); |
| 254 | mirror::CharArray::SetArrayClass(char_array_class.Get()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 255 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 256 | // Setup String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 257 | Handle<mirror::Class> java_lang_String(hs.NewHandle( |
| 258 | AllocClass(self, java_lang_Class.Get(), mirror::String::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 259 | mirror::String::SetClass(java_lang_String.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 260 | java_lang_String->SetObjectSize(mirror::String::InstanceSize()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 261 | java_lang_String->SetStatus(mirror::Class::kStatusResolved, self); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 262 | |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 263 | // Setup Reference. |
| 264 | Handle<mirror::Class> java_lang_ref_Reference(hs.NewHandle( |
| 265 | AllocClass(self, java_lang_Class.Get(), mirror::Reference::ClassSize()))); |
| 266 | mirror::Reference::SetClass(java_lang_ref_Reference.Get()); |
| 267 | java_lang_ref_Reference->SetObjectSize(mirror::Reference::InstanceSize()); |
| 268 | java_lang_ref_Reference->SetStatus(mirror::Class::kStatusResolved, self); |
| 269 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 270 | // Create storage for root classes, save away our work so far (requires descriptors). |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 271 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class> >( |
| 272 | mirror::ObjectArray<mirror::Class>::Alloc(self, object_array_class.Get(), |
| 273 | kClassRootsMax)); |
| 274 | CHECK(!class_roots_.IsNull()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 275 | SetClassRoot(kJavaLangClass, java_lang_Class.Get()); |
| 276 | SetClassRoot(kJavaLangObject, java_lang_Object.Get()); |
| 277 | SetClassRoot(kClassArrayClass, class_array_class.Get()); |
| 278 | SetClassRoot(kObjectArrayClass, object_array_class.Get()); |
| 279 | SetClassRoot(kCharArrayClass, char_array_class.Get()); |
| 280 | SetClassRoot(kJavaLangString, java_lang_String.Get()); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 281 | SetClassRoot(kJavaLangRefReference, java_lang_ref_Reference.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 282 | |
| 283 | // Setup the primitive type classes. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 284 | SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass(self, Primitive::kPrimBoolean)); |
| 285 | SetClassRoot(kPrimitiveByte, CreatePrimitiveClass(self, Primitive::kPrimByte)); |
| 286 | SetClassRoot(kPrimitiveShort, CreatePrimitiveClass(self, Primitive::kPrimShort)); |
| 287 | SetClassRoot(kPrimitiveInt, CreatePrimitiveClass(self, Primitive::kPrimInt)); |
| 288 | SetClassRoot(kPrimitiveLong, CreatePrimitiveClass(self, Primitive::kPrimLong)); |
| 289 | SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass(self, Primitive::kPrimFloat)); |
| 290 | SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass(self, Primitive::kPrimDouble)); |
| 291 | SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass(self, Primitive::kPrimVoid)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 292 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 293 | // Create array interface entries to populate once we can load system classes. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 294 | array_iftable_ = GcRoot<mirror::IfTable>(AllocIfTable(self, 2)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 295 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 296 | // Create int array type for AllocDexCache (done in AppendToBootClassPath). |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 297 | Handle<mirror::Class> int_array_class(hs.NewHandle( |
| 298 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize()))); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 299 | int_array_class->SetComponentType(GetClassRoot(kPrimitiveInt)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 300 | mirror::IntArray::SetArrayClass(int_array_class.Get()); |
| 301 | SetClassRoot(kIntArrayClass, int_array_class.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 302 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 303 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 304 | |
Ian Rogers | 52813c9 | 2012-10-11 11:50:38 -0700 | [diff] [blame] | 305 | // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 306 | Handle<mirror::Class> java_lang_DexCache(hs.NewHandle( |
| 307 | AllocClass(self, java_lang_Class.Get(), mirror::DexCache::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 308 | SetClassRoot(kJavaLangDexCache, java_lang_DexCache.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 309 | java_lang_DexCache->SetObjectSize(mirror::DexCache::InstanceSize()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 310 | java_lang_DexCache->SetStatus(mirror::Class::kStatusResolved, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 311 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 312 | // Constructor, Field, Method, and AbstractMethod are necessary so |
| 313 | // that FindClass can link members. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 314 | Handle<mirror::Class> java_lang_reflect_ArtField(hs.NewHandle( |
| 315 | AllocClass(self, java_lang_Class.Get(), mirror::ArtField::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 316 | CHECK(java_lang_reflect_ArtField.Get() != NULL); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 317 | java_lang_reflect_ArtField->SetObjectSize(mirror::ArtField::InstanceSize()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 318 | SetClassRoot(kJavaLangReflectArtField, java_lang_reflect_ArtField.Get()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 319 | java_lang_reflect_ArtField->SetStatus(mirror::Class::kStatusResolved, self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 320 | mirror::ArtField::SetClass(java_lang_reflect_ArtField.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 321 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 322 | Handle<mirror::Class> java_lang_reflect_ArtMethod(hs.NewHandle( |
| 323 | AllocClass(self, java_lang_Class.Get(), mirror::ArtMethod::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 324 | CHECK(java_lang_reflect_ArtMethod.Get() != NULL); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 325 | java_lang_reflect_ArtMethod->SetObjectSize(mirror::ArtMethod::InstanceSize()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 326 | SetClassRoot(kJavaLangReflectArtMethod, java_lang_reflect_ArtMethod.Get()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 327 | java_lang_reflect_ArtMethod->SetStatus(mirror::Class::kStatusResolved, self); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 328 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 329 | mirror::ArtMethod::SetClass(java_lang_reflect_ArtMethod.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 330 | |
| 331 | // Set up array classes for string, field, method |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 332 | Handle<mirror::Class> object_array_string(hs.NewHandle( |
| 333 | AllocClass(self, java_lang_Class.Get(), |
| 334 | mirror::ObjectArray<mirror::String>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 335 | object_array_string->SetComponentType(java_lang_String.Get()); |
| 336 | SetClassRoot(kJavaLangStringArrayClass, object_array_string.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 337 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 338 | Handle<mirror::Class> object_array_art_method(hs.NewHandle( |
| 339 | AllocClass(self, java_lang_Class.Get(), |
| 340 | mirror::ObjectArray<mirror::ArtMethod>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 341 | object_array_art_method->SetComponentType(java_lang_reflect_ArtMethod.Get()); |
| 342 | SetClassRoot(kJavaLangReflectArtMethodArrayClass, object_array_art_method.Get()); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 343 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 344 | Handle<mirror::Class> object_array_art_field(hs.NewHandle( |
| 345 | AllocClass(self, java_lang_Class.Get(), |
| 346 | mirror::ObjectArray<mirror::ArtField>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 347 | object_array_art_field->SetComponentType(java_lang_reflect_ArtField.Get()); |
| 348 | SetClassRoot(kJavaLangReflectArtFieldArrayClass, object_array_art_field.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 349 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 350 | // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create |
| 351 | // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses |
| 352 | // these roots. |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 353 | CHECK_NE(0U, boot_class_path.size()); |
| 354 | for (size_t i = 0; i != boot_class_path.size(); ++i) { |
| 355 | const DexFile* dex_file = boot_class_path[i]; |
| 356 | CHECK(dex_file != NULL); |
| 357 | AppendToBootClassPath(*dex_file); |
| 358 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 359 | |
| 360 | // now we can use FindSystemClass |
| 361 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 362 | // run char class through InitializePrimitiveClass to finish init |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 363 | InitializePrimitiveClass(char_class.Get(), Primitive::kPrimChar); |
| 364 | SetClassRoot(kPrimitiveChar, char_class.Get()); // needs descriptor |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 365 | |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 366 | // Create runtime resolution and imt conflict methods. Also setup the default imt. |
| 367 | Runtime* runtime = Runtime::Current(); |
| 368 | runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); |
| 369 | runtime->SetImtConflictMethod(runtime->CreateImtConflictMethod()); |
| 370 | runtime->SetDefaultImt(runtime->CreateDefaultImt(this)); |
| 371 | |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 372 | // Set up GenericJNI entrypoint. That is mainly a hack for common_compiler_test.h so that |
| 373 | // we do not need friend classes or a publicly exposed setter. |
| 374 | quick_generic_jni_trampoline_ = reinterpret_cast<void*>(art_quick_generic_jni_trampoline); |
| 375 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 376 | // 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] | 377 | java_lang_Object->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 378 | mirror::Class* Object_class = FindSystemClass(self, "Ljava/lang/Object;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 379 | CHECK_EQ(java_lang_Object.Get(), Object_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 380 | CHECK_EQ(java_lang_Object->GetObjectSize(), mirror::Object::InstanceSize()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 381 | java_lang_String->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 382 | mirror::Class* String_class = FindSystemClass(self, "Ljava/lang/String;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 383 | std::ostringstream os1, os2; |
| 384 | java_lang_String->DumpClass(os1, mirror::Class::kDumpClassFullDetail); |
| 385 | String_class->DumpClass(os2, mirror::Class::kDumpClassFullDetail); |
| 386 | CHECK_EQ(java_lang_String.Get(), String_class) << os1.str() << "\n\n" << os2.str(); |
| 387 | CHECK_EQ(java_lang_String->GetObjectSize(), mirror::String::InstanceSize()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 388 | java_lang_DexCache->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 389 | mirror::Class* DexCache_class = FindSystemClass(self, "Ljava/lang/DexCache;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 390 | CHECK_EQ(java_lang_String.Get(), String_class); |
| 391 | CHECK_EQ(java_lang_DexCache.Get(), DexCache_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 392 | CHECK_EQ(java_lang_DexCache->GetObjectSize(), mirror::DexCache::InstanceSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 393 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 394 | // Setup the primitive array type classes - can't be done until Object has a vtable. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 395 | SetClassRoot(kBooleanArrayClass, FindSystemClass(self, "[Z")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 396 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 397 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 398 | SetClassRoot(kByteArrayClass, FindSystemClass(self, "[B")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 399 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 400 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 401 | mirror::Class* found_char_array_class = FindSystemClass(self, "[C"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 402 | CHECK_EQ(char_array_class.Get(), found_char_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 403 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 404 | SetClassRoot(kShortArrayClass, FindSystemClass(self, "[S")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 405 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 406 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 407 | mirror::Class* found_int_array_class = FindSystemClass(self, "[I"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 408 | CHECK_EQ(int_array_class.Get(), found_int_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 409 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 410 | SetClassRoot(kLongArrayClass, FindSystemClass(self, "[J")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 411 | mirror::LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 412 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 413 | SetClassRoot(kFloatArrayClass, FindSystemClass(self, "[F")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 414 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 415 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 416 | SetClassRoot(kDoubleArrayClass, FindSystemClass(self, "[D")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 417 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 418 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 419 | mirror::Class* found_class_array_class = FindSystemClass(self, "[Ljava/lang/Class;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 420 | CHECK_EQ(class_array_class.Get(), found_class_array_class); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 421 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 422 | mirror::Class* found_object_array_class = FindSystemClass(self, "[Ljava/lang/Object;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 423 | CHECK_EQ(object_array_class.Get(), found_object_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 424 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 425 | // Setup the single, global copy of "iftable". |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 426 | mirror::Class* java_lang_Cloneable = FindSystemClass(self, "Ljava/lang/Cloneable;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 427 | CHECK(java_lang_Cloneable != NULL); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 428 | mirror::Class* java_io_Serializable = FindSystemClass(self, "Ljava/io/Serializable;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 429 | CHECK(java_io_Serializable != NULL); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 430 | // We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to |
| 431 | // crawl up and explicitly list all of the supers as well. |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 432 | { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 433 | mirror::IfTable* array_iftable = array_iftable_.Read(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 434 | array_iftable->SetInterface(0, java_lang_Cloneable); |
| 435 | array_iftable->SetInterface(1, java_io_Serializable); |
| 436 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 437 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 438 | // Sanity check Class[] and Object[]'s interfaces. |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 439 | CHECK_EQ(java_lang_Cloneable, mirror::Class::GetDirectInterface(self, class_array_class, 0)); |
| 440 | CHECK_EQ(java_io_Serializable, mirror::Class::GetDirectInterface(self, class_array_class, 1)); |
| 441 | CHECK_EQ(java_lang_Cloneable, mirror::Class::GetDirectInterface(self, object_array_class, 0)); |
| 442 | CHECK_EQ(java_io_Serializable, mirror::Class::GetDirectInterface(self, object_array_class, 1)); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 443 | // Run Class, ArtField, and ArtMethod through FindSystemClass. This initializes their |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 444 | // dex_cache_ fields and register them in class_table_. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 445 | mirror::Class* Class_class = FindSystemClass(self, "Ljava/lang/Class;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 446 | CHECK_EQ(java_lang_Class.Get(), Class_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 447 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 448 | java_lang_reflect_ArtMethod->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 449 | mirror::Class* Art_method_class = FindSystemClass(self, "Ljava/lang/reflect/ArtMethod;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 450 | CHECK_EQ(java_lang_reflect_ArtMethod.Get(), Art_method_class); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 451 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 452 | java_lang_reflect_ArtField->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 453 | mirror::Class* Art_field_class = FindSystemClass(self, "Ljava/lang/reflect/ArtField;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 454 | CHECK_EQ(java_lang_reflect_ArtField.Get(), Art_field_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 455 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 456 | mirror::Class* String_array_class = |
| 457 | FindSystemClass(self, class_roots_descriptors_[kJavaLangStringArrayClass]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 458 | CHECK_EQ(object_array_string.Get(), String_array_class); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 459 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 460 | mirror::Class* Art_method_array_class = |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 461 | FindSystemClass(self, class_roots_descriptors_[kJavaLangReflectArtMethodArrayClass]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 462 | CHECK_EQ(object_array_art_method.Get(), Art_method_array_class); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 463 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 464 | mirror::Class* Art_field_array_class = |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 465 | FindSystemClass(self, class_roots_descriptors_[kJavaLangReflectArtFieldArrayClass]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 466 | CHECK_EQ(object_array_art_field.Get(), Art_field_array_class); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 467 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 468 | // End of special init trickery, subsequent classes may be loaded via FindSystemClass. |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 469 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 470 | // Create java.lang.reflect.Proxy root. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 471 | mirror::Class* java_lang_reflect_Proxy = FindSystemClass(self, "Ljava/lang/reflect/Proxy;"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 472 | SetClassRoot(kJavaLangReflectProxy, java_lang_reflect_Proxy); |
| 473 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 474 | // java.lang.ref classes need to be specially flagged, but otherwise are normal classes |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 475 | // finish initializing Reference class |
| 476 | java_lang_ref_Reference->SetStatus(mirror::Class::kStatusNotReady, self); |
| 477 | mirror::Class* Reference_class = FindSystemClass(self, "Ljava/lang/ref/Reference;"); |
| 478 | CHECK_EQ(java_lang_ref_Reference.Get(), Reference_class); |
| 479 | CHECK_EQ(java_lang_ref_Reference->GetObjectSize(), mirror::Reference::InstanceSize()); |
| 480 | CHECK_EQ(java_lang_ref_Reference->GetClassSize(), mirror::Reference::ClassSize()); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 481 | mirror::Class* java_lang_ref_FinalizerReference = |
| 482 | FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 483 | java_lang_ref_FinalizerReference->SetAccessFlags( |
| 484 | java_lang_ref_FinalizerReference->GetAccessFlags() | |
| 485 | kAccClassIsReference | kAccClassIsFinalizerReference); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 486 | mirror::Class* java_lang_ref_PhantomReference = |
| 487 | FindSystemClass(self, "Ljava/lang/ref/PhantomReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 488 | java_lang_ref_PhantomReference->SetAccessFlags( |
| 489 | java_lang_ref_PhantomReference->GetAccessFlags() | |
| 490 | kAccClassIsReference | kAccClassIsPhantomReference); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 491 | mirror::Class* java_lang_ref_SoftReference = |
| 492 | FindSystemClass(self, "Ljava/lang/ref/SoftReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 493 | java_lang_ref_SoftReference->SetAccessFlags( |
| 494 | java_lang_ref_SoftReference->GetAccessFlags() | kAccClassIsReference); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 495 | mirror::Class* java_lang_ref_WeakReference = |
| 496 | FindSystemClass(self, "Ljava/lang/ref/WeakReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 497 | java_lang_ref_WeakReference->SetAccessFlags( |
| 498 | java_lang_ref_WeakReference->GetAccessFlags() | |
| 499 | kAccClassIsReference | kAccClassIsWeakReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 500 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 501 | // Setup the ClassLoader, verifying the object_size_. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 502 | mirror::Class* java_lang_ClassLoader = FindSystemClass(self, "Ljava/lang/ClassLoader;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 503 | CHECK_EQ(java_lang_ClassLoader->GetObjectSize(), mirror::ClassLoader::InstanceSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 504 | SetClassRoot(kJavaLangClassLoader, java_lang_ClassLoader); |
| 505 | |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 506 | // Set up java.lang.Throwable, java.lang.ClassNotFoundException, and |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 507 | // java.lang.StackTraceElement as a convenience. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 508 | SetClassRoot(kJavaLangThrowable, FindSystemClass(self, "Ljava/lang/Throwable;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 509 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 510 | SetClassRoot(kJavaLangClassNotFoundException, |
| 511 | FindSystemClass(self, "Ljava/lang/ClassNotFoundException;")); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 512 | SetClassRoot(kJavaLangStackTraceElement, FindSystemClass(self, "Ljava/lang/StackTraceElement;")); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 513 | SetClassRoot(kJavaLangStackTraceElementArrayClass, |
| 514 | FindSystemClass(self, "[Ljava/lang/StackTraceElement;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 515 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 516 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 517 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 518 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 519 | VLOG(startup) << "ClassLinker::InitFromCompiler exiting"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 522 | void ClassLinker::FinishInit(Thread* self) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 523 | VLOG(startup) << "ClassLinker::FinishInit entering"; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 524 | |
| 525 | // Let the heap know some key offsets into java.lang.ref instances |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 526 | // Note: we hard code the field indexes here rather than using FindInstanceField |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 527 | // as the types of the field can't be resolved prior to the runtime being |
| 528 | // fully initialized |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 529 | mirror::Class* java_lang_ref_Reference = GetClassRoot(kJavaLangRefReference); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 530 | mirror::Class* java_lang_ref_FinalizerReference = |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 531 | FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 532 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 533 | mirror::ArtField* pendingNext = java_lang_ref_Reference->GetInstanceField(0); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 534 | CHECK_STREQ(pendingNext->GetName(), "pendingNext"); |
| 535 | CHECK_STREQ(pendingNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 536 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 537 | mirror::ArtField* queue = java_lang_ref_Reference->GetInstanceField(1); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 538 | CHECK_STREQ(queue->GetName(), "queue"); |
| 539 | CHECK_STREQ(queue->GetTypeDescriptor(), "Ljava/lang/ref/ReferenceQueue;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 540 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 541 | mirror::ArtField* queueNext = java_lang_ref_Reference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 542 | CHECK_STREQ(queueNext->GetName(), "queueNext"); |
| 543 | CHECK_STREQ(queueNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 544 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 545 | mirror::ArtField* referent = java_lang_ref_Reference->GetInstanceField(3); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 546 | CHECK_STREQ(referent->GetName(), "referent"); |
| 547 | CHECK_STREQ(referent->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 548 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 549 | mirror::ArtField* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 550 | CHECK_STREQ(zombie->GetName(), "zombie"); |
| 551 | CHECK_STREQ(zombie->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 552 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 553 | // ensure all class_roots_ are initialized |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 554 | for (size_t i = 0; i < kClassRootsMax; i++) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 555 | ClassRoot class_root = static_cast<ClassRoot>(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 556 | mirror::Class* klass = GetClassRoot(class_root); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 557 | CHECK(klass != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 558 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != NULL); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 559 | // note SetClassRoot does additional validation. |
| 560 | // if possible add new checks there to catch errors early |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 563 | CHECK(!array_iftable_.IsNull()); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 564 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 565 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 566 | // that Object, Class, and Object[] are setup |
| 567 | init_done_ = true; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 568 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 569 | VLOG(startup) << "ClassLinker::FinishInit exiting"; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 572 | void ClassLinker::RunRootClinits() { |
| 573 | Thread* self = Thread::Current(); |
| 574 | for (size_t i = 0; i < ClassLinker::kClassRootsMax; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 575 | mirror::Class* c = GetClassRoot(ClassRoot(i)); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 576 | if (!c->IsArrayClass() && !c->IsPrimitive()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 577 | StackHandleScope<1> hs(self); |
| 578 | Handle<mirror::Class> h_class(hs.NewHandle(GetClassRoot(ClassRoot(i)))); |
| 579 | EnsureInitialized(h_class, true, true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 580 | self->AssertNoPendingException(); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | } |
| 584 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 585 | bool ClassLinker::GenerateOatFile(const char* dex_filename, |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 586 | int oat_fd, |
Vladimir Marko | 60836d5 | 2014-01-16 15:53:38 +0000 | [diff] [blame] | 587 | const char* oat_cache_filename, |
| 588 | std::string* error_msg) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 589 | Locks::mutator_lock_->AssertNotHeld(Thread::Current()); // Avoid starving GC. |
Tsu Chiang Chuang | 12e6d74 | 2014-05-22 10:22:25 -0700 | [diff] [blame] | 590 | std::string dex2oat(Runtime::Current()->GetCompilerExecutable()); |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 591 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 592 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 593 | std::string boot_image_option("--boot-image="); |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 594 | boot_image_option += heap->GetImageSpace()->GetImageLocation(); |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 595 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 596 | std::string dex_file_option("--dex-file="); |
| 597 | dex_file_option += dex_filename; |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 598 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 599 | std::string oat_fd_option("--oat-fd="); |
| 600 | StringAppendF(&oat_fd_option, "%d", oat_fd); |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 601 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 602 | std::string oat_location_option("--oat-location="); |
| 603 | oat_location_option += oat_cache_filename; |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 604 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 605 | std::vector<std::string> argv; |
| 606 | argv.push_back(dex2oat); |
| 607 | argv.push_back("--runtime-arg"); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 608 | argv.push_back("-classpath"); |
| 609 | argv.push_back("--runtime-arg"); |
Brian Carlstrom | 35d8b8e | 2014-02-25 10:51:11 -0800 | [diff] [blame] | 610 | argv.push_back(Runtime::Current()->GetClassPathString()); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 611 | |
Ian Rogers | 8afeb85 | 2014-04-02 14:55:49 -0700 | [diff] [blame] | 612 | Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 613 | |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 614 | if (!Runtime::Current()->IsVerificationEnabled()) { |
| 615 | argv.push_back("--compiler-filter=verify-none"); |
| 616 | } |
| 617 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 618 | if (Runtime::Current()->MustRelocateIfPossible()) { |
| 619 | argv.push_back("--runtime-arg"); |
| 620 | argv.push_back("-Xrelocate"); |
| 621 | } else { |
| 622 | argv.push_back("--runtime-arg"); |
| 623 | argv.push_back("-Xnorelocate"); |
| 624 | } |
| 625 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 626 | if (!kIsTargetBuild) { |
| 627 | argv.push_back("--host"); |
buzbee | a024a06 | 2013-07-31 10:47:37 -0700 | [diff] [blame] | 628 | } |
Ian Rogers | 8afeb85 | 2014-04-02 14:55:49 -0700 | [diff] [blame] | 629 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 630 | argv.push_back(boot_image_option); |
| 631 | argv.push_back(dex_file_option); |
| 632 | argv.push_back(oat_fd_option); |
| 633 | argv.push_back(oat_location_option); |
| 634 | const std::vector<std::string>& compiler_options = Runtime::Current()->GetCompilerOptions(); |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 635 | for (size_t i = 0; i < compiler_options.size(); ++i) { |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 636 | argv.push_back(compiler_options[i].c_str()); |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 637 | } |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 638 | |
| 639 | return Exec(argv, error_msg); |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 642 | const OatFile* ClassLinker::RegisterOatFile(const OatFile* oat_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 643 | WriterMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 644 | if (kIsDebugBuild) { |
| 645 | for (size_t i = 0; i < oat_files_.size(); ++i) { |
| 646 | CHECK_NE(oat_file, oat_files_[i]) << oat_file->GetLocation(); |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 647 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 648 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 649 | VLOG(class_linker) << "Registering " << oat_file->GetLocation(); |
| 650 | oat_files_.push_back(oat_file); |
| 651 | return oat_file; |
Brian Carlstrom | 866c862 | 2012-01-06 16:35:13 -0800 | [diff] [blame] | 652 | } |
| 653 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 654 | OatFile& ClassLinker::GetImageOatFile(gc::space::ImageSpace* space) { |
| 655 | VLOG(startup) << "ClassLinker::GetImageOatFile entering"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 656 | OatFile* oat_file = space->ReleaseOatFile(); |
| 657 | CHECK_EQ(RegisterOatFile(oat_file), oat_file); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 658 | VLOG(startup) << "ClassLinker::GetImageOatFile exiting"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 659 | return *oat_file; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 660 | } |
| 661 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 662 | const OatFile* ClassLinker::FindOpenedOatFileForDexFile(const DexFile& dex_file) { |
Brian Carlstrom | 08cbf66 | 2013-12-10 16:52:57 -0800 | [diff] [blame] | 663 | const char* dex_location = dex_file.GetLocation().c_str(); |
| 664 | uint32_t dex_location_checksum = dex_file.GetLocationChecksum(); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 665 | return FindOpenedOatFile(nullptr, dex_location, &dex_location_checksum); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 666 | } |
| 667 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 668 | const OatFile* ClassLinker::FindOpenedOatFile(const char* oat_location, const char* dex_location, |
| 669 | const uint32_t* const dex_location_checksum) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 670 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 671 | for (size_t i = 0; i < oat_files_.size(); i++) { |
| 672 | const OatFile* oat_file = oat_files_[i]; |
| 673 | DCHECK(oat_file != NULL); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 674 | |
| 675 | if (oat_location != nullptr) { |
| 676 | if (oat_file->GetLocation() != oat_location) { |
| 677 | continue; |
| 678 | } |
| 679 | } |
| 680 | |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 681 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, |
Brian Carlstrom | 08cbf66 | 2013-12-10 16:52:57 -0800 | [diff] [blame] | 682 | dex_location_checksum, |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 683 | false); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 684 | if (oat_dex_file != NULL) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 685 | return oat_file; |
| 686 | } |
| 687 | } |
| 688 | return NULL; |
| 689 | } |
| 690 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 691 | static bool LoadMultiDexFilesFromOatFile(const OatFile* oat_file, const char* dex_location, |
| 692 | bool generated, |
| 693 | std::vector<std::string>* error_msgs, |
| 694 | std::vector<const DexFile*>* dex_files) { |
| 695 | if (oat_file == nullptr) { |
| 696 | return false; |
| 697 | } |
| 698 | |
| 699 | size_t old_size = dex_files->size(); // To rollback on error. |
| 700 | |
| 701 | bool success = true; |
| 702 | for (size_t i = 0; success; ++i) { |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 703 | std::string next_name_str = DexFile::GetMultiDexClassesDexName(i, dex_location); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 704 | const char* next_name = next_name_str.c_str(); |
| 705 | |
| 706 | uint32_t dex_location_checksum; |
| 707 | uint32_t* dex_location_checksum_pointer = &dex_location_checksum; |
| 708 | std::string error_msg; |
| 709 | if (!DexFile::GetChecksum(next_name, dex_location_checksum_pointer, &error_msg)) { |
| 710 | DCHECK_EQ(false, i == 0 && generated); |
| 711 | dex_location_checksum_pointer = nullptr; |
| 712 | } |
| 713 | |
| 714 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(next_name, nullptr, false); |
| 715 | |
| 716 | if (oat_dex_file == nullptr) { |
| 717 | if (i == 0 && generated) { |
| 718 | std::string error_msg; |
| 719 | error_msg = StringPrintf("\nFailed to find dex file '%s' (checksum 0x%x) in generated out " |
| 720 | " file'%s'", dex_location, dex_location_checksum, |
| 721 | oat_file->GetLocation().c_str()); |
| 722 | error_msgs->push_back(error_msg); |
| 723 | } |
| 724 | break; // Not found, done. |
| 725 | } |
| 726 | |
| 727 | // Checksum test. Test must succeed when generated. |
| 728 | success = !generated; |
| 729 | if (dex_location_checksum_pointer != nullptr) { |
| 730 | success = dex_location_checksum == oat_dex_file->GetDexFileLocationChecksum(); |
| 731 | } |
| 732 | |
| 733 | if (success) { |
| 734 | const DexFile* dex_file = oat_dex_file->OpenDexFile(&error_msg); |
| 735 | if (dex_file == nullptr) { |
| 736 | success = false; |
| 737 | error_msgs->push_back(error_msg); |
| 738 | } else { |
| 739 | dex_files->push_back(dex_file); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | // When we generated the file, we expect success, or something is terribly wrong. |
| 744 | CHECK_EQ(false, generated && !success) |
| 745 | << "dex_location=" << next_name << " oat_location=" << oat_file->GetLocation().c_str() |
| 746 | << std::hex << " dex_location_checksum=" << dex_location_checksum |
| 747 | << " OatDexFile::GetLocationChecksum()=" << oat_dex_file->GetDexFileLocationChecksum(); |
| 748 | } |
| 749 | |
| 750 | if (dex_files->size() == old_size) { |
| 751 | success = false; // We did not even find classes.dex |
| 752 | } |
| 753 | |
| 754 | if (success) { |
| 755 | return true; |
| 756 | } else { |
| 757 | // Free all the dex files we have loaded. |
| 758 | auto it = dex_files->begin() + old_size; |
| 759 | auto it_end = dex_files->end(); |
| 760 | for (; it != it_end; it++) { |
| 761 | delete *it; |
| 762 | } |
| 763 | dex_files->erase(dex_files->begin() + old_size, it_end); |
| 764 | |
| 765 | return false; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | // Multidex files make it possible that some, but not all, dex files can be broken/outdated. This |
| 770 | // complicates the loading process, as we should not use an iterative loading process, because that |
| 771 | // would register the oat file and dex files that come before the broken one. Instead, check all |
| 772 | // multidex ahead of time. |
| 773 | bool ClassLinker::OpenDexFilesFromOat(const char* dex_location, const char* oat_location, |
| 774 | std::vector<std::string>* error_msgs, |
| 775 | std::vector<const DexFile*>* dex_files) { |
| 776 | // 1) Check whether we have an open oat file. |
| 777 | // This requires a dex checksum, use the "primary" one. |
| 778 | uint32_t dex_location_checksum; |
| 779 | uint32_t* dex_location_checksum_pointer = &dex_location_checksum; |
| 780 | bool have_checksum = true; |
| 781 | std::string checksum_error_msg; |
| 782 | if (!DexFile::GetChecksum(dex_location, dex_location_checksum_pointer, &checksum_error_msg)) { |
| 783 | dex_location_checksum_pointer = nullptr; |
| 784 | have_checksum = false; |
| 785 | } |
| 786 | |
| 787 | bool needs_registering = false; |
| 788 | |
| 789 | std::unique_ptr<const OatFile> open_oat_file(FindOpenedOatFile(oat_location, dex_location, |
| 790 | dex_location_checksum_pointer)); |
| 791 | |
| 792 | // 2) If we do not have an open one, maybe there's one on disk already. |
| 793 | |
| 794 | // In case the oat file is not open, we play a locking game here so |
| 795 | // that if two different processes race to load and register or generate |
| 796 | // (or worse, one tries to open a partial generated file) we will be okay. |
| 797 | // This is actually common with apps that use DexClassLoader to work |
| 798 | // around the dex method reference limit and that have a background |
| 799 | // service running in a separate process. |
| 800 | ScopedFlock scoped_flock; |
| 801 | |
| 802 | if (open_oat_file.get() == nullptr) { |
| 803 | if (oat_location != nullptr) { |
| 804 | // Can only do this if we have a checksum, else error. |
| 805 | if (!have_checksum) { |
| 806 | error_msgs->push_back(checksum_error_msg); |
| 807 | return false; |
| 808 | } |
| 809 | |
| 810 | std::string error_msg; |
| 811 | |
| 812 | // We are loading or creating one in the future. Time to set up the file lock. |
| 813 | if (!scoped_flock.Init(oat_location, &error_msg)) { |
| 814 | error_msgs->push_back(error_msg); |
| 815 | return false; |
| 816 | } |
| 817 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 818 | // TODO Caller specifically asks for this oat_location. We should honor it. Probably? |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 819 | open_oat_file.reset(FindOatFileInOatLocationForDexFile(dex_location, dex_location_checksum, |
| 820 | oat_location, &error_msg)); |
| 821 | |
| 822 | if (open_oat_file.get() == nullptr) { |
| 823 | std::string compound_msg = StringPrintf("Failed to find dex file '%s' in oat location '%s': %s", |
| 824 | dex_location, oat_location, error_msg.c_str()); |
| 825 | VLOG(class_linker) << compound_msg; |
| 826 | error_msgs->push_back(compound_msg); |
| 827 | } |
| 828 | } else { |
| 829 | // TODO: What to lock here? |
Calin Juravle | c54aea7 | 2014-07-16 14:45:03 +0100 | [diff] [blame] | 830 | bool obsolete_file_cleanup_failed; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 831 | open_oat_file.reset(FindOatFileContainingDexFileFromDexLocation(dex_location, |
| 832 | dex_location_checksum_pointer, |
Calin Juravle | c54aea7 | 2014-07-16 14:45:03 +0100 | [diff] [blame] | 833 | kRuntimeISA, error_msgs, |
| 834 | &obsolete_file_cleanup_failed)); |
| 835 | // There's no point in going forward and eventually try to regenerate the |
| 836 | // file if we couldn't remove the obsolete one. Mostly likely we will fail |
| 837 | // with the same error when trying to write the new file. |
Calin Juravle | c54aea7 | 2014-07-16 14:45:03 +0100 | [diff] [blame] | 838 | // TODO: should we maybe do this only when we get permission issues? (i.e. EACCESS). |
| 839 | if (obsolete_file_cleanup_failed) { |
| 840 | return false; |
| 841 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 842 | } |
| 843 | needs_registering = true; |
| 844 | } |
| 845 | |
| 846 | // 3) If we have an oat file, check all contained multidex files for our dex_location. |
| 847 | // Note: LoadMultiDexFilesFromOatFile will check for nullptr in the first argument. |
| 848 | bool success = LoadMultiDexFilesFromOatFile(open_oat_file.get(), dex_location, false, error_msgs, |
| 849 | dex_files); |
| 850 | if (success) { |
| 851 | const OatFile* oat_file = open_oat_file.release(); // Avoid deleting it. |
| 852 | if (needs_registering) { |
| 853 | // We opened the oat file, so we must register it. |
| 854 | RegisterOatFile(oat_file); |
| 855 | } |
| 856 | return true; |
| 857 | } else { |
| 858 | if (needs_registering) { |
| 859 | // We opened it, delete it. |
| 860 | open_oat_file.reset(); |
| 861 | } else { |
| 862 | open_oat_file.release(); // Do not delete open oat files. |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | // 4) If it's not the case (either no oat file or mismatches), regenerate and load. |
| 867 | |
| 868 | // Need a checksum, fail else. |
| 869 | if (!have_checksum) { |
| 870 | error_msgs->push_back(checksum_error_msg); |
| 871 | return false; |
| 872 | } |
| 873 | |
| 874 | // Look in cache location if no oat_location is given. |
| 875 | std::string cache_location; |
| 876 | if (oat_location == nullptr) { |
| 877 | // Use the dalvik cache. |
| 878 | const std::string dalvik_cache(GetDalvikCacheOrDie(GetInstructionSetString(kRuntimeISA))); |
| 879 | cache_location = GetDalvikCacheFilenameOrDie(dex_location, dalvik_cache.c_str()); |
| 880 | oat_location = cache_location.c_str(); |
| 881 | } |
| 882 | |
| 883 | // Definitely need to lock now. |
| 884 | if (!scoped_flock.HasFile()) { |
| 885 | std::string error_msg; |
| 886 | if (!scoped_flock.Init(oat_location, &error_msg)) { |
| 887 | error_msgs->push_back(error_msg); |
| 888 | return false; |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | // Create the oat file. |
| 893 | open_oat_file.reset(CreateOatFileForDexLocation(dex_location, scoped_flock.GetFile()->Fd(), |
| 894 | oat_location, error_msgs)); |
| 895 | |
| 896 | // Failed, bail. |
| 897 | if (open_oat_file.get() == nullptr) { |
| 898 | return false; |
| 899 | } |
| 900 | |
| 901 | // Try to load again, but stronger checks. |
| 902 | success = LoadMultiDexFilesFromOatFile(open_oat_file.get(), dex_location, true, error_msgs, |
| 903 | dex_files); |
| 904 | if (success) { |
| 905 | RegisterOatFile(open_oat_file.release()); |
| 906 | return true; |
| 907 | } else { |
| 908 | return false; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | const OatFile* ClassLinker::FindOatFileInOatLocationForDexFile(const char* dex_location, |
| 913 | uint32_t dex_location_checksum, |
| 914 | const char* oat_location, |
| 915 | std::string* error_msg) { |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 916 | std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_location, oat_location, NULL, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 917 | !Runtime::Current()->IsCompiler(), |
| 918 | error_msg)); |
| 919 | if (oat_file.get() == nullptr) { |
| 920 | *error_msg = StringPrintf("Failed to find existing oat file at %s: %s", oat_location, |
| 921 | error_msg->c_str()); |
| 922 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 923 | } |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 924 | Runtime* runtime = Runtime::Current(); |
| 925 | const ImageHeader& image_header = runtime->GetHeap()->GetImageSpace()->GetImageHeader(); |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 926 | uint32_t expected_image_oat_checksum = image_header.GetOatChecksum(); |
| 927 | uint32_t actual_image_oat_checksum = oat_file->GetOatHeader().GetImageFileLocationOatChecksum(); |
| 928 | if (expected_image_oat_checksum != actual_image_oat_checksum) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 929 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected image oat checksum of " |
| 930 | "0x%x, found 0x%x", oat_location, expected_image_oat_checksum, |
| 931 | actual_image_oat_checksum); |
| 932 | return nullptr; |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 933 | } |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 934 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 935 | uintptr_t expected_image_oat_offset = reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()); |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 936 | uint32_t actual_image_oat_offset = oat_file->GetOatHeader().GetImageFileLocationOatDataBegin(); |
| 937 | if (expected_image_oat_offset != actual_image_oat_offset) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 938 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected image oat offset %" |
| 939 | PRIuPTR ", found %ud", oat_location, expected_image_oat_offset, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 940 | actual_image_oat_offset); |
| 941 | return nullptr; |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 942 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 943 | int32_t expected_patch_delta = image_header.GetPatchDelta(); |
| 944 | int32_t actual_patch_delta = oat_file->GetOatHeader().GetImagePatchDelta(); |
| 945 | if (expected_patch_delta != actual_patch_delta) { |
| 946 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected patch delta %d, " |
| 947 | " found %d", oat_location, expected_patch_delta, actual_patch_delta); |
| 948 | return nullptr; |
| 949 | } |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 950 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, |
| 951 | &dex_location_checksum); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 952 | if (oat_dex_file == nullptr) { |
| 953 | *error_msg = StringPrintf("Failed to find oat file at '%s' containing '%s'", oat_location, |
| 954 | dex_location); |
| 955 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 956 | } |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 957 | uint32_t expected_dex_checksum = dex_location_checksum; |
| 958 | uint32_t actual_dex_checksum = oat_dex_file->GetDexFileLocationChecksum(); |
| 959 | if (expected_dex_checksum != actual_dex_checksum) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 960 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected dex checksum of 0x%x, " |
| 961 | "found 0x%x", oat_location, expected_dex_checksum, |
| 962 | actual_dex_checksum); |
| 963 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 964 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 965 | std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(error_msg)); |
| 966 | if (dex_file.get() != nullptr) { |
| 967 | return oat_file.release(); |
| 968 | } else { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 969 | return nullptr; |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 970 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 971 | } |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 972 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 973 | const OatFile* ClassLinker::CreateOatFileForDexLocation(const char* dex_location, |
| 974 | int fd, const char* oat_location, |
| 975 | std::vector<std::string>* error_msgs) { |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 976 | // Generate the output oat file for the dex file |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 977 | VLOG(class_linker) << "Generating oat file " << oat_location << " for " << dex_location; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 978 | std::string error_msg; |
| 979 | if (!GenerateOatFile(dex_location, fd, oat_location, &error_msg)) { |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 980 | CHECK(!error_msg.empty()); |
| 981 | error_msgs->push_back(error_msg); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 982 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 983 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 984 | std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_location, oat_location, NULL, |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 985 | !Runtime::Current()->IsCompiler(), |
| 986 | &error_msg)); |
| 987 | if (oat_file.get() == nullptr) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 988 | std::string compound_msg = StringPrintf("\nFailed to open generated oat file '%s': %s", |
| 989 | oat_location, error_msg.c_str()); |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 990 | error_msgs->push_back(compound_msg); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 991 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 992 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 993 | |
| 994 | return oat_file.release(); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 995 | } |
| 996 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 997 | bool ClassLinker::VerifyOatImageChecksum(const OatFile* oat_file, |
| 998 | const InstructionSet instruction_set) { |
| 999 | Runtime* runtime = Runtime::Current(); |
| 1000 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace(); |
| 1001 | uint32_t image_oat_checksum = 0; |
| 1002 | if (instruction_set == kRuntimeISA) { |
| 1003 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1004 | image_oat_checksum = image_header.GetOatChecksum(); |
| 1005 | } else { |
| 1006 | std::unique_ptr<ImageHeader> image_header(gc::space::ImageSpace::ReadImageHeaderOrDie( |
| 1007 | image_space->GetImageLocation().c_str(), instruction_set)); |
| 1008 | image_oat_checksum = image_header->GetOatChecksum(); |
| 1009 | } |
| 1010 | return oat_file->GetOatHeader().GetImageFileLocationOatChecksum() == image_oat_checksum; |
| 1011 | } |
| 1012 | |
| 1013 | bool ClassLinker::VerifyOatChecksums(const OatFile* oat_file, |
| 1014 | const InstructionSet instruction_set, |
| 1015 | std::string* error_msg) { |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 1016 | Runtime* runtime = Runtime::Current(); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1017 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace(); |
| 1018 | |
| 1019 | // If the requested instruction set is the same as the current runtime, |
| 1020 | // we can use the checksums directly. If it isn't, we'll have to read the |
| 1021 | // image header from the image for the right instruction set. |
| 1022 | uint32_t image_oat_checksum = 0; |
| 1023 | uintptr_t image_oat_data_begin = 0; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1024 | int32_t image_patch_delta = 0; |
| 1025 | if (instruction_set == Runtime::Current()->GetInstructionSet()) { |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1026 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1027 | image_oat_checksum = image_header.GetOatChecksum(); |
| 1028 | image_oat_data_begin = reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1029 | image_patch_delta = image_header.GetPatchDelta(); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1030 | } else { |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1031 | std::unique_ptr<ImageHeader> image_header(gc::space::ImageSpace::ReadImageHeaderOrDie( |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1032 | image_space->GetImageLocation().c_str(), instruction_set)); |
| 1033 | image_oat_checksum = image_header->GetOatChecksum(); |
| 1034 | image_oat_data_begin = reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1035 | image_patch_delta = image_header->GetPatchDelta(); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1036 | } |
| 1037 | const OatHeader& oat_header = oat_file->GetOatHeader(); |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1038 | bool ret = ((oat_header.GetImageFileLocationOatChecksum() == image_oat_checksum) |
| 1039 | && (oat_header.GetImagePatchDelta() == image_patch_delta) |
| 1040 | && (oat_header.GetImageFileLocationOatDataBegin() == image_oat_data_begin)); |
| 1041 | if (!ret) { |
| 1042 | *error_msg = StringPrintf("oat file '%s' mismatch (0x%x, %d, %d) with (0x%x, %" PRIdPTR ", %d)", |
| 1043 | oat_file->GetLocation().c_str(), |
| 1044 | oat_file->GetOatHeader().GetImageFileLocationOatChecksum(), |
| 1045 | oat_file->GetOatHeader().GetImageFileLocationOatDataBegin(), |
| 1046 | oat_file->GetOatHeader().GetImagePatchDelta(), |
| 1047 | image_oat_checksum, image_oat_data_begin, image_patch_delta); |
| 1048 | } |
| 1049 | return ret; |
| 1050 | } |
| 1051 | |
| 1052 | bool ClassLinker::VerifyOatAndDexFileChecksums(const OatFile* oat_file, |
| 1053 | const char* dex_location, |
| 1054 | uint32_t dex_location_checksum, |
| 1055 | const InstructionSet instruction_set, |
| 1056 | std::string* error_msg) { |
| 1057 | if (!VerifyOatChecksums(oat_file, instruction_set, error_msg)) { |
| 1058 | return false; |
| 1059 | } |
Brian Carlstrom | 46b8a62 | 2012-06-19 23:13:22 -0700 | [diff] [blame] | 1060 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1061 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, |
| 1062 | &dex_location_checksum); |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 1063 | if (oat_dex_file == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1064 | *error_msg = StringPrintf("oat file '%s' does not contain contents for '%s' with checksum 0x%x", |
| 1065 | oat_file->GetLocation().c_str(), dex_location, dex_location_checksum); |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 1066 | std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file->GetOatDexFiles(); |
| 1067 | for (size_t i = 0; i < oat_dex_files.size(); i++) { |
| 1068 | const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i]; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1069 | *error_msg += StringPrintf("\noat file '%s' contains contents for '%s'", |
| 1070 | oat_file->GetLocation().c_str(), |
| 1071 | oat_dex_file->GetDexFileLocation().c_str()); |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 1072 | } |
| 1073 | return false; |
| 1074 | } |
Brian Carlstrom | 46b8a62 | 2012-06-19 23:13:22 -0700 | [diff] [blame] | 1075 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1076 | if (dex_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1077 | *error_msg = StringPrintf("oat file '%s' mismatch (0x%x) with '%s' (0x%x)", |
| 1078 | oat_file->GetLocation().c_str(), |
| 1079 | oat_dex_file->GetDexFileLocationChecksum(), |
| 1080 | dex_location, dex_location_checksum); |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1081 | return false; |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 1082 | } |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1083 | return true; |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1086 | bool ClassLinker::VerifyOatWithDexFile(const OatFile* oat_file, |
| 1087 | const char* dex_location, |
| 1088 | std::string* error_msg) { |
| 1089 | CHECK(oat_file != nullptr); |
| 1090 | CHECK(dex_location != nullptr); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1091 | std::unique_ptr<const DexFile> dex_file; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1092 | uint32_t dex_location_checksum; |
| 1093 | if (!DexFile::GetChecksum(dex_location, &dex_location_checksum, error_msg)) { |
| 1094 | // If no classes.dex found in dex_location, it has been stripped or is corrupt, assume oat is |
| 1095 | // up-to-date. This is the common case in user builds for jar's and apk's in the /system |
| 1096 | // directory. |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1097 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, NULL); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1098 | if (oat_dex_file == nullptr) { |
| 1099 | *error_msg = StringPrintf("Dex checksum mismatch for location '%s' and failed to find oat " |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1100 | "dex file '%s': %s", oat_file->GetLocation().c_str(), dex_location, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1101 | error_msg->c_str()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1102 | return false; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1103 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1104 | dex_file.reset(oat_dex_file->OpenDexFile(error_msg)); |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1105 | } else { |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1106 | bool verified = VerifyOatAndDexFileChecksums(oat_file, dex_location, dex_location_checksum, |
| 1107 | kRuntimeISA, error_msg); |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1108 | if (!verified) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1109 | return false; |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1110 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1111 | dex_file.reset(oat_file->GetOatDexFile(dex_location, |
| 1112 | &dex_location_checksum)->OpenDexFile(error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1113 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1114 | return dex_file.get() != nullptr; |
Brian Carlstrom | 46b8a62 | 2012-06-19 23:13:22 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1117 | const OatFile* ClassLinker::FindOatFileContainingDexFileFromDexLocation( |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1118 | const char* dex_location, |
| 1119 | const uint32_t* const dex_location_checksum, |
| 1120 | InstructionSet isa, |
Calin Juravle | c54aea7 | 2014-07-16 14:45:03 +0100 | [diff] [blame] | 1121 | std::vector<std::string>* error_msgs, |
| 1122 | bool* obsolete_file_cleanup_failed) { |
| 1123 | *obsolete_file_cleanup_failed = false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1124 | bool already_opened = false; |
| 1125 | std::string dex_location_str(dex_location); |
| 1126 | std::unique_ptr<const OatFile> oat_file(OpenOatFileFromDexLocation(dex_location_str, isa, |
| 1127 | &already_opened, |
| 1128 | obsolete_file_cleanup_failed, |
| 1129 | error_msgs)); |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 1130 | std::string error_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1131 | if (oat_file.get() == nullptr) { |
| 1132 | error_msgs->push_back(StringPrintf("Failed to open oat file from dex location '%s'", |
| 1133 | dex_location)); |
| 1134 | return nullptr; |
| 1135 | } else if (!VerifyOatWithDexFile(oat_file.get(), dex_location, &error_msg)) { |
| 1136 | error_msgs->push_back(StringPrintf("Failed to verify oat file '%s' found for dex location " |
| 1137 | "'%s': %s", oat_file->GetLocation().c_str(), dex_location, |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 1138 | error_msg.c_str())); |
Brian Carlstrom | 08cbf66 | 2013-12-10 16:52:57 -0800 | [diff] [blame] | 1139 | return nullptr; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1140 | } else { |
| 1141 | return oat_file.release(); |
Brian Carlstrom | 08cbf66 | 2013-12-10 16:52:57 -0800 | [diff] [blame] | 1142 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1145 | const OatFile* ClassLinker::FindOpenedOatFileFromOatLocation(const std::string& oat_location) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1146 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1147 | for (size_t i = 0; i < oat_files_.size(); i++) { |
| 1148 | const OatFile* oat_file = oat_files_[i]; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1149 | DCHECK(oat_file != nullptr); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1150 | if (oat_file->GetLocation() == oat_location) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1151 | return oat_file; |
| 1152 | } |
| 1153 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1154 | return nullptr; |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 1155 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1156 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1157 | const OatFile* ClassLinker::OpenOatFileFromDexLocation(const std::string& dex_location, |
| 1158 | InstructionSet isa, |
| 1159 | bool *already_opened, |
| 1160 | bool *obsolete_file_cleanup_failed, |
| 1161 | std::vector<std::string>* error_msgs) { |
| 1162 | // Find out if we've already opened the file |
| 1163 | const OatFile* ret = nullptr; |
| 1164 | std::string odex_filename(DexFilenameToOdexFilename(dex_location, isa)); |
| 1165 | ret = FindOpenedOatFileFromOatLocation(odex_filename); |
| 1166 | if (ret != nullptr) { |
| 1167 | *already_opened = true; |
| 1168 | return ret; |
| 1169 | } |
| 1170 | |
| 1171 | std::string dalvik_cache; |
| 1172 | bool have_android_data = false; |
| 1173 | bool have_dalvik_cache = false; |
| 1174 | GetDalvikCache(GetInstructionSetString(kRuntimeISA), false, &dalvik_cache, |
| 1175 | &have_android_data, &have_dalvik_cache); |
| 1176 | std::string cache_filename; |
| 1177 | if (have_dalvik_cache) { |
| 1178 | cache_filename = GetDalvikCacheFilenameOrDie(dex_location.c_str(), dalvik_cache.c_str()); |
| 1179 | ret = FindOpenedOatFileFromOatLocation(cache_filename); |
| 1180 | if (ret != nullptr) { |
| 1181 | *already_opened = true; |
| 1182 | return ret; |
| 1183 | } |
| 1184 | } else { |
| 1185 | // If we need to relocate we should just place odex back where it started. |
| 1186 | cache_filename = odex_filename; |
| 1187 | } |
| 1188 | |
| 1189 | ret = nullptr; |
| 1190 | |
| 1191 | // We know that neither the odex nor the cache'd version is already in use, if it even exists. |
| 1192 | // |
| 1193 | // Now we do the following: |
| 1194 | // 1) Try and open the odex version |
| 1195 | // 2) If present, checksum-verified & relocated correctly return it |
| 1196 | // 3) Close the odex version to free up its address space. |
| 1197 | // 4) Try and open the cache version |
| 1198 | // 5) If present, checksum-verified & relocated correctly return it |
| 1199 | // 6) Close the cache version to free up its address space. |
| 1200 | // 7) If we should relocate: |
| 1201 | // a) If we have opened and checksum-verified the odex version relocate it to |
| 1202 | // 'cache_filename' and return it |
| 1203 | // b) If we have opened and checksum-verified the cache version relocate it in place and return |
| 1204 | // it. This should not happen often (I think only the run-test's will hit this case). |
| 1205 | // 8) If the cache-version was present we should delete it since it must be obsolete if we get to |
| 1206 | // this point. |
| 1207 | // 9) Return nullptr |
| 1208 | |
| 1209 | *already_opened = false; |
| 1210 | const Runtime* runtime = Runtime::Current(); |
| 1211 | CHECK(runtime != nullptr); |
| 1212 | bool executable = !runtime->IsCompiler(); |
| 1213 | |
| 1214 | std::string odex_error_msg; |
| 1215 | bool should_patch_system = false; |
| 1216 | bool odex_checksum_verified = false; |
| 1217 | { |
| 1218 | // There is a high probability that these both these oat files map similar/the same address |
| 1219 | // spaces so we must scope them like this so they each gets its turn. |
| 1220 | std::unique_ptr<OatFile> odex_oat_file(OatFile::Open(odex_filename, odex_filename, NULL, |
| 1221 | executable, &odex_error_msg)); |
| 1222 | if (odex_oat_file.get() != nullptr && CheckOatFile(odex_oat_file.get(), isa, |
| 1223 | &odex_checksum_verified, |
| 1224 | &odex_error_msg)) { |
| 1225 | error_msgs->push_back(odex_error_msg); |
| 1226 | return odex_oat_file.release(); |
| 1227 | } else if (odex_checksum_verified) { |
| 1228 | // We can just relocate |
| 1229 | should_patch_system = true; |
| 1230 | odex_error_msg = "Image Patches are incorrect"; |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | |
| 1235 | std::string cache_error_msg; |
| 1236 | bool should_patch_cache = false; |
| 1237 | bool cache_checksum_verified = false; |
| 1238 | if (have_dalvik_cache) { |
| 1239 | std::unique_ptr<OatFile> cache_oat_file(OatFile::Open(cache_filename, cache_filename, NULL, |
| 1240 | executable, &cache_error_msg)); |
| 1241 | if (cache_oat_file.get() != nullptr && CheckOatFile(cache_oat_file.get(), isa, |
| 1242 | &cache_checksum_verified, |
| 1243 | &cache_error_msg)) { |
| 1244 | error_msgs->push_back(cache_error_msg); |
| 1245 | return cache_oat_file.release(); |
| 1246 | } else if (cache_checksum_verified) { |
| 1247 | // We can just relocate |
| 1248 | should_patch_cache = true; |
| 1249 | cache_error_msg = "Image Patches are incorrect"; |
| 1250 | } |
| 1251 | } else if (have_android_data) { |
| 1252 | // dalvik_cache does not exist but android data does. This means we should be able to create |
| 1253 | // it, so we should try. |
| 1254 | GetDalvikCacheOrDie(GetInstructionSetString(kRuntimeISA), true); |
| 1255 | } |
| 1256 | |
| 1257 | ret = nullptr; |
| 1258 | std::string error_msg; |
| 1259 | if (runtime->CanRelocate()) { |
| 1260 | // Run relocation |
| 1261 | const std::string& image_location = |
| 1262 | Runtime::Current()->GetHeap()->GetImageSpace()->GetImageLocation(); |
| 1263 | if (odex_checksum_verified && should_patch_system) { |
| 1264 | ret = PatchAndRetrieveOat(odex_filename, cache_filename, image_location, isa, &error_msg); |
| 1265 | } else if (cache_checksum_verified && should_patch_cache) { |
| 1266 | CHECK(have_dalvik_cache); |
| 1267 | ret = PatchAndRetrieveOat(cache_filename, cache_filename, image_location, isa, &error_msg); |
| 1268 | } |
| 1269 | } |
| 1270 | if (ret == nullptr && have_dalvik_cache && OS::FileExists(cache_filename.c_str())) { |
| 1271 | // implicitly: were able to fine where the cached version is but we were unable to use it, |
| 1272 | // either as a destination for relocation or to open a file. We should delete it if it is |
| 1273 | // there. |
| 1274 | if (TEMP_FAILURE_RETRY(unlink(cache_filename.c_str())) != 0) { |
| 1275 | std::string rm_error_msg = StringPrintf("Failed to remove obsolete file from %s when " |
| 1276 | "searching for dex file %s: %s", |
| 1277 | cache_filename.c_str(), dex_location.c_str(), |
| 1278 | strerror(errno)); |
| 1279 | error_msgs->push_back(rm_error_msg); |
| 1280 | VLOG(class_linker) << rm_error_msg; |
| 1281 | // Let the caller know that we couldn't remove the obsolete file. |
| 1282 | // This is a good indication that further writes may fail as well. |
| 1283 | *obsolete_file_cleanup_failed = true; |
| 1284 | } |
| 1285 | } |
| 1286 | if (ret == nullptr) { |
| 1287 | VLOG(class_linker) << error_msg; |
| 1288 | error_msgs->push_back(error_msg); |
| 1289 | std::string relocation_msg; |
| 1290 | if (runtime->CanRelocate()) { |
| 1291 | relocation_msg = StringPrintf(" and relocation failed"); |
| 1292 | } |
| 1293 | if (have_dalvik_cache && cache_checksum_verified) { |
| 1294 | error_msg = StringPrintf("Failed to open oat file from %s (error %s) or %s " |
| 1295 | "(error %s)%s.", odex_filename.c_str(), odex_error_msg.c_str(), |
| 1296 | cache_filename.c_str(), cache_error_msg.c_str(), |
| 1297 | relocation_msg.c_str()); |
| 1298 | } else { |
| 1299 | error_msg = StringPrintf("Failed to open oat file from %s (error %s) (no " |
| 1300 | "dalvik_cache availible)%s.", odex_filename.c_str(), |
| 1301 | odex_error_msg.c_str(), relocation_msg.c_str()); |
| 1302 | } |
| 1303 | VLOG(class_linker) << error_msg; |
| 1304 | error_msgs->push_back(error_msg); |
| 1305 | } |
| 1306 | return ret; |
| 1307 | } |
| 1308 | |
| 1309 | const OatFile* ClassLinker::PatchAndRetrieveOat(const std::string& input_oat, |
| 1310 | const std::string& output_oat, |
| 1311 | const std::string& image_location, |
| 1312 | InstructionSet isa, |
| 1313 | std::string* error_msg) { |
| 1314 | Locks::mutator_lock_->AssertNotHeld(Thread::Current()); // Avoid starving GC. |
| 1315 | std::string patchoat(Runtime::Current()->GetPatchoatExecutable()); |
| 1316 | |
| 1317 | std::string isa_arg("--instruction-set="); |
| 1318 | isa_arg += GetInstructionSetString(isa); |
| 1319 | std::string input_oat_filename_arg("--input-oat-file="); |
| 1320 | input_oat_filename_arg += input_oat; |
| 1321 | std::string output_oat_filename_arg("--output-oat-file="); |
| 1322 | output_oat_filename_arg += output_oat; |
| 1323 | std::string patched_image_arg("--patched-image-location="); |
| 1324 | patched_image_arg += image_location; |
| 1325 | |
| 1326 | std::vector<std::string> argv; |
| 1327 | argv.push_back(patchoat); |
| 1328 | argv.push_back(isa_arg); |
| 1329 | argv.push_back(input_oat_filename_arg); |
| 1330 | argv.push_back(output_oat_filename_arg); |
| 1331 | argv.push_back(patched_image_arg); |
| 1332 | |
| 1333 | std::string command_line(Join(argv, ' ')); |
| 1334 | LOG(INFO) << "Relocate Oat File: " << command_line; |
| 1335 | bool success = Exec(argv, error_msg); |
| 1336 | if (success) { |
| 1337 | std::unique_ptr<OatFile> output(OatFile::Open(output_oat, output_oat, NULL, |
| 1338 | !Runtime::Current()->IsCompiler(), error_msg)); |
| 1339 | bool checksum_verified = false; |
| 1340 | if (output.get() != nullptr && CheckOatFile(output.get(), isa, &checksum_verified, error_msg)) { |
| 1341 | return output.release(); |
| 1342 | } else if (output.get() != nullptr) { |
| 1343 | *error_msg = StringPrintf("Patching of oat file '%s' succeeded " |
| 1344 | "but output file '%s' failed verifcation: %s", |
| 1345 | input_oat.c_str(), output_oat.c_str(), error_msg->c_str()); |
| 1346 | } else { |
| 1347 | *error_msg = StringPrintf("Patching of oat file '%s' succeeded " |
| 1348 | "but was unable to open output file '%s': %s", |
| 1349 | input_oat.c_str(), output_oat.c_str(), error_msg->c_str()); |
| 1350 | } |
| 1351 | } else { |
| 1352 | *error_msg = StringPrintf("Patching of oat file '%s to '%s' " |
| 1353 | "failed: %s", input_oat.c_str(), output_oat.c_str(), |
| 1354 | error_msg->c_str()); |
| 1355 | } |
| 1356 | return nullptr; |
| 1357 | } |
| 1358 | |
| 1359 | int32_t ClassLinker::GetRequiredDelta(const OatFile* oat_file, InstructionSet isa) { |
| 1360 | Runtime* runtime = Runtime::Current(); |
| 1361 | int32_t real_patch_delta; |
| 1362 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace(); |
| 1363 | if (isa == Runtime::Current()->GetInstructionSet()) { |
| 1364 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1365 | real_patch_delta = image_header.GetPatchDelta(); |
| 1366 | } else { |
| 1367 | std::unique_ptr<ImageHeader> image_header(gc::space::ImageSpace::ReadImageHeaderOrDie( |
| 1368 | image_space->GetImageLocation().c_str(), isa)); |
| 1369 | real_patch_delta = image_header->GetPatchDelta(); |
| 1370 | } |
| 1371 | const OatHeader& oat_header = oat_file->GetOatHeader(); |
| 1372 | return real_patch_delta - oat_header.GetImagePatchDelta(); |
| 1373 | } |
| 1374 | |
| 1375 | bool ClassLinker::CheckOatFile(const OatFile* oat_file, InstructionSet isa, |
| 1376 | bool* checksum_verified, |
| 1377 | std::string* error_msg) { |
| 1378 | std::string compound_msg("Oat file failed to verify: "); |
| 1379 | Runtime* runtime = Runtime::Current(); |
| 1380 | uint32_t real_image_checksum; |
| 1381 | void* real_image_oat_offset; |
| 1382 | int32_t real_patch_delta; |
| 1383 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace(); |
| 1384 | if (isa == Runtime::Current()->GetInstructionSet()) { |
| 1385 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1386 | real_image_checksum = image_header.GetOatChecksum(); |
| 1387 | real_image_oat_offset = image_header.GetOatDataBegin(); |
| 1388 | real_patch_delta = image_header.GetPatchDelta(); |
| 1389 | } else { |
| 1390 | std::unique_ptr<ImageHeader> image_header(gc::space::ImageSpace::ReadImageHeaderOrDie( |
| 1391 | image_space->GetImageLocation().c_str(), isa)); |
| 1392 | real_image_checksum = image_header->GetOatChecksum(); |
| 1393 | real_image_oat_offset = image_header->GetOatDataBegin(); |
| 1394 | real_patch_delta = image_header->GetPatchDelta(); |
| 1395 | } |
| 1396 | |
| 1397 | const OatHeader& oat_header = oat_file->GetOatHeader(); |
| 1398 | |
| 1399 | uint32_t oat_image_checksum = oat_header.GetImageFileLocationOatChecksum(); |
| 1400 | *checksum_verified = oat_image_checksum == real_image_checksum; |
| 1401 | if (!*checksum_verified) { |
| 1402 | compound_msg += StringPrintf(" Oat Image Checksum Incorrect (expected 0x%x, recieved 0x%x)", |
| 1403 | real_image_checksum, oat_image_checksum); |
| 1404 | } |
| 1405 | |
| 1406 | void* oat_image_oat_offset = |
| 1407 | reinterpret_cast<void*>(oat_header.GetImageFileLocationOatDataBegin()); |
| 1408 | bool offset_verified = oat_image_oat_offset == real_image_oat_offset; |
| 1409 | if (!offset_verified) { |
| 1410 | compound_msg += StringPrintf(" Oat Image oat offset incorrect (expected 0x%p, recieved 0x%p)", |
| 1411 | real_image_oat_offset, oat_image_oat_offset); |
| 1412 | } |
| 1413 | |
| 1414 | int32_t oat_patch_delta = oat_header.GetImagePatchDelta(); |
| 1415 | bool patch_delta_verified = oat_patch_delta == real_patch_delta; |
| 1416 | if (!patch_delta_verified) { |
| 1417 | compound_msg += StringPrintf(" Oat image patch delta incorrect (expected 0x%x, recieved 0x%x)", |
| 1418 | real_patch_delta, oat_patch_delta); |
| 1419 | } |
| 1420 | |
| 1421 | bool ret = (*checksum_verified && offset_verified && patch_delta_verified); |
| 1422 | if (ret) { |
| 1423 | *error_msg = compound_msg; |
| 1424 | } |
| 1425 | return ret; |
| 1426 | } |
| 1427 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1428 | const OatFile* ClassLinker::FindOatFileFromOatLocation(const std::string& oat_location, |
| 1429 | std::string* error_msg) { |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 1430 | const OatFile* oat_file = FindOpenedOatFileFromOatLocation(oat_location); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1431 | if (oat_file != nullptr) { |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 1432 | return oat_file; |
| 1433 | } |
| 1434 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1435 | oat_file = OatFile::Open(oat_location, oat_location, NULL, !Runtime::Current()->IsCompiler(), |
| 1436 | error_msg); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1437 | if (oat_file == NULL) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 1438 | return NULL; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1439 | } |
| 1440 | return oat_file; |
| 1441 | } |
| 1442 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1443 | static void InitFromImageInterpretOnlyCallback(mirror::Object* obj, void* arg) |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1444 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1445 | ClassLinker* class_linker = reinterpret_cast<ClassLinker*>(arg); |
| 1446 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1447 | DCHECK(obj != NULL); |
| 1448 | DCHECK(class_linker != NULL); |
| 1449 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1450 | if (obj->IsArtMethod()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1451 | mirror::ArtMethod* method = obj->AsArtMethod(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1452 | if (!method->IsNative()) { |
| 1453 | method->SetEntryPointFromInterpreter(interpreter::artInterpreterToInterpreterBridge); |
| 1454 | if (method != Runtime::Current()->GetResolutionMethod()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1455 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 1456 | method->SetEntryPointFromPortableCompiledCode(GetPortableToInterpreterBridge()); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1457 | } |
| 1458 | } |
| 1459 | } |
| 1460 | } |
| 1461 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1462 | void ClassLinker::InitFromImage() { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1463 | VLOG(startup) << "ClassLinker::InitFromImage entering"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1464 | CHECK(!init_done_); |
| 1465 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1466 | Thread* self = Thread::Current(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1467 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 1468 | gc::space::ImageSpace* space = heap->GetImageSpace(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1469 | dex_cache_image_class_lookup_required_ = true; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1470 | CHECK(space != NULL); |
| 1471 | OatFile& oat_file = GetImageOatFile(space); |
| 1472 | CHECK_EQ(oat_file.GetOatHeader().GetImageFileLocationOatChecksum(), 0U); |
| 1473 | CHECK_EQ(oat_file.GetOatHeader().GetImageFileLocationOatDataBegin(), 0U); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1474 | const char* image_file_location = oat_file.GetOatHeader(). |
| 1475 | GetStoreValueByKey(OatHeader::kImageLocationKey); |
| 1476 | CHECK(image_file_location == nullptr || *image_file_location == 0); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1477 | portable_resolution_trampoline_ = oat_file.GetOatHeader().GetPortableResolutionTrampoline(); |
| 1478 | quick_resolution_trampoline_ = oat_file.GetOatHeader().GetQuickResolutionTrampoline(); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1479 | portable_imt_conflict_trampoline_ = oat_file.GetOatHeader().GetPortableImtConflictTrampoline(); |
| 1480 | quick_imt_conflict_trampoline_ = oat_file.GetOatHeader().GetQuickImtConflictTrampoline(); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1481 | quick_generic_jni_trampoline_ = oat_file.GetOatHeader().GetQuickGenericJniTrampoline(); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 1482 | quick_to_interpreter_bridge_trampoline_ = oat_file.GetOatHeader().GetQuickToInterpreterBridge(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1483 | mirror::Object* dex_caches_object = space->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
| 1484 | mirror::ObjectArray<mirror::DexCache>* dex_caches = |
| 1485 | dex_caches_object->AsObjectArray<mirror::DexCache>(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1486 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1487 | StackHandleScope<1> hs(self); |
| 1488 | Handle<mirror::ObjectArray<mirror::Class>> class_roots(hs.NewHandle( |
| 1489 | space->GetImageHeader().GetImageRoot(ImageHeader::kClassRoots)-> |
| 1490 | AsObjectArray<mirror::Class>())); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1491 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>(class_roots.Get()); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1492 | |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1493 | // Special case of setting up the String class early so that we can test arbitrary objects |
| 1494 | // as being Strings or not |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 1495 | mirror::String::SetClass(GetClassRoot(kJavaLangString)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1496 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1497 | CHECK_EQ(oat_file.GetOatHeader().GetDexFileCount(), |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1498 | static_cast<uint32_t>(dex_caches->GetLength())); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1499 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1500 | StackHandleScope<1> hs(self); |
| 1501 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(dex_caches->Get(i))); |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1502 | const std::string& dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1503 | const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(dex_file_location.c_str(), |
| 1504 | nullptr); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1505 | CHECK(oat_dex_file != NULL) << oat_file.GetLocation() << " " << dex_file_location; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1506 | std::string error_msg; |
| 1507 | const DexFile* dex_file = oat_dex_file->OpenDexFile(&error_msg); |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1508 | if (dex_file == NULL) { |
| 1509 | LOG(FATAL) << "Failed to open dex file " << dex_file_location |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1510 | << " from within oat file " << oat_file.GetLocation() |
| 1511 | << " error '" << error_msg << "'"; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1512 | } |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1513 | |
| 1514 | CHECK_EQ(dex_file->GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum()); |
| 1515 | |
| 1516 | AppendToBootClassPath(*dex_file, dex_cache); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1517 | } |
| 1518 | |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 1519 | // Set classes on AbstractMethod early so that IsMethod tests can be performed during the live |
| 1520 | // bitmap walk. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1521 | mirror::ArtMethod::SetClass(GetClassRoot(kJavaLangReflectArtMethod)); |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 1522 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1523 | // Set entry point to interpreter if in InterpretOnly mode. |
| 1524 | if (Runtime::Current()->GetInstrumentation()->InterpretOnly()) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1525 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1526 | heap->VisitObjects(InitFromImageInterpretOnlyCallback, this); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1527 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1528 | |
| 1529 | // reinit class_roots_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1530 | mirror::Class::SetClassClass(class_roots->Get(kJavaLangClass)); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1531 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>(class_roots.Get()); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1532 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1533 | // reinit array_iftable_ from any array class instance, they should be == |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1534 | array_iftable_ = GcRoot<mirror::IfTable>(GetClassRoot(kObjectArrayClass)->GetIfTable()); |
| 1535 | DCHECK(array_iftable_.Read() == GetClassRoot(kBooleanArrayClass)->GetIfTable()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1536 | // String class root was set above |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 1537 | mirror::Reference::SetClass(GetClassRoot(kJavaLangRefReference)); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1538 | mirror::ArtField::SetClass(GetClassRoot(kJavaLangReflectArtField)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1539 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
| 1540 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
| 1541 | mirror::CharArray::SetArrayClass(GetClassRoot(kCharArrayClass)); |
| 1542 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
| 1543 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
| 1544 | mirror::IntArray::SetArrayClass(GetClassRoot(kIntArrayClass)); |
| 1545 | mirror::LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
| 1546 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
| 1547 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
| 1548 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1549 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1550 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1551 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1552 | VLOG(startup) << "ClassLinker::InitFromImage exiting"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1553 | } |
| 1554 | |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1555 | void ClassLinker::VisitClassRoots(RootCallback* callback, void* arg, VisitRootFlags flags) { |
| 1556 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 1557 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1558 | for (std::pair<const size_t, GcRoot<mirror::Class> >& it : class_table_) { |
| 1559 | it.second.VisitRoot(callback, arg, 0, kRootStickyClass); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1560 | } |
| 1561 | } else if ((flags & kVisitRootFlagNewRoots) != 0) { |
| 1562 | for (auto& pair : new_class_roots_) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1563 | mirror::Class* old_ref = pair.second.Read<kWithoutReadBarrier>(); |
| 1564 | pair.second.VisitRoot(callback, arg, 0, kRootStickyClass); |
| 1565 | mirror::Class* new_ref = pair.second.Read<kWithoutReadBarrier>(); |
| 1566 | if (UNLIKELY(new_ref != old_ref)) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1567 | // Uh ohes, GC moved a root in the log. Need to search the class_table and update the |
| 1568 | // corresponding object. This is slow, but luckily for us, this may only happen with a |
| 1569 | // concurrent moving GC. |
| 1570 | for (auto it = class_table_.lower_bound(pair.first), end = class_table_.end(); |
| 1571 | it != end && it->first == pair.first; ++it) { |
| 1572 | // If the class stored matches the old class, update it to the new value. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1573 | if (old_ref == it->second.Read<kWithoutReadBarrier>()) { |
| 1574 | it->second = GcRoot<mirror::Class>(new_ref); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1575 | } |
| 1576 | } |
| 1577 | } |
| 1578 | } |
| 1579 | } |
| 1580 | if ((flags & kVisitRootFlagClearRootLog) != 0) { |
| 1581 | new_class_roots_.clear(); |
| 1582 | } |
| 1583 | if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
| 1584 | log_new_class_table_roots_ = true; |
| 1585 | } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
| 1586 | log_new_class_table_roots_ = false; |
| 1587 | } |
| 1588 | // We deliberately ignore the class roots in the image since we |
| 1589 | // handle image roots by using the MS/CMS rescanning of dirty cards. |
| 1590 | } |
| 1591 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1592 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 1593 | // reinit references to when reinitializing a ClassLinker from a |
| 1594 | // mapped image. |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 1595 | void ClassLinker::VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1596 | class_roots_.VisitRoot(callback, arg, 0, kRootVMInternal); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1597 | Thread* self = Thread::Current(); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1598 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 1599 | ReaderMutexLock mu(self, dex_lock_); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 1600 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1601 | for (GcRoot<mirror::DexCache>& dex_cache : dex_caches_) { |
| 1602 | dex_cache.VisitRoot(callback, arg, 0, kRootVMInternal); |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 1603 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 1604 | } else if ((flags & kVisitRootFlagNewRoots) != 0) { |
| 1605 | for (size_t index : new_dex_cache_roots_) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1606 | dex_caches_[index].VisitRoot(callback, arg, 0, kRootVMInternal); |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 1607 | } |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1608 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 1609 | if ((flags & kVisitRootFlagClearRootLog) != 0) { |
| 1610 | new_dex_cache_roots_.clear(); |
| 1611 | } |
| 1612 | if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
| 1613 | log_new_dex_caches_roots_ = true; |
| 1614 | } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
| 1615 | log_new_dex_caches_roots_ = false; |
| 1616 | } |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1617 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1618 | VisitClassRoots(callback, arg, flags); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1619 | array_iftable_.VisitRoot(callback, arg, 0, kRootVMInternal); |
| 1620 | DCHECK(!array_iftable_.IsNull()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1621 | for (size_t i = 0; i < kFindArrayCacheSize; ++i) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1622 | if (!find_array_class_cache_[i].IsNull()) { |
| 1623 | find_array_class_cache_[i].VisitRoot(callback, arg, 0, kRootVMInternal); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1624 | } |
| 1625 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1626 | } |
| 1627 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1628 | void ClassLinker::VisitClasses(ClassVisitor* visitor, void* arg) { |
| 1629 | if (dex_cache_image_class_lookup_required_) { |
| 1630 | MoveImageClassesToClassTable(); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1631 | } |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 1632 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1633 | for (std::pair<const size_t, GcRoot<mirror::Class> >& it : class_table_) { |
| 1634 | mirror::Class* c = it.second.Read(); |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 1635 | if (!visitor(c, arg)) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1636 | return; |
| 1637 | } |
| 1638 | } |
| 1639 | } |
| 1640 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1641 | static bool GetClassesVisitor(mirror::Class* c, void* arg) { |
| 1642 | std::set<mirror::Class*>* classes = reinterpret_cast<std::set<mirror::Class*>*>(arg); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1643 | classes->insert(c); |
| 1644 | return true; |
| 1645 | } |
| 1646 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1647 | void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor, void* arg) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1648 | std::set<mirror::Class*> classes; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1649 | VisitClasses(GetClassesVisitor, &classes); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1650 | for (mirror::Class* klass : classes) { |
| 1651 | if (!visitor(klass, arg)) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1652 | return; |
| 1653 | } |
| 1654 | } |
| 1655 | } |
| 1656 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1657 | ClassLinker::~ClassLinker() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1658 | mirror::Class::ResetClass(); |
| 1659 | mirror::String::ResetClass(); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 1660 | mirror::Reference::ResetClass(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1661 | mirror::ArtField::ResetClass(); |
| 1662 | mirror::ArtMethod::ResetClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1663 | mirror::BooleanArray::ResetArrayClass(); |
| 1664 | mirror::ByteArray::ResetArrayClass(); |
| 1665 | mirror::CharArray::ResetArrayClass(); |
| 1666 | mirror::DoubleArray::ResetArrayClass(); |
| 1667 | mirror::FloatArray::ResetArrayClass(); |
| 1668 | mirror::IntArray::ResetArrayClass(); |
| 1669 | mirror::LongArray::ResetArrayClass(); |
| 1670 | mirror::ShortArray::ResetArrayClass(); |
| 1671 | mirror::Throwable::ResetClass(); |
| 1672 | mirror::StackTraceElement::ResetClass(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1673 | STLDeleteElements(&boot_class_path_); |
| 1674 | STLDeleteElements(&oat_files_); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1675 | } |
| 1676 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1677 | mirror::DexCache* ClassLinker::AllocDexCache(Thread* self, const DexFile& dex_file) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1678 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1679 | StackHandleScope<16> hs(self); |
| 1680 | Handle<mirror::Class> dex_cache_class(hs.NewHandle(GetClassRoot(kJavaLangDexCache))); |
| 1681 | Handle<mirror::DexCache> dex_cache( |
| 1682 | hs.NewHandle(down_cast<mirror::DexCache*>( |
| 1683 | heap->AllocObject<true>(self, dex_cache_class.Get(), dex_cache_class->GetObjectSize(), |
| 1684 | VoidFunctor())))); |
| 1685 | if (dex_cache.Get() == NULL) { |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1686 | return NULL; |
| 1687 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1688 | Handle<mirror::String> |
| 1689 | location(hs.NewHandle(intern_table_->InternStrong(dex_file.GetLocation().c_str()))); |
| 1690 | if (location.Get() == NULL) { |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1691 | return NULL; |
| 1692 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1693 | Handle<mirror::ObjectArray<mirror::String>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1694 | strings(hs.NewHandle(AllocStringArray(self, dex_file.NumStringIds()))); |
| 1695 | if (strings.Get() == NULL) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1696 | return NULL; |
| 1697 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1698 | Handle<mirror::ObjectArray<mirror::Class>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1699 | types(hs.NewHandle(AllocClassArray(self, dex_file.NumTypeIds()))); |
| 1700 | if (types.Get() == NULL) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1701 | return NULL; |
| 1702 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1703 | Handle<mirror::ObjectArray<mirror::ArtMethod>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1704 | methods(hs.NewHandle(AllocArtMethodArray(self, dex_file.NumMethodIds()))); |
| 1705 | if (methods.Get() == NULL) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1706 | return NULL; |
| 1707 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1708 | Handle<mirror::ObjectArray<mirror::ArtField>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1709 | fields(hs.NewHandle(AllocArtFieldArray(self, dex_file.NumFieldIds()))); |
| 1710 | if (fields.Get() == NULL) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1711 | return NULL; |
| 1712 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1713 | dex_cache->Init(&dex_file, location.Get(), strings.Get(), types.Get(), methods.Get(), |
| 1714 | fields.Get()); |
| 1715 | return dex_cache.Get(); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1718 | mirror::Class* ClassLinker::AllocClass(Thread* self, mirror::Class* java_lang_Class, |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1719 | uint32_t class_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1720 | DCHECK_GE(class_size, sizeof(mirror::Class)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1721 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1722 | mirror::Class::InitializeClassVisitor visitor(class_size); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1723 | mirror::Object* k = kMovingClasses ? |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1724 | heap->AllocObject<true>(self, java_lang_Class, class_size, visitor) : |
| 1725 | heap->AllocNonMovableObject<true>(self, java_lang_Class, class_size, visitor); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1726 | if (UNLIKELY(k == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1727 | CHECK(self->IsExceptionPending()); // OOME. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1728 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1729 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1730 | return k->AsClass(); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1731 | } |
| 1732 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1733 | mirror::Class* ClassLinker::AllocClass(Thread* self, uint32_t class_size) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1734 | return AllocClass(self, GetClassRoot(kJavaLangClass), class_size); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1735 | } |
| 1736 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1737 | mirror::ArtField* ClassLinker::AllocArtField(Thread* self) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1738 | return down_cast<mirror::ArtField*>( |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1739 | GetClassRoot(kJavaLangReflectArtField)->AllocNonMovableObject(self)); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1740 | } |
| 1741 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1742 | mirror::ArtMethod* ClassLinker::AllocArtMethod(Thread* self) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1743 | return down_cast<mirror::ArtMethod*>( |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 1744 | GetClassRoot(kJavaLangReflectArtMethod)->AllocNonMovableObject(self)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 1745 | } |
| 1746 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1747 | mirror::ObjectArray<mirror::StackTraceElement>* ClassLinker::AllocStackTraceElementArray( |
| 1748 | Thread* self, size_t length) { |
| 1749 | return mirror::ObjectArray<mirror::StackTraceElement>::Alloc( |
| 1750 | self, GetClassRoot(kJavaLangStackTraceElementArrayClass), length); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1751 | } |
| 1752 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1753 | mirror::Class* ClassLinker::EnsureResolved(Thread* self, const char* descriptor, |
| 1754 | mirror::Class* klass) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1755 | DCHECK(klass != NULL); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1756 | |
| 1757 | // For temporary classes we must wait for them to be retired. |
| 1758 | if (init_done_ && klass->IsTemp()) { |
| 1759 | CHECK(!klass->IsResolved()); |
| 1760 | if (klass->IsErroneous()) { |
| 1761 | ThrowEarlierClassFailure(klass); |
| 1762 | return nullptr; |
| 1763 | } |
| 1764 | StackHandleScope<1> hs(self); |
| 1765 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 1766 | ObjectLock<mirror::Class> lock(self, h_class); |
| 1767 | // Loop and wait for the resolving thread to retire this class. |
| 1768 | while (!h_class->IsRetired() && !h_class->IsErroneous()) { |
| 1769 | lock.WaitIgnoringInterrupts(); |
| 1770 | } |
| 1771 | if (h_class->IsErroneous()) { |
| 1772 | ThrowEarlierClassFailure(h_class.Get()); |
| 1773 | return nullptr; |
| 1774 | } |
| 1775 | CHECK(h_class->IsRetired()); |
| 1776 | // Get the updated class from class table. |
| 1777 | klass = LookupClass(descriptor, h_class.Get()->GetClassLoader()); |
| 1778 | } |
| 1779 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1780 | // Wait for the class if it has not already been linked. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 1781 | if (!klass->IsResolved() && !klass->IsErroneous()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1782 | StackHandleScope<1> hs(self); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 1783 | HandleWrapper<mirror::Class> h_class(hs.NewHandleWrapper(&klass)); |
| 1784 | ObjectLock<mirror::Class> lock(self, h_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1785 | // Check for circular dependencies between classes. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1786 | if (!h_class->IsResolved() && h_class->GetClinitThreadId() == self->GetTid()) { |
| 1787 | ThrowClassCircularityError(h_class.Get()); |
| 1788 | h_class->SetStatus(mirror::Class::kStatusError, self); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 1789 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1790 | } |
| 1791 | // Wait for the pending initialization to complete. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1792 | while (!h_class->IsResolved() && !h_class->IsErroneous()) { |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 1793 | lock.WaitIgnoringInterrupts(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1794 | } |
| 1795 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1796 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1797 | if (klass->IsErroneous()) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 1798 | ThrowEarlierClassFailure(klass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 1799 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1800 | } |
| 1801 | // Return the loaded class. No exceptions should be pending. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1802 | CHECK(klass->IsResolved()) << PrettyClass(klass); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1803 | self->AssertNoPendingException(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1804 | return klass; |
| 1805 | } |
| 1806 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1807 | mirror::Class* ClassLinker::FindClass(Thread* self, const char* descriptor, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 1808 | Handle<mirror::ClassLoader> class_loader) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 1809 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1810 | DCHECK(self != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1811 | self->AssertNoPendingException(); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 1812 | if (descriptor[1] == '\0') { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1813 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 1814 | // for primitive classes that aren't backed by dex files. |
| 1815 | return FindPrimitiveClass(descriptor[0]); |
| 1816 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1817 | // Find the class in the loaded classes table. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1818 | mirror::Class* klass = LookupClass(descriptor, class_loader.Get()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1819 | if (klass != NULL) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1820 | return EnsureResolved(self, descriptor, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1821 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1822 | // Class is not yet loaded. |
| 1823 | if (descriptor[0] == '[') { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1824 | return CreateArrayClass(self, descriptor, class_loader); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1825 | } else if (class_loader.Get() == nullptr) { |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1826 | DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, boot_class_path_); |
| 1827 | if (pair.second != NULL) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1828 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 1829 | return DefineClass(descriptor, NullHandle<mirror::ClassLoader>(), *pair.first, *pair.second); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1830 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1831 | } else if (Runtime::Current()->UseCompileTimeClassPath()) { |
Ian Rogers | be7149f | 2013-08-20 09:29:39 -0700 | [diff] [blame] | 1832 | // First try the boot class path, we check the descriptor first to avoid an unnecessary |
| 1833 | // throw of a NoClassDefFoundError. |
| 1834 | if (IsInBootClassPath(descriptor)) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1835 | mirror::Class* system_class = FindSystemClass(self, descriptor); |
Ian Rogers | be7149f | 2013-08-20 09:29:39 -0700 | [diff] [blame] | 1836 | CHECK(system_class != NULL); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1837 | return system_class; |
| 1838 | } |
Ian Rogers | be7149f | 2013-08-20 09:29:39 -0700 | [diff] [blame] | 1839 | // Next try the compile time class path. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1840 | const std::vector<const DexFile*>* class_path; |
| 1841 | { |
Ian Rogers | be7149f | 2013-08-20 09:29:39 -0700 | [diff] [blame] | 1842 | ScopedObjectAccessUnchecked soa(self); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1843 | ScopedLocalRef<jobject> jclass_loader(soa.Env(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1844 | soa.AddLocalReference<jobject>(class_loader.Get())); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1845 | class_path = &Runtime::Current()->GetCompileTimeClassPath(jclass_loader.get()); |
| 1846 | } |
| 1847 | |
| 1848 | DexFile::ClassPathEntry pair = DexFile::FindInClassPath(descriptor, *class_path); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1849 | if (pair.second != NULL) { |
| 1850 | return DefineClass(descriptor, class_loader, *pair.first, *pair.second); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1851 | } |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1852 | |
| 1853 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1854 | ScopedObjectAccessUnchecked soa(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1855 | ScopedLocalRef<jobject> class_loader_object(soa.Env(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1856 | soa.AddLocalReference<jobject>(class_loader.Get())); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 1857 | std::string class_name_string(DescriptorToDot(descriptor)); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1858 | ScopedLocalRef<jobject> result(soa.Env(), NULL); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 1859 | { |
| 1860 | ScopedThreadStateChange tsc(self, kNative); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1861 | ScopedLocalRef<jobject> class_name_object(soa.Env(), |
| 1862 | soa.Env()->NewStringUTF(class_name_string.c_str())); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 1863 | if (class_name_object.get() == NULL) { |
| 1864 | return NULL; |
| 1865 | } |
| 1866 | CHECK(class_loader_object.get() != NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1867 | result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(), |
| 1868 | WellKnownClasses::java_lang_ClassLoader_loadClass, |
| 1869 | class_name_object.get())); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1870 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1871 | if (self->IsExceptionPending()) { |
Elliott Hughes | 748382f | 2012-01-26 18:07:38 -0800 | [diff] [blame] | 1872 | // If the ClassLoader threw, pass that exception up. |
| 1873 | return NULL; |
Ian Rogers | 761bfa8 | 2012-01-11 10:14:05 -0800 | [diff] [blame] | 1874 | } else if (result.get() == NULL) { |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 1875 | // broken loader - throw NPE to be compatible with Dalvik |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 1876 | ThrowNullPointerException(NULL, StringPrintf("ClassLoader.loadClass returned null for %s", |
| 1877 | class_name_string.c_str()).c_str()); |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 1878 | return NULL; |
Ian Rogers | 761bfa8 | 2012-01-11 10:14:05 -0800 | [diff] [blame] | 1879 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1880 | // success, return mirror::Class* |
| 1881 | return soa.Decode<mirror::Class*>(result.get()); |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 1882 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1883 | } |
| 1884 | |
Elliott Hughes | 82914b6 | 2012-04-09 15:56:29 -0700 | [diff] [blame] | 1885 | ThrowNoClassDefFoundError("Class %s not found", PrintableString(descriptor).c_str()); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 1886 | return NULL; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1887 | } |
| 1888 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1889 | mirror::Class* ClassLinker::DefineClass(const char* descriptor, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 1890 | Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1891 | const DexFile& dex_file, |
| 1892 | const DexFile::ClassDef& dex_class_def) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 1893 | Thread* self = Thread::Current(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1894 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1895 | auto klass = hs.NewHandle<mirror::Class>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1896 | bool should_allocate = false; |
| 1897 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1898 | // Load the class from the dex file. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1899 | if (UNLIKELY(!init_done_)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1900 | // finish up init of hand crafted class_roots_ |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1901 | if (strcmp(descriptor, "Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1902 | klass.Assign(GetClassRoot(kJavaLangObject)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1903 | } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1904 | klass.Assign(GetClassRoot(kJavaLangClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1905 | } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1906 | klass.Assign(GetClassRoot(kJavaLangString)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 1907 | } else if (strcmp(descriptor, "Ljava/lang/ref/Reference;") == 0) { |
| 1908 | klass.Assign(GetClassRoot(kJavaLangRefReference)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1909 | } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1910 | klass.Assign(GetClassRoot(kJavaLangDexCache)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1911 | } else if (strcmp(descriptor, "Ljava/lang/reflect/ArtField;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1912 | klass.Assign(GetClassRoot(kJavaLangReflectArtField)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1913 | } else if (strcmp(descriptor, "Ljava/lang/reflect/ArtMethod;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1914 | klass.Assign(GetClassRoot(kJavaLangReflectArtMethod)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1915 | } else { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1916 | should_allocate = true; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1917 | } |
| 1918 | } else { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1919 | should_allocate = true; |
| 1920 | } |
| 1921 | |
| 1922 | if (should_allocate) { |
| 1923 | // Allocate a class with the status of not ready. |
| 1924 | // Interface object should get the right size here. Regular class will |
| 1925 | // figure out the right size later and be replaced with one of the right |
| 1926 | // size when the class becomes resolved. |
| 1927 | klass.Assign(AllocClass(self, SizeOfClassWithoutEmbeddedTables(dex_file, dex_class_def))); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1928 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 1929 | if (UNLIKELY(klass.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1930 | CHECK(self->IsExceptionPending()); // Expect an OOME. |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 1931 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1932 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1933 | klass->SetDexCache(FindDexCache(dex_file)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1934 | LoadClass(dex_file, dex_class_def, klass, class_loader.Get()); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 1935 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 1936 | if (self->IsExceptionPending()) { |
| 1937 | // An exception occured during load, set status to erroneous while holding klass' lock in case |
| 1938 | // notification is necessary. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 1939 | if (!klass->IsErroneous()) { |
| 1940 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 1941 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 1942 | return nullptr; |
| 1943 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1944 | klass->SetClinitThreadId(self->GetTid()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1945 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1946 | // Add the newly loaded class to the loaded classes table. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1947 | mirror::Class* existing = InsertClass(descriptor, klass.Get(), Hash(descriptor)); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 1948 | if (existing != nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1949 | // We failed to insert because we raced with another thread. Calling EnsureResolved may cause |
| 1950 | // this thread to block. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1951 | return EnsureResolved(self, descriptor, existing); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1952 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1953 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1954 | // Finish loading (if necessary) by finding parents |
| 1955 | CHECK(!klass->IsLoaded()); |
| 1956 | if (!LoadSuperAndInterfaces(klass, dex_file)) { |
| 1957 | // Loading failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 1958 | if (!klass->IsErroneous()) { |
| 1959 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 1960 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 1961 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1962 | } |
| 1963 | CHECK(klass->IsLoaded()); |
| 1964 | // Link the class (if necessary) |
| 1965 | CHECK(!klass->IsResolved()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1966 | // TODO: Use fast jobjects? |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1967 | auto interfaces = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1968 | |
| 1969 | mirror::Class* new_class = nullptr; |
| 1970 | if (!LinkClass(self, descriptor, klass, interfaces, &new_class)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1971 | // Linking failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 1972 | if (!klass->IsErroneous()) { |
| 1973 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 1974 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 1975 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1976 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1977 | CHECK(new_class != nullptr) << descriptor; |
| 1978 | CHECK(new_class->IsResolved()) << descriptor; |
| 1979 | |
| 1980 | Handle<mirror::Class> new_class_h(hs.NewHandle(new_class)); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1981 | |
| 1982 | /* |
| 1983 | * We send CLASS_PREPARE events to the debugger from here. The |
| 1984 | * definition of "preparation" is creating the static fields for a |
| 1985 | * class and initializing them to the standard default values, but not |
| 1986 | * executing any code (that comes later, during "initialization"). |
| 1987 | * |
| 1988 | * We did the static preparation in LinkClass. |
| 1989 | * |
| 1990 | * The class has been prepared and resolved but possibly not yet verified |
| 1991 | * at this point. |
| 1992 | */ |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1993 | Dbg::PostClassPrepare(new_class_h.Get()); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 1994 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1995 | return new_class_h.Get(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1996 | } |
| 1997 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1998 | uint32_t ClassLinker::SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, |
| 1999 | const DexFile::ClassDef& dex_class_def) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2000 | const byte* class_data = dex_file.GetClassData(dex_class_def); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2001 | size_t num_ref = 0; |
| 2002 | size_t num_32 = 0; |
| 2003 | size_t num_64 = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2004 | if (class_data != NULL) { |
| 2005 | for (ClassDataItemIterator it(dex_file, class_data); it.HasNextStaticField(); it.Next()) { |
| 2006 | const DexFile::FieldId& field_id = dex_file.GetFieldId(it.GetMemberIndex()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2007 | const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2008 | char c = descriptor[0]; |
| 2009 | if (c == 'L' || c == '[') { |
| 2010 | num_ref++; |
| 2011 | } else if (c == 'J' || c == 'D') { |
| 2012 | num_64++; |
| 2013 | } else { |
| 2014 | num_32++; |
| 2015 | } |
| 2016 | } |
| 2017 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2018 | return mirror::Class::ComputeClassSize(false, 0, num_32, num_64, num_ref); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2019 | } |
| 2020 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2021 | OatFile::OatClass ClassLinker::GetOatClass(const DexFile& dex_file, uint16_t class_def_idx) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2022 | DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2023 | const OatFile* oat_file = FindOpenedOatFileForDexFile(dex_file); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2024 | CHECK(oat_file != NULL) << dex_file.GetLocation(); |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 2025 | uint dex_location_checksum = dex_file.GetLocationChecksum(); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 2026 | 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] | 2027 | &dex_location_checksum); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2028 | CHECK(oat_dex_file != NULL) << dex_file.GetLocation(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2029 | return oat_dex_file->GetOatClass(class_def_idx); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2030 | } |
| 2031 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2032 | static uint32_t GetOatMethodIndexFromMethodIndex(const DexFile& dex_file, uint16_t class_def_idx, |
| 2033 | uint32_t method_idx) { |
| 2034 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_idx); |
| 2035 | const byte* class_data = dex_file.GetClassData(class_def); |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2036 | CHECK(class_data != NULL); |
| 2037 | ClassDataItemIterator it(dex_file, class_data); |
| 2038 | // Skip fields |
| 2039 | while (it.HasNextStaticField()) { |
| 2040 | it.Next(); |
| 2041 | } |
| 2042 | while (it.HasNextInstanceField()) { |
| 2043 | it.Next(); |
| 2044 | } |
| 2045 | // Process methods |
| 2046 | size_t class_def_method_index = 0; |
| 2047 | while (it.HasNextDirectMethod()) { |
| 2048 | if (it.GetMemberIndex() == method_idx) { |
| 2049 | return class_def_method_index; |
| 2050 | } |
| 2051 | class_def_method_index++; |
| 2052 | it.Next(); |
| 2053 | } |
| 2054 | while (it.HasNextVirtualMethod()) { |
| 2055 | if (it.GetMemberIndex() == method_idx) { |
| 2056 | return class_def_method_index; |
| 2057 | } |
| 2058 | class_def_method_index++; |
| 2059 | it.Next(); |
| 2060 | } |
| 2061 | DCHECK(!it.HasNext()); |
| 2062 | LOG(FATAL) << "Failed to find method index " << method_idx << " in " << dex_file.GetLocation(); |
| 2063 | return 0; |
| 2064 | } |
| 2065 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2066 | const OatFile::OatMethod ClassLinker::GetOatMethodFor(mirror::ArtMethod* method) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2067 | // 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] | 2068 | // method for direct methods (or virtual methods made direct). |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2069 | mirror::Class* declaring_class = method->GetDeclaringClass(); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2070 | size_t oat_method_index; |
| 2071 | if (method->IsStatic() || method->IsDirect()) { |
| 2072 | // Simple case where the oat method index was stashed at load time. |
| 2073 | oat_method_index = method->GetMethodIndex(); |
| 2074 | } else { |
| 2075 | // We're invoking a virtual method directly (thanks to sharpening), compute the oat_method_index |
| 2076 | // by search for its position in the declared virtual methods. |
| 2077 | oat_method_index = declaring_class->NumDirectMethods(); |
| 2078 | size_t end = declaring_class->NumVirtualMethods(); |
| 2079 | bool found = false; |
| 2080 | for (size_t i = 0; i < end; i++) { |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2081 | if (declaring_class->GetVirtualMethod(i) == method) { |
| 2082 | found = true; |
| 2083 | break; |
| 2084 | } |
Ian Rogers | f320b63 | 2012-03-13 18:47:47 -0700 | [diff] [blame] | 2085 | oat_method_index++; |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2086 | } |
| 2087 | CHECK(found) << "Didn't find oat method index for virtual method: " << PrettyMethod(method); |
| 2088 | } |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2089 | DCHECK_EQ(oat_method_index, |
| 2090 | GetOatMethodIndexFromMethodIndex(*declaring_class->GetDexCache()->GetDexFile(), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2091 | method->GetDeclaringClass()->GetDexClassDefIndex(), |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2092 | method->GetDexMethodIndex())); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2093 | const OatFile::OatClass oat_class = GetOatClass(*declaring_class->GetDexCache()->GetDexFile(), |
| 2094 | declaring_class->GetDexClassDefIndex()); |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2095 | |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2096 | return oat_class.GetOatMethod(oat_method_index); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2097 | } |
| 2098 | |
| 2099 | // Special case to get oat code without overwriting a trampoline. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2100 | const void* ClassLinker::GetQuickOatCodeFor(mirror::ArtMethod* method) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2101 | CHECK(!method->IsAbstract()) << PrettyMethod(method); |
Jeff Hao | 8df6cea | 2013-07-29 13:54:48 -0700 | [diff] [blame] | 2102 | if (method->IsProxyMethod()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2103 | return GetQuickProxyInvokeHandler(); |
Jeff Hao | 8df6cea | 2013-07-29 13:54:48 -0700 | [diff] [blame] | 2104 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2105 | const void* result = GetOatMethodFor(method).GetQuickCode(); |
| 2106 | if (result == nullptr) { |
Ian Rogers | 1a57066 | 2014-03-12 01:02:21 -0700 | [diff] [blame] | 2107 | if (method->IsNative()) { |
| 2108 | // No code and native? Use generic trampoline. |
| 2109 | result = GetQuickGenericJniTrampoline(); |
| 2110 | } else if (method->IsPortableCompiled()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2111 | // No code? Do we expect portable code? |
| 2112 | result = GetQuickToPortableBridge(); |
| 2113 | } else { |
| 2114 | // No code? You must mean to go into the interpreter. |
| 2115 | result = GetQuickToInterpreterBridge(); |
| 2116 | } |
Ian Rogers | f3e9855 | 2013-03-20 15:49:49 -0700 | [diff] [blame] | 2117 | } |
| 2118 | return result; |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2119 | } |
| 2120 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2121 | const void* ClassLinker::GetPortableOatCodeFor(mirror::ArtMethod* method, |
| 2122 | bool* have_portable_code) { |
| 2123 | CHECK(!method->IsAbstract()) << PrettyMethod(method); |
| 2124 | *have_portable_code = false; |
| 2125 | if (method->IsProxyMethod()) { |
| 2126 | return GetPortableProxyInvokeHandler(); |
| 2127 | } |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 2128 | const OatFile::OatMethod oat_method = GetOatMethodFor(method); |
| 2129 | const void* result = oat_method.GetPortableCode(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2130 | if (result == nullptr) { |
Sebastien Hertz | 7ec2f1c | 2014-03-27 20:06:47 +0100 | [diff] [blame] | 2131 | if (oat_method.GetQuickCode() == nullptr) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2132 | // No code? You must mean to go into the interpreter. |
| 2133 | result = GetPortableToInterpreterBridge(); |
| 2134 | } else { |
| 2135 | // No code? But there's quick code, so use a bridge. |
| 2136 | result = GetPortableToQuickBridge(); |
| 2137 | } |
| 2138 | } else { |
| 2139 | *have_portable_code = true; |
| 2140 | } |
| 2141 | return result; |
| 2142 | } |
| 2143 | |
| 2144 | const void* ClassLinker::GetQuickOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, |
| 2145 | uint32_t method_idx) { |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2146 | const OatFile::OatClass oat_class = GetOatClass(dex_file, class_def_idx); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2147 | uint32_t oat_method_idx = GetOatMethodIndexFromMethodIndex(dex_file, class_def_idx, method_idx); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2148 | return oat_class.GetOatMethod(oat_method_idx).GetQuickCode(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | const void* ClassLinker::GetPortableOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, |
| 2152 | uint32_t method_idx) { |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2153 | const OatFile::OatClass oat_class = GetOatClass(dex_file, class_def_idx); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2154 | uint32_t oat_method_idx = GetOatMethodIndexFromMethodIndex(dex_file, class_def_idx, method_idx); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2155 | return oat_class.GetOatMethod(oat_method_idx).GetPortableCode(); |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2156 | } |
| 2157 | |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2158 | // Returns true if the method must run with interpreter, false otherwise. |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 2159 | static bool NeedsInterpreter( |
| 2160 | mirror::ArtMethod* method, const void* quick_code, const void* portable_code) |
| 2161 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2162 | if ((quick_code == nullptr) && (portable_code == nullptr)) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2163 | // No code: need interpreter. |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2164 | // May return true for native code, in the case of generic JNI |
| 2165 | // DCHECK(!method->IsNative()); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2166 | return true; |
| 2167 | } |
Dragos Sbirlea | 90af14d | 2013-08-15 17:50:16 -0700 | [diff] [blame] | 2168 | #ifdef ART_SEA_IR_MODE |
| 2169 | ScopedObjectAccess soa(Thread::Current()); |
| 2170 | if (std::string::npos != PrettyMethod(method).find("fibonacci")) { |
| 2171 | LOG(INFO) << "Found " << PrettyMethod(method); |
| 2172 | return false; |
| 2173 | } |
| 2174 | #endif |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2175 | // If interpreter mode is enabled, every method (except native and proxy) must |
| 2176 | // be run with interpreter. |
| 2177 | return Runtime::Current()->GetInstrumentation()->InterpretOnly() && |
| 2178 | !method->IsNative() && !method->IsProxyMethod(); |
| 2179 | } |
| 2180 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2181 | void ClassLinker::FixupStaticTrampolines(mirror::Class* klass) { |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 2182 | DCHECK(klass->IsInitialized()) << PrettyDescriptor(klass); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2183 | if (klass->NumDirectMethods() == 0) { |
| 2184 | return; // No direct methods => no static methods. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2185 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2186 | Runtime* runtime = Runtime::Current(); |
| 2187 | if (!runtime->IsStarted() || runtime->UseCompileTimeClassPath()) { |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2188 | return; // OAT file unavailable. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2189 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 2190 | const DexFile& dex_file = klass->GetDexFile(); |
| 2191 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2192 | CHECK(dex_class_def != nullptr); |
| 2193 | const byte* class_data = dex_file.GetClassData(*dex_class_def); |
| 2194 | // There should always be class data if there were direct methods. |
| 2195 | CHECK(class_data != nullptr) << PrettyDescriptor(klass); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2196 | const OatFile::OatClass oat_class = GetOatClass(dex_file, klass->GetDexClassDefIndex()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2197 | ClassDataItemIterator it(dex_file, class_data); |
| 2198 | // Skip fields |
| 2199 | while (it.HasNextStaticField()) { |
| 2200 | it.Next(); |
| 2201 | } |
| 2202 | while (it.HasNextInstanceField()) { |
| 2203 | it.Next(); |
| 2204 | } |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2205 | // Link the code of methods skipped by LinkCode. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2206 | for (size_t method_index = 0; it.HasNextDirectMethod(); ++method_index, it.Next()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2207 | mirror::ArtMethod* method = klass->GetDirectMethod(method_index); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2208 | if (!method->IsStatic()) { |
| 2209 | // Only update static methods. |
| 2210 | continue; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2211 | } |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2212 | const void* portable_code = oat_class.GetOatMethod(method_index).GetPortableCode(); |
| 2213 | const void* quick_code = oat_class.GetOatMethod(method_index).GetQuickCode(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2214 | const bool enter_interpreter = NeedsInterpreter(method, quick_code, portable_code); |
| 2215 | bool have_portable_code = false; |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2216 | if (enter_interpreter) { |
| 2217 | // Use interpreter entry point. |
Ian Rogers | 1a57066 | 2014-03-12 01:02:21 -0700 | [diff] [blame] | 2218 | // Check whether the method is native, in which case it's generic JNI. |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2219 | if (quick_code == nullptr && portable_code == nullptr && method->IsNative()) { |
| 2220 | quick_code = GetQuickGenericJniTrampoline(); |
Ian Rogers | 1a57066 | 2014-03-12 01:02:21 -0700 | [diff] [blame] | 2221 | portable_code = GetPortableToQuickBridge(); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2222 | } else { |
Ian Rogers | 1a57066 | 2014-03-12 01:02:21 -0700 | [diff] [blame] | 2223 | portable_code = GetPortableToInterpreterBridge(); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2224 | quick_code = GetQuickToInterpreterBridge(); |
| 2225 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2226 | } else { |
| 2227 | if (portable_code == nullptr) { |
| 2228 | portable_code = GetPortableToQuickBridge(); |
| 2229 | } else { |
| 2230 | have_portable_code = true; |
| 2231 | } |
| 2232 | if (quick_code == nullptr) { |
| 2233 | quick_code = GetQuickToPortableBridge(); |
| 2234 | } |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2235 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2236 | runtime->GetInstrumentation()->UpdateMethodsCode(method, quick_code, portable_code, |
| 2237 | have_portable_code); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2238 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2239 | // Ignore virtual methods on the iterator. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2240 | } |
| 2241 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 2242 | void ClassLinker::LinkCode(Handle<mirror::ArtMethod> method, const OatFile::OatClass* oat_class, |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 2243 | const DexFile& dex_file, uint32_t dex_method_index, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 2244 | uint32_t method_index) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2245 | // Method shouldn't have already been linked. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2246 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr); |
| 2247 | DCHECK(method->GetEntryPointFromPortableCompiledCode() == nullptr); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2248 | // Every kind of method should at least get an invoke stub from the oat_method. |
| 2249 | // non-abstract methods also get their code pointers. |
| 2250 | const OatFile::OatMethod oat_method = oat_class->GetOatMethod(method_index); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2251 | oat_method.LinkMethod(method.Get()); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2252 | |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 2253 | // Install entry point from interpreter. |
Jeff Hao | 790ad90 | 2013-05-22 15:02:08 -0700 | [diff] [blame] | 2254 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2255 | bool enter_interpreter = NeedsInterpreter(method.Get(), |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2256 | method->GetEntryPointFromQuickCompiledCode(), |
| 2257 | method->GetEntryPointFromPortableCompiledCode()); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2258 | if (enter_interpreter && !method->IsNative()) { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2259 | method->SetEntryPointFromInterpreter(interpreter::artInterpreterToInterpreterBridge); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 2260 | } else { |
Dragos Sbirlea | 08bf196 | 2013-08-12 08:53:04 -0700 | [diff] [blame] | 2261 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 2262 | } |
| 2263 | |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2264 | if (method->IsAbstract()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2265 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 2266 | method->SetEntryPointFromPortableCompiledCode(GetPortableToInterpreterBridge()); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2267 | return; |
| 2268 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2269 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2270 | bool have_portable_code = false; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2271 | if (method->IsStatic() && !method->IsConstructor()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2272 | // For static methods excluding the class initializer, install the trampoline. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2273 | // It will be replaced by the proper entry point by ClassLinker::FixupStaticTrampolines |
| 2274 | // after initializing class (see ClassLinker::InitializeClass method). |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 2275 | method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionTrampoline()); |
| 2276 | method->SetEntryPointFromPortableCompiledCode(GetPortableResolutionTrampoline()); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2277 | } else if (enter_interpreter) { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2278 | if (!method->IsNative()) { |
| 2279 | // Set entry point from compiled code if there's no code or in interpreter only mode. |
| 2280 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 2281 | method->SetEntryPointFromPortableCompiledCode(GetPortableToInterpreterBridge()); |
| 2282 | } else { |
| 2283 | method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniTrampoline()); |
| 2284 | method->SetEntryPointFromPortableCompiledCode(GetPortableToQuickBridge()); |
| 2285 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2286 | } else if (method->GetEntryPointFromPortableCompiledCode() != nullptr) { |
| 2287 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr); |
| 2288 | have_portable_code = true; |
| 2289 | method->SetEntryPointFromQuickCompiledCode(GetQuickToPortableBridge()); |
| 2290 | } else { |
| 2291 | DCHECK(method->GetEntryPointFromQuickCompiledCode() != nullptr); |
| 2292 | method->SetEntryPointFromPortableCompiledCode(GetPortableToQuickBridge()); |
Ian Rogers | 0d6de04 | 2012-02-29 08:50:26 -0800 | [diff] [blame] | 2293 | } |
jeffhao | 26c0a1a | 2012-01-17 16:28:33 -0800 | [diff] [blame] | 2294 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2295 | if (method->IsNative()) { |
| 2296 | // Unregistering restores the dlsym lookup stub. |
| 2297 | method->UnregisterNative(Thread::Current()); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2298 | |
| 2299 | if (enter_interpreter) { |
| 2300 | // We have a native method here without code. Then it should have either the GenericJni |
| 2301 | // trampoline as entrypoint (non-static), or the Resolution trampoline (static). |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 2302 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == GetQuickResolutionTrampoline() |
Andreas Gampe | 9a6a99a | 2014-03-14 07:52:20 -0700 | [diff] [blame] | 2303 | || method->GetEntryPointFromQuickCompiledCode() == GetQuickGenericJniTrampoline()); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2304 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2305 | } |
Ian Rogers | f3e9855 | 2013-03-20 15:49:49 -0700 | [diff] [blame] | 2306 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2307 | // Allow instrumentation its chance to hijack code. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2308 | runtime->GetInstrumentation()->UpdateMethodsCode(method.Get(), |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2309 | method->GetEntryPointFromQuickCompiledCode(), |
| 2310 | method->GetEntryPointFromPortableCompiledCode(), |
| 2311 | have_portable_code); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2312 | } |
| 2313 | |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 2314 | void ClassLinker::LoadClass(const DexFile& dex_file, |
| 2315 | const DexFile::ClassDef& dex_class_def, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 2316 | Handle<mirror::Class> klass, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2317 | mirror::ClassLoader* class_loader) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2318 | CHECK(klass.Get() != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2319 | CHECK(klass->GetDexCache() != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2320 | CHECK_EQ(mirror::Class::kStatusNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 2321 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2322 | CHECK(descriptor != NULL); |
| 2323 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2324 | klass->SetClass(GetClassRoot(kJavaLangClass)); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 2325 | if (kUseBakerOrBrooksReadBarrier) { |
| 2326 | klass->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 2327 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2328 | uint32_t access_flags = dex_class_def.access_flags_; |
Elliott Hughes | 582a7d1 | 2011-10-10 18:38:42 -0700 | [diff] [blame] | 2329 | // Make sure that none of our runtime-only flags are set. |
Brian Carlstrom | 8e3fb14 | 2013-10-09 21:00:27 -0700 | [diff] [blame] | 2330 | CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2331 | klass->SetAccessFlags(access_flags); |
| 2332 | klass->SetClassLoader(class_loader); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2333 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2334 | klass->SetStatus(mirror::Class::kStatusIdx, NULL); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2335 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2336 | klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2337 | klass->SetDexTypeIndex(dex_class_def.class_idx_); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2338 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2339 | const byte* class_data = dex_file.GetClassData(dex_class_def); |
| 2340 | if (class_data == NULL) { |
| 2341 | return; // no fields or methods - for example a marker interface |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2342 | } |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2343 | |
| 2344 | if (Runtime::Current()->IsStarted() && !Runtime::Current()->UseCompileTimeClassPath()) { |
| 2345 | const OatFile::OatClass oat_class = GetOatClass(dex_file, klass->GetDexClassDefIndex()); |
| 2346 | LoadClassMembers(dex_file, class_data, klass, class_loader, &oat_class); |
| 2347 | } else { |
| 2348 | LoadClassMembers(dex_file, class_data, klass, class_loader, nullptr); |
| 2349 | } |
| 2350 | } |
| 2351 | |
| 2352 | void ClassLinker::LoadClassMembers(const DexFile& dex_file, |
| 2353 | const byte* class_data, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 2354 | Handle<mirror::Class> klass, |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2355 | mirror::ClassLoader* class_loader, |
| 2356 | const OatFile::OatClass* oat_class) { |
| 2357 | // Load fields. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2358 | ClassDataItemIterator it(dex_file, class_data); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2359 | Thread* self = Thread::Current(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2360 | if (it.NumStaticFields() != 0) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2361 | mirror::ObjectArray<mirror::ArtField>* statics = AllocArtFieldArray(self, it.NumStaticFields()); |
| 2362 | if (UNLIKELY(statics == NULL)) { |
| 2363 | CHECK(self->IsExceptionPending()); // OOME. |
| 2364 | return; |
| 2365 | } |
| 2366 | klass->SetSFields(statics); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2367 | } |
| 2368 | if (it.NumInstanceFields() != 0) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2369 | mirror::ObjectArray<mirror::ArtField>* fields = |
| 2370 | AllocArtFieldArray(self, it.NumInstanceFields()); |
| 2371 | if (UNLIKELY(fields == NULL)) { |
| 2372 | CHECK(self->IsExceptionPending()); // OOME. |
| 2373 | return; |
| 2374 | } |
| 2375 | klass->SetIFields(fields); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2376 | } |
| 2377 | for (size_t i = 0; it.HasNextStaticField(); i++, it.Next()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2378 | StackHandleScope<1> hs(self); |
| 2379 | Handle<mirror::ArtField> sfield(hs.NewHandle(AllocArtField(self))); |
| 2380 | if (UNLIKELY(sfield.Get() == NULL)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2381 | CHECK(self->IsExceptionPending()); // OOME. |
| 2382 | return; |
| 2383 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2384 | klass->SetStaticField(i, sfield.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2385 | LoadField(dex_file, it, klass, sfield); |
| 2386 | } |
| 2387 | for (size_t i = 0; it.HasNextInstanceField(); i++, it.Next()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2388 | StackHandleScope<1> hs(self); |
| 2389 | Handle<mirror::ArtField> ifield(hs.NewHandle(AllocArtField(self))); |
| 2390 | if (UNLIKELY(ifield.Get() == NULL)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2391 | CHECK(self->IsExceptionPending()); // OOME. |
| 2392 | return; |
| 2393 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2394 | klass->SetInstanceField(i, ifield.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2395 | LoadField(dex_file, it, klass, ifield); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2396 | } |
| 2397 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2398 | // Load methods. |
| 2399 | if (it.NumDirectMethods() != 0) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2400 | // TODO: append direct methods to class object |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2401 | mirror::ObjectArray<mirror::ArtMethod>* directs = |
| 2402 | AllocArtMethodArray(self, it.NumDirectMethods()); |
| 2403 | if (UNLIKELY(directs == NULL)) { |
| 2404 | CHECK(self->IsExceptionPending()); // OOME. |
| 2405 | return; |
| 2406 | } |
| 2407 | klass->SetDirectMethods(directs); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2408 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2409 | if (it.NumVirtualMethods() != 0) { |
| 2410 | // TODO: append direct methods to class object |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2411 | mirror::ObjectArray<mirror::ArtMethod>* virtuals = |
| 2412 | AllocArtMethodArray(self, it.NumVirtualMethods()); |
| 2413 | if (UNLIKELY(virtuals == NULL)) { |
| 2414 | CHECK(self->IsExceptionPending()); // OOME. |
| 2415 | return; |
| 2416 | } |
| 2417 | klass->SetVirtualMethods(virtuals); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2418 | } |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2419 | size_t class_def_method_index = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2420 | for (size_t i = 0; it.HasNextDirectMethod(); i++, it.Next()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2421 | StackHandleScope<1> hs(self); |
| 2422 | Handle<mirror::ArtMethod> method(hs.NewHandle(LoadMethod(self, dex_file, it, klass))); |
| 2423 | if (UNLIKELY(method.Get() == NULL)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2424 | CHECK(self->IsExceptionPending()); // OOME. |
| 2425 | return; |
| 2426 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2427 | klass->SetDirectMethod(i, method.Get()); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2428 | if (oat_class != nullptr) { |
| 2429 | LinkCode(method, oat_class, dex_file, it.GetMemberIndex(), class_def_method_index); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2430 | } |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2431 | method->SetMethodIndex(class_def_method_index); |
| 2432 | class_def_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2433 | } |
| 2434 | for (size_t i = 0; it.HasNextVirtualMethod(); i++, it.Next()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2435 | StackHandleScope<1> hs(self); |
| 2436 | Handle<mirror::ArtMethod> method(hs.NewHandle(LoadMethod(self, dex_file, it, klass))); |
| 2437 | if (UNLIKELY(method.Get() == NULL)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2438 | CHECK(self->IsExceptionPending()); // OOME. |
| 2439 | return; |
| 2440 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2441 | klass->SetVirtualMethod(i, method.Get()); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2442 | DCHECK_EQ(class_def_method_index, it.NumDirectMethods() + i); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2443 | if (oat_class != nullptr) { |
| 2444 | LinkCode(method, oat_class, dex_file, it.GetMemberIndex(), class_def_method_index); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2445 | } |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2446 | class_def_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2447 | } |
| 2448 | DCHECK(!it.HasNext()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2449 | } |
| 2450 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 2451 | void ClassLinker::LoadField(const DexFile& /*dex_file*/, const ClassDataItemIterator& it, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 2452 | Handle<mirror::Class> klass, Handle<mirror::ArtField> dst) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2453 | uint32_t field_idx = it.GetMemberIndex(); |
| 2454 | dst->SetDexFieldIndex(field_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2455 | dst->SetDeclaringClass(klass.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2456 | dst->SetAccessFlags(it.GetMemberAccessFlags()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2457 | } |
| 2458 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2459 | mirror::ArtMethod* ClassLinker::LoadMethod(Thread* self, const DexFile& dex_file, |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2460 | const ClassDataItemIterator& it, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 2461 | Handle<mirror::Class> klass) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2462 | uint32_t dex_method_idx = it.GetMemberIndex(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2463 | const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2464 | const char* method_name = dex_file.StringDataByIdx(method_id.name_idx_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2465 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2466 | mirror::ArtMethod* dst = AllocArtMethod(self); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2467 | if (UNLIKELY(dst == NULL)) { |
| 2468 | CHECK(self->IsExceptionPending()); // OOME. |
| 2469 | return NULL; |
| 2470 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2471 | DCHECK(dst->IsArtMethod()) << PrettyDescriptor(dst->GetClass()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2472 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2473 | const char* old_cause = self->StartAssertNoThreadSuspension("LoadMethod"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2474 | dst->SetDexMethodIndex(dex_method_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2475 | dst->SetDeclaringClass(klass.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2476 | dst->SetCodeItemOffset(it.GetMethodCodeItemOffset()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2477 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2478 | dst->SetDexCacheStrings(klass->GetDexCache()->GetStrings()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2479 | dst->SetDexCacheResolvedMethods(klass->GetDexCache()->GetResolvedMethods()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2480 | dst->SetDexCacheResolvedTypes(klass->GetDexCache()->GetResolvedTypes()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2481 | |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2482 | uint32_t access_flags = it.GetMemberAccessFlags(); |
| 2483 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2484 | if (UNLIKELY(strcmp("finalize", method_name) == 0)) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2485 | // Set finalizable flag on declaring class. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2486 | if (strcmp("V", dex_file.GetShorty(method_id.proto_idx_)) == 0) { |
| 2487 | // Void return type. |
| 2488 | if (klass->GetClassLoader() != NULL) { // All non-boot finalizer methods are flagged |
| 2489 | klass->SetFinalizable(); |
| 2490 | } else { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 2491 | std::string klass_descriptor = klass->GetDescriptor(); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2492 | // The Enum class declares a "final" finalize() method to prevent subclasses from |
| 2493 | // introducing a finalizer. We don't want to set the finalizable flag for Enum or its |
| 2494 | // subclasses, so we exclude it here. |
| 2495 | // We also want to avoid setting the flag on Object, where we know that finalize() is |
| 2496 | // empty. |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 2497 | if (klass_descriptor.compare("Ljava/lang/Object;") != 0 && |
| 2498 | klass_descriptor.compare("Ljava/lang/Enum;") != 0) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2499 | klass->SetFinalizable(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2500 | } |
| 2501 | } |
| 2502 | } |
| 2503 | } else if (method_name[0] == '<') { |
| 2504 | // Fix broken access flags for initializers. Bug 11157540. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2505 | bool is_init = (strcmp("<init>", method_name) == 0); |
| 2506 | bool is_clinit = !is_init && (strcmp("<clinit>", method_name) == 0); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2507 | if (UNLIKELY(!is_init && !is_clinit)) { |
| 2508 | LOG(WARNING) << "Unexpected '<' at start of method name " << method_name; |
| 2509 | } else { |
| 2510 | if (UNLIKELY((access_flags & kAccConstructor) == 0)) { |
| 2511 | LOG(WARNING) << method_name << " didn't have expected constructor access flag in class " |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2512 | << PrettyDescriptor(klass.Get()) << " in dex file " << dex_file.GetLocation(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2513 | access_flags |= kAccConstructor; |
| 2514 | } |
| 2515 | } |
| 2516 | } |
| 2517 | dst->SetAccessFlags(access_flags); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2518 | |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2519 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2520 | return dst; |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2521 | } |
| 2522 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2523 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2524 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2525 | StackHandleScope<1> hs(self); |
| 2526 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(AllocDexCache(self, dex_file))); |
| 2527 | 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] | 2528 | AppendToBootClassPath(dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2529 | } |
| 2530 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2531 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 2532 | Handle<mirror::DexCache> dex_cache) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2533 | CHECK(dex_cache.Get() != NULL) << dex_file.GetLocation(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2534 | boot_class_path_.push_back(&dex_file); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2535 | RegisterDexFile(dex_file, dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2536 | } |
| 2537 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2538 | bool ClassLinker::IsDexFileRegisteredLocked(const DexFile& dex_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2539 | dex_lock_.AssertSharedHeld(Thread::Current()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2540 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2541 | mirror::DexCache* dex_cache = GetDexCache(i); |
| 2542 | if (dex_cache->GetDexFile() == &dex_file) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2543 | return true; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2544 | } |
| 2545 | } |
| 2546 | return false; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2547 | } |
| 2548 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2549 | bool ClassLinker::IsDexFileRegistered(const DexFile& dex_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2550 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | 0691851 | 2011-10-16 23:39:12 -0700 | [diff] [blame] | 2551 | return IsDexFileRegisteredLocked(dex_file); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2552 | } |
| 2553 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2554 | void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 2555 | Handle<mirror::DexCache> dex_cache) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2556 | dex_lock_.AssertExclusiveHeld(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2557 | CHECK(dex_cache.Get() != NULL) << dex_file.GetLocation(); |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 2558 | CHECK(dex_cache->GetLocation()->Equals(dex_file.GetLocation())) |
| 2559 | << dex_cache->GetLocation()->ToModifiedUtf8() << " " << dex_file.GetLocation(); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 2560 | dex_caches_.push_back(GcRoot<mirror::DexCache>(dex_cache.Get())); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2561 | dex_cache->SetDexFile(&dex_file); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 2562 | if (log_new_dex_caches_roots_) { |
| 2563 | // TODO: This is not safe if we can remove dex caches. |
| 2564 | new_dex_cache_roots_.push_back(dex_caches_.size() - 1); |
| 2565 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2566 | } |
| 2567 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2568 | void ClassLinker::RegisterDexFile(const DexFile& dex_file) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 2569 | Thread* self = Thread::Current(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2570 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2571 | ReaderMutexLock mu(self, dex_lock_); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2572 | if (IsDexFileRegisteredLocked(dex_file)) { |
| 2573 | return; |
| 2574 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2575 | } |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2576 | // Don't alloc while holding the lock, since allocation may need to |
| 2577 | // suspend all threads and another thread may need the dex_lock_ to |
| 2578 | // get to a suspend point. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2579 | StackHandleScope<1> hs(self); |
| 2580 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(AllocDexCache(self, dex_file))); |
| 2581 | 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] | 2582 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2583 | WriterMutexLock mu(self, dex_lock_); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 2584 | if (IsDexFileRegisteredLocked(dex_file)) { |
| 2585 | return; |
| 2586 | } |
| 2587 | RegisterDexFileLocked(dex_file, dex_cache); |
| 2588 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2589 | } |
| 2590 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2591 | void ClassLinker::RegisterDexFile(const DexFile& dex_file, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 2592 | Handle<mirror::DexCache> dex_cache) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2593 | WriterMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2594 | RegisterDexFileLocked(dex_file, dex_cache); |
| 2595 | } |
| 2596 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2597 | mirror::DexCache* ClassLinker::FindDexCache(const DexFile& dex_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2598 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2599 | // Search assuming unique-ness of dex file. |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2600 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2601 | mirror::DexCache* dex_cache = GetDexCache(i); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2602 | if (dex_cache->GetDexFile() == &dex_file) { |
| 2603 | return dex_cache; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2604 | } |
| 2605 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2606 | // Search matching by location name. |
| 2607 | std::string location(dex_file.GetLocation()); |
| 2608 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2609 | mirror::DexCache* dex_cache = GetDexCache(i); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2610 | if (dex_cache->GetDexFile()->GetLocation() == location) { |
| 2611 | return dex_cache; |
| 2612 | } |
| 2613 | } |
| 2614 | // Failure, dump diagnostic and abort. |
| 2615 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2616 | mirror::DexCache* dex_cache = GetDexCache(i); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 2617 | LOG(ERROR) << "Registered dex file " << i << " = " << dex_cache->GetDexFile()->GetLocation(); |
| 2618 | } |
| 2619 | LOG(FATAL) << "Failed to find DexCache for DexFile " << location; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2620 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2621 | } |
| 2622 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2623 | void ClassLinker::FixupDexCaches(mirror::ArtMethod* resolution_method) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2624 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2625 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2626 | mirror::DexCache* dex_cache = GetDexCache(i); |
| 2627 | dex_cache->Fixup(resolution_method); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2628 | } |
| 2629 | } |
| 2630 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2631 | mirror::Class* ClassLinker::CreatePrimitiveClass(Thread* self, Primitive::Type type) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2632 | mirror::Class* klass = AllocClass(self, mirror::Class::PrimitiveClassSize()); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2633 | if (UNLIKELY(klass == NULL)) { |
| 2634 | return NULL; |
| 2635 | } |
| 2636 | return InitializePrimitiveClass(klass, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2637 | } |
| 2638 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2639 | mirror::Class* ClassLinker::InitializePrimitiveClass(mirror::Class* primitive_class, |
| 2640 | Primitive::Type type) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2641 | CHECK(primitive_class != NULL); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 2642 | // Must hold lock on object when initializing. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2643 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2644 | StackHandleScope<1> hs(self); |
| 2645 | Handle<mirror::Class> h_class(hs.NewHandle(primitive_class)); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 2646 | ObjectLock<mirror::Class> lock(self, h_class); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2647 | primitive_class->SetAccessFlags(kAccPublic | kAccFinal | kAccAbstract); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2648 | primitive_class->SetPrimitiveType(type); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2649 | primitive_class->SetStatus(mirror::Class::kStatusInitialized, self); |
| 2650 | const char* descriptor = Primitive::Descriptor(type); |
| 2651 | mirror::Class* existing = InsertClass(descriptor, primitive_class, Hash(descriptor)); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 2652 | CHECK(existing == NULL) << "InitPrimitiveClass(" << type << ") failed"; |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2653 | return primitive_class; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 2654 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2655 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 2656 | // Create an array class (i.e. the class object for the array, not the |
| 2657 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 2658 | // "[Ljava/lang/String;". |
| 2659 | // |
| 2660 | // If "descriptor" refers to an array of primitives, look up the |
| 2661 | // primitive type's internally-generated class object. |
| 2662 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2663 | // "class_loader" is the class loader of the class that's referring to |
| 2664 | // us. It's used to ensure that we're looking for the element type in |
| 2665 | // the right context. It does NOT become the class loader for the |
| 2666 | // array class; that always comes from the base element class. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 2667 | // |
| 2668 | // Returns NULL with an exception raised on failure. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2669 | mirror::Class* ClassLinker::CreateArrayClass(Thread* self, const char* descriptor, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 2670 | Handle<mirror::ClassLoader> class_loader) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 2671 | // Identify the underlying component type |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2672 | CHECK_EQ('[', descriptor[0]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2673 | StackHandleScope<2> hs(self); |
| 2674 | Handle<mirror::Class> component_type(hs.NewHandle(FindClass(self, descriptor + 1, class_loader))); |
| 2675 | if (component_type.Get() == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 2676 | DCHECK(self->IsExceptionPending()); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 2677 | // We need to accept erroneous classes as component types. |
| 2678 | component_type.Assign(LookupClass(descriptor + 1, class_loader.Get())); |
| 2679 | if (component_type.Get() == nullptr) { |
| 2680 | DCHECK(self->IsExceptionPending()); |
| 2681 | return nullptr; |
| 2682 | } else { |
| 2683 | self->ClearException(); |
| 2684 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2685 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 2686 | if (UNLIKELY(component_type->IsPrimitiveVoid())) { |
| 2687 | ThrowNoClassDefFoundError("Attempt to create array of void primitive type"); |
| 2688 | return nullptr; |
| 2689 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2690 | // See if the component type is already loaded. Array classes are |
| 2691 | // always associated with the class loader of their underlying |
| 2692 | // element type -- an array of Strings goes with the loader for |
| 2693 | // java/lang/String -- so we need to look for it there. (The |
| 2694 | // caller should have checked for the existence of the class |
| 2695 | // before calling here, but they did so with *their* class loader, |
| 2696 | // not the component type's loader.) |
| 2697 | // |
| 2698 | // If we find it, the caller adds "loader" to the class' initiating |
| 2699 | // loader list, which should prevent us from going through this again. |
| 2700 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2701 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2702 | // are the same, because our caller (FindClass) just did the |
| 2703 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 2704 | // because we effectively do this lookup again when we add the new |
| 2705 | // class to the hash table --- necessary because of possible races with |
| 2706 | // other threads.) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2707 | if (class_loader.Get() != component_type->GetClassLoader()) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2708 | mirror::Class* new_class = LookupClass(descriptor, component_type->GetClassLoader()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2709 | if (new_class != NULL) { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 2710 | return new_class; |
| 2711 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2712 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 2713 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2714 | // Fill out the fields in the Class. |
| 2715 | // |
| 2716 | // It is possible to execute some methods against arrays, because |
| 2717 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 2718 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 2719 | // |
| 2720 | // Array classes are simple enough that we don't need to do a full |
| 2721 | // link step. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2722 | auto new_class = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2723 | if (UNLIKELY(!init_done_)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2724 | // Classes that were hand created, ie not by FindSystemClass |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2725 | if (strcmp(descriptor, "[Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2726 | new_class.Assign(GetClassRoot(kClassArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2727 | } else if (strcmp(descriptor, "[Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2728 | new_class.Assign(GetClassRoot(kObjectArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2729 | } else if (strcmp(descriptor, class_roots_descriptors_[kJavaLangStringArrayClass]) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2730 | new_class.Assign(GetClassRoot(kJavaLangStringArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2731 | } else if (strcmp(descriptor, |
| 2732 | class_roots_descriptors_[kJavaLangReflectArtMethodArrayClass]) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2733 | new_class.Assign(GetClassRoot(kJavaLangReflectArtMethodArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2734 | } else if (strcmp(descriptor, |
| 2735 | class_roots_descriptors_[kJavaLangReflectArtFieldArrayClass]) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2736 | new_class.Assign(GetClassRoot(kJavaLangReflectArtFieldArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2737 | } else if (strcmp(descriptor, "[C") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2738 | new_class.Assign(GetClassRoot(kCharArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2739 | } else if (strcmp(descriptor, "[I") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2740 | new_class.Assign(GetClassRoot(kIntArrayClass)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2741 | } |
| 2742 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2743 | if (new_class.Get() == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2744 | new_class.Assign(AllocClass(self, mirror::Array::ClassSize())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2745 | if (new_class.Get() == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 2746 | return nullptr; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2747 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2748 | new_class->SetComponentType(component_type.Get()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2749 | } |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 2750 | ObjectLock<mirror::Class> lock(self, new_class); // Must hold lock on object when initializing. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2751 | DCHECK(new_class->GetComponentType() != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2752 | mirror::Class* java_lang_Object = GetClassRoot(kJavaLangObject); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2753 | new_class->SetSuperClass(java_lang_Object); |
| 2754 | new_class->SetVTable(java_lang_Object->GetVTable()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2755 | new_class->SetPrimitiveType(Primitive::kPrimNot); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2756 | new_class->SetClassLoader(component_type->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2757 | new_class->SetStatus(mirror::Class::kStatusLoaded, self); |
| 2758 | new_class->PopulateEmbeddedImtAndVTable(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2759 | new_class->SetStatus(mirror::Class::kStatusInitialized, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2760 | // don't need to set new_class->SetObjectSize(..) |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 2761 | // because Object::SizeOf delegates to Array::SizeOf |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2762 | |
| 2763 | |
| 2764 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 2765 | // interfaces. We need to set that up here, so that stuff like |
| 2766 | // "instanceof" works right. |
| 2767 | // |
| 2768 | // Note: The GC could run during the call to FindSystemClass, |
| 2769 | // so we need to make sure the class object is GC-valid while we're in |
| 2770 | // there. Do this by clearing the interface list so the GC will just |
| 2771 | // think that the entries are null. |
| 2772 | |
| 2773 | |
| 2774 | // Use the single, global copies of "interfaces" and "iftable" |
| 2775 | // (remember not to free them for arrays). |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2776 | { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 2777 | mirror::IfTable* array_iftable = array_iftable_.Read(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 2778 | CHECK(array_iftable != nullptr); |
| 2779 | new_class->SetIfTable(array_iftable); |
| 2780 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2781 | |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 2782 | // Inherit access flags from the component type. |
| 2783 | int access_flags = new_class->GetComponentType()->GetAccessFlags(); |
| 2784 | // Lose any implementation detail flags; in particular, arrays aren't finalizable. |
| 2785 | access_flags &= kAccJavaFlagsMask; |
| 2786 | // 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] | 2787 | // and remove "interface". |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 2788 | access_flags |= kAccAbstract | kAccFinal; |
| 2789 | access_flags &= ~kAccInterface; |
| 2790 | |
| 2791 | new_class->SetAccessFlags(access_flags); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2792 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2793 | mirror::Class* existing = InsertClass(descriptor, new_class.Get(), Hash(descriptor)); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 2794 | if (existing == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2795 | return new_class.Get(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2796 | } |
| 2797 | // Another thread must have loaded the class after we |
| 2798 | // started but before we finished. Abandon what we've |
| 2799 | // done. |
| 2800 | // |
| 2801 | // (Yes, this happens.) |
| 2802 | |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 2803 | return existing; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 2804 | } |
| 2805 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2806 | mirror::Class* ClassLinker::FindPrimitiveClass(char type) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2807 | switch (type) { |
| 2808 | case 'B': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2809 | return GetClassRoot(kPrimitiveByte); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2810 | case 'C': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2811 | return GetClassRoot(kPrimitiveChar); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2812 | case 'D': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2813 | return GetClassRoot(kPrimitiveDouble); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2814 | case 'F': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2815 | return GetClassRoot(kPrimitiveFloat); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2816 | case 'I': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2817 | return GetClassRoot(kPrimitiveInt); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2818 | case 'J': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2819 | return GetClassRoot(kPrimitiveLong); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2820 | case 'S': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2821 | return GetClassRoot(kPrimitiveShort); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2822 | case 'Z': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2823 | return GetClassRoot(kPrimitiveBoolean); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2824 | case 'V': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2825 | return GetClassRoot(kPrimitiveVoid); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 2826 | default: |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2827 | break; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 2828 | } |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 2829 | std::string printable_type(PrintableChar(type)); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2830 | ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str()); |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 2831 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2832 | } |
| 2833 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2834 | mirror::Class* ClassLinker::InsertClass(const char* descriptor, mirror::Class* klass, |
| 2835 | size_t hash) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 2836 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2837 | mirror::DexCache* dex_cache = klass->GetDexCache(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 2838 | std::string source; |
| 2839 | if (dex_cache != NULL) { |
| 2840 | source += " from "; |
| 2841 | source += dex_cache->GetLocation()->ToModifiedUtf8(); |
| 2842 | } |
| 2843 | LOG(INFO) << "Loaded class " << descriptor << source; |
| 2844 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2845 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2846 | mirror::Class* existing = |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2847 | LookupClassFromTableLocked(descriptor, klass->GetClassLoader(), hash); |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 2848 | if (existing != NULL) { |
| 2849 | return existing; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 2850 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2851 | if (kIsDebugBuild && !klass->IsTemp() && klass->GetClassLoader() == NULL && |
| 2852 | dex_cache_image_class_lookup_required_) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2853 | // Check a class loaded with the system class loader matches one in the image if the class |
| 2854 | // is in the image. |
| 2855 | existing = LookupClassFromImage(descriptor); |
| 2856 | if (existing != NULL) { |
| 2857 | CHECK(klass == existing); |
| 2858 | } |
| 2859 | } |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 2860 | VerifyObject(klass); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 2861 | class_table_.insert(std::make_pair(hash, GcRoot<mirror::Class>(klass))); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 2862 | if (log_new_class_table_roots_) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 2863 | new_class_roots_.push_back(std::make_pair(hash, GcRoot<mirror::Class>(klass))); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 2864 | } |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 2865 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2866 | } |
| 2867 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2868 | mirror::Class* ClassLinker::UpdateClass(const char* descriptor, mirror::Class* klass, |
| 2869 | size_t hash) { |
| 2870 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 2871 | mirror::Class* existing = |
| 2872 | LookupClassFromTableLocked(descriptor, klass->GetClassLoader(), hash); |
| 2873 | |
| 2874 | if (existing == nullptr) { |
| 2875 | CHECK(klass->IsProxyClass()); |
| 2876 | return nullptr; |
| 2877 | } |
| 2878 | |
| 2879 | CHECK_NE(existing, klass) << descriptor; |
| 2880 | CHECK(!existing->IsResolved()) << descriptor; |
| 2881 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusResolving) << descriptor; |
| 2882 | |
| 2883 | for (auto it = class_table_.lower_bound(hash), end = class_table_.end(); it != end && it->first == hash; |
| 2884 | ++it) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 2885 | mirror::Class* klass = it->second.Read(); |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 2886 | if (klass == existing) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2887 | class_table_.erase(it); |
| 2888 | break; |
| 2889 | } |
| 2890 | } |
| 2891 | |
| 2892 | CHECK(!klass->IsTemp()) << descriptor; |
| 2893 | if (kIsDebugBuild && klass->GetClassLoader() == nullptr && |
| 2894 | dex_cache_image_class_lookup_required_) { |
| 2895 | // Check a class loaded with the system class loader matches one in the image if the class |
| 2896 | // is in the image. |
| 2897 | existing = LookupClassFromImage(descriptor); |
| 2898 | if (existing != nullptr) { |
| 2899 | CHECK(klass == existing) << descriptor; |
| 2900 | } |
| 2901 | } |
| 2902 | VerifyObject(klass); |
| 2903 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 2904 | class_table_.insert(std::make_pair(hash, GcRoot<mirror::Class>(klass))); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2905 | if (log_new_class_table_roots_) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 2906 | new_class_roots_.push_back(std::make_pair(hash, GcRoot<mirror::Class>(klass))); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2907 | } |
| 2908 | |
| 2909 | return existing; |
| 2910 | } |
| 2911 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2912 | bool ClassLinker::RemoveClass(const char* descriptor, const mirror::ClassLoader* class_loader) { |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 2913 | size_t hash = Hash(descriptor); |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 2914 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 2915 | for (auto it = class_table_.lower_bound(hash), end = class_table_.end(); |
| 2916 | it != end && it->first == hash; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2917 | ++it) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 2918 | mirror::Class* klass = it->second.Read(); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 2919 | if (klass->GetClassLoader() == class_loader && klass->DescriptorEquals(descriptor)) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2920 | class_table_.erase(it); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 2921 | return true; |
| 2922 | } |
| 2923 | } |
| 2924 | return false; |
| 2925 | } |
| 2926 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2927 | mirror::Class* ClassLinker::LookupClass(const char* descriptor, |
| 2928 | const mirror::ClassLoader* class_loader) { |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 2929 | size_t hash = Hash(descriptor); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2930 | { |
| 2931 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 2932 | mirror::Class* result = LookupClassFromTableLocked(descriptor, class_loader, hash); |
| 2933 | if (result != NULL) { |
| 2934 | return result; |
| 2935 | } |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 2936 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2937 | if (class_loader != NULL || !dex_cache_image_class_lookup_required_) { |
| 2938 | return NULL; |
| 2939 | } else { |
| 2940 | // Lookup failed but need to search dex_caches_. |
| 2941 | mirror::Class* result = LookupClassFromImage(descriptor); |
| 2942 | if (result != NULL) { |
| 2943 | InsertClass(descriptor, result, hash); |
| 2944 | } else { |
| 2945 | // Searching the image dex files/caches failed, we don't want to get into this situation |
| 2946 | // often as map searches are faster, so after kMaxFailedDexCacheLookups move all image |
| 2947 | // classes into the class table. |
| 2948 | const int32_t kMaxFailedDexCacheLookups = 1000; |
| 2949 | if (++failed_dex_cache_class_lookups_ > kMaxFailedDexCacheLookups) { |
| 2950 | MoveImageClassesToClassTable(); |
| 2951 | } |
| 2952 | } |
| 2953 | return result; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2954 | } |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 2955 | } |
| 2956 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2957 | mirror::Class* ClassLinker::LookupClassFromTableLocked(const char* descriptor, |
| 2958 | const mirror::ClassLoader* class_loader, |
| 2959 | size_t hash) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2960 | auto end = class_table_.end(); |
| 2961 | for (auto it = class_table_.lower_bound(hash); it != end && it->first == hash; ++it) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 2962 | mirror::Class* klass = it->second.Read(); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 2963 | if (klass->GetClassLoader() == class_loader && klass->DescriptorEquals(descriptor)) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2964 | if (kIsDebugBuild) { |
| 2965 | // Check for duplicates in the table. |
| 2966 | for (++it; it != end && it->first == hash; ++it) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 2967 | mirror::Class* klass2 = it->second.Read(); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 2968 | CHECK(!(klass2->GetClassLoader() == class_loader && |
| 2969 | klass2->DescriptorEquals(descriptor))) |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 2970 | << PrettyClass(klass) << " " << klass << " " << klass->GetClassLoader() << " " |
| 2971 | << PrettyClass(klass2) << " " << klass2 << " " << klass2->GetClassLoader(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2972 | } |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 2973 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 2974 | return klass; |
| 2975 | } |
| 2976 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 2977 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2978 | } |
| 2979 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2980 | static mirror::ObjectArray<mirror::DexCache>* GetImageDexCaches() |
| 2981 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 2982 | gc::space::ImageSpace* image = Runtime::Current()->GetHeap()->GetImageSpace(); |
| 2983 | CHECK(image != NULL); |
| 2984 | mirror::Object* root = image->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
| 2985 | return root->AsObjectArray<mirror::DexCache>(); |
| 2986 | } |
| 2987 | |
| 2988 | void ClassLinker::MoveImageClassesToClassTable() { |
| 2989 | Thread* self = Thread::Current(); |
| 2990 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2991 | if (!dex_cache_image_class_lookup_required_) { |
| 2992 | return; // All dex cache classes are already in the class table. |
| 2993 | } |
| 2994 | const char* old_no_suspend_cause = |
| 2995 | self->StartAssertNoThreadSuspension("Moving image classes to class table"); |
| 2996 | mirror::ObjectArray<mirror::DexCache>* dex_caches = GetImageDexCaches(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2997 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 2998 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 2999 | mirror::ObjectArray<mirror::Class>* types = dex_cache->GetResolvedTypes(); |
| 3000 | for (int32_t j = 0; j < types->GetLength(); j++) { |
| 3001 | mirror::Class* klass = types->Get(j); |
| 3002 | if (klass != NULL) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3003 | DCHECK(klass->GetClassLoader() == NULL); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 3004 | std::string descriptor = klass->GetDescriptor(); |
| 3005 | size_t hash = Hash(descriptor.c_str()); |
| 3006 | mirror::Class* existing = LookupClassFromTableLocked(descriptor.c_str(), NULL, hash); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3007 | if (existing != NULL) { |
| 3008 | CHECK(existing == klass) << PrettyClassAndClassLoader(existing) << " != " |
| 3009 | << PrettyClassAndClassLoader(klass); |
| 3010 | } else { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 3011 | class_table_.insert(std::make_pair(hash, GcRoot<mirror::Class>(klass))); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3012 | if (log_new_class_table_roots_) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 3013 | new_class_roots_.push_back(std::make_pair(hash, GcRoot<mirror::Class>(klass))); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3014 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3015 | } |
| 3016 | } |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 3017 | } |
| 3018 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3019 | dex_cache_image_class_lookup_required_ = false; |
| 3020 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
| 3021 | } |
| 3022 | |
| 3023 | mirror::Class* ClassLinker::LookupClassFromImage(const char* descriptor) { |
| 3024 | Thread* self = Thread::Current(); |
| 3025 | const char* old_no_suspend_cause = |
| 3026 | self->StartAssertNoThreadSuspension("Image class lookup"); |
| 3027 | mirror::ObjectArray<mirror::DexCache>* dex_caches = GetImageDexCaches(); |
| 3028 | for (int32_t i = 0; i < dex_caches->GetLength(); ++i) { |
| 3029 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 3030 | const DexFile* dex_file = dex_cache->GetDexFile(); |
Vladimir Marko | 801a811 | 2014-01-06 14:28:16 +0000 | [diff] [blame] | 3031 | // Try binary searching the string/type index. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3032 | const DexFile::StringId* string_id = dex_file->FindStringId(descriptor); |
| 3033 | if (string_id != NULL) { |
| 3034 | const DexFile::TypeId* type_id = |
| 3035 | dex_file->FindTypeId(dex_file->GetIndexForStringId(*string_id)); |
| 3036 | if (type_id != NULL) { |
| 3037 | uint16_t type_idx = dex_file->GetIndexForTypeId(*type_id); |
| 3038 | mirror::Class* klass = dex_cache->GetResolvedType(type_idx); |
| 3039 | if (klass != NULL) { |
| 3040 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
| 3041 | return klass; |
| 3042 | } |
| 3043 | } |
| 3044 | } |
| 3045 | } |
| 3046 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
| 3047 | return NULL; |
| 3048 | } |
| 3049 | |
| 3050 | void ClassLinker::LookupClasses(const char* descriptor, std::vector<mirror::Class*>& result) { |
| 3051 | result.clear(); |
| 3052 | if (dex_cache_image_class_lookup_required_) { |
| 3053 | MoveImageClassesToClassTable(); |
| 3054 | } |
| 3055 | size_t hash = Hash(descriptor); |
| 3056 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3057 | for (auto it = class_table_.lower_bound(hash), end = class_table_.end(); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 3058 | it != end && it->first == hash; ++it) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 3059 | mirror::Class* klass = it->second.Read(); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 3060 | if (klass->DescriptorEquals(descriptor)) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3061 | result.push_back(klass); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 3062 | } |
| 3063 | } |
| 3064 | } |
| 3065 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 3066 | void ClassLinker::VerifyClass(Handle<mirror::Class> klass) { |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 3067 | // TODO: assert that the monitor on the Class is held |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3068 | Thread* self = Thread::Current(); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3069 | ObjectLock<mirror::Class> lock(self, klass); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 3070 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3071 | // Don't attempt to re-verify if already sufficiently verified. |
| 3072 | if (klass->IsVerified() || |
| 3073 | (klass->IsCompileTimeVerified() && Runtime::Current()->IsCompiler())) { |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3074 | return; |
| 3075 | } |
| 3076 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3077 | // The class might already be erroneous, for example at compile time if we attempted to verify |
| 3078 | // this class as a parent to another. |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 3079 | if (klass->IsErroneous()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3080 | ThrowEarlierClassFailure(klass.Get()); |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 3081 | return; |
| 3082 | } |
| 3083 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3084 | if (klass->GetStatus() == mirror::Class::kStatusResolved) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3085 | klass->SetStatus(mirror::Class::kStatusVerifying, self); |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3086 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3087 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3088 | << PrettyClass(klass.Get()); |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3089 | CHECK(!Runtime::Current()->IsCompiler()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3090 | klass->SetStatus(mirror::Class::kStatusVerifyingAtRuntime, self); |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3091 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3092 | |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 3093 | // Skip verification if disabled. |
| 3094 | if (!Runtime::Current()->IsVerificationEnabled()) { |
| 3095 | klass->SetStatus(mirror::Class::kStatusVerified, self); |
| 3096 | return; |
| 3097 | } |
| 3098 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3099 | // Verify super class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3100 | StackHandleScope<2> hs(self); |
| 3101 | Handle<mirror::Class> super(hs.NewHandle(klass->GetSuperClass())); |
| 3102 | if (super.Get() != NULL) { |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3103 | // Acquire lock to prevent races on verifying the super class. |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3104 | ObjectLock<mirror::Class> lock(self, super); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3105 | |
| 3106 | if (!super->IsVerified() && !super->IsErroneous()) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3107 | VerifyClass(super); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3108 | } |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 3109 | if (!super->IsCompileTimeVerified()) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3110 | std::string error_msg( |
| 3111 | StringPrintf("Rejecting class %s that attempts to sub-class erroneous class %s", |
| 3112 | PrettyDescriptor(klass.Get()).c_str(), |
| 3113 | PrettyDescriptor(super.Get()).c_str())); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3114 | LOG(ERROR) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3115 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException(nullptr))); |
| 3116 | if (cause.Get() != nullptr) { |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3117 | self->ClearException(); |
| 3118 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3119 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
| 3120 | if (cause.Get() != nullptr) { |
| 3121 | self->GetException(nullptr)->SetCause(cause.Get()); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3122 | } |
Jeff Hao | 22cb09b | 2013-12-12 14:29:15 -0800 | [diff] [blame] | 3123 | ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex()); |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 3124 | if (Runtime::Current()->IsCompiler()) { |
| 3125 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
| 3126 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3127 | klass->SetStatus(mirror::Class::kStatusError, self); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3128 | return; |
| 3129 | } |
| 3130 | } |
| 3131 | |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 3132 | // 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] | 3133 | const DexFile& dex_file = *klass->GetDexCache()->GetDexFile(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3134 | mirror::Class::Status oat_file_class_status(mirror::Class::kStatusNotReady); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3135 | bool preverified = VerifyClassUsingOatFile(dex_file, klass.Get(), oat_file_class_status); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3136 | if (oat_file_class_status == mirror::Class::kStatusError) { |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 3137 | VLOG(class_linker) << "Skipping runtime verification of erroneous class " |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3138 | << PrettyDescriptor(klass.Get()) << " in " |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 3139 | << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3140 | ThrowVerifyError(klass.Get(), "Rejecting class %s because it failed compile-time verification", |
| 3141 | PrettyDescriptor(klass.Get()).c_str()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3142 | klass->SetStatus(mirror::Class::kStatusError, self); |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 3143 | return; |
| 3144 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3145 | verifier::MethodVerifier::FailureKind verifier_failure = verifier::MethodVerifier::kNoFailure; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3146 | std::string error_msg; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 3147 | if (!preverified) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3148 | verifier_failure = verifier::MethodVerifier::VerifyClass(klass.Get(), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3149 | Runtime::Current()->IsCompiler(), |
| 3150 | &error_msg); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 3151 | } |
| 3152 | if (preverified || verifier_failure != verifier::MethodVerifier::kHardFailure) { |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 3153 | if (!preverified && verifier_failure != verifier::MethodVerifier::kNoFailure) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3154 | VLOG(class_linker) << "Soft verification failure in class " << PrettyDescriptor(klass.Get()) |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 3155 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 3156 | << " because: " << error_msg; |
| 3157 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3158 | self->AssertNoPendingException(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3159 | // Make sure all classes referenced by catch blocks are resolved. |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3160 | ResolveClassExceptionHandlerTypes(dex_file, klass); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3161 | if (verifier_failure == verifier::MethodVerifier::kNoFailure) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3162 | // Even though there were no verifier failures we need to respect whether the super-class |
| 3163 | // was verified or requiring runtime reverification. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3164 | if (super.Get() == NULL || super->IsVerified()) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3165 | klass->SetStatus(mirror::Class::kStatusVerified, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3166 | } else { |
| 3167 | CHECK_EQ(super->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3168 | klass->SetStatus(mirror::Class::kStatusRetryVerificationAtRuntime, self); |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 3169 | // Pretend a soft failure occured so that we don't consider the class verified below. |
| 3170 | verifier_failure = verifier::MethodVerifier::kSoftFailure; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3171 | } |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3172 | } else { |
| 3173 | CHECK_EQ(verifier_failure, verifier::MethodVerifier::kSoftFailure); |
| 3174 | // Soft failures at compile time should be retried at runtime. Soft |
| 3175 | // failures at runtime will be handled by slow paths in the generated |
| 3176 | // code. Set status accordingly. |
| 3177 | if (Runtime::Current()->IsCompiler()) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3178 | klass->SetStatus(mirror::Class::kStatusRetryVerificationAtRuntime, self); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3179 | } else { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3180 | klass->SetStatus(mirror::Class::kStatusVerified, self); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3181 | } |
| 3182 | } |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 3183 | } else { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3184 | LOG(ERROR) << "Verification failed on class " << PrettyDescriptor(klass.Get()) |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3185 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 3186 | << " because: " << error_msg; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3187 | self->AssertNoPendingException(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3188 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3189 | klass->SetStatus(mirror::Class::kStatusError, self); |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 3190 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3191 | if (preverified || verifier_failure == verifier::MethodVerifier::kNoFailure) { |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 3192 | // 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] | 3193 | // Let the interpreter know it by setting the kAccPreverified flag onto each |
| 3194 | // method. |
| 3195 | // Note: we're going here during compilation and at runtime. When we set the |
| 3196 | // kAccPreverified flag when compiling image classes, the flag is recorded |
| 3197 | // in the image and is set when loading the image. |
| 3198 | klass->SetPreverifiedFlagOnAllMethods(); |
| 3199 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3200 | } |
| 3201 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3202 | bool ClassLinker::VerifyClassUsingOatFile(const DexFile& dex_file, mirror::Class* klass, |
| 3203 | mirror::Class::Status& oat_file_class_status) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3204 | // If we're compiling, we can only verify the class using the oat file if |
| 3205 | // we are not compiling the image or if the class we're verifying is not part of |
| 3206 | // the app. In other words, we will only check for preverification of bootclasspath |
| 3207 | // classes. |
| 3208 | if (Runtime::Current()->IsCompiler()) { |
| 3209 | // Are we compiling the bootclasspath? |
| 3210 | if (!Runtime::Current()->UseCompileTimeClassPath()) { |
| 3211 | return false; |
| 3212 | } |
| 3213 | // We are compiling an app (not the image). |
| 3214 | |
| 3215 | // Is this an app class? (I.e. not a bootclasspath class) |
| 3216 | if (klass->GetClassLoader() != NULL) { |
| 3217 | return false; |
| 3218 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3219 | } |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3220 | |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 3221 | const OatFile* oat_file = FindOpenedOatFileForDexFile(dex_file); |
Anwar Ghuloum | ad256bb | 2013-07-18 14:58:55 -0700 | [diff] [blame] | 3222 | // Make this work with gtests, which do not set up the image properly. |
| 3223 | // TODO: we should clean up gtests to set up the image path properly. |
| 3224 | if (Runtime::Current()->IsCompiler() && (oat_file == NULL)) { |
| 3225 | return false; |
| 3226 | } |
| 3227 | |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 3228 | CHECK(oat_file != NULL) << dex_file.GetLocation() << " " << PrettyClass(klass); |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 3229 | uint dex_location_checksum = dex_file.GetLocationChecksum(); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 3230 | 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] | 3231 | &dex_location_checksum); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 3232 | CHECK(oat_dex_file != NULL) << dex_file.GetLocation() << " " << PrettyClass(klass); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3233 | uint16_t class_def_index = klass->GetDexClassDefIndex(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 3234 | oat_file_class_status = oat_dex_file->GetOatClass(class_def_index).GetStatus(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3235 | if (oat_file_class_status == mirror::Class::kStatusVerified || |
| 3236 | oat_file_class_status == mirror::Class::kStatusInitialized) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3237 | return true; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3238 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3239 | if (oat_file_class_status == mirror::Class::kStatusRetryVerificationAtRuntime) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 3240 | // Compile time verification failed with a soft error. Compile time verification can fail |
| 3241 | // because we have incomplete type information. Consider the following: |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3242 | // class ... { |
| 3243 | // Foo x; |
| 3244 | // .... () { |
| 3245 | // if (...) { |
| 3246 | // v1 gets assigned a type of resolved class Foo |
| 3247 | // } else { |
| 3248 | // v1 gets assigned a type of unresolved class Bar |
| 3249 | // } |
| 3250 | // iput x = v1 |
| 3251 | // } } |
| 3252 | // when we merge v1 following the if-the-else it results in Conflict |
| 3253 | // (see verifier::RegType::Merge) as we can't know the type of Bar and we could possibly be |
| 3254 | // allowing an unsafe assignment to the field x in the iput (javac may have compiled this as |
| 3255 | // it knew Bar was a sub-class of Foo, but for us this may have been moved into a separate apk |
| 3256 | // at compile time). |
| 3257 | return false; |
| 3258 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3259 | if (oat_file_class_status == mirror::Class::kStatusError) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 3260 | // Compile time verification failed with a hard error. This is caused by invalid instructions |
| 3261 | // in the class. These errors are unrecoverable. |
| 3262 | return false; |
| 3263 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3264 | if (oat_file_class_status == mirror::Class::kStatusNotReady) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3265 | // Status is uninitialized if we couldn't determine the status at compile time, for example, |
| 3266 | // not loading the class. |
| 3267 | // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy |
| 3268 | // isn't a problem and this case shouldn't occur |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3269 | return false; |
| 3270 | } |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 3271 | LOG(FATAL) << "Unexpected class status: " << oat_file_class_status |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3272 | << " " << dex_file.GetLocation() << " " << PrettyClass(klass) << " " |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 3273 | << klass->GetDescriptor(); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 3274 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3275 | return false; |
| 3276 | } |
| 3277 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3278 | void ClassLinker::ResolveClassExceptionHandlerTypes(const DexFile& dex_file, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 3279 | Handle<mirror::Class> klass) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3280 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
| 3281 | ResolveMethodExceptionHandlerTypes(dex_file, klass->GetDirectMethod(i)); |
| 3282 | } |
| 3283 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
| 3284 | ResolveMethodExceptionHandlerTypes(dex_file, klass->GetVirtualMethod(i)); |
| 3285 | } |
| 3286 | } |
| 3287 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3288 | void ClassLinker::ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3289 | mirror::ArtMethod* method) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3290 | // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod. |
| 3291 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset()); |
| 3292 | if (code_item == NULL) { |
| 3293 | return; // native or abstract method |
| 3294 | } |
| 3295 | if (code_item->tries_size_ == 0) { |
| 3296 | return; // nothing to process |
| 3297 | } |
| 3298 | const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item, 0); |
| 3299 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 3300 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 3301 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 3302 | CatchHandlerIterator iterator(handlers_ptr); |
| 3303 | for (; iterator.HasNext(); iterator.Next()) { |
| 3304 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 3305 | // unresolved exception types will be ignored by exception delivery |
| 3306 | if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3307 | mirror::Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3308 | if (exception_type == NULL) { |
| 3309 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 3310 | Thread::Current()->ClearException(); |
| 3311 | } |
| 3312 | } |
| 3313 | } |
| 3314 | handlers_ptr = iterator.EndDataPointer(); |
| 3315 | } |
| 3316 | } |
| 3317 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3318 | static void CheckProxyConstructor(mirror::ArtMethod* constructor); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3319 | static void CheckProxyMethod(Handle<mirror::ArtMethod> method, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 3320 | Handle<mirror::ArtMethod> prototype); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3321 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 3322 | mirror::Class* ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, jstring name, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3323 | jobjectArray interfaces, jobject loader, |
| 3324 | jobjectArray methods, jobjectArray throws) { |
| 3325 | Thread* self = soa.Self(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3326 | StackHandleScope<8> hs(self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3327 | Handle<mirror::Class> klass(hs.NewHandle( |
| 3328 | AllocClass(self, GetClassRoot(kJavaLangClass), sizeof(mirror::Class)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3329 | if (klass.Get() == NULL) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3330 | CHECK(self->IsExceptionPending()); // OOME. |
| 3331 | return NULL; |
| 3332 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3333 | DCHECK(klass->GetClass() != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3334 | klass->SetObjectSize(sizeof(mirror::Proxy)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3335 | klass->SetAccessFlags(kAccClassIsProxy | kAccPublic | kAccFinal); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3336 | klass->SetClassLoader(soa.Decode<mirror::ClassLoader*>(loader)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3337 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3338 | klass->SetName(soa.Decode<mirror::String*>(name)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3339 | mirror::Class* proxy_class = GetClassRoot(kJavaLangReflectProxy); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3340 | klass->SetDexCache(proxy_class->GetDexCache()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3341 | klass->SetStatus(mirror::Class::kStatusIdx, self); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3342 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3343 | // Instance fields are inherited, but we add a couple of static fields... |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3344 | { |
| 3345 | mirror::ObjectArray<mirror::ArtField>* sfields = AllocArtFieldArray(self, 2); |
| 3346 | if (UNLIKELY(sfields == NULL)) { |
| 3347 | CHECK(self->IsExceptionPending()); // OOME. |
| 3348 | return NULL; |
| 3349 | } |
| 3350 | klass->SetSFields(sfields); |
| 3351 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3352 | // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by |
| 3353 | // our proxy, so Class.getInterfaces doesn't return the flattened set. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3354 | Handle<mirror::ArtField> interfaces_sfield(hs.NewHandle(AllocArtField(self))); |
| 3355 | if (UNLIKELY(interfaces_sfield.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3356 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3357 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3358 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3359 | klass->SetStaticField(0, interfaces_sfield.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3360 | interfaces_sfield->SetDexFieldIndex(0); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3361 | interfaces_sfield->SetDeclaringClass(klass.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3362 | interfaces_sfield->SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
| 3363 | // 2. Create a static field 'throws' that holds exceptions thrown by our methods. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3364 | Handle<mirror::ArtField> throws_sfield(hs.NewHandle(AllocArtField(self))); |
| 3365 | if (UNLIKELY(throws_sfield.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3366 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3367 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3368 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3369 | klass->SetStaticField(1, throws_sfield.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3370 | throws_sfield->SetDexFieldIndex(1); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3371 | throws_sfield->SetDeclaringClass(klass.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3372 | throws_sfield->SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3373 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3374 | // Proxies have 1 direct method, the constructor |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3375 | { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3376 | mirror::ObjectArray<mirror::ArtMethod>* directs = AllocArtMethodArray(self, 1); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3377 | if (UNLIKELY(directs == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3378 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3379 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3380 | } |
| 3381 | klass->SetDirectMethods(directs); |
| 3382 | mirror::ArtMethod* constructor = CreateProxyConstructor(self, klass, proxy_class); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3383 | if (UNLIKELY(constructor == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3384 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3385 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3386 | } |
| 3387 | klass->SetDirectMethod(0, constructor); |
| 3388 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3389 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3390 | // Create virtual method using specified prototypes. |
| 3391 | size_t num_virtual_methods = |
| 3392 | soa.Decode<mirror::ObjectArray<mirror::ArtMethod>*>(methods)->GetLength(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3393 | { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3394 | mirror::ObjectArray<mirror::ArtMethod>* virtuals = AllocArtMethodArray(self, |
| 3395 | num_virtual_methods); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3396 | if (UNLIKELY(virtuals == NULL)) { |
| 3397 | CHECK(self->IsExceptionPending()); // OOME. |
| 3398 | return NULL; |
| 3399 | } |
| 3400 | klass->SetVirtualMethods(virtuals); |
| 3401 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3402 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3403 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3404 | mirror::ObjectArray<mirror::ArtMethod>* decoded_methods = |
| 3405 | soa.Decode<mirror::ObjectArray<mirror::ArtMethod>*>(methods); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3406 | Handle<mirror::ArtMethod> prototype(hs.NewHandle(decoded_methods->Get(i))); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3407 | mirror::ArtMethod* clone = CreateProxyMethod(self, klass, prototype); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3408 | if (UNLIKELY(clone == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3409 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3410 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3411 | } |
| 3412 | klass->SetVirtualMethod(i, clone); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3413 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3414 | |
| 3415 | klass->SetSuperClass(proxy_class); // The super class is java.lang.reflect.Proxy |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3416 | klass->SetStatus(mirror::Class::kStatusLoaded, self); // Now effectively in the loaded state. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3417 | self->AssertNoPendingException(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3418 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3419 | std::string descriptor(GetDescriptorForProxy(klass.Get())); |
| 3420 | mirror::Class* new_class = nullptr; |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 3421 | { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3422 | ObjectLock<mirror::Class> resolution_lock(self, klass); // Must hold lock on object when resolved. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3423 | // Link the fields and virtual methods, creating vtable and iftables |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3424 | Handle<mirror::ObjectArray<mirror::Class> > h_interfaces( |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3425 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces))); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3426 | if (!LinkClass(self, descriptor.c_str(), klass, h_interfaces, &new_class)) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3427 | klass->SetStatus(mirror::Class::kStatusError, self); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3428 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3429 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3430 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3431 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3432 | CHECK(klass->IsRetired()); |
| 3433 | CHECK_NE(klass.Get(), new_class); |
| 3434 | klass.Assign(new_class); |
| 3435 | |
| 3436 | CHECK_EQ(interfaces_sfield->GetDeclaringClass(), new_class); |
| 3437 | interfaces_sfield->SetObject<false>(klass.Get(), soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces)); |
| 3438 | CHECK_EQ(throws_sfield->GetDeclaringClass(), new_class); |
| 3439 | throws_sfield->SetObject<false>(klass.Get(), soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class> >*>(throws)); |
| 3440 | |
| 3441 | { |
| 3442 | // Lock on klass is released. Lock new class object. |
| 3443 | ObjectLock<mirror::Class> initialization_lock(self, klass); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3444 | klass->SetStatus(mirror::Class::kStatusInitialized, self); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 3445 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3446 | |
| 3447 | // sanity checks |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 3448 | if (kIsDebugBuild) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3449 | CHECK(klass->GetIFields() == nullptr); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3450 | CheckProxyConstructor(klass->GetDirectMethod(0)); |
| 3451 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3452 | StackHandleScope<2> hs(self); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3453 | mirror::ObjectArray<mirror::ArtMethod>* decoded_methods = |
| 3454 | soa.Decode<mirror::ObjectArray<mirror::ArtMethod>*>(methods); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3455 | Handle<mirror::ArtMethod> prototype(hs.NewHandle(decoded_methods->Get(i))); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3456 | Handle<mirror::ArtMethod> virtual_method(hs.NewHandle(klass->GetVirtualMethod(i))); |
| 3457 | CheckProxyMethod(virtual_method, prototype); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3458 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3459 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3460 | mirror::String* decoded_name = soa.Decode<mirror::String*>(name); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3461 | std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3462 | decoded_name->ToModifiedUtf8().c_str())); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3463 | CHECK_EQ(PrettyField(klass->GetStaticField(0)), interfaces_field_name); |
| 3464 | |
| 3465 | std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3466 | decoded_name->ToModifiedUtf8().c_str())); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3467 | CHECK_EQ(PrettyField(klass->GetStaticField(1)), throws_field_name); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3468 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3469 | CHECK_EQ(klass.Get()->GetInterfaces(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3470 | soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3471 | CHECK_EQ(klass.Get()->GetThrows(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3472 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>*>(throws)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3473 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3474 | mirror::Class* existing = InsertClass(descriptor.c_str(), klass.Get(), Hash(descriptor.c_str())); |
Mathieu Chartier | 31b9d66 | 2013-10-14 11:53:12 -0700 | [diff] [blame] | 3475 | CHECK(existing == nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3476 | return klass.Get(); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3477 | } |
| 3478 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3479 | std::string ClassLinker::GetDescriptorForProxy(mirror::Class* proxy_class) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3480 | DCHECK(proxy_class->IsProxyClass()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3481 | mirror::String* name = proxy_class->GetName(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3482 | DCHECK(name != NULL); |
| 3483 | return DotToDescriptor(name->ToModifiedUtf8().c_str()); |
| 3484 | } |
| 3485 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3486 | mirror::ArtMethod* ClassLinker::FindMethodForProxy(mirror::Class* proxy_class, |
| 3487 | mirror::ArtMethod* proxy_method) { |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3488 | DCHECK(proxy_class->IsProxyClass()); |
| 3489 | DCHECK(proxy_method->IsProxyMethod()); |
| 3490 | // Locate the dex cache of the original interface/Object |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame^] | 3491 | mirror::DexCache* dex_cache = nullptr; |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3492 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3493 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3494 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3495 | mirror::DexCache* a_dex_cache = GetDexCache(i); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame^] | 3496 | if (proxy_method->HasSameDexCacheResolvedTypes(a_dex_cache->GetResolvedTypes())) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3497 | dex_cache = a_dex_cache; |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3498 | break; |
| 3499 | } |
| 3500 | } |
| 3501 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame^] | 3502 | CHECK(dex_cache != nullptr); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3503 | uint32_t method_idx = proxy_method->GetDexMethodIndex(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3504 | mirror::ArtMethod* resolved_method = dex_cache->GetResolvedMethod(method_idx); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame^] | 3505 | CHECK(resolved_method != nullptr); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3506 | return resolved_method; |
| 3507 | } |
| 3508 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3509 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3510 | mirror::ArtMethod* ClassLinker::CreateProxyConstructor(Thread* self, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 3511 | Handle<mirror::Class> klass, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3512 | mirror::Class* proxy_class) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3513 | // Create constructor for Proxy that must initialize h |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3514 | mirror::ObjectArray<mirror::ArtMethod>* proxy_direct_methods = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3515 | proxy_class->GetDirectMethods(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3516 | CHECK_EQ(proxy_direct_methods->GetLength(), 16); |
| 3517 | mirror::ArtMethod* proxy_constructor = proxy_direct_methods->Get(2); |
Jeff Hao | f0a3f09 | 2014-07-24 16:26:09 -0700 | [diff] [blame] | 3518 | mirror::ArtMethod* constructor = down_cast<mirror::ArtMethod*>(proxy_constructor->Clone(self)); |
| 3519 | if (constructor == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3520 | CHECK(self->IsExceptionPending()); // OOME. |
Jeff Hao | f0a3f09 | 2014-07-24 16:26:09 -0700 | [diff] [blame] | 3521 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3522 | } |
Jeff Hao | f0a3f09 | 2014-07-24 16:26:09 -0700 | [diff] [blame] | 3523 | // Make the proxy constructor's code always point to the uninstrumented code. This avoids |
| 3524 | // getting a method enter event for the proxy constructor as the proxy constructor doesn't |
| 3525 | // have an activation. |
| 3526 | bool have_portable_code; |
| 3527 | constructor->SetEntryPointFromQuickCompiledCode(GetQuickOatCodeFor(proxy_constructor)); |
| 3528 | constructor->SetEntryPointFromPortableCompiledCode(GetPortableOatCodeFor(proxy_constructor, |
| 3529 | &have_portable_code)); |
| 3530 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3531 | // Make this constructor public and fix the class to be our Proxy version |
| 3532 | constructor->SetAccessFlags((constructor->GetAccessFlags() & ~kAccProtected) | kAccPublic); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3533 | constructor->SetDeclaringClass(klass.Get()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3534 | return constructor; |
| 3535 | } |
| 3536 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3537 | static void CheckProxyConstructor(mirror::ArtMethod* constructor) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 3538 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3539 | CHECK(constructor->IsConstructor()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3540 | CHECK_STREQ(constructor->GetName(), "<init>"); |
| 3541 | CHECK_STREQ(constructor->GetSignature().ToString().c_str(), |
| 3542 | "(Ljava/lang/reflect/InvocationHandler;)V"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3543 | DCHECK(constructor->IsPublic()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3544 | } |
| 3545 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3546 | mirror::ArtMethod* ClassLinker::CreateProxyMethod(Thread* self, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 3547 | Handle<mirror::Class> klass, |
| 3548 | Handle<mirror::ArtMethod> prototype) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3549 | // Ensure prototype is in dex cache so that we can use the dex cache to look up the overridden |
| 3550 | // prototype method |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3551 | prototype->GetDeclaringClass()->GetDexCache()->SetResolvedMethod(prototype->GetDexMethodIndex(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3552 | prototype.Get()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3553 | // 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] | 3554 | // as necessary |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3555 | mirror::ArtMethod* method = down_cast<mirror::ArtMethod*>(prototype->Clone(self)); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3556 | if (UNLIKELY(method == NULL)) { |
| 3557 | CHECK(self->IsExceptionPending()); // OOME. |
| 3558 | return NULL; |
| 3559 | } |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3560 | |
| 3561 | // Set class to be the concrete proxy class and clear the abstract flag, modify exceptions to |
| 3562 | // the intersection of throw exceptions as defined in Proxy |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3563 | method->SetDeclaringClass(klass.Get()); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3564 | method->SetAccessFlags((method->GetAccessFlags() & ~kAccAbstract) | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3565 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3566 | // At runtime the method looks like a reference and argument saving method, clone the code |
| 3567 | // related parameters from this method. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3568 | method->SetEntryPointFromQuickCompiledCode(GetQuickProxyInvokeHandler()); |
| 3569 | method->SetEntryPointFromPortableCompiledCode(GetPortableProxyInvokeHandler()); |
Dragos Sbirlea | 08bf196 | 2013-08-12 08:53:04 -0700 | [diff] [blame] | 3570 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3571 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3572 | return method; |
| 3573 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3574 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3575 | static void CheckProxyMethod(Handle<mirror::ArtMethod> method, Handle<mirror::ArtMethod> prototype) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 3576 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3577 | // Basic sanity |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3578 | CHECK(!prototype->IsFinal()); |
| 3579 | CHECK(method->IsFinal()); |
| 3580 | CHECK(!method->IsAbstract()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3581 | |
| 3582 | // The proxy method doesn't have its own dex cache or dex file and so it steals those of its |
| 3583 | // interface prototype. The exception to this are Constructors and the Class of the Proxy itself. |
| 3584 | CHECK_EQ(prototype->GetDexCacheStrings(), method->GetDexCacheStrings()); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame^] | 3585 | CHECK(prototype->HasSameDexCacheResolvedMethods(method.Get())); |
| 3586 | CHECK(prototype->HasSameDexCacheResolvedTypes(method.Get())); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3587 | CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex()); |
| 3588 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3589 | MethodHelper mh(method); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3590 | MethodHelper mh2(prototype); |
| 3591 | CHECK_STREQ(method->GetName(), prototype->GetName()); |
| 3592 | CHECK_STREQ(method->GetShorty(), prototype->GetShorty()); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3593 | // More complex sanity - via dex cache |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3594 | CHECK_EQ(mh.GetReturnType(), mh2.GetReturnType()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3595 | } |
| 3596 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3597 | static bool CanWeInitializeClass(mirror::Class* klass, bool can_init_statics, |
| 3598 | bool can_init_parents) |
| 3599 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 3600 | if (can_init_statics && can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3601 | return true; |
| 3602 | } |
| 3603 | if (!can_init_statics) { |
| 3604 | // Check if there's a class initializer. |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 3605 | mirror::ArtMethod* clinit = klass->FindClassInitializer(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3606 | if (clinit != NULL) { |
| 3607 | return false; |
| 3608 | } |
| 3609 | // Check if there are encoded static values needing initialization. |
| 3610 | if (klass->NumStaticFields() != 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 3611 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3612 | DCHECK(dex_class_def != NULL); |
| 3613 | if (dex_class_def->static_values_off_ != 0) { |
| 3614 | return false; |
| 3615 | } |
| 3616 | } |
| 3617 | } |
| 3618 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
| 3619 | mirror::Class* super_class = klass->GetSuperClass(); |
| 3620 | if (!can_init_parents && !super_class->IsInitialized()) { |
| 3621 | return false; |
| 3622 | } else { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 3623 | if (!CanWeInitializeClass(super_class, can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3624 | return false; |
| 3625 | } |
| 3626 | } |
| 3627 | } |
| 3628 | return true; |
| 3629 | } |
| 3630 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3631 | bool ClassLinker::IsInitialized() const { |
| 3632 | return init_done_; |
| 3633 | } |
| 3634 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 3635 | bool ClassLinker::InitializeClass(Handle<mirror::Class> klass, bool can_init_statics, |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3636 | bool can_init_parents) { |
| 3637 | // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol |
| 3638 | |
| 3639 | // Are we already initialized and therefore done? |
| 3640 | // Note: we differ from the JLS here as we don't do this under the lock, this is benign as |
| 3641 | // an initialized class will never change its state. |
| 3642 | if (klass->IsInitialized()) { |
| 3643 | return true; |
| 3644 | } |
| 3645 | |
| 3646 | // Fast fail if initialization requires a full runtime. Not part of the JLS. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3647 | if (!CanWeInitializeClass(klass.Get(), can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3648 | return false; |
| 3649 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3650 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 3651 | Thread* self = Thread::Current(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3652 | uint64_t t0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3653 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3654 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3655 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3656 | // Re-check under the lock in case another thread initialized ahead of us. |
| 3657 | if (klass->IsInitialized()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3658 | return true; |
| 3659 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3660 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3661 | // 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] | 3662 | if (klass->IsErroneous()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3663 | ThrowEarlierClassFailure(klass.Get()); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3664 | return false; |
| 3665 | } |
| 3666 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3667 | CHECK(klass->IsResolved()) << PrettyClass(klass.Get()) << ": state=" << klass->GetStatus(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3668 | |
| 3669 | if (!klass->IsVerified()) { |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3670 | VerifyClass(klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3671 | if (!klass->IsVerified()) { |
| 3672 | // We failed to verify, expect either the klass to be erroneous or verification failed at |
| 3673 | // compile time. |
| 3674 | if (klass->IsErroneous()) { |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 3675 | CHECK(self->IsExceptionPending()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3676 | } else { |
| 3677 | CHECK(Runtime::Current()->IsCompiler()); |
| 3678 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 3679 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3680 | return false; |
| 3681 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3682 | } |
| 3683 | |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3684 | // If the class is kStatusInitializing, either this thread is |
| 3685 | // initializing higher up the stack or another thread has beat us |
| 3686 | // to initializing and we need to wait. Either way, this |
| 3687 | // invocation of InitializeClass will not be responsible for |
| 3688 | // running <clinit> and will return. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3689 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 3690 | // We caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 3691 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3692 | // Yes. That's fine. Return so we can continue initializing. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3693 | return true; |
| 3694 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3695 | // No. That's fine. Wait for another thread to finish initializing. |
| 3696 | return WaitForInitializeClass(klass, self, lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3697 | } |
| 3698 | |
| 3699 | if (!ValidateSuperClassDescriptors(klass)) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3700 | klass->SetStatus(mirror::Class::kStatusError, self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3701 | return false; |
| 3702 | } |
| 3703 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3704 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusVerified) << PrettyClass(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3705 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3706 | // From here out other threads may observe that we're initializing and so changes of state |
| 3707 | // require the a notification. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 3708 | klass->SetClinitThreadId(self->GetTid()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3709 | klass->SetStatus(mirror::Class::kStatusInitializing, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3710 | |
| 3711 | t0 = NanoTime(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3712 | } |
| 3713 | |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 3714 | // Initialize super classes, must be done while initializing for the JLS. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3715 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
| 3716 | mirror::Class* super_class = klass->GetSuperClass(); |
| 3717 | if (!super_class->IsInitialized()) { |
| 3718 | CHECK(!super_class->IsInterface()); |
| 3719 | CHECK(can_init_parents); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3720 | StackHandleScope<1> hs(self); |
| 3721 | Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class)); |
| 3722 | bool super_initialized = InitializeClass(handle_scope_super, can_init_statics, true); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3723 | if (!super_initialized) { |
| 3724 | // The super class was verified ahead of entering initializing, we should only be here if |
| 3725 | // the super class became erroneous due to initialization. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3726 | CHECK(handle_scope_super->IsErroneous() && self->IsExceptionPending()) |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3727 | << "Super class initialization failed for " |
| 3728 | << PrettyDescriptor(handle_scope_super.Get()) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3729 | << " that has unexpected status " << handle_scope_super->GetStatus() |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3730 | << "\nPending exception:\n" |
| 3731 | << (self->GetException(NULL) != NULL ? self->GetException(NULL)->Dump() : ""); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3732 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3733 | // Initialization failed because the super-class is erroneous. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3734 | klass->SetStatus(mirror::Class::kStatusError, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3735 | return false; |
| 3736 | } |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 3737 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3738 | } |
| 3739 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3740 | if (klass->NumStaticFields() > 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 3741 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3742 | CHECK(dex_class_def != NULL); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 3743 | const DexFile& dex_file = klass->GetDexFile(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3744 | StackHandleScope<2> hs(self); |
| 3745 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 3746 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3747 | EncodedStaticFieldValueIterator it(dex_file, &dex_cache, &class_loader, |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3748 | this, *dex_class_def); |
| 3749 | if (it.HasNext()) { |
| 3750 | CHECK(can_init_statics); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3751 | // We reordered the fields, so we need to be able to map the |
| 3752 | // field indexes to the right fields. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3753 | SafeMap<uint32_t, mirror::ArtField*> field_map; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3754 | ConstructFieldMap(dex_file, *dex_class_def, klass.Get(), field_map); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3755 | for (size_t i = 0; it.HasNext(); i++, it.Next()) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 3756 | if (Runtime::Current()->IsActiveTransaction()) { |
| 3757 | it.ReadValueToField<true>(field_map.Get(i)); |
| 3758 | } else { |
| 3759 | it.ReadValueToField<false>(field_map.Get(i)); |
| 3760 | } |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3761 | } |
| 3762 | } |
| 3763 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3764 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 3765 | mirror::ArtMethod* clinit = klass->FindClassInitializer(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3766 | if (clinit != NULL) { |
| 3767 | CHECK(can_init_statics); |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 3768 | JValue result; |
| 3769 | clinit->Invoke(self, NULL, 0, &result, "V"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3770 | } |
| 3771 | |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 3772 | uint64_t t1 = NanoTime(); |
| 3773 | |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 3774 | bool success = true; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3775 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3776 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3777 | |
| 3778 | if (self->IsExceptionPending()) { |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 3779 | WrapExceptionInInitializer(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3780 | klass->SetStatus(mirror::Class::kStatusError, self); |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 3781 | success = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3782 | } else { |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 3783 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 3784 | RuntimeStats* thread_stats = self->GetStats(); |
| 3785 | ++global_stats->class_init_count; |
| 3786 | ++thread_stats->class_init_count; |
| 3787 | global_stats->class_init_time_ns += (t1 - t0); |
| 3788 | thread_stats->class_init_time_ns += (t1 - t0); |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 3789 | // Set the class as initialized except if failed to initialize static fields. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3790 | klass->SetStatus(mirror::Class::kStatusInitialized, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3791 | if (VLOG_IS_ON(class_linker)) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 3792 | LOG(INFO) << "Initialized class " << klass->GetDescriptor() << " from " << |
| 3793 | klass->GetLocation(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3794 | } |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 3795 | // Opportunistically set static method trampolines to their destination. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3796 | FixupStaticTrampolines(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3797 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3798 | } |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 3799 | return success; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3800 | } |
| 3801 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 3802 | bool ClassLinker::WaitForInitializeClass(Handle<mirror::Class> klass, Thread* self, |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3803 | ObjectLock<mirror::Class>& lock) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 3804 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3805 | while (true) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3806 | self->AssertNoPendingException(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3807 | CHECK(!klass->IsInitialized()); |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 3808 | lock.WaitIgnoringInterrupts(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3809 | |
| 3810 | // When we wake up, repeat the test for init-in-progress. If |
| 3811 | // there's an exception pending (only possible if |
| 3812 | // "interruptShouldThrow" was set), bail out. |
| 3813 | if (self->IsExceptionPending()) { |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 3814 | WrapExceptionInInitializer(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3815 | klass->SetStatus(mirror::Class::kStatusError, self); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3816 | return false; |
| 3817 | } |
| 3818 | // Spurious wakeup? Go back to waiting. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3819 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3820 | continue; |
| 3821 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3822 | if (klass->GetStatus() == mirror::Class::kStatusVerified && Runtime::Current()->IsCompiler()) { |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 3823 | // Compile time initialization failed. |
| 3824 | return false; |
| 3825 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3826 | if (klass->IsErroneous()) { |
| 3827 | // The caller wants an exception, but it was thrown in a |
| 3828 | // different thread. Synthesize one here. |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 3829 | ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread", |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3830 | PrettyDescriptor(klass.Get()).c_str()); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3831 | return false; |
| 3832 | } |
| 3833 | if (klass->IsInitialized()) { |
| 3834 | return true; |
| 3835 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3836 | LOG(FATAL) << "Unexpected class status. " << PrettyClass(klass.Get()) << " is " |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3837 | << klass->GetStatus(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3838 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3839 | LOG(FATAL) << "Not Reached" << PrettyClass(klass.Get()); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 3840 | } |
| 3841 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 3842 | bool ClassLinker::ValidateSuperClassDescriptors(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3843 | if (klass->IsInterface()) { |
| 3844 | return true; |
| 3845 | } |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 3846 | // Begin with the methods local to the superclass. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3847 | StackHandleScope<2> hs(Thread::Current()); |
| 3848 | MethodHelper mh(hs.NewHandle<mirror::ArtMethod>(nullptr)); |
| 3849 | MethodHelper super_mh(hs.NewHandle<mirror::ArtMethod>(nullptr)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3850 | if (klass->HasSuperClass() && |
| 3851 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 3852 | for (int i = klass->GetSuperClass()->GetVTableLength() - 1; i >= 0; --i) { |
| 3853 | mh.ChangeMethod(klass->GetVTableEntry(i)); |
| 3854 | super_mh.ChangeMethod(klass->GetSuperClass()->GetVTableEntry(i)); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3855 | if (mh.GetMethod() != super_mh.GetMethod() && |
| 3856 | !mh.HasSameSignatureWithDifferentClassLoaders(&super_mh)) { |
| 3857 | ThrowLinkageError(klass.Get(), |
| 3858 | "Class %s method %s resolves differently in superclass %s", |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3859 | PrettyDescriptor(klass.Get()).c_str(), |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 3860 | PrettyMethod(mh.GetMethod()).c_str(), |
| 3861 | PrettyDescriptor(klass->GetSuperClass()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3862 | return false; |
| 3863 | } |
| 3864 | } |
| 3865 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 3866 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 3867 | if (klass->GetClassLoader() != klass->GetIfTable()->GetInterface(i)->GetClassLoader()) { |
| 3868 | uint32_t num_methods = klass->GetIfTable()->GetInterface(i)->NumVirtualMethods(); |
| 3869 | for (uint32_t j = 0; j < num_methods; ++j) { |
| 3870 | mh.ChangeMethod(klass->GetIfTable()->GetMethodArray(i)->GetWithoutChecks(j)); |
| 3871 | super_mh.ChangeMethod(klass->GetIfTable()->GetInterface(i)->GetVirtualMethod(j)); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3872 | if (mh.GetMethod() != super_mh.GetMethod() && |
| 3873 | !mh.HasSameSignatureWithDifferentClassLoaders(&super_mh)) { |
| 3874 | ThrowLinkageError(klass.Get(), |
| 3875 | "Class %s method %s resolves differently in interface %s", |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3876 | PrettyDescriptor(klass.Get()).c_str(), |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 3877 | PrettyMethod(mh.GetMethod()).c_str(), |
| 3878 | PrettyDescriptor(klass->GetIfTable()->GetInterface(i)).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3879 | return false; |
| 3880 | } |
| 3881 | } |
| 3882 | } |
| 3883 | } |
| 3884 | return true; |
| 3885 | } |
| 3886 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 3887 | bool ClassLinker::EnsureInitialized(Handle<mirror::Class> c, bool can_init_fields, |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3888 | bool can_init_parents) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 3889 | DCHECK(c.Get() != nullptr); |
| 3890 | const bool success = c->IsInitialized() || InitializeClass(c, can_init_fields, can_init_parents); |
| 3891 | if (!success && can_init_fields && can_init_parents) { |
| 3892 | CHECK(Thread::Current()->IsExceptionPending()) << PrettyClass(c.Get()); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 3893 | } |
| 3894 | return success; |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 3895 | } |
| 3896 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 3897 | 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] | 3898 | mirror::Class* c, |
| 3899 | SafeMap<uint32_t, mirror::ArtField*>& field_map) { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 3900 | const byte* class_data = dex_file.GetClassData(dex_class_def); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3901 | ClassDataItemIterator it(dex_file, class_data); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3902 | StackHandleScope<2> hs(Thread::Current()); |
| 3903 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(c->GetDexCache())); |
| 3904 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(c->GetClassLoader())); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3905 | CHECK(!kMovingFields); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3906 | for (size_t i = 0; it.HasNextStaticField(); i++, it.Next()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3907 | 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] | 3908 | } |
| 3909 | } |
| 3910 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3911 | void ClassLinker::FixupTemporaryDeclaringClass(mirror::Class* temp_class, mirror::Class* new_class) { |
| 3912 | mirror::ObjectArray<mirror::ArtField>* fields = new_class->GetIFields(); |
| 3913 | if (fields != nullptr) { |
| 3914 | for (int index = 0; index < fields->GetLength(); index ++) { |
| 3915 | if (fields->Get(index)->GetDeclaringClass() == temp_class) { |
| 3916 | fields->Get(index)->SetDeclaringClass(new_class); |
| 3917 | } |
| 3918 | } |
| 3919 | } |
| 3920 | |
| 3921 | fields = new_class->GetSFields(); |
| 3922 | if (fields != nullptr) { |
| 3923 | for (int index = 0; index < fields->GetLength(); index ++) { |
| 3924 | if (fields->Get(index)->GetDeclaringClass() == temp_class) { |
| 3925 | fields->Get(index)->SetDeclaringClass(new_class); |
| 3926 | } |
| 3927 | } |
| 3928 | } |
| 3929 | |
| 3930 | mirror::ObjectArray<mirror::ArtMethod>* methods = new_class->GetDirectMethods(); |
| 3931 | if (methods != nullptr) { |
| 3932 | for (int index = 0; index < methods->GetLength(); index ++) { |
| 3933 | if (methods->Get(index)->GetDeclaringClass() == temp_class) { |
| 3934 | methods->Get(index)->SetDeclaringClass(new_class); |
| 3935 | } |
| 3936 | } |
| 3937 | } |
| 3938 | |
| 3939 | methods = new_class->GetVirtualMethods(); |
| 3940 | if (methods != nullptr) { |
| 3941 | for (int index = 0; index < methods->GetLength(); index ++) { |
| 3942 | if (methods->Get(index)->GetDeclaringClass() == temp_class) { |
| 3943 | methods->Get(index)->SetDeclaringClass(new_class); |
| 3944 | } |
| 3945 | } |
| 3946 | } |
| 3947 | } |
| 3948 | |
| 3949 | bool ClassLinker::LinkClass(Thread* self, const char* descriptor, Handle<mirror::Class> klass, |
| 3950 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
| 3951 | mirror::Class** new_class) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3952 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3953 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3954 | if (!LinkSuperClass(klass)) { |
| 3955 | return false; |
| 3956 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3957 | if (!LinkMethods(self, klass, interfaces)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3958 | return false; |
| 3959 | } |
| 3960 | if (!LinkInstanceFields(klass)) { |
| 3961 | return false; |
| 3962 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3963 | size_t class_size; |
| 3964 | if (!LinkStaticFields(klass, &class_size)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3965 | return false; |
| 3966 | } |
| 3967 | CreateReferenceInstanceOffsets(klass); |
| 3968 | CreateReferenceStaticOffsets(klass); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3969 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3970 | |
| 3971 | if (!klass->IsTemp() || (!init_done_ && klass->GetClassSize() == class_size)) { |
| 3972 | // We don't need to retire this class as it has no embedded tables or it was created the |
| 3973 | // correct size during class linker initialization. |
| 3974 | CHECK_EQ(klass->GetClassSize(), class_size) << PrettyDescriptor(klass.Get()); |
| 3975 | |
| 3976 | if (klass->ShouldHaveEmbeddedImtAndVTable()) { |
| 3977 | klass->PopulateEmbeddedImtAndVTable(); |
| 3978 | } |
| 3979 | |
| 3980 | // This will notify waiters on klass that saw the not yet resolved |
| 3981 | // class in the class_table_ during EnsureResolved. |
| 3982 | klass->SetStatus(mirror::Class::kStatusResolved, self); |
| 3983 | *new_class = klass.Get(); |
| 3984 | } else { |
| 3985 | CHECK(!klass->IsResolved()); |
| 3986 | // Retire the temporary class and create the correctly sized resolved class. |
| 3987 | *new_class = klass->CopyOf(self, class_size); |
| 3988 | if (UNLIKELY(*new_class == NULL)) { |
| 3989 | CHECK(self->IsExceptionPending()); // Expect an OOME. |
| 3990 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 3991 | return false; |
| 3992 | } |
| 3993 | |
| 3994 | CHECK_EQ((*new_class)->GetClassSize(), class_size); |
| 3995 | StackHandleScope<1> hs(self); |
| 3996 | auto new_class_h = hs.NewHandleWrapper<mirror::Class>(new_class); |
| 3997 | ObjectLock<mirror::Class> lock(self, new_class_h); |
| 3998 | |
| 3999 | FixupTemporaryDeclaringClass(klass.Get(), new_class_h.Get()); |
| 4000 | |
| 4001 | mirror::Class* existing = UpdateClass(descriptor, new_class_h.Get(), Hash(descriptor)); |
| 4002 | CHECK(existing == NULL || existing == klass.Get()); |
| 4003 | |
| 4004 | // This will notify waiters on temp class that saw the not yet resolved class in the |
| 4005 | // class_table_ during EnsureResolved. |
| 4006 | klass->SetStatus(mirror::Class::kStatusRetired, self); |
| 4007 | |
| 4008 | CHECK_EQ(new_class_h->GetStatus(), mirror::Class::kStatusResolving); |
| 4009 | // This will notify waiters on new_class that saw the not yet resolved |
| 4010 | // class in the class_table_ during EnsureResolved. |
| 4011 | new_class_h->SetStatus(mirror::Class::kStatusResolved, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4012 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4013 | return true; |
| 4014 | } |
| 4015 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4016 | bool ClassLinker::LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4017 | CHECK_EQ(mirror::Class::kStatusIdx, klass->GetStatus()); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4018 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
| 4019 | uint16_t super_class_idx = class_def.superclass_idx_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4020 | if (super_class_idx != DexFile::kDexNoIndex16) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4021 | mirror::Class* super_class = ResolveType(dex_file, super_class_idx, klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4022 | if (super_class == NULL) { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 4023 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4024 | return false; |
| 4025 | } |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 4026 | // Verify |
| 4027 | if (!klass->CanAccess(super_class)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4028 | ThrowIllegalAccessError(klass.Get(), "Class %s extended by class %s is inaccessible", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4029 | PrettyDescriptor(super_class).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4030 | PrettyDescriptor(klass.Get()).c_str()); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 4031 | return false; |
| 4032 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4033 | CHECK(super_class->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4034 | klass->SetSuperClass(super_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4035 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4036 | const DexFile::TypeList* interfaces = dex_file.GetInterfacesList(class_def); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4037 | if (interfaces != NULL) { |
| 4038 | for (size_t i = 0; i < interfaces->Size(); i++) { |
| 4039 | uint16_t idx = interfaces->GetTypeItem(i).type_idx_; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4040 | mirror::Class* interface = ResolveType(dex_file, idx, klass.Get()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4041 | if (interface == NULL) { |
| 4042 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4043 | return false; |
| 4044 | } |
| 4045 | // Verify |
| 4046 | if (!klass->CanAccess(interface)) { |
| 4047 | // TODO: the RI seemed to ignore this in my testing. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4048 | ThrowIllegalAccessError(klass.Get(), "Interface %s implemented by class %s is inaccessible", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4049 | PrettyDescriptor(interface).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4050 | PrettyDescriptor(klass.Get()).c_str()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4051 | return false; |
| 4052 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4053 | } |
| 4054 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4055 | // Mark the class as loaded. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4056 | klass->SetStatus(mirror::Class::kStatusLoaded, NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4057 | return true; |
| 4058 | } |
| 4059 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4060 | bool ClassLinker::LinkSuperClass(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4061 | CHECK(!klass->IsPrimitive()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4062 | mirror::Class* super = klass->GetSuperClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4063 | if (klass.Get() == GetClassRoot(kJavaLangObject)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4064 | if (super != NULL) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4065 | ThrowClassFormatError(klass.Get(), "java.lang.Object must not have a superclass"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4066 | return false; |
| 4067 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4068 | return true; |
| 4069 | } |
| 4070 | if (super == NULL) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4071 | ThrowLinkageError(klass.Get(), "No superclass defined for class %s", |
| 4072 | PrettyDescriptor(klass.Get()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4073 | return false; |
| 4074 | } |
| 4075 | // Verify |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 4076 | if (super->IsFinal() || super->IsInterface()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4077 | ThrowIncompatibleClassChangeError(klass.Get(), "Superclass %s of %s is %s", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4078 | PrettyDescriptor(super).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4079 | PrettyDescriptor(klass.Get()).c_str(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4080 | super->IsFinal() ? "declared final" : "an interface"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4081 | return false; |
| 4082 | } |
| 4083 | if (!klass->CanAccess(super)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4084 | ThrowIllegalAccessError(klass.Get(), "Superclass %s is inaccessible to class %s", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4085 | PrettyDescriptor(super).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4086 | PrettyDescriptor(klass.Get()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4087 | return false; |
| 4088 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 4089 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4090 | // Inherit kAccClassIsFinalizable from the superclass in case this |
| 4091 | // class doesn't override finalize. |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 4092 | if (super->IsFinalizable()) { |
| 4093 | klass->SetFinalizable(); |
| 4094 | } |
| 4095 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 4096 | // Inherit reference flags (if any) from the superclass. |
| 4097 | int reference_flags = (super->GetAccessFlags() & kAccReferenceFlagsMask); |
| 4098 | if (reference_flags != 0) { |
| 4099 | klass->SetAccessFlags(klass->GetAccessFlags() | reference_flags); |
| 4100 | } |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 4101 | // Disallow custom direct subclasses of java.lang.ref.Reference. |
Elliott Hughes | bf61ba3 | 2011-10-11 10:53:09 -0700 | [diff] [blame] | 4102 | if (init_done_ && super == GetClassRoot(kJavaLangRefReference)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4103 | ThrowLinkageError(klass.Get(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4104 | "Class %s attempts to subclass java.lang.ref.Reference, which is not allowed", |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4105 | PrettyDescriptor(klass.Get()).c_str()); |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 4106 | return false; |
| 4107 | } |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 4108 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4109 | if (kIsDebugBuild) { |
| 4110 | // Ensure super classes are fully resolved prior to resolving fields.. |
| 4111 | while (super != NULL) { |
| 4112 | CHECK(super->IsResolved()); |
| 4113 | super = super->GetSuperClass(); |
| 4114 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4115 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4116 | return true; |
| 4117 | } |
| 4118 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4119 | // Populate the class vtable and itable. Compute return type indices. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4120 | bool ClassLinker::LinkMethods(Thread* self, Handle<mirror::Class> klass, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4121 | Handle<mirror::ObjectArray<mirror::Class>> interfaces) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4122 | if (klass->IsInterface()) { |
| 4123 | // No vtable. |
| 4124 | size_t count = klass->NumVirtualMethods(); |
| 4125 | if (!IsUint(16, count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4126 | ThrowClassFormatError(klass.Get(), "Too many methods on interface: %zd", count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4127 | return false; |
| 4128 | } |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 4129 | for (size_t i = 0; i < count; ++i) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4130 | klass->GetVirtualMethodDuringLinking(i)->SetMethodIndex(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4131 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4132 | // Link interface method tables |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4133 | return LinkInterfaceMethods(klass, interfaces); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4134 | } else { |
Elliott Hughes | bc258fa | 2011-10-06 14:45:21 -0700 | [diff] [blame] | 4135 | // Link virtual and interface method tables |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4136 | return LinkVirtualMethods(self, klass) && LinkInterfaceMethods(klass, interfaces); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4137 | } |
| 4138 | return true; |
| 4139 | } |
| 4140 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4141 | bool ClassLinker::LinkVirtualMethods(Thread* self, Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4142 | if (klass->HasSuperClass()) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4143 | uint32_t max_count = klass->NumVirtualMethods() + |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4144 | klass->GetSuperClass()->GetVTableLength(); |
| 4145 | size_t actual_count = klass->GetSuperClass()->GetVTableLength(); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 4146 | CHECK_LE(actual_count, max_count); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4147 | StackHandleScope<3> hs(self); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4148 | Handle<mirror::ObjectArray<mirror::ArtMethod>> vtable; |
| 4149 | mirror::Class* super_class = klass->GetSuperClass(); |
| 4150 | if (super_class->ShouldHaveEmbeddedImtAndVTable()) { |
| 4151 | vtable = hs.NewHandle(AllocArtMethodArray(self, max_count)); |
| 4152 | if (UNLIKELY(vtable.Get() == nullptr)) { |
| 4153 | CHECK(self->IsExceptionPending()); // OOME. |
| 4154 | return false; |
| 4155 | } |
| 4156 | int len = super_class->GetVTableLength(); |
Mingyao Yang | 1a12858 | 2014-07-22 17:33:25 -0700 | [diff] [blame] | 4157 | for (int i = 0; i < len; i++) { |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4158 | vtable->Set<false>(i, super_class->GetVTableEntry(i)); |
| 4159 | } |
| 4160 | } else { |
| 4161 | CHECK(super_class->GetVTable() != nullptr) << PrettyClass(super_class); |
| 4162 | vtable = hs.NewHandle(super_class->GetVTable()->CopyOf(self, max_count)); |
| 4163 | if (UNLIKELY(vtable.Get() == nullptr)) { |
| 4164 | CHECK(self->IsExceptionPending()); // OOME. |
| 4165 | return false; |
| 4166 | } |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4167 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4168 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4169 | // See if any of our virtual methods override the superclass. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4170 | MethodHelper local_mh(hs.NewHandle<mirror::ArtMethod>(nullptr)); |
| 4171 | MethodHelper super_mh(hs.NewHandle<mirror::ArtMethod>(nullptr)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4172 | for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4173 | mirror::ArtMethod* local_method = klass->GetVirtualMethodDuringLinking(i); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4174 | local_mh.ChangeMethod(local_method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4175 | size_t j = 0; |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 4176 | for (; j < actual_count; ++j) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4177 | mirror::ArtMethod* super_method = vtable->Get(j); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4178 | super_mh.ChangeMethod(super_method); |
Elliott Hughes | 3971737 | 2012-07-13 16:21:23 -0700 | [diff] [blame] | 4179 | if (local_mh.HasSameNameAndSignature(&super_mh)) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4180 | if (klass->CanAccessMember(super_method->GetDeclaringClass(), |
| 4181 | super_method->GetAccessFlags())) { |
Elliott Hughes | 3971737 | 2012-07-13 16:21:23 -0700 | [diff] [blame] | 4182 | if (super_method->IsFinal()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4183 | ThrowLinkageError(klass.Get(), "Method %s overrides final method in class %s", |
Elliott Hughes | 3971737 | 2012-07-13 16:21:23 -0700 | [diff] [blame] | 4184 | PrettyMethod(local_method).c_str(), |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4185 | super_method->GetDeclaringClassDescriptor()); |
Elliott Hughes | 3971737 | 2012-07-13 16:21:23 -0700 | [diff] [blame] | 4186 | return false; |
| 4187 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4188 | vtable->Set<false>(j, local_method); |
Elliott Hughes | 3971737 | 2012-07-13 16:21:23 -0700 | [diff] [blame] | 4189 | local_method->SetMethodIndex(j); |
| 4190 | break; |
| 4191 | } else { |
| 4192 | LOG(WARNING) << "Before Android 4.1, method " << PrettyMethod(local_method) |
| 4193 | << " would have incorrectly overridden the package-private method in " |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4194 | << PrettyDescriptor(super_method->GetDeclaringClassDescriptor()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4195 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4196 | } |
| 4197 | } |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 4198 | if (j == actual_count) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4199 | // Not overriding, append. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4200 | vtable->Set<false>(actual_count, local_method); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4201 | local_method->SetMethodIndex(actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4202 | actual_count += 1; |
| 4203 | } |
| 4204 | } |
| 4205 | if (!IsUint(16, actual_count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4206 | ThrowClassFormatError(klass.Get(), "Too many methods defined on class: %zd", actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4207 | return false; |
| 4208 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4209 | // Shrink vtable if possible |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4210 | CHECK_LE(actual_count, max_count); |
| 4211 | if (actual_count < max_count) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4212 | vtable.Assign(vtable->CopyOf(self, actual_count)); |
| 4213 | if (UNLIKELY(vtable.Get() == NULL)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4214 | CHECK(self->IsExceptionPending()); // OOME. |
| 4215 | return false; |
| 4216 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4217 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4218 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4219 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4220 | CHECK_EQ(klass.Get(), GetClassRoot(kJavaLangObject)); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 4221 | uint32_t num_virtual_methods = klass->NumVirtualMethods(); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 4222 | if (!IsUint(16, num_virtual_methods)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4223 | ThrowClassFormatError(klass.Get(), "Too many methods: %d", num_virtual_methods); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4224 | return false; |
| 4225 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4226 | StackHandleScope<1> hs(self); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 4227 | Handle<mirror::ObjectArray<mirror::ArtMethod>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4228 | vtable(hs.NewHandle(AllocArtMethodArray(self, num_virtual_methods))); |
| 4229 | if (UNLIKELY(vtable.Get() == NULL)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4230 | CHECK(self->IsExceptionPending()); // OOME. |
| 4231 | return false; |
| 4232 | } |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 4233 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4234 | mirror::ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4235 | vtable->Set<false>(i, virtual_method); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4236 | virtual_method->SetMethodIndex(i & 0xFFFF); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4237 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4238 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4239 | } |
| 4240 | return true; |
| 4241 | } |
| 4242 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4243 | bool ClassLinker::LinkInterfaceMethods(Handle<mirror::Class> klass, |
| 4244 | Handle<mirror::ObjectArray<mirror::Class>> interfaces) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4245 | Thread* const self = Thread::Current(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4246 | Runtime* const runtime = Runtime::Current(); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 4247 | // Set the imt table to be all conflicts by default. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4248 | klass->SetImTable(runtime->GetDefaultImt()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4249 | size_t super_ifcount; |
| 4250 | if (klass->HasSuperClass()) { |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4251 | super_ifcount = klass->GetSuperClass()->GetIfTableCount(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4252 | } else { |
| 4253 | super_ifcount = 0; |
| 4254 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4255 | uint32_t num_interfaces = |
| 4256 | interfaces.Get() == nullptr ? klass->NumDirectInterfaces() : interfaces->GetLength(); |
| 4257 | size_t ifcount = super_ifcount + num_interfaces; |
| 4258 | for (size_t i = 0; i < num_interfaces; i++) { |
| 4259 | mirror::Class* interface = |
| 4260 | interfaces.Get() == nullptr ? mirror::Class::GetDirectInterface(self, klass, i) : |
| 4261 | interfaces->Get(i); |
| 4262 | ifcount += interface->GetIfTableCount(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4263 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 4264 | if (ifcount == 0) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4265 | // Class implements no interfaces. |
| 4266 | DCHECK_EQ(klass->GetIfTableCount(), 0); |
| 4267 | DCHECK(klass->GetIfTable() == NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4268 | return true; |
| 4269 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4270 | if (ifcount == super_ifcount) { |
| 4271 | // Class implements same interfaces as parent, are any of these not marker interfaces? |
| 4272 | bool has_non_marker_interface = false; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4273 | mirror::IfTable* super_iftable = klass->GetSuperClass()->GetIfTable(); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4274 | for (size_t i = 0; i < ifcount; ++i) { |
| 4275 | if (super_iftable->GetMethodArrayCount(i) > 0) { |
| 4276 | has_non_marker_interface = true; |
| 4277 | break; |
| 4278 | } |
| 4279 | } |
| 4280 | if (!has_non_marker_interface) { |
| 4281 | // Class just inherits marker interfaces from parent so recycle parent's iftable. |
| 4282 | klass->SetIfTable(super_iftable); |
| 4283 | return true; |
| 4284 | } |
| 4285 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4286 | StackHandleScope<4> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4287 | Handle<mirror::IfTable> iftable(hs.NewHandle(AllocIfTable(self, ifcount))); |
| 4288 | if (UNLIKELY(iftable.Get() == NULL)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4289 | CHECK(self->IsExceptionPending()); // OOME. |
| 4290 | return false; |
| 4291 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4292 | if (super_ifcount != 0) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4293 | mirror::IfTable* super_iftable = klass->GetSuperClass()->GetIfTable(); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4294 | for (size_t i = 0; i < super_ifcount; i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4295 | mirror::Class* super_interface = super_iftable->GetInterface(i); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4296 | iftable->SetInterface(i, super_interface); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4297 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4298 | } |
| 4299 | // Flatten the interface inheritance hierarchy. |
| 4300 | size_t idx = super_ifcount; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4301 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4302 | mirror::Class* interface = |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4303 | interfaces.Get() == nullptr ? mirror::Class::GetDirectInterface(self, klass, i) : |
| 4304 | interfaces->Get(i); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4305 | DCHECK(interface != NULL); |
| 4306 | if (!interface->IsInterface()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4307 | ThrowIncompatibleClassChangeError(klass.Get(), "Class %s implements non-interface class %s", |
| 4308 | PrettyDescriptor(klass.Get()).c_str(), |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4309 | PrettyDescriptor(interface->GetDescriptor()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4310 | return false; |
| 4311 | } |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4312 | // Check if interface is already in iftable |
| 4313 | bool duplicate = false; |
| 4314 | for (size_t j = 0; j < idx; j++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4315 | mirror::Class* existing_interface = iftable->GetInterface(j); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4316 | if (existing_interface == interface) { |
| 4317 | duplicate = true; |
| 4318 | break; |
| 4319 | } |
| 4320 | } |
| 4321 | if (!duplicate) { |
| 4322 | // Add this non-duplicate interface. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4323 | iftable->SetInterface(idx++, interface); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4324 | // Add this interface's non-duplicate super-interfaces. |
| 4325 | for (int32_t j = 0; j < interface->GetIfTableCount(); j++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4326 | mirror::Class* super_interface = interface->GetIfTable()->GetInterface(j); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4327 | bool super_duplicate = false; |
| 4328 | for (size_t k = 0; k < idx; k++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4329 | mirror::Class* existing_interface = iftable->GetInterface(k); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4330 | if (existing_interface == super_interface) { |
| 4331 | super_duplicate = true; |
| 4332 | break; |
| 4333 | } |
| 4334 | } |
| 4335 | if (!super_duplicate) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4336 | iftable->SetInterface(idx++, super_interface); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4337 | } |
| 4338 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4339 | } |
| 4340 | } |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4341 | // Shrink iftable in case duplicates were found |
| 4342 | if (idx < ifcount) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4343 | iftable.Assign(down_cast<mirror::IfTable*>(iftable->CopyOf(self, idx * mirror::IfTable::kMax))); |
| 4344 | if (UNLIKELY(iftable.Get() == NULL)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4345 | CHECK(self->IsExceptionPending()); // OOME. |
| 4346 | return false; |
| 4347 | } |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4348 | ifcount = idx; |
| 4349 | } else { |
| 4350 | CHECK_EQ(idx, ifcount); |
| 4351 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4352 | klass->SetIfTable(iftable.Get()); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 4353 | |
| 4354 | // 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] | 4355 | if (klass->IsInterface()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4356 | return true; |
| 4357 | } |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 4358 | // Allocate imtable |
| 4359 | bool imtable_changed = false; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 4360 | Handle<mirror::ObjectArray<mirror::ArtMethod>> imtable( |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4361 | hs.NewHandle(AllocArtMethodArray(self, mirror::Class::kImtSize))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4362 | if (UNLIKELY(imtable.Get() == NULL)) { |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 4363 | CHECK(self->IsExceptionPending()); // OOME. |
| 4364 | return false; |
| 4365 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4366 | MethodHelper interface_mh(hs.NewHandle<mirror::ArtMethod>(nullptr)); |
| 4367 | MethodHelper vtable_mh(hs.NewHandle<mirror::ArtMethod>(nullptr)); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4368 | std::vector<mirror::ArtMethod*> miranda_list; |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4369 | for (size_t i = 0; i < ifcount; ++i) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4370 | size_t num_methods = iftable->GetInterface(i)->NumVirtualMethods(); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4371 | if (num_methods > 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4372 | StackHandleScope<2> hs(self); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 4373 | Handle<mirror::ObjectArray<mirror::ArtMethod>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4374 | method_array(hs.NewHandle(AllocArtMethodArray(self, num_methods))); |
| 4375 | if (UNLIKELY(method_array.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4376 | CHECK(self->IsExceptionPending()); // OOME. |
| 4377 | return false; |
| 4378 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4379 | iftable->SetMethodArray(i, method_array.Get()); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 4380 | Handle<mirror::ObjectArray<mirror::ArtMethod>> vtable( |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4381 | hs.NewHandle(klass->GetVTableDuringLinking())); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4382 | for (size_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4383 | mirror::ArtMethod* interface_method = iftable->GetInterface(i)->GetVirtualMethod(j); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4384 | interface_mh.ChangeMethod(interface_method); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4385 | int32_t k; |
| 4386 | // For each method listed in the interface's method list, find the |
| 4387 | // matching method in our class's method list. We want to favor the |
| 4388 | // subclass over the superclass, which just requires walking |
| 4389 | // back from the end of the vtable. (This only matters if the |
| 4390 | // superclass defines a private method and this class redefines |
| 4391 | // it -- otherwise it would use the same vtable slot. In .dex files |
| 4392 | // those don't end up in the virtual method table, so it shouldn't |
| 4393 | // matter which direction we go. We walk it backward anyway.) |
| 4394 | for (k = vtable->GetLength() - 1; k >= 0; --k) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4395 | mirror::ArtMethod* vtable_method = vtable->Get(k); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4396 | vtable_mh.ChangeMethod(vtable_method); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4397 | if (interface_mh.HasSameNameAndSignature(&vtable_mh)) { |
Elliott Hughes | 6824361 | 2013-02-22 17:16:07 -0800 | [diff] [blame] | 4398 | if (!vtable_method->IsAbstract() && !vtable_method->IsPublic()) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4399 | ThrowIllegalAccessError( |
| 4400 | klass.Get(), |
| 4401 | "Method '%s' implementing interface method '%s' is not public", |
| 4402 | PrettyMethod(vtable_method).c_str(), |
| 4403 | PrettyMethod(interface_method).c_str()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4404 | return false; |
| 4405 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4406 | method_array->Set<false>(j, vtable_method); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 4407 | // Place method in imt if entry is empty, place conflict otherwise. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4408 | uint32_t imt_index = interface_method->GetDexMethodIndex() % mirror::Class::kImtSize; |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 4409 | if (imtable->Get(imt_index) == NULL) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4410 | imtable->Set<false>(imt_index, vtable_method); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 4411 | imtable_changed = true; |
| 4412 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4413 | imtable->Set<false>(imt_index, runtime->GetImtConflictMethod()); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 4414 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4415 | break; |
| 4416 | } |
| 4417 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4418 | if (k < 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4419 | StackHandleScope<1> hs(self); |
| 4420 | auto miranda_method = hs.NewHandle<mirror::ArtMethod>(nullptr); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4421 | for (mirror::ArtMethod* mir_method : miranda_list) { |
| 4422 | vtable_mh.ChangeMethod(mir_method); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4423 | if (interface_mh.HasSameNameAndSignature(&vtable_mh)) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4424 | miranda_method.Assign(mir_method); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4425 | break; |
| 4426 | } |
| 4427 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4428 | if (miranda_method.Get() == NULL) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4429 | // Point the interface table at a phantom slot. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4430 | miranda_method.Assign(down_cast<mirror::ArtMethod*>(interface_method->Clone(self))); |
| 4431 | if (UNLIKELY(miranda_method.Get() == NULL)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4432 | CHECK(self->IsExceptionPending()); // OOME. |
| 4433 | return false; |
| 4434 | } |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4435 | // TODO: If a methods move then the miranda_list may hold stale references. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4436 | miranda_list.push_back(miranda_method.Get()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4437 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4438 | method_array->Set<false>(j, miranda_method.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4439 | } |
| 4440 | } |
| 4441 | } |
| 4442 | } |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 4443 | if (imtable_changed) { |
| 4444 | // Fill in empty entries in interface method table with conflict. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4445 | mirror::ArtMethod* imt_conflict_method = runtime->GetImtConflictMethod(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4446 | for (size_t i = 0; i < mirror::Class::kImtSize; i++) { |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 4447 | if (imtable->Get(i) == NULL) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4448 | imtable->Set<false>(i, imt_conflict_method); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 4449 | } |
| 4450 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4451 | klass->SetImTable(imtable.Get()); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 4452 | } |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 4453 | if (!miranda_list.empty()) { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 4454 | int old_method_count = klass->NumVirtualMethods(); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 4455 | int new_method_count = old_method_count + miranda_list.size(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4456 | mirror::ObjectArray<mirror::ArtMethod>* virtuals; |
| 4457 | if (old_method_count == 0) { |
| 4458 | virtuals = AllocArtMethodArray(self, new_method_count); |
| 4459 | } else { |
| 4460 | virtuals = klass->GetVirtualMethods()->CopyOf(self, new_method_count); |
| 4461 | } |
| 4462 | if (UNLIKELY(virtuals == NULL)) { |
| 4463 | CHECK(self->IsExceptionPending()); // OOME. |
| 4464 | return false; |
| 4465 | } |
| 4466 | klass->SetVirtualMethods(virtuals); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4467 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4468 | StackHandleScope<1> hs(self); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 4469 | Handle<mirror::ObjectArray<mirror::ArtMethod>> vtable( |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4470 | hs.NewHandle(klass->GetVTableDuringLinking())); |
| 4471 | CHECK(vtable.Get() != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4472 | int old_vtable_count = vtable->GetLength(); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 4473 | int new_vtable_count = old_vtable_count + miranda_list.size(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4474 | vtable.Assign(vtable->CopyOf(self, new_vtable_count)); |
| 4475 | if (UNLIKELY(vtable.Get() == NULL)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4476 | CHECK(self->IsExceptionPending()); // OOME. |
| 4477 | return false; |
| 4478 | } |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 4479 | for (size_t i = 0; i < miranda_list.size(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4480 | mirror::ArtMethod* method = miranda_list[i]; |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 4481 | // Leave the declaring class alone as type indices are relative to it |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 4482 | method->SetAccessFlags(method->GetAccessFlags() | kAccMiranda); |
| 4483 | method->SetMethodIndex(0xFFFF & (old_vtable_count + i)); |
| 4484 | klass->SetVirtualMethod(old_method_count + i, method); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4485 | vtable->Set<false>(old_vtable_count + i, method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4486 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4487 | // TODO: do not assign to the vtable field until it is fully constructed. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4488 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4489 | } |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 4490 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4491 | mirror::ObjectArray<mirror::ArtMethod>* vtable = klass->GetVTableDuringLinking(); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 4492 | for (int i = 0; i < vtable->GetLength(); ++i) { |
| 4493 | CHECK(vtable->Get(i) != NULL); |
| 4494 | } |
| 4495 | |
| 4496 | // klass->DumpClass(std::cerr, Class::kDumpClassFullDetail); |
| 4497 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4498 | return true; |
| 4499 | } |
| 4500 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4501 | bool ClassLinker::LinkInstanceFields(Handle<mirror::Class> klass) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4502 | CHECK(klass.Get() != NULL); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4503 | return LinkFields(klass, false, nullptr); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 4504 | } |
| 4505 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4506 | bool ClassLinker::LinkStaticFields(Handle<mirror::Class> klass, size_t* class_size) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4507 | CHECK(klass.Get() != NULL); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4508 | return LinkFields(klass, true, class_size); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 4509 | } |
| 4510 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4511 | struct LinkFieldsComparator { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4512 | explicit LinkFieldsComparator() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 4513 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4514 | // No thread safety analysis as will be called from STL. Checked lock held in constructor. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 4515 | bool operator()(mirror::ArtField* field1, mirror::ArtField* field2) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4516 | NO_THREAD_SAFETY_ANALYSIS { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4517 | // First come reference fields, then 64-bit, and finally 32-bit |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 4518 | Primitive::Type type1 = field1->GetTypeAsPrimitiveType(); |
| 4519 | Primitive::Type type2 = field2->GetTypeAsPrimitiveType(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 4520 | if (type1 != type2) { |
| 4521 | bool is_primitive1 = type1 != Primitive::kPrimNot; |
| 4522 | bool is_primitive2 = type2 != Primitive::kPrimNot; |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4523 | bool is64bit1 = is_primitive1 && (type1 == Primitive::kPrimLong || |
| 4524 | type1 == Primitive::kPrimDouble); |
| 4525 | bool is64bit2 = is_primitive2 && (type2 == Primitive::kPrimLong || |
| 4526 | type2 == Primitive::kPrimDouble); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 4527 | int order1 = !is_primitive1 ? 0 : (is64bit1 ? 1 : 2); |
| 4528 | int order2 = !is_primitive2 ? 0 : (is64bit2 ? 1 : 2); |
| 4529 | if (order1 != order2) { |
| 4530 | return order1 < order2; |
| 4531 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4532 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4533 | // same basic group? then sort by string. |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 4534 | return strcmp(field1->GetName(), field2->GetName()) < 0; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4535 | } |
| 4536 | }; |
| 4537 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4538 | bool ClassLinker::LinkFields(Handle<mirror::Class> klass, bool is_static, size_t* class_size) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4539 | size_t num_fields = |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 4540 | is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4541 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4542 | mirror::ObjectArray<mirror::ArtField>* fields = |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 4543 | is_static ? klass->GetSFields() : klass->GetIFields(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4544 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4545 | // Initialize field_offset |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 4546 | MemberOffset field_offset(0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 4547 | if (is_static) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4548 | uint32_t base = sizeof(mirror::Class); // Static fields come after the class. |
| 4549 | if (klass->ShouldHaveEmbeddedImtAndVTable()) { |
| 4550 | // Static fields come after the embedded tables. |
| 4551 | base = mirror::Class::ComputeClassSize(true, klass->GetVTableDuringLinking()->GetLength(), |
| 4552 | 0, 0, 0); |
| 4553 | } |
| 4554 | field_offset = MemberOffset(base); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 4555 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4556 | mirror::Class* super_class = klass->GetSuperClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4557 | if (super_class != NULL) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4558 | CHECK(super_class->IsResolved()) |
| 4559 | << PrettyClass(klass.Get()) << " " << PrettyClass(super_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4560 | field_offset = MemberOffset(super_class->GetObjectSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4561 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4562 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4563 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4564 | CHECK_EQ(num_fields == 0, fields == NULL) << PrettyClass(klass.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4565 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4566 | // we want a relatively stable order so that adding new fields |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 4567 | // minimizes disruption of C++ version such as Class and Method. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4568 | std::deque<mirror::ArtField*> grouped_and_sorted_fields; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4569 | for (size_t i = 0; i < num_fields; i++) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4570 | mirror::ArtField* f = fields->Get(i); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4571 | CHECK(f != NULL) << PrettyClass(klass.Get()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4572 | grouped_and_sorted_fields.push_back(f); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4573 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4574 | std::sort(grouped_and_sorted_fields.begin(), grouped_and_sorted_fields.end(), |
| 4575 | LinkFieldsComparator()); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4576 | |
| 4577 | // References should be at the front. |
| 4578 | size_t current_field = 0; |
| 4579 | size_t num_reference_fields = 0; |
| 4580 | for (; current_field < num_fields; current_field++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4581 | mirror::ArtField* field = grouped_and_sorted_fields.front(); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 4582 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 4583 | bool isPrimitive = type != Primitive::kPrimNot; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4584 | if (isPrimitive) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 4585 | break; // past last reference, move on to the next phase |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4586 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4587 | grouped_and_sorted_fields.pop_front(); |
| 4588 | num_reference_fields++; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4589 | fields->Set<false>(current_field, field); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4590 | field->SetOffset(field_offset); |
| 4591 | field_offset = MemberOffset(field_offset.Uint32Value() + sizeof(uint32_t)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4592 | } |
| 4593 | |
| 4594 | // Now we want to pack all of the double-wide fields together. If |
| 4595 | // we're not aligned, though, we want to shuffle one 32-bit field |
| 4596 | // 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] | 4597 | if (current_field != num_fields && !IsAligned<8>(field_offset.Uint32Value())) { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4598 | for (size_t i = 0; i < grouped_and_sorted_fields.size(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4599 | mirror::ArtField* field = grouped_and_sorted_fields[i]; |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 4600 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4601 | CHECK(type != Primitive::kPrimNot) << PrettyField(field); // should be primitive types |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 4602 | if (type == Primitive::kPrimLong || type == Primitive::kPrimDouble) { |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4603 | continue; |
| 4604 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4605 | fields->Set<false>(current_field++, field); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4606 | field->SetOffset(field_offset); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4607 | // drop the consumed field |
| 4608 | grouped_and_sorted_fields.erase(grouped_and_sorted_fields.begin() + i); |
| 4609 | break; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4610 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4611 | // whether we found a 32-bit field for padding or not, we advance |
| 4612 | field_offset = MemberOffset(field_offset.Uint32Value() + sizeof(uint32_t)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4613 | } |
| 4614 | |
| 4615 | // Alignment is good, shuffle any double-wide fields forward, and |
| 4616 | // finish assigning field offsets to all fields. |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4617 | DCHECK(current_field == num_fields || IsAligned<8>(field_offset.Uint32Value())) |
| 4618 | << PrettyClass(klass.Get()); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4619 | while (!grouped_and_sorted_fields.empty()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4620 | mirror::ArtField* field = grouped_and_sorted_fields.front(); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4621 | grouped_and_sorted_fields.pop_front(); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 4622 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4623 | CHECK(type != Primitive::kPrimNot) << PrettyField(field); // should be primitive types |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4624 | fields->Set<false>(current_field, field); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4625 | field->SetOffset(field_offset); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4626 | field_offset = MemberOffset(field_offset.Uint32Value() + |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 4627 | ((type == Primitive::kPrimLong || type == Primitive::kPrimDouble) |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4628 | ? sizeof(uint64_t) |
| 4629 | : sizeof(uint32_t))); |
| 4630 | current_field++; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4631 | } |
| 4632 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 4633 | // We lie to the GC about the java.lang.ref.Reference.referent field, so it doesn't scan it. |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4634 | if (!is_static && klass->DescriptorEquals("Ljava/lang/ref/Reference;")) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 4635 | // We know there are no non-reference fields in the Reference classes, and we know |
| 4636 | // that 'referent' is alphabetically last, so this is easy... |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4637 | CHECK_EQ(num_reference_fields, num_fields) << PrettyClass(klass.Get()); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 4638 | CHECK_STREQ(fields->Get(num_fields - 1)->GetName(), "referent") << PrettyClass(klass.Get()); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 4639 | --num_reference_fields; |
| 4640 | } |
| 4641 | |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4642 | if (kIsDebugBuild) { |
| 4643 | // Make sure that all reference fields appear before |
| 4644 | // non-reference fields, and all double-wide fields are aligned. |
| 4645 | bool seen_non_ref = false; |
| 4646 | for (size_t i = 0; i < num_fields; i++) { |
| 4647 | mirror::ArtField* field = fields->Get(i); |
| 4648 | if (false) { // enable to debug field layout |
| 4649 | LOG(INFO) << "LinkFields: " << (is_static ? "static" : "instance") |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4650 | << " class=" << PrettyClass(klass.Get()) |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4651 | << " field=" << PrettyField(field) |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 4652 | << " offset=" |
| 4653 | << field->GetField32(MemberOffset(mirror::ArtField::OffsetOffset())); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4654 | } |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 4655 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4656 | bool is_primitive = type != Primitive::kPrimNot; |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4657 | if (klass->DescriptorEquals("Ljava/lang/ref/Reference;") && |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 4658 | strcmp("referent", field->GetName()) == 0) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4659 | is_primitive = true; // We lied above, so we have to expect a lie here. |
| 4660 | } |
| 4661 | if (is_primitive) { |
| 4662 | if (!seen_non_ref) { |
| 4663 | seen_non_ref = true; |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4664 | DCHECK_EQ(num_reference_fields, i) << PrettyField(field); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4665 | } |
| 4666 | } else { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4667 | DCHECK(!seen_non_ref) << PrettyField(field); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4668 | } |
| 4669 | } |
| 4670 | if (!seen_non_ref) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4671 | DCHECK_EQ(num_fields, num_reference_fields) << PrettyClass(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4672 | } |
| 4673 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4674 | |
| 4675 | size_t size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4676 | // Update klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 4677 | if (is_static) { |
| 4678 | klass->SetNumReferenceStaticFields(num_reference_fields); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4679 | *class_size = size; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 4680 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4681 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 4682 | if (!klass->IsVariableSize()) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4683 | DCHECK_GE(size, sizeof(mirror::Object)) << klass->GetDescriptor(); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 4684 | size_t previous_size = klass->GetObjectSize(); |
| 4685 | if (previous_size != 0) { |
| 4686 | // Make sure that we didn't originally have an incorrect size. |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4687 | CHECK_EQ(previous_size, size) << klass->GetDescriptor(); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 4688 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4689 | klass->SetObjectSize(size); |
| 4690 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4691 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4692 | return true; |
| 4693 | } |
| 4694 | |
| 4695 | // Set the bitmap of reference offsets, refOffsets, from the ifields |
| 4696 | // list. |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4697 | void ClassLinker::CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4698 | uint32_t reference_offsets = 0; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4699 | mirror::Class* super_class = klass->GetSuperClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4700 | if (super_class != NULL) { |
| 4701 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 4702 | // If our superclass overflowed, we don't stand a chance. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4703 | if (reference_offsets == CLASS_WALK_SUPER) { |
| 4704 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 4705 | return; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4706 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4707 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 4708 | CreateReferenceOffsets(klass, false, reference_offsets); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 4709 | } |
| 4710 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4711 | void ClassLinker::CreateReferenceStaticOffsets(Handle<mirror::Class> klass) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 4712 | CreateReferenceOffsets(klass, true, 0); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 4713 | } |
| 4714 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4715 | void ClassLinker::CreateReferenceOffsets(Handle<mirror::Class> klass, bool is_static, |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4716 | uint32_t reference_offsets) { |
| 4717 | size_t num_reference_fields = |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 4718 | is_static ? klass->NumReferenceStaticFieldsDuringLinking() |
| 4719 | : klass->NumReferenceInstanceFieldsDuringLinking(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 4720 | mirror::ObjectArray<mirror::ArtField>* fields = |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 4721 | is_static ? klass->GetSFields() : klass->GetIFields(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 4722 | // All of the fields that contain object references are guaranteed |
| 4723 | // to be at the beginning of the fields list. |
| 4724 | for (size_t i = 0; i < num_reference_fields; ++i) { |
| 4725 | // Note that byte_offset is the offset from the beginning of |
| 4726 | // object, not the offset into instance data |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 4727 | mirror::ArtField* field = fields->Get(i); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4728 | MemberOffset byte_offset = field->GetOffsetDuringLinking(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4729 | CHECK_EQ(byte_offset.Uint32Value() & (CLASS_OFFSET_ALIGNMENT - 1), 0U); |
| 4730 | if (CLASS_CAN_ENCODE_OFFSET(byte_offset.Uint32Value())) { |
| 4731 | uint32_t new_bit = CLASS_BIT_FROM_OFFSET(byte_offset.Uint32Value()); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 4732 | CHECK_NE(new_bit, 0U); |
| 4733 | reference_offsets |= new_bit; |
| 4734 | } else { |
| 4735 | reference_offsets = CLASS_WALK_SUPER; |
| 4736 | break; |
| 4737 | } |
| 4738 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4739 | // Update fields in klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 4740 | if (is_static) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4741 | klass->SetReferenceStaticOffsets(reference_offsets); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 4742 | } else { |
| 4743 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4744 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4745 | } |
| 4746 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4747 | mirror::String* ClassLinker::ResolveString(const DexFile& dex_file, uint32_t string_idx, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4748 | Handle<mirror::DexCache> dex_cache) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4749 | DCHECK(dex_cache.Get() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4750 | mirror::String* resolved = dex_cache->GetResolvedString(string_idx); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4751 | if (resolved != NULL) { |
| 4752 | return resolved; |
| 4753 | } |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 4754 | uint32_t utf16_length; |
| 4755 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4756 | mirror::String* string = intern_table_->InternStrong(utf16_length, utf8_data); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4757 | dex_cache->SetResolvedString(string_idx, string); |
| 4758 | return string; |
| 4759 | } |
| 4760 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4761 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, uint16_t type_idx, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 4762 | mirror::Class* referrer) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4763 | StackHandleScope<2> hs(Thread::Current()); |
| 4764 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 4765 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4766 | return ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 4767 | } |
| 4768 | |
| 4769 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, uint16_t type_idx, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4770 | Handle<mirror::DexCache> dex_cache, |
| 4771 | Handle<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4772 | DCHECK(dex_cache.Get() != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4773 | mirror::Class* resolved = dex_cache->GetResolvedType(type_idx); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4774 | if (resolved == NULL) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 4775 | Thread* self = Thread::Current(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 4776 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 4777 | resolved = FindClass(self, descriptor, class_loader); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4778 | if (resolved != NULL) { |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 4779 | // TODO: we used to throw here if resolved's class loader was not the |
| 4780 | // boot class loader. This was to permit different classes with the |
| 4781 | // same name to be loaded simultaneously by different loaders |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4782 | dex_cache->SetResolvedType(type_idx, resolved); |
| 4783 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4784 | CHECK(self->IsExceptionPending()) |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 4785 | << "Expected pending exception for failed resolution of: " << descriptor; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4786 | // Convert a ClassNotFoundException to a NoClassDefFoundError. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4787 | StackHandleScope<1> hs(self); |
| 4788 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException(nullptr))); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4789 | if (cause->InstanceOf(GetClassRoot(kJavaLangClassNotFoundException))) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4790 | DCHECK(resolved == NULL); // No Handle needed to preserve resolved. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 4791 | self->ClearException(); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4792 | ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4793 | self->GetException(NULL)->SetCause(cause.Get()); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4794 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4795 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4796 | } |
Brian Carlstrom | abcf7ae | 2013-09-23 22:19:52 -0700 | [diff] [blame] | 4797 | DCHECK((resolved == NULL) || resolved->IsResolved() || resolved->IsErroneous()) |
| 4798 | << PrettyDescriptor(resolved) << " " << resolved->GetStatus(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4799 | return resolved; |
| 4800 | } |
| 4801 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4802 | mirror::ArtMethod* ClassLinker::ResolveMethod(const DexFile& dex_file, uint32_t method_idx, |
| 4803 | Handle<mirror::DexCache> dex_cache, |
| 4804 | Handle<mirror::ClassLoader> class_loader, |
| 4805 | Handle<mirror::ArtMethod> referrer, |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 4806 | InvokeType type) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4807 | DCHECK(dex_cache.Get() != NULL); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4808 | // Check for hit in the dex cache. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4809 | mirror::ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4810 | if (resolved != nullptr && !resolved->IsRuntimeMethod()) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4811 | return resolved; |
| 4812 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4813 | // Fail, get the declaring class. |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4814 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4815 | 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] | 4816 | if (klass == NULL) { |
Elliott Hughes | cc5f9a9 | 2011-09-28 19:17:29 -0700 | [diff] [blame] | 4817 | DCHECK(Thread::Current()->IsExceptionPending()); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4818 | return NULL; |
| 4819 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4820 | // Scan using method_idx, this saves string compares but will only hit for matching dex |
| 4821 | // caches/files. |
| 4822 | switch (type) { |
| 4823 | case kDirect: // Fall-through. |
| 4824 | case kStatic: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4825 | resolved = klass->FindDirectMethod(dex_cache.Get(), method_idx); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4826 | break; |
| 4827 | case kInterface: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4828 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx); |
Ian Rogers | 4c5b265 | 2012-12-15 21:36:16 -0800 | [diff] [blame] | 4829 | DCHECK(resolved == NULL || resolved->GetDeclaringClass()->IsInterface()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4830 | break; |
| 4831 | case kSuper: // Fall-through. |
| 4832 | case kVirtual: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4833 | resolved = klass->FindVirtualMethod(dex_cache.Get(), method_idx); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4834 | break; |
| 4835 | default: |
| 4836 | LOG(FATAL) << "Unreachable - invocation type: " << type; |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 4837 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4838 | if (resolved == NULL) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4839 | // Search by name, which works across dex files. |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4840 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 4841 | const Signature signature = dex_file.GetMethodSignature(method_id); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4842 | switch (type) { |
| 4843 | case kDirect: // Fall-through. |
| 4844 | case kStatic: |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 4845 | resolved = klass->FindDirectMethod(name, signature); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4846 | break; |
| 4847 | case kInterface: |
| 4848 | resolved = klass->FindInterfaceMethod(name, signature); |
Ian Rogers | 4c5b265 | 2012-12-15 21:36:16 -0800 | [diff] [blame] | 4849 | DCHECK(resolved == NULL || resolved->GetDeclaringClass()->IsInterface()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4850 | break; |
| 4851 | case kSuper: // Fall-through. |
| 4852 | case kVirtual: |
| 4853 | resolved = klass->FindVirtualMethod(name, signature); |
| 4854 | break; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4855 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4856 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4857 | if (resolved != NULL) { |
| 4858 | // We found a method, check for incompatible class changes. |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 4859 | if (resolved->CheckIncompatibleClassChange(type)) { |
| 4860 | resolved = NULL; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4861 | } |
| 4862 | } |
| 4863 | if (resolved != NULL) { |
| 4864 | // Be a good citizen and update the dex cache to speed subsequent calls. |
| 4865 | dex_cache->SetResolvedMethod(method_idx, resolved); |
| 4866 | return resolved; |
| 4867 | } else { |
jeffhao | c0228b8 | 2012-08-29 18:15:05 -0700 | [diff] [blame] | 4868 | // We failed to find the method which means either an access error, an incompatible class |
| 4869 | // 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] | 4870 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 4871 | const Signature signature = dex_file.GetMethodSignature(method_id); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4872 | switch (type) { |
| 4873 | case kDirect: |
| 4874 | case kStatic: |
| 4875 | resolved = klass->FindVirtualMethod(name, signature); |
jeffhao | c0228b8 | 2012-08-29 18:15:05 -0700 | [diff] [blame] | 4876 | break; |
| 4877 | case kInterface: |
| 4878 | case kVirtual: |
| 4879 | case kSuper: |
| 4880 | resolved = klass->FindDirectMethod(name, signature); |
| 4881 | break; |
| 4882 | } |
| 4883 | |
| 4884 | // If we found something, check that it can be accessed by the referrer. |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4885 | if (resolved != NULL && referrer.Get() != NULL) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4886 | mirror::Class* methods_class = resolved->GetDeclaringClass(); |
| 4887 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
jeffhao | c0228b8 | 2012-08-29 18:15:05 -0700 | [diff] [blame] | 4888 | if (!referring_class->CanAccess(methods_class)) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 4889 | ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, methods_class, |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 4890 | resolved, type); |
jeffhao | c0228b8 | 2012-08-29 18:15:05 -0700 | [diff] [blame] | 4891 | return NULL; |
| 4892 | } else if (!referring_class->CanAccessMember(methods_class, |
| 4893 | resolved->GetAccessFlags())) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 4894 | ThrowIllegalAccessErrorMethod(referring_class, resolved); |
jeffhao | c0228b8 | 2012-08-29 18:15:05 -0700 | [diff] [blame] | 4895 | return NULL; |
| 4896 | } |
| 4897 | } |
| 4898 | |
| 4899 | // Otherwise, throw an IncompatibleClassChangeError if we found something, and check interface |
| 4900 | // methods and throw if we find the method there. If we find nothing, throw a NoSuchMethodError. |
| 4901 | switch (type) { |
| 4902 | case kDirect: |
| 4903 | case kStatic: |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4904 | if (resolved != NULL) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4905 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer.Get()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4906 | } else { |
| 4907 | resolved = klass->FindInterfaceMethod(name, signature); |
| 4908 | if (resolved != NULL) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4909 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer.Get()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4910 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4911 | ThrowNoSuchMethodError(type, klass, name, signature); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4912 | } |
| 4913 | } |
| 4914 | break; |
| 4915 | case kInterface: |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4916 | if (resolved != NULL) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4917 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer.Get()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4918 | } else { |
| 4919 | resolved = klass->FindVirtualMethod(name, signature); |
| 4920 | if (resolved != NULL) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4921 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer.Get()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4922 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4923 | ThrowNoSuchMethodError(type, klass, name, signature); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4924 | } |
| 4925 | } |
| 4926 | break; |
| 4927 | case kSuper: |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4928 | ThrowNoSuchMethodError(type, klass, name, signature); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4929 | break; |
| 4930 | case kVirtual: |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4931 | if (resolved != NULL) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4932 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer.Get()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4933 | } else { |
| 4934 | resolved = klass->FindInterfaceMethod(name, signature); |
| 4935 | if (resolved != NULL) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4936 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer.Get()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4937 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4938 | ThrowNoSuchMethodError(type, klass, name, signature); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 4939 | } |
| 4940 | } |
| 4941 | break; |
| 4942 | } |
| 4943 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4944 | return NULL; |
| 4945 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4946 | } |
| 4947 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4948 | mirror::ArtField* ClassLinker::ResolveField(const DexFile& dex_file, uint32_t field_idx, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4949 | Handle<mirror::DexCache> dex_cache, |
| 4950 | Handle<mirror::ClassLoader> class_loader, |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 4951 | bool is_static) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4952 | DCHECK(dex_cache.Get() != nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4953 | mirror::ArtField* resolved = dex_cache->GetResolvedField(field_idx); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame^] | 4954 | if (resolved != nullptr) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4955 | return resolved; |
| 4956 | } |
| 4957 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4958 | Thread* const self = Thread::Current(); |
| 4959 | StackHandleScope<1> hs(self); |
| 4960 | Handle<mirror::Class> klass( |
| 4961 | hs.NewHandle(ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader))); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame^] | 4962 | if (klass.Get() == nullptr) { |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 4963 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame^] | 4964 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4965 | } |
| 4966 | |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 4967 | if (is_static) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4968 | resolved = mirror::Class::FindStaticField(self, klass, dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 4969 | } else { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4970 | resolved = klass->FindInstanceField(dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 4971 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4972 | |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame^] | 4973 | if (resolved == nullptr) { |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4974 | const char* name = dex_file.GetFieldName(field_id); |
| 4975 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 4976 | if (is_static) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4977 | resolved = mirror::Class::FindStaticField(self, klass, name, type); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4978 | } else { |
| 4979 | resolved = klass->FindInstanceField(name, type); |
| 4980 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame^] | 4981 | if (resolved == nullptr) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4982 | ThrowNoSuchFieldError(is_static ? "static " : "instance ", klass.Get(), type, name); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4983 | return NULL; |
| 4984 | } |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 4985 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 4986 | dex_cache->SetResolvedField(field_idx, resolved); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 4987 | return resolved; |
| 4988 | } |
| 4989 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4990 | mirror::ArtField* ClassLinker::ResolveFieldJLS(const DexFile& dex_file, |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4991 | uint32_t field_idx, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4992 | Handle<mirror::DexCache> dex_cache, |
| 4993 | Handle<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4994 | DCHECK(dex_cache.Get() != nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4995 | mirror::ArtField* resolved = dex_cache->GetResolvedField(field_idx); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame^] | 4996 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 4997 | return resolved; |
| 4998 | } |
| 4999 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5000 | Thread* self = Thread::Current(); |
| 5001 | StackHandleScope<1> hs(self); |
| 5002 | Handle<mirror::Class> klass( |
| 5003 | hs.NewHandle(ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader))); |
| 5004 | if (klass.Get() == NULL) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5005 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5006 | return NULL; |
| 5007 | } |
| 5008 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 5009 | StringPiece name(dex_file.StringDataByIdx(field_id.name_idx_)); |
| 5010 | StringPiece type(dex_file.StringDataByIdx( |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5011 | dex_file.GetTypeId(field_id.type_idx_).descriptor_idx_)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5012 | resolved = mirror::Class::FindField(self, klass, name, type); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5013 | if (resolved != NULL) { |
| 5014 | dex_cache->SetResolvedField(field_idx, resolved); |
| 5015 | } else { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5016 | ThrowNoSuchFieldError("", klass.Get(), type, name); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5017 | } |
| 5018 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 5019 | } |
| 5020 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5021 | const char* ClassLinker::MethodShorty(uint32_t method_idx, mirror::ArtMethod* referrer, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5022 | uint32_t* length) { |
| 5023 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
| 5024 | mirror::DexCache* dex_cache = declaring_class->GetDexCache(); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 5025 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 5026 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 5027 | return dex_file.GetMethodShorty(method_id, length); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 5028 | } |
| 5029 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5030 | void ClassLinker::DumpAllClasses(int flags) { |
| 5031 | if (dex_cache_image_class_lookup_required_) { |
| 5032 | MoveImageClassesToClassTable(); |
| 5033 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 5034 | // TODO: at the time this was written, it wasn't safe to call PrettyField with the ClassLinker |
| 5035 | // 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] | 5036 | std::vector<mirror::Class*> all_classes; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 5037 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 5038 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 5039 | for (std::pair<const size_t, GcRoot<mirror::Class> >& it : class_table_) { |
| 5040 | mirror::Class* klass = it.second.Read(); |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 5041 | all_classes.push_back(klass); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 5042 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 5043 | } |
| 5044 | |
| 5045 | for (size_t i = 0; i < all_classes.size(); ++i) { |
| 5046 | all_classes[i]->DumpClass(std::cerr, flags); |
| 5047 | } |
| 5048 | } |
| 5049 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5050 | void ClassLinker::DumpForSigQuit(std::ostream& os) { |
| 5051 | if (dex_cache_image_class_lookup_required_) { |
| 5052 | MoveImageClassesToClassTable(); |
| 5053 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 5054 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5055 | os << "Loaded classes: " << class_table_.size() << " allocated classes\n"; |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 5056 | } |
| 5057 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5058 | size_t ClassLinker::NumLoadedClasses() { |
| 5059 | if (dex_cache_image_class_lookup_required_) { |
| 5060 | MoveImageClassesToClassTable(); |
| 5061 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 5062 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5063 | return class_table_.size(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 5064 | } |
| 5065 | |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 5066 | pid_t ClassLinker::GetClassesLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 5067 | return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 5068 | } |
| 5069 | |
| 5070 | pid_t ClassLinker::GetDexLockOwner() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 5071 | return dex_lock_.GetExclusiveOwnerTid(); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 5072 | } |
| 5073 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5074 | void ClassLinker::SetClassRoot(ClassRoot class_root, mirror::Class* klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5075 | DCHECK(!init_done_); |
| 5076 | |
| 5077 | DCHECK(klass != NULL); |
| 5078 | DCHECK(klass->GetClassLoader() == NULL); |
| 5079 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 5080 | mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 5081 | DCHECK(class_roots != NULL); |
| 5082 | DCHECK(class_roots->Get(class_root) == NULL); |
| 5083 | class_roots->Set<false>(class_root, klass); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5084 | } |
| 5085 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5086 | } // namespace art |