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 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 20 | #include <deque> |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 21 | #include <iostream> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 22 | #include <memory> |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 23 | #include <queue> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 24 | #include <string> |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 25 | #include <tuple> |
Andreas Gampe | a696c0a | 2014-12-10 20:51:45 -0800 | [diff] [blame] | 26 | #include <unistd.h> |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 27 | #include <unordered_map> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 28 | #include <utility> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 29 | #include <vector> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 30 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 31 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 32 | #include "art_method-inl.h" |
| 33 | #include "base/arena_allocator.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 34 | #include "base/casts.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 35 | #include "base/logging.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 36 | #include "base/scoped_arena_containers.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 37 | #include "base/scoped_flock.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 38 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 39 | #include "base/systrace.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 40 | #include "base/time_utils.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 41 | #include "base/unix_file/fd_file.h" |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 42 | #include "base/value_object.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 43 | #include "class_linker-inl.h" |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 44 | #include "class_table-inl.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 45 | #include "compiler_callbacks.h" |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 46 | #include "debugger.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 47 | #include "dex_file-inl.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 48 | #include "entrypoints/entrypoint_utils.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 49 | #include "entrypoints/runtime_asm_entrypoints.h" |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 50 | #include "experimental_flags.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 51 | #include "gc_root-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 52 | #include "gc/accounting/card_table-inl.h" |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 53 | #include "gc/accounting/heap_bitmap-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 54 | #include "gc/heap.h" |
Mathieu Chartier | 1b1e31f | 2016-05-19 10:13:04 -0700 | [diff] [blame] | 55 | #include "gc/scoped_gc_critical_section.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 56 | #include "gc/space/image_space.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 57 | #include "handle_scope-inl.h" |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 58 | #include "image-inl.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 59 | #include "intern_table.h" |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 60 | #include "interpreter/interpreter.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 61 | #include "jit/jit.h" |
| 62 | #include "jit/jit_code_cache.h" |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 63 | #include "jit/offline_profiling_info.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 64 | #include "leb128.h" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 65 | #include "linear_alloc.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 66 | #include "mirror/class.h" |
| 67 | #include "mirror/class-inl.h" |
| 68 | #include "mirror/class_loader.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 69 | #include "mirror/dex_cache-inl.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 70 | #include "mirror/field.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 71 | #include "mirror/iftable-inl.h" |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 72 | #include "mirror/method.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 73 | #include "mirror/object-inl.h" |
| 74 | #include "mirror/object_array-inl.h" |
| 75 | #include "mirror/proxy.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 76 | #include "mirror/reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 77 | #include "mirror/stack_trace_element.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 78 | #include "mirror/string-inl.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 79 | #include "native/dalvik_system_DexFile.h" |
| 80 | #include "oat.h" |
| 81 | #include "oat_file.h" |
| 82 | #include "oat_file-inl.h" |
| 83 | #include "oat_file_assistant.h" |
| 84 | #include "oat_file_manager.h" |
| 85 | #include "object_lock.h" |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 86 | #include "os.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 87 | #include "runtime.h" |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 88 | #include "ScopedLocalRef.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 89 | #include "scoped_thread_state_change.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 90 | #include "thread-inl.h" |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 91 | #include "trace.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 92 | #include "utils.h" |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 93 | #include "utils/dex_cache_arrays_layout-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 94 | #include "verifier/method_verifier.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 95 | #include "well_known_classes.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 96 | |
| 97 | namespace art { |
| 98 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 99 | static constexpr bool kSanityCheckObjects = kIsDebugBuild; |
Mathieu Chartier | b0026b4 | 2016-03-31 15:05:45 -0700 | [diff] [blame] | 100 | static constexpr bool kVerifyArtMethodDeclaringClasses = kIsDebugBuild; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 101 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 102 | static void ThrowNoClassDefFoundError(const char* fmt, ...) |
| 103 | __attribute__((__format__(__printf__, 1, 2))) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 104 | SHARED_REQUIRES(Locks::mutator_lock_); |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 105 | static void ThrowNoClassDefFoundError(const char* fmt, ...) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 106 | va_list args; |
| 107 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 108 | Thread* self = Thread::Current(); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 109 | self->ThrowNewExceptionV("Ljava/lang/NoClassDefFoundError;", fmt, args); |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 110 | va_end(args); |
| 111 | } |
| 112 | |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 113 | static bool HasInitWithString(Thread* self, ClassLinker* class_linker, const char* descriptor) |
| 114 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 115 | ArtMethod* method = self->GetCurrentMethod(nullptr); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 116 | StackHandleScope<1> hs(self); |
| 117 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(method != nullptr ? |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 118 | method->GetDeclaringClass()->GetClassLoader() : nullptr)); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 119 | mirror::Class* exception_class = class_linker->FindClass(self, descriptor, class_loader); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 120 | |
| 121 | if (exception_class == nullptr) { |
| 122 | // No exc class ~ no <init>-with-string. |
| 123 | CHECK(self->IsExceptionPending()); |
| 124 | self->ClearException(); |
| 125 | return false; |
| 126 | } |
| 127 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 128 | ArtMethod* exception_init_method = exception_class->FindDeclaredDirectMethod( |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 129 | "<init>", "(Ljava/lang/String;)V", class_linker->GetImagePointerSize()); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 130 | return exception_init_method != nullptr; |
| 131 | } |
| 132 | |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 133 | // Helper for ThrowEarlierClassFailure. Throws the stored error. |
| 134 | static void HandleEarlierVerifyError(Thread* self, ClassLinker* class_linker, mirror::Class* c) |
| 135 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 136 | mirror::Object* obj = c->GetVerifyError(); |
| 137 | DCHECK(obj != nullptr); |
| 138 | self->AssertNoPendingException(); |
| 139 | if (obj->IsClass()) { |
| 140 | // Previous error has been stored as class. Create a new exception of that type. |
| 141 | |
| 142 | // It's possible the exception doesn't have a <init>(String). |
| 143 | std::string temp; |
| 144 | const char* descriptor = obj->AsClass()->GetDescriptor(&temp); |
| 145 | |
| 146 | if (HasInitWithString(self, class_linker, descriptor)) { |
| 147 | self->ThrowNewException(descriptor, PrettyDescriptor(c).c_str()); |
| 148 | } else { |
| 149 | self->ThrowNewException(descriptor, nullptr); |
| 150 | } |
| 151 | } else { |
| 152 | // Previous error has been stored as an instance. Just rethrow. |
| 153 | mirror::Class* throwable_class = |
| 154 | self->DecodeJObject(WellKnownClasses::java_lang_Throwable)->AsClass(); |
| 155 | mirror::Class* error_class = obj->GetClass(); |
| 156 | CHECK(throwable_class->IsAssignableFrom(error_class)); |
| 157 | self->SetException(obj->AsThrowable()); |
| 158 | } |
| 159 | self->AssertPendingException(); |
| 160 | } |
| 161 | |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 162 | void ClassLinker::ThrowEarlierClassFailure(mirror::Class* c, bool wrap_in_no_class_def) { |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 163 | // The class failed to initialize on a previous attempt, so we want to throw |
| 164 | // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we |
| 165 | // failed in verification, in which case v2 5.4.1 says we need to re-throw |
| 166 | // the previous error. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 167 | Runtime* const runtime = Runtime::Current(); |
| 168 | if (!runtime->IsAotCompiler()) { // Give info if this occurs at runtime. |
Andreas Gampe | 3d6b470 | 2015-09-21 08:35:52 -0700 | [diff] [blame] | 169 | std::string extra; |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 170 | if (c->GetVerifyError() != nullptr) { |
Andreas Gampe | 369c851 | 2016-01-28 15:31:39 -0800 | [diff] [blame] | 171 | mirror::Object* verify_error = c->GetVerifyError(); |
| 172 | if (verify_error->IsClass()) { |
| 173 | extra = PrettyDescriptor(verify_error->AsClass()); |
| 174 | } else { |
| 175 | extra = verify_error->AsThrowable()->Dump(); |
| 176 | } |
Andreas Gampe | 3d6b470 | 2015-09-21 08:35:52 -0700 | [diff] [blame] | 177 | } |
| 178 | LOG(INFO) << "Rejecting re-init on previously-failed class " << PrettyClass(c) << ": " << extra; |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 179 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 180 | |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 181 | CHECK(c->IsErroneous()) << PrettyClass(c) << " " << c->GetStatus(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 182 | Thread* self = Thread::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 183 | if (runtime->IsAotCompiler()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 184 | // At compile time, accurate errors and NCDFE are disabled to speed compilation. |
| 185 | mirror::Throwable* pre_allocated = runtime->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 186 | self->SetException(pre_allocated); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 187 | } else { |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 188 | if (c->GetVerifyError() != nullptr) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 189 | // Rethrow stored error. |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 190 | HandleEarlierVerifyError(self, this, c); |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 191 | } |
| 192 | if (c->GetVerifyError() == nullptr || wrap_in_no_class_def) { |
| 193 | // If there isn't a recorded earlier error, or this is a repeat throw from initialization, |
| 194 | // the top-level exception must be a NoClassDefFoundError. The potentially already pending |
| 195 | // exception will be a cause. |
| 196 | self->ThrowNewWrappedException("Ljava/lang/NoClassDefFoundError;", |
| 197 | PrettyDescriptor(c).c_str()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 198 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 202 | static void VlogClassInitializationFailure(Handle<mirror::Class> klass) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 203 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 204 | if (VLOG_IS_ON(class_linker)) { |
| 205 | std::string temp; |
| 206 | LOG(INFO) << "Failed to initialize class " << klass->GetDescriptor(&temp) << " from " |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 207 | << klass->GetLocation() << "\n" << Thread::Current()->GetException()->Dump(); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | static void WrapExceptionInInitializer(Handle<mirror::Class> klass) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 212 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 213 | Thread* self = Thread::Current(); |
| 214 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 215 | |
| 216 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 217 | CHECK(cause.get() != nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 218 | |
| 219 | env->ExceptionClear(); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 220 | bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error); |
| 221 | env->Throw(cause.get()); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 222 | |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 223 | // We only wrap non-Error exceptions; an Error can just be used as-is. |
| 224 | if (!is_error) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 225 | self->ThrowNewWrappedException("Ljava/lang/ExceptionInInitializerError;", nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 226 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 227 | VlogClassInitializationFailure(klass); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 230 | // Gap between two fields in object layout. |
| 231 | struct FieldGap { |
| 232 | uint32_t start_offset; // The offset from the start of the object. |
| 233 | uint32_t size; // The gap size of 1, 2, or 4 bytes. |
| 234 | }; |
| 235 | struct FieldGapsComparator { |
| 236 | explicit FieldGapsComparator() { |
| 237 | } |
| 238 | bool operator() (const FieldGap& lhs, const FieldGap& rhs) |
| 239 | NO_THREAD_SAFETY_ANALYSIS { |
Andreas Gampe | f52857f | 2015-02-18 15:38:57 -0800 | [diff] [blame] | 240 | // Sort by gap size, largest first. Secondary sort by starting offset. |
Richard Uhler | fab6788 | 2015-07-13 17:00:35 -0700 | [diff] [blame] | 241 | // Note that the priority queue returns the largest element, so operator() |
| 242 | // should return true if lhs is less than rhs. |
| 243 | return lhs.size < rhs.size || (lhs.size == rhs.size && lhs.start_offset > rhs.start_offset); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 244 | } |
| 245 | }; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 246 | typedef std::priority_queue<FieldGap, std::vector<FieldGap>, FieldGapsComparator> FieldGaps; |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 247 | |
| 248 | // Adds largest aligned gaps to queue of gaps. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 249 | static void AddFieldGap(uint32_t gap_start, uint32_t gap_end, FieldGaps* gaps) { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 250 | DCHECK(gaps != nullptr); |
| 251 | |
| 252 | uint32_t current_offset = gap_start; |
| 253 | while (current_offset != gap_end) { |
| 254 | size_t remaining = gap_end - current_offset; |
| 255 | if (remaining >= sizeof(uint32_t) && IsAligned<4>(current_offset)) { |
| 256 | gaps->push(FieldGap {current_offset, sizeof(uint32_t)}); |
| 257 | current_offset += sizeof(uint32_t); |
| 258 | } else if (remaining >= sizeof(uint16_t) && IsAligned<2>(current_offset)) { |
| 259 | gaps->push(FieldGap {current_offset, sizeof(uint16_t)}); |
| 260 | current_offset += sizeof(uint16_t); |
| 261 | } else { |
| 262 | gaps->push(FieldGap {current_offset, sizeof(uint8_t)}); |
| 263 | current_offset += sizeof(uint8_t); |
| 264 | } |
| 265 | DCHECK_LE(current_offset, gap_end) << "Overran gap"; |
| 266 | } |
| 267 | } |
| 268 | // Shuffle fields forward, making use of gaps whenever possible. |
| 269 | template<int n> |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 270 | static void ShuffleForward(size_t* current_field_idx, |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 271 | MemberOffset* field_offset, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 272 | std::deque<ArtField*>* grouped_and_sorted_fields, |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 273 | FieldGaps* gaps) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 274 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 275 | DCHECK(current_field_idx != nullptr); |
| 276 | DCHECK(grouped_and_sorted_fields != nullptr); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 277 | DCHECK(gaps != nullptr); |
| 278 | DCHECK(field_offset != nullptr); |
| 279 | |
| 280 | DCHECK(IsPowerOfTwo(n)); |
| 281 | while (!grouped_and_sorted_fields->empty()) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 282 | ArtField* field = grouped_and_sorted_fields->front(); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 283 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 284 | if (Primitive::ComponentSize(type) < n) { |
| 285 | break; |
| 286 | } |
| 287 | if (!IsAligned<n>(field_offset->Uint32Value())) { |
| 288 | MemberOffset old_offset = *field_offset; |
| 289 | *field_offset = MemberOffset(RoundUp(field_offset->Uint32Value(), n)); |
| 290 | AddFieldGap(old_offset.Uint32Value(), field_offset->Uint32Value(), gaps); |
| 291 | } |
| 292 | CHECK(type != Primitive::kPrimNot) << PrettyField(field); // should be primitive types |
| 293 | grouped_and_sorted_fields->pop_front(); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 294 | if (!gaps->empty() && gaps->top().size >= n) { |
| 295 | FieldGap gap = gaps->top(); |
| 296 | gaps->pop(); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 297 | DCHECK_ALIGNED(gap.start_offset, n); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 298 | field->SetOffset(MemberOffset(gap.start_offset)); |
| 299 | if (gap.size > n) { |
| 300 | AddFieldGap(gap.start_offset + n, gap.start_offset + gap.size, gaps); |
| 301 | } |
| 302 | } else { |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 303 | DCHECK_ALIGNED(field_offset->Uint32Value(), n); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 304 | field->SetOffset(*field_offset); |
| 305 | *field_offset = MemberOffset(field_offset->Uint32Value() + n); |
| 306 | } |
| 307 | ++(*current_field_idx); |
| 308 | } |
| 309 | } |
| 310 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 311 | ClassLinker::ClassLinker(InternTable* intern_table) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 312 | // dex_lock_ is recursive as it may be used in stack dumping. |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 313 | : dex_lock_("ClassLinker dex lock", kDefaultMutexLevel), |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 314 | dex_cache_boot_image_class_lookup_required_(false), |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 315 | failed_dex_cache_class_lookups_(0), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 316 | class_roots_(nullptr), |
| 317 | array_iftable_(nullptr), |
| 318 | find_array_class_cache_next_victim_(0), |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 319 | init_done_(false), |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 320 | log_new_class_table_roots_(false), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 321 | intern_table_(intern_table), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 322 | quick_resolution_trampoline_(nullptr), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 323 | quick_imt_conflict_trampoline_(nullptr), |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 324 | quick_generic_jni_trampoline_(nullptr), |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 325 | quick_to_interpreter_bridge_trampoline_(nullptr), |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 326 | image_pointer_size_(kRuntimePointerSize) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 327 | CHECK(intern_table_ != nullptr); |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 328 | static_assert(kFindArrayCacheSize == arraysize(find_array_class_cache_), |
| 329 | "Array cache size wrong."); |
| 330 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 331 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 332 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 333 | void ClassLinker::CheckSystemClass(Thread* self, Handle<mirror::Class> c1, const char* descriptor) { |
| 334 | mirror::Class* c2 = FindSystemClass(self, descriptor); |
| 335 | if (c2 == nullptr) { |
| 336 | LOG(FATAL) << "Could not find class " << descriptor; |
| 337 | UNREACHABLE(); |
| 338 | } |
| 339 | if (c1.Get() != c2) { |
| 340 | std::ostringstream os1, os2; |
| 341 | c1->DumpClass(os1, mirror::Class::kDumpClassFullDetail); |
| 342 | c2->DumpClass(os2, mirror::Class::kDumpClassFullDetail); |
| 343 | LOG(FATAL) << "InitWithoutImage: Class mismatch for " << descriptor |
| 344 | << ". This is most likely the result of a broken build. Make sure that " |
| 345 | << "libcore and art projects match.\n\n" |
| 346 | << os1.str() << "\n\n" << os2.str(); |
| 347 | UNREACHABLE(); |
| 348 | } |
| 349 | } |
| 350 | |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 351 | bool ClassLinker::InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path, |
| 352 | std::string* error_msg) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 353 | VLOG(startup) << "ClassLinker::Init"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 354 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 355 | Thread* const self = Thread::Current(); |
| 356 | Runtime* const runtime = Runtime::Current(); |
| 357 | gc::Heap* const heap = runtime->GetHeap(); |
| 358 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 359 | CHECK(!heap->HasBootImageSpace()) << "Runtime has image. We should use it."; |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 360 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 361 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 362 | // Use the pointer size from the runtime since we are probably creating the image. |
| 363 | image_pointer_size_ = InstructionSetPointerSize(runtime->GetInstructionSet()); |
| 364 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 365 | // java_lang_Class comes first, it's needed for AllocClass |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 366 | // 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] | 367 | heap->IncrementDisableMovingGC(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 368 | StackHandleScope<64> hs(self); // 64 is picked arbitrarily. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 369 | auto class_class_size = mirror::Class::ClassClassSize(image_pointer_size_); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 370 | Handle<mirror::Class> java_lang_Class(hs.NewHandle(down_cast<mirror::Class*>( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 371 | heap->AllocNonMovableObject<true>(self, nullptr, class_class_size, VoidFunctor())))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 372 | CHECK(java_lang_Class.Get() != nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 373 | mirror::Class::SetClassClass(java_lang_Class.Get()); |
| 374 | java_lang_Class->SetClass(java_lang_Class.Get()); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 375 | if (kUseBakerOrBrooksReadBarrier) { |
| 376 | java_lang_Class->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 377 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 378 | java_lang_Class->SetClassSize(class_class_size); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 379 | java_lang_Class->SetPrimitiveType(Primitive::kPrimNot); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 380 | heap->DecrementDisableMovingGC(self); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 381 | // AllocClass(mirror::Class*) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 382 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 383 | // Class[] is used for reflection support. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 384 | auto class_array_class_size = mirror::ObjectArray<mirror::Class>::ClassSize(image_pointer_size_); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 385 | Handle<mirror::Class> class_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 386 | AllocClass(self, java_lang_Class.Get(), class_array_class_size))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 387 | class_array_class->SetComponentType(java_lang_Class.Get()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 388 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 389 | // 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] | 390 | Handle<mirror::Class> java_lang_Object(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 391 | AllocClass(self, java_lang_Class.Get(), mirror::Object::ClassSize(image_pointer_size_)))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 392 | CHECK(java_lang_Object.Get() != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 393 | // backfill Object as the super class of Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 394 | java_lang_Class->SetSuperClass(java_lang_Object.Get()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 395 | mirror::Class::SetStatus(java_lang_Object, mirror::Class::kStatusLoaded, self); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 396 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 397 | java_lang_Object->SetObjectSize(sizeof(mirror::Object)); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 398 | // Allocate in non-movable so that it's possible to check if a JNI weak global ref has been |
| 399 | // cleared without triggering the read barrier and unintentionally mark the sentinel alive. |
| 400 | runtime->SetSentinel(heap->AllocNonMovableObject<true>(self, |
| 401 | java_lang_Object.Get(), |
| 402 | java_lang_Object->GetObjectSize(), |
| 403 | VoidFunctor())); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 404 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 405 | // Object[] next to hold class roots. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 406 | Handle<mirror::Class> object_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 407 | AllocClass(self, java_lang_Class.Get(), |
| 408 | mirror::ObjectArray<mirror::Object>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 409 | object_array_class->SetComponentType(java_lang_Object.Get()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 410 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 411 | // Setup the char (primitive) class to be used for char[]. |
| 412 | Handle<mirror::Class> char_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 413 | AllocClass(self, java_lang_Class.Get(), |
| 414 | mirror::Class::PrimitiveClassSize(image_pointer_size_)))); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 415 | // The primitive char class won't be initialized by |
| 416 | // InitializePrimitiveClass until line 459, but strings (and |
| 417 | // internal char arrays) will be allocated before that and the |
| 418 | // component size, which is computed from the primitive type, needs |
| 419 | // to be set here. |
| 420 | char_class->SetPrimitiveType(Primitive::kPrimChar); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 421 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 422 | // Setup the char[] class to be used for String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 423 | Handle<mirror::Class> char_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 424 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 425 | char_array_class->SetComponentType(char_class.Get()); |
| 426 | mirror::CharArray::SetArrayClass(char_array_class.Get()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 427 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 428 | // Setup String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 429 | Handle<mirror::Class> java_lang_String(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 430 | AllocClass(self, java_lang_Class.Get(), mirror::String::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 431 | java_lang_String->SetStringClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 432 | mirror::String::SetClass(java_lang_String.Get()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 433 | mirror::Class::SetStatus(java_lang_String, mirror::Class::kStatusResolved, self); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 434 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 435 | // Setup java.lang.ref.Reference. |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 436 | Handle<mirror::Class> java_lang_ref_Reference(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 437 | AllocClass(self, java_lang_Class.Get(), mirror::Reference::ClassSize(image_pointer_size_)))); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 438 | mirror::Reference::SetClass(java_lang_ref_Reference.Get()); |
| 439 | java_lang_ref_Reference->SetObjectSize(mirror::Reference::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 440 | mirror::Class::SetStatus(java_lang_ref_Reference, mirror::Class::kStatusResolved, self); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 441 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 442 | // Create storage for root classes, save away our work so far (requires descriptors). |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 443 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 444 | mirror::ObjectArray<mirror::Class>::Alloc(self, object_array_class.Get(), |
| 445 | kClassRootsMax)); |
| 446 | CHECK(!class_roots_.IsNull()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 447 | SetClassRoot(kJavaLangClass, java_lang_Class.Get()); |
| 448 | SetClassRoot(kJavaLangObject, java_lang_Object.Get()); |
| 449 | SetClassRoot(kClassArrayClass, class_array_class.Get()); |
| 450 | SetClassRoot(kObjectArrayClass, object_array_class.Get()); |
| 451 | SetClassRoot(kCharArrayClass, char_array_class.Get()); |
| 452 | SetClassRoot(kJavaLangString, java_lang_String.Get()); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 453 | SetClassRoot(kJavaLangRefReference, java_lang_ref_Reference.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 454 | |
| 455 | // Setup the primitive type classes. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 456 | SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass(self, Primitive::kPrimBoolean)); |
| 457 | SetClassRoot(kPrimitiveByte, CreatePrimitiveClass(self, Primitive::kPrimByte)); |
| 458 | SetClassRoot(kPrimitiveShort, CreatePrimitiveClass(self, Primitive::kPrimShort)); |
| 459 | SetClassRoot(kPrimitiveInt, CreatePrimitiveClass(self, Primitive::kPrimInt)); |
| 460 | SetClassRoot(kPrimitiveLong, CreatePrimitiveClass(self, Primitive::kPrimLong)); |
| 461 | SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass(self, Primitive::kPrimFloat)); |
| 462 | SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass(self, Primitive::kPrimDouble)); |
| 463 | SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass(self, Primitive::kPrimVoid)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 464 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 465 | // Create array interface entries to populate once we can load system classes. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 466 | array_iftable_ = GcRoot<mirror::IfTable>(AllocIfTable(self, 2)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 467 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 468 | // Create int array type for AllocDexCache (done in AppendToBootClassPath). |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 469 | Handle<mirror::Class> int_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 470 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize(image_pointer_size_)))); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 471 | int_array_class->SetComponentType(GetClassRoot(kPrimitiveInt)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 472 | mirror::IntArray::SetArrayClass(int_array_class.Get()); |
| 473 | SetClassRoot(kIntArrayClass, int_array_class.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 474 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 475 | // Create long array type for AllocDexCache (done in AppendToBootClassPath). |
| 476 | Handle<mirror::Class> long_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 477 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 478 | long_array_class->SetComponentType(GetClassRoot(kPrimitiveLong)); |
| 479 | mirror::LongArray::SetArrayClass(long_array_class.Get()); |
| 480 | SetClassRoot(kLongArrayClass, long_array_class.Get()); |
| 481 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 482 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 483 | |
Ian Rogers | 52813c9 | 2012-10-11 11:50:38 -0700 | [diff] [blame] | 484 | // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 485 | Handle<mirror::Class> java_lang_DexCache(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 486 | AllocClass(self, java_lang_Class.Get(), mirror::DexCache::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 487 | SetClassRoot(kJavaLangDexCache, java_lang_DexCache.Get()); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 488 | java_lang_DexCache->SetDexCacheClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 489 | java_lang_DexCache->SetObjectSize(mirror::DexCache::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 490 | mirror::Class::SetStatus(java_lang_DexCache, mirror::Class::kStatusResolved, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 491 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 492 | // Set up array classes for string, field, method |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 493 | Handle<mirror::Class> object_array_string(hs.NewHandle( |
| 494 | AllocClass(self, java_lang_Class.Get(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 495 | mirror::ObjectArray<mirror::String>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 496 | object_array_string->SetComponentType(java_lang_String.Get()); |
| 497 | SetClassRoot(kJavaLangStringArrayClass, object_array_string.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 498 | |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 499 | LinearAlloc* linear_alloc = runtime->GetLinearAlloc(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 500 | // Create runtime resolution and imt conflict methods. |
| 501 | runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 502 | runtime->SetImtConflictMethod(runtime->CreateImtConflictMethod(linear_alloc)); |
| 503 | runtime->SetImtUnimplementedMethod(runtime->CreateImtConflictMethod(linear_alloc)); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 504 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 505 | // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create |
| 506 | // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses |
| 507 | // these roots. |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 508 | if (boot_class_path.empty()) { |
| 509 | *error_msg = "Boot classpath is empty."; |
| 510 | return false; |
| 511 | } |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 512 | for (auto& dex_file : boot_class_path) { |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 513 | if (dex_file.get() == nullptr) { |
| 514 | *error_msg = "Null dex file."; |
| 515 | return false; |
| 516 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 517 | AppendToBootClassPath(self, *dex_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 518 | boot_dex_files_.push_back(std::move(dex_file)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 519 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 520 | |
| 521 | // now we can use FindSystemClass |
| 522 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 523 | // run char class through InitializePrimitiveClass to finish init |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 524 | InitializePrimitiveClass(char_class.Get(), Primitive::kPrimChar); |
| 525 | SetClassRoot(kPrimitiveChar, char_class.Get()); // needs descriptor |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 526 | |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 527 | // Set up GenericJNI entrypoint. That is mainly a hack for common_compiler_test.h so that |
| 528 | // we do not need friend classes or a publicly exposed setter. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 529 | quick_generic_jni_trampoline_ = GetQuickGenericJniStub(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 530 | if (!runtime->IsAotCompiler()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 531 | // We need to set up the generic trampolines since we don't have an image. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 532 | quick_resolution_trampoline_ = GetQuickResolutionStub(); |
| 533 | quick_imt_conflict_trampoline_ = GetQuickImtConflictStub(); |
| 534 | quick_to_interpreter_bridge_trampoline_ = GetQuickToInterpreterBridge(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 535 | } |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 536 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 537 | // Object, String and DexCache need to be rerun through FindSystemClass to finish init |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 538 | mirror::Class::SetStatus(java_lang_Object, mirror::Class::kStatusNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 539 | CheckSystemClass(self, java_lang_Object, "Ljava/lang/Object;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 540 | CHECK_EQ(java_lang_Object->GetObjectSize(), mirror::Object::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 541 | mirror::Class::SetStatus(java_lang_String, mirror::Class::kStatusNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 542 | CheckSystemClass(self, java_lang_String, "Ljava/lang/String;"); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 543 | mirror::Class::SetStatus(java_lang_DexCache, mirror::Class::kStatusNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 544 | CheckSystemClass(self, java_lang_DexCache, "Ljava/lang/DexCache;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 545 | CHECK_EQ(java_lang_DexCache->GetObjectSize(), mirror::DexCache::InstanceSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 546 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 547 | // 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] | 548 | SetClassRoot(kBooleanArrayClass, FindSystemClass(self, "[Z")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 549 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 550 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 551 | SetClassRoot(kByteArrayClass, FindSystemClass(self, "[B")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 552 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 553 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 554 | CheckSystemClass(self, char_array_class, "[C"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 555 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 556 | SetClassRoot(kShortArrayClass, FindSystemClass(self, "[S")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 557 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 558 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 559 | CheckSystemClass(self, int_array_class, "[I"); |
| 560 | CheckSystemClass(self, long_array_class, "[J"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 561 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 562 | SetClassRoot(kFloatArrayClass, FindSystemClass(self, "[F")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 563 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 564 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 565 | SetClassRoot(kDoubleArrayClass, FindSystemClass(self, "[D")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 566 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 567 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 568 | // Run Class through FindSystemClass. This initializes the dex_cache_ fields and register it |
| 569 | // in class_table_. |
| 570 | CheckSystemClass(self, java_lang_Class, "Ljava/lang/Class;"); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 571 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 572 | CheckSystemClass(self, class_array_class, "[Ljava/lang/Class;"); |
| 573 | CheckSystemClass(self, object_array_class, "[Ljava/lang/Object;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 574 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 575 | // Setup the single, global copy of "iftable". |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 576 | auto java_lang_Cloneable = hs.NewHandle(FindSystemClass(self, "Ljava/lang/Cloneable;")); |
| 577 | CHECK(java_lang_Cloneable.Get() != nullptr); |
| 578 | auto java_io_Serializable = hs.NewHandle(FindSystemClass(self, "Ljava/io/Serializable;")); |
| 579 | CHECK(java_io_Serializable.Get() != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 580 | // We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to |
| 581 | // crawl up and explicitly list all of the supers as well. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 582 | array_iftable_.Read()->SetInterface(0, java_lang_Cloneable.Get()); |
| 583 | array_iftable_.Read()->SetInterface(1, java_io_Serializable.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 584 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 585 | // Sanity check Class[] and Object[]'s interfaces. GetDirectInterface may cause thread |
| 586 | // suspension. |
| 587 | CHECK_EQ(java_lang_Cloneable.Get(), |
| 588 | mirror::Class::GetDirectInterface(self, class_array_class, 0)); |
| 589 | CHECK_EQ(java_io_Serializable.Get(), |
| 590 | mirror::Class::GetDirectInterface(self, class_array_class, 1)); |
| 591 | CHECK_EQ(java_lang_Cloneable.Get(), |
| 592 | mirror::Class::GetDirectInterface(self, object_array_class, 0)); |
| 593 | CHECK_EQ(java_io_Serializable.Get(), |
| 594 | mirror::Class::GetDirectInterface(self, object_array_class, 1)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 595 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 596 | CHECK_EQ(object_array_string.Get(), |
| 597 | FindSystemClass(self, GetClassRootDescriptor(kJavaLangStringArrayClass))); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 598 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 599 | // End of special init trickery, all subsequent classes may be loaded via FindSystemClass. |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 600 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 601 | // Create java.lang.reflect.Proxy root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 602 | SetClassRoot(kJavaLangReflectProxy, FindSystemClass(self, "Ljava/lang/reflect/Proxy;")); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 603 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 604 | // Create java.lang.reflect.Field.class root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 605 | auto* class_root = FindSystemClass(self, "Ljava/lang/reflect/Field;"); |
| 606 | CHECK(class_root != nullptr); |
| 607 | SetClassRoot(kJavaLangReflectField, class_root); |
| 608 | mirror::Field::SetClass(class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 609 | |
| 610 | // Create java.lang.reflect.Field array root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 611 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Field;"); |
| 612 | CHECK(class_root != nullptr); |
| 613 | SetClassRoot(kJavaLangReflectFieldArrayClass, class_root); |
| 614 | mirror::Field::SetArrayClass(class_root); |
| 615 | |
| 616 | // Create java.lang.reflect.Constructor.class root and array root. |
| 617 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Constructor;"); |
| 618 | CHECK(class_root != nullptr); |
| 619 | SetClassRoot(kJavaLangReflectConstructor, class_root); |
| 620 | mirror::Constructor::SetClass(class_root); |
| 621 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Constructor;"); |
| 622 | CHECK(class_root != nullptr); |
| 623 | SetClassRoot(kJavaLangReflectConstructorArrayClass, class_root); |
| 624 | mirror::Constructor::SetArrayClass(class_root); |
| 625 | |
| 626 | // Create java.lang.reflect.Method.class root and array root. |
| 627 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Method;"); |
| 628 | CHECK(class_root != nullptr); |
| 629 | SetClassRoot(kJavaLangReflectMethod, class_root); |
| 630 | mirror::Method::SetClass(class_root); |
| 631 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Method;"); |
| 632 | CHECK(class_root != nullptr); |
| 633 | SetClassRoot(kJavaLangReflectMethodArrayClass, class_root); |
| 634 | mirror::Method::SetArrayClass(class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 635 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 636 | // 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] | 637 | // finish initializing Reference class |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 638 | mirror::Class::SetStatus(java_lang_ref_Reference, mirror::Class::kStatusNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 639 | CheckSystemClass(self, java_lang_ref_Reference, "Ljava/lang/ref/Reference;"); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 640 | CHECK_EQ(java_lang_ref_Reference->GetObjectSize(), mirror::Reference::InstanceSize()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 641 | CHECK_EQ(java_lang_ref_Reference->GetClassSize(), |
| 642 | mirror::Reference::ClassSize(image_pointer_size_)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 643 | class_root = FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 644 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 645 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagFinalizerReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 646 | class_root = FindSystemClass(self, "Ljava/lang/ref/PhantomReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 647 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 648 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagPhantomReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 649 | class_root = FindSystemClass(self, "Ljava/lang/ref/SoftReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 650 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 651 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagSoftReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 652 | class_root = FindSystemClass(self, "Ljava/lang/ref/WeakReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 653 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 654 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagWeakReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 655 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 656 | // Setup the ClassLoader, verifying the object_size_. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 657 | class_root = FindSystemClass(self, "Ljava/lang/ClassLoader;"); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 658 | class_root->SetClassLoaderClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 659 | CHECK_EQ(class_root->GetObjectSize(), mirror::ClassLoader::InstanceSize()); |
| 660 | SetClassRoot(kJavaLangClassLoader, class_root); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 661 | |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 662 | // Set up java.lang.Throwable, java.lang.ClassNotFoundException, and |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 663 | // java.lang.StackTraceElement as a convenience. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 664 | SetClassRoot(kJavaLangThrowable, FindSystemClass(self, "Ljava/lang/Throwable;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 665 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 666 | SetClassRoot(kJavaLangClassNotFoundException, |
| 667 | FindSystemClass(self, "Ljava/lang/ClassNotFoundException;")); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 668 | SetClassRoot(kJavaLangStackTraceElement, FindSystemClass(self, "Ljava/lang/StackTraceElement;")); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 669 | SetClassRoot(kJavaLangStackTraceElementArrayClass, |
| 670 | FindSystemClass(self, "[Ljava/lang/StackTraceElement;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 671 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 672 | |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 673 | // Ensure void type is resolved in the core's dex cache so java.lang.Void is correctly |
| 674 | // initialized. |
| 675 | { |
| 676 | const DexFile& dex_file = java_lang_Object->GetDexFile(); |
Mathieu Chartier | 9507fa2 | 2015-10-29 15:08:57 -0700 | [diff] [blame] | 677 | const DexFile::TypeId* void_type_id = dex_file.FindTypeId("V"); |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 678 | CHECK(void_type_id != nullptr); |
| 679 | uint16_t void_type_idx = dex_file.GetIndexForTypeId(*void_type_id); |
| 680 | // Now we resolve void type so the dex cache contains it. We use java.lang.Object class |
| 681 | // as referrer so the used dex cache is core's one. |
| 682 | mirror::Class* resolved_type = ResolveType(dex_file, void_type_idx, java_lang_Object.Get()); |
| 683 | CHECK_EQ(resolved_type, GetClassRoot(kPrimitiveVoid)); |
| 684 | self->AssertNoPendingException(); |
| 685 | } |
| 686 | |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 687 | // Create conflict tables that depend on the class linker. |
| 688 | runtime->FixupConflictTables(); |
| 689 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 690 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 691 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 692 | VLOG(startup) << "ClassLinker::InitFromCompiler exiting"; |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 693 | |
| 694 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 695 | } |
| 696 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 697 | void ClassLinker::FinishInit(Thread* self) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 698 | VLOG(startup) << "ClassLinker::FinishInit entering"; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 699 | |
| 700 | // Let the heap know some key offsets into java.lang.ref instances |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 701 | // Note: we hard code the field indexes here rather than using FindInstanceField |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 702 | // as the types of the field can't be resolved prior to the runtime being |
| 703 | // fully initialized |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 704 | mirror::Class* java_lang_ref_Reference = GetClassRoot(kJavaLangRefReference); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 705 | mirror::Class* java_lang_ref_FinalizerReference = |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 706 | FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 707 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 708 | ArtField* pendingNext = java_lang_ref_Reference->GetInstanceField(0); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 709 | CHECK_STREQ(pendingNext->GetName(), "pendingNext"); |
| 710 | CHECK_STREQ(pendingNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 711 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 712 | ArtField* queue = java_lang_ref_Reference->GetInstanceField(1); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 713 | CHECK_STREQ(queue->GetName(), "queue"); |
| 714 | CHECK_STREQ(queue->GetTypeDescriptor(), "Ljava/lang/ref/ReferenceQueue;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 715 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 716 | ArtField* queueNext = java_lang_ref_Reference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 717 | CHECK_STREQ(queueNext->GetName(), "queueNext"); |
| 718 | CHECK_STREQ(queueNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 719 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 720 | ArtField* referent = java_lang_ref_Reference->GetInstanceField(3); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 721 | CHECK_STREQ(referent->GetName(), "referent"); |
| 722 | CHECK_STREQ(referent->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 723 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 724 | ArtField* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 725 | CHECK_STREQ(zombie->GetName(), "zombie"); |
| 726 | CHECK_STREQ(zombie->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 727 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 728 | // ensure all class_roots_ are initialized |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 729 | for (size_t i = 0; i < kClassRootsMax; i++) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 730 | ClassRoot class_root = static_cast<ClassRoot>(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 731 | mirror::Class* klass = GetClassRoot(class_root); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 732 | CHECK(klass != nullptr); |
| 733 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != nullptr); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 734 | // note SetClassRoot does additional validation. |
| 735 | // if possible add new checks there to catch errors early |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 738 | CHECK(!array_iftable_.IsNull()); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 739 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 740 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 741 | // that Object, Class, and Object[] are setup |
| 742 | init_done_ = true; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 743 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 744 | VLOG(startup) << "ClassLinker::FinishInit exiting"; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 747 | void ClassLinker::RunRootClinits() { |
| 748 | Thread* self = Thread::Current(); |
| 749 | for (size_t i = 0; i < ClassLinker::kClassRootsMax; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 750 | mirror::Class* c = GetClassRoot(ClassRoot(i)); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 751 | if (!c->IsArrayClass() && !c->IsPrimitive()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 752 | StackHandleScope<1> hs(self); |
| 753 | Handle<mirror::Class> h_class(hs.NewHandle(GetClassRoot(ClassRoot(i)))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 754 | EnsureInitialized(self, h_class, true, true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 755 | self->AssertNoPendingException(); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | } |
| 759 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 760 | static void SanityCheckArtMethod(ArtMethod* m, |
| 761 | mirror::Class* expected_class, |
Jeff Hao | acd7a6a | 2016-01-08 10:44:20 -0800 | [diff] [blame] | 762 | const std::vector<gc::space::ImageSpace*>& spaces) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 763 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 764 | if (m->IsRuntimeMethod()) { |
Mathieu Chartier | 9894fc8 | 2016-03-17 19:19:15 -0700 | [diff] [blame] | 765 | mirror::Class* declaring_class = m->GetDeclaringClassUnchecked(); |
| 766 | CHECK(declaring_class == nullptr) << declaring_class << " " << PrettyMethod(m); |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 767 | } else if (m->IsCopied()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 768 | CHECK(m->GetDeclaringClass() != nullptr) << PrettyMethod(m); |
| 769 | } else if (expected_class != nullptr) { |
| 770 | CHECK_EQ(m->GetDeclaringClassUnchecked(), expected_class) << PrettyMethod(m); |
| 771 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 772 | if (!spaces.empty()) { |
| 773 | bool contains = false; |
| 774 | for (gc::space::ImageSpace* space : spaces) { |
| 775 | auto& header = space->GetImageHeader(); |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 776 | size_t offset = reinterpret_cast<uint8_t*>(m) - space->Begin(); |
| 777 | |
| 778 | const ImageSection& methods = header.GetMethodsSection(); |
| 779 | contains = contains || methods.Contains(offset); |
| 780 | |
| 781 | const ImageSection& runtime_methods = header.GetRuntimeMethodsSection(); |
| 782 | contains = contains || runtime_methods.Contains(offset); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 783 | } |
| 784 | CHECK(contains) << m << " not found"; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 788 | static void SanityCheckArtMethodPointerArray(mirror::PointerArray* arr, |
| 789 | mirror::Class* expected_class, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 790 | PointerSize pointer_size, |
Jeff Hao | acd7a6a | 2016-01-08 10:44:20 -0800 | [diff] [blame] | 791 | const std::vector<gc::space::ImageSpace*>& spaces) |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 792 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 793 | CHECK(arr != nullptr); |
| 794 | for (int32_t j = 0; j < arr->GetLength(); ++j) { |
| 795 | auto* method = arr->GetElementPtrSize<ArtMethod*>(j, pointer_size); |
| 796 | // expected_class == null means we are a dex cache. |
| 797 | if (expected_class != nullptr) { |
| 798 | CHECK(method != nullptr); |
| 799 | } |
| 800 | if (method != nullptr) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 801 | SanityCheckArtMethod(method, expected_class, spaces); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 802 | } |
| 803 | } |
| 804 | } |
| 805 | |
Jeff Hao | 1c1a342 | 2016-01-05 14:13:00 -0800 | [diff] [blame] | 806 | static void SanityCheckArtMethodPointerArray(ArtMethod** arr, |
| 807 | size_t size, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 808 | PointerSize pointer_size, |
Jeff Hao | acd7a6a | 2016-01-08 10:44:20 -0800 | [diff] [blame] | 809 | const std::vector<gc::space::ImageSpace*>& spaces) |
Jeff Hao | 1c1a342 | 2016-01-05 14:13:00 -0800 | [diff] [blame] | 810 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 811 | CHECK_EQ(arr != nullptr, size != 0u); |
| 812 | if (arr != nullptr) { |
Jeff Hao | 1c1a342 | 2016-01-05 14:13:00 -0800 | [diff] [blame] | 813 | bool contains = false; |
| 814 | for (auto space : spaces) { |
| 815 | auto offset = reinterpret_cast<uint8_t*>(arr) - space->Begin(); |
| 816 | if (space->GetImageHeader().GetImageSection( |
| 817 | ImageHeader::kSectionDexCacheArrays).Contains(offset)) { |
| 818 | contains = true; |
| 819 | break; |
| 820 | } |
| 821 | } |
| 822 | CHECK(contains); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 823 | } |
| 824 | for (size_t j = 0; j < size; ++j) { |
| 825 | ArtMethod* method = mirror::DexCache::GetElementPtrSize(arr, j, pointer_size); |
| 826 | // expected_class == null means we are a dex cache. |
| 827 | if (method != nullptr) { |
Jeff Hao | 1c1a342 | 2016-01-05 14:13:00 -0800 | [diff] [blame] | 828 | SanityCheckArtMethod(method, nullptr, spaces); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | } |
| 832 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 833 | static void SanityCheckObjectsCallback(mirror::Object* obj, void* arg ATTRIBUTE_UNUSED) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 834 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 835 | DCHECK(obj != nullptr); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 836 | CHECK(obj->GetClass() != nullptr) << "Null class in object " << obj; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 837 | CHECK(obj->GetClass()->GetClass() != nullptr) << "Null class class " << obj; |
| 838 | if (obj->IsClass()) { |
| 839 | auto klass = obj->AsClass(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 840 | for (ArtField& field : klass->GetIFields()) { |
| 841 | CHECK_EQ(field.GetDeclaringClass(), klass); |
| 842 | } |
| 843 | for (ArtField& field : klass->GetSFields()) { |
| 844 | CHECK_EQ(field.GetDeclaringClass(), klass); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 845 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 846 | auto* runtime = Runtime::Current(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 847 | auto image_spaces = runtime->GetHeap()->GetBootImageSpaces(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 848 | auto pointer_size = runtime->GetClassLinker()->GetImagePointerSize(); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 849 | for (auto& m : klass->GetMethods(pointer_size)) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 850 | SanityCheckArtMethod(&m, klass, image_spaces); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 851 | } |
| 852 | auto* vtable = klass->GetVTable(); |
| 853 | if (vtable != nullptr) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 854 | SanityCheckArtMethodPointerArray(vtable, nullptr, pointer_size, image_spaces); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 855 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 856 | if (klass->ShouldHaveImt()) { |
| 857 | ImTable* imt = klass->GetImt(pointer_size); |
| 858 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 859 | SanityCheckArtMethod(imt->Get(i, pointer_size), nullptr, image_spaces); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 860 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 861 | } |
| 862 | if (klass->ShouldHaveEmbeddedVTable()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 863 | for (int32_t i = 0; i < klass->GetEmbeddedVTableLength(); ++i) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 864 | SanityCheckArtMethod(klass->GetEmbeddedVTableEntry(i, pointer_size), nullptr, image_spaces); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | auto* iftable = klass->GetIfTable(); |
| 868 | if (iftable != nullptr) { |
| 869 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 870 | if (iftable->GetMethodArrayCount(i) > 0) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 871 | SanityCheckArtMethodPointerArray( |
| 872 | iftable->GetMethodArray(i), nullptr, pointer_size, image_spaces); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 879 | // Set image methods' entry point to interpreter. |
| 880 | class SetInterpreterEntrypointArtMethodVisitor : public ArtMethodVisitor { |
| 881 | public: |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 882 | explicit SetInterpreterEntrypointArtMethodVisitor(PointerSize image_pointer_size) |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 883 | : image_pointer_size_(image_pointer_size) {} |
| 884 | |
| 885 | void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
| 886 | if (kIsDebugBuild && !method->IsRuntimeMethod()) { |
| 887 | CHECK(method->GetDeclaringClass() != nullptr); |
| 888 | } |
| 889 | if (!method->IsNative() && !method->IsRuntimeMethod() && !method->IsResolutionMethod()) { |
| 890 | method->SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), |
| 891 | image_pointer_size_); |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | private: |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 896 | const PointerSize image_pointer_size_; |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 897 | |
| 898 | DISALLOW_COPY_AND_ASSIGN(SetInterpreterEntrypointArtMethodVisitor); |
| 899 | }; |
| 900 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 901 | struct TrampolineCheckData { |
| 902 | const void* quick_resolution_trampoline; |
| 903 | const void* quick_imt_conflict_trampoline; |
| 904 | const void* quick_generic_jni_trampoline; |
| 905 | const void* quick_to_interpreter_bridge_trampoline; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 906 | PointerSize pointer_size; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 907 | ArtMethod* m; |
| 908 | bool error; |
| 909 | }; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 910 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 911 | static void CheckTrampolines(mirror::Object* obj, void* arg) NO_THREAD_SAFETY_ANALYSIS { |
| 912 | if (obj->IsClass()) { |
| 913 | mirror::Class* klass = obj->AsClass(); |
| 914 | TrampolineCheckData* d = reinterpret_cast<TrampolineCheckData*>(arg); |
| 915 | for (ArtMethod& m : klass->GetMethods(d->pointer_size)) { |
| 916 | const void* entrypoint = m.GetEntryPointFromQuickCompiledCodePtrSize(d->pointer_size); |
| 917 | if (entrypoint == d->quick_resolution_trampoline || |
| 918 | entrypoint == d->quick_imt_conflict_trampoline || |
| 919 | entrypoint == d->quick_generic_jni_trampoline || |
| 920 | entrypoint == d->quick_to_interpreter_bridge_trampoline) { |
| 921 | d->m = &m; |
| 922 | d->error = true; |
| 923 | return; |
| 924 | } |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 929 | bool ClassLinker::InitFromBootImage(std::string* error_msg) { |
| 930 | VLOG(startup) << __FUNCTION__ << " entering"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 931 | CHECK(!init_done_); |
| 932 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 933 | Runtime* const runtime = Runtime::Current(); |
| 934 | Thread* const self = Thread::Current(); |
| 935 | gc::Heap* const heap = runtime->GetHeap(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 936 | std::vector<gc::space::ImageSpace*> spaces = heap->GetBootImageSpaces(); |
| 937 | CHECK(!spaces.empty()); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 938 | uint32_t pointer_size_unchecked = spaces[0]->GetImageHeader().GetPointerSizeUnchecked(); |
| 939 | if (!ValidPointerSize(pointer_size_unchecked)) { |
| 940 | *error_msg = StringPrintf("Invalid image pointer size: %u", pointer_size_unchecked); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 941 | return false; |
| 942 | } |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 943 | image_pointer_size_ = spaces[0]->GetImageHeader().GetPointerSize(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 944 | if (!runtime->IsAotCompiler()) { |
| 945 | // Only the Aot compiler supports having an image with a different pointer size than the |
| 946 | // runtime. This happens on the host for compiling 32 bit tests since we use a 64 bit libart |
| 947 | // compiler. We may also use 32 bit dex2oat on a system with 64 bit apps. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 948 | if (image_pointer_size_ != kRuntimePointerSize) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 949 | *error_msg = StringPrintf("Runtime must use current image pointer size: %zu vs %zu", |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 950 | static_cast<size_t>(image_pointer_size_), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 951 | sizeof(void*)); |
| 952 | return false; |
| 953 | } |
| 954 | } |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 955 | dex_cache_boot_image_class_lookup_required_ = true; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 956 | std::vector<const OatFile*> oat_files = |
| 957 | runtime->GetOatFileManager().RegisterImageOatFiles(spaces); |
| 958 | DCHECK(!oat_files.empty()); |
| 959 | const OatHeader& default_oat_header = oat_files[0]->GetOatHeader(); |
| 960 | CHECK_EQ(default_oat_header.GetImageFileLocationOatChecksum(), 0U); |
| 961 | CHECK_EQ(default_oat_header.GetImageFileLocationOatDataBegin(), 0U); |
| 962 | const char* image_file_location = oat_files[0]->GetOatHeader(). |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 963 | GetStoreValueByKey(OatHeader::kImageLocationKey); |
| 964 | CHECK(image_file_location == nullptr || *image_file_location == 0); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 965 | quick_resolution_trampoline_ = default_oat_header.GetQuickResolutionTrampoline(); |
| 966 | quick_imt_conflict_trampoline_ = default_oat_header.GetQuickImtConflictTrampoline(); |
| 967 | quick_generic_jni_trampoline_ = default_oat_header.GetQuickGenericJniTrampoline(); |
| 968 | quick_to_interpreter_bridge_trampoline_ = default_oat_header.GetQuickToInterpreterBridge(); |
| 969 | if (kIsDebugBuild) { |
| 970 | // Check that the other images use the same trampoline. |
| 971 | for (size_t i = 1; i < oat_files.size(); ++i) { |
| 972 | const OatHeader& ith_oat_header = oat_files[i]->GetOatHeader(); |
| 973 | const void* ith_quick_resolution_trampoline = |
| 974 | ith_oat_header.GetQuickResolutionTrampoline(); |
| 975 | const void* ith_quick_imt_conflict_trampoline = |
| 976 | ith_oat_header.GetQuickImtConflictTrampoline(); |
| 977 | const void* ith_quick_generic_jni_trampoline = |
| 978 | ith_oat_header.GetQuickGenericJniTrampoline(); |
| 979 | const void* ith_quick_to_interpreter_bridge_trampoline = |
| 980 | ith_oat_header.GetQuickToInterpreterBridge(); |
| 981 | if (ith_quick_resolution_trampoline != quick_resolution_trampoline_ || |
| 982 | ith_quick_imt_conflict_trampoline != quick_imt_conflict_trampoline_ || |
| 983 | ith_quick_generic_jni_trampoline != quick_generic_jni_trampoline_ || |
| 984 | ith_quick_to_interpreter_bridge_trampoline != quick_to_interpreter_bridge_trampoline_) { |
| 985 | // Make sure that all methods in this image do not contain those trampolines as |
| 986 | // entrypoints. Otherwise the class-linker won't be able to work with a single set. |
| 987 | TrampolineCheckData data; |
| 988 | data.error = false; |
| 989 | data.pointer_size = GetImagePointerSize(); |
| 990 | data.quick_resolution_trampoline = ith_quick_resolution_trampoline; |
| 991 | data.quick_imt_conflict_trampoline = ith_quick_imt_conflict_trampoline; |
| 992 | data.quick_generic_jni_trampoline = ith_quick_generic_jni_trampoline; |
| 993 | data.quick_to_interpreter_bridge_trampoline = ith_quick_to_interpreter_bridge_trampoline; |
| 994 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 995 | spaces[i]->GetLiveBitmap()->Walk(CheckTrampolines, &data); |
| 996 | if (data.error) { |
| 997 | ArtMethod* m = data.m; |
| 998 | LOG(ERROR) << "Found a broken ArtMethod: " << PrettyMethod(m); |
| 999 | *error_msg = "Found an ArtMethod with a bad entrypoint"; |
| 1000 | return false; |
| 1001 | } |
| 1002 | } |
| 1003 | } |
| 1004 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1005 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1006 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
| 1007 | down_cast<mirror::ObjectArray<mirror::Class>*>( |
| 1008 | spaces[0]->GetImageHeader().GetImageRoot(ImageHeader::kClassRoots))); |
| 1009 | mirror::Class::SetClassClass(class_roots_.Read()->Get(kJavaLangClass)); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1010 | |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1011 | // Special case of setting up the String class early so that we can test arbitrary objects |
| 1012 | // as being Strings or not |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 1013 | mirror::String::SetClass(GetClassRoot(kJavaLangString)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1014 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1015 | mirror::Class* java_lang_Object = GetClassRoot(kJavaLangObject); |
| 1016 | java_lang_Object->SetObjectSize(sizeof(mirror::Object)); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 1017 | // Allocate in non-movable so that it's possible to check if a JNI weak global ref has been |
| 1018 | // cleared without triggering the read barrier and unintentionally mark the sentinel alive. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1019 | runtime->SetSentinel(heap->AllocNonMovableObject<true>( |
| 1020 | self, java_lang_Object, java_lang_Object->GetObjectSize(), VoidFunctor())); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1021 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1022 | // reinit array_iftable_ from any array class instance, they should be == |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1023 | array_iftable_ = GcRoot<mirror::IfTable>(GetClassRoot(kObjectArrayClass)->GetIfTable()); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 1024 | DCHECK_EQ(array_iftable_.Read(), GetClassRoot(kBooleanArrayClass)->GetIfTable()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1025 | // String class root was set above |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 1026 | mirror::Field::SetClass(GetClassRoot(kJavaLangReflectField)); |
| 1027 | mirror::Field::SetArrayClass(GetClassRoot(kJavaLangReflectFieldArrayClass)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1028 | mirror::Constructor::SetClass(GetClassRoot(kJavaLangReflectConstructor)); |
| 1029 | mirror::Constructor::SetArrayClass(GetClassRoot(kJavaLangReflectConstructorArrayClass)); |
| 1030 | mirror::Method::SetClass(GetClassRoot(kJavaLangReflectMethod)); |
| 1031 | mirror::Method::SetArrayClass(GetClassRoot(kJavaLangReflectMethodArrayClass)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 1032 | mirror::Reference::SetClass(GetClassRoot(kJavaLangRefReference)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1033 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
| 1034 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
| 1035 | mirror::CharArray::SetArrayClass(GetClassRoot(kCharArrayClass)); |
| 1036 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
| 1037 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
| 1038 | mirror::IntArray::SetArrayClass(GetClassRoot(kIntArrayClass)); |
| 1039 | mirror::LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
| 1040 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
| 1041 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
| 1042 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1043 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1044 | for (gc::space::ImageSpace* image_space : spaces) { |
| 1045 | // Boot class loader, use a null handle. |
| 1046 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1047 | if (!AddImageSpace(image_space, |
| 1048 | ScopedNullHandle<mirror::ClassLoader>(), |
| 1049 | /*dex_elements*/nullptr, |
| 1050 | /*dex_location*/nullptr, |
| 1051 | /*out*/&dex_files, |
| 1052 | error_msg)) { |
| 1053 | return false; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1054 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1055 | // Append opened dex files at the end. |
| 1056 | boot_dex_files_.insert(boot_dex_files_.end(), |
| 1057 | std::make_move_iterator(dex_files.begin()), |
| 1058 | std::make_move_iterator(dex_files.end())); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1059 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1060 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1061 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1062 | VLOG(startup) << __FUNCTION__ << " exiting"; |
| 1063 | return true; |
| 1064 | } |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 1065 | |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 1066 | bool ClassLinker::IsBootClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
| 1067 | mirror::ClassLoader* class_loader) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1068 | return class_loader == nullptr || |
| 1069 | class_loader->GetClass() == |
| 1070 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_BootClassLoader); |
| 1071 | } |
| 1072 | |
| 1073 | static mirror::String* GetDexPathListElementName(ScopedObjectAccessUnchecked& soa, |
| 1074 | mirror::Object* element) |
| 1075 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1076 | ArtField* const dex_file_field = |
| 1077 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
| 1078 | ArtField* const dex_file_name_field = |
| 1079 | soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_fileName); |
| 1080 | DCHECK(dex_file_field != nullptr); |
| 1081 | DCHECK(dex_file_name_field != nullptr); |
| 1082 | DCHECK(element != nullptr); |
| 1083 | CHECK_EQ(dex_file_field->GetDeclaringClass(), element->GetClass()) << PrettyTypeOf(element); |
| 1084 | mirror::Object* dex_file = dex_file_field->GetObject(element); |
| 1085 | if (dex_file == nullptr) { |
| 1086 | return nullptr; |
| 1087 | } |
| 1088 | mirror::Object* const name_object = dex_file_name_field->GetObject(dex_file); |
| 1089 | if (name_object != nullptr) { |
| 1090 | return name_object->AsString(); |
| 1091 | } |
| 1092 | return nullptr; |
| 1093 | } |
| 1094 | |
| 1095 | static bool FlattenPathClassLoader(mirror::ClassLoader* class_loader, |
| 1096 | std::list<mirror::String*>* out_dex_file_names, |
| 1097 | std::string* error_msg) |
| 1098 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1099 | DCHECK(out_dex_file_names != nullptr); |
| 1100 | DCHECK(error_msg != nullptr); |
| 1101 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1102 | ArtField* const dex_path_list_field = |
| 1103 | soa.DecodeField(WellKnownClasses::dalvik_system_PathClassLoader_pathList); |
| 1104 | ArtField* const dex_elements_field = |
| 1105 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList_dexElements); |
| 1106 | CHECK(dex_path_list_field != nullptr); |
| 1107 | CHECK(dex_elements_field != nullptr); |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 1108 | while (!ClassLinker::IsBootClassLoader(soa, class_loader)) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1109 | if (class_loader->GetClass() != |
| 1110 | soa.Decode<mirror::Class*>(WellKnownClasses::dalvik_system_PathClassLoader)) { |
| 1111 | *error_msg = StringPrintf("Unknown class loader type %s", PrettyTypeOf(class_loader).c_str()); |
| 1112 | // Unsupported class loader. |
| 1113 | return false; |
| 1114 | } |
| 1115 | mirror::Object* dex_path_list = dex_path_list_field->GetObject(class_loader); |
| 1116 | if (dex_path_list != nullptr) { |
| 1117 | // DexPathList has an array dexElements of Elements[] which each contain a dex file. |
| 1118 | mirror::Object* dex_elements_obj = dex_elements_field->GetObject(dex_path_list); |
| 1119 | // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look |
| 1120 | // at the mCookie which is a DexFile vector. |
| 1121 | if (dex_elements_obj != nullptr) { |
| 1122 | mirror::ObjectArray<mirror::Object>* dex_elements = |
| 1123 | dex_elements_obj->AsObjectArray<mirror::Object>(); |
| 1124 | // Reverse order since we insert the parent at the front. |
| 1125 | for (int32_t i = dex_elements->GetLength() - 1; i >= 0; --i) { |
| 1126 | mirror::Object* const element = dex_elements->GetWithoutChecks(i); |
| 1127 | if (element == nullptr) { |
| 1128 | *error_msg = StringPrintf("Null dex element at index %d", i); |
| 1129 | return false; |
| 1130 | } |
| 1131 | mirror::String* const name = GetDexPathListElementName(soa, element); |
| 1132 | if (name == nullptr) { |
| 1133 | *error_msg = StringPrintf("Null name for dex element at index %d", i); |
| 1134 | return false; |
| 1135 | } |
| 1136 | out_dex_file_names->push_front(name); |
| 1137 | } |
| 1138 | } |
| 1139 | } |
| 1140 | class_loader = class_loader->GetParent(); |
| 1141 | } |
| 1142 | return true; |
| 1143 | } |
| 1144 | |
| 1145 | class FixupArtMethodArrayVisitor : public ArtMethodVisitor { |
| 1146 | public: |
| 1147 | explicit FixupArtMethodArrayVisitor(const ImageHeader& header) : header_(header) {} |
| 1148 | |
| 1149 | virtual void Visit(ArtMethod* method) SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1150 | GcRoot<mirror::Class>* resolved_types = method->GetDexCacheResolvedTypes(kRuntimePointerSize); |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 1151 | const bool is_copied = method->IsCopied(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1152 | if (resolved_types != nullptr) { |
| 1153 | bool in_image_space = false; |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 1154 | if (kIsDebugBuild || is_copied) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1155 | in_image_space = header_.GetImageSection(ImageHeader::kSectionDexCacheArrays).Contains( |
| 1156 | reinterpret_cast<const uint8_t*>(resolved_types) - header_.GetImageBegin()); |
| 1157 | } |
| 1158 | // Must be in image space for non-miranda method. |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 1159 | DCHECK(is_copied || in_image_space) |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1160 | << resolved_types << " is not in image starting at " |
| 1161 | << reinterpret_cast<void*>(header_.GetImageBegin()); |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 1162 | if (!is_copied || in_image_space) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1163 | // Go through the array so that we don't need to do a slow map lookup. |
| 1164 | method->SetDexCacheResolvedTypes(*reinterpret_cast<GcRoot<mirror::Class>**>(resolved_types), |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1165 | kRuntimePointerSize); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1166 | } |
| 1167 | } |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1168 | ArtMethod** resolved_methods = method->GetDexCacheResolvedMethods(kRuntimePointerSize); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1169 | if (resolved_methods != nullptr) { |
| 1170 | bool in_image_space = false; |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 1171 | if (kIsDebugBuild || is_copied) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1172 | in_image_space = header_.GetImageSection(ImageHeader::kSectionDexCacheArrays).Contains( |
| 1173 | reinterpret_cast<const uint8_t*>(resolved_methods) - header_.GetImageBegin()); |
| 1174 | } |
| 1175 | // Must be in image space for non-miranda method. |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 1176 | DCHECK(is_copied || in_image_space) |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1177 | << resolved_methods << " is not in image starting at " |
| 1178 | << reinterpret_cast<void*>(header_.GetImageBegin()); |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 1179 | if (!is_copied || in_image_space) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1180 | // Go through the array so that we don't need to do a slow map lookup. |
| 1181 | method->SetDexCacheResolvedMethods(*reinterpret_cast<ArtMethod***>(resolved_methods), |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1182 | kRuntimePointerSize); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1183 | } |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | private: |
| 1188 | const ImageHeader& header_; |
| 1189 | }; |
| 1190 | |
| 1191 | class VerifyClassInTableArtMethodVisitor : public ArtMethodVisitor { |
| 1192 | public: |
| 1193 | explicit VerifyClassInTableArtMethodVisitor(ClassTable* table) : table_(table) {} |
| 1194 | |
| 1195 | virtual void Visit(ArtMethod* method) |
| 1196 | SHARED_REQUIRES(Locks::mutator_lock_, Locks::classlinker_classes_lock_) { |
| 1197 | mirror::Class* klass = method->GetDeclaringClass(); |
| 1198 | if (klass != nullptr && !Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) { |
| 1199 | CHECK_EQ(table_->LookupByDescriptor(klass), klass) << PrettyClass(klass); |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | private: |
| 1204 | ClassTable* const table_; |
| 1205 | }; |
| 1206 | |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1207 | class VerifyDeclaringClassVisitor : public ArtMethodVisitor { |
| 1208 | public: |
| 1209 | VerifyDeclaringClassVisitor() SHARED_REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) |
| 1210 | : live_bitmap_(Runtime::Current()->GetHeap()->GetLiveBitmap()) {} |
| 1211 | |
| 1212 | virtual void Visit(ArtMethod* method) |
| 1213 | SHARED_REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
| 1214 | mirror::Class* klass = method->GetDeclaringClassUnchecked(); |
| 1215 | if (klass != nullptr) { |
| 1216 | CHECK(live_bitmap_->Test(klass)) << "Image method has unmarked declaring class"; |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | private: |
| 1221 | gc::accounting::HeapBitmap* const live_bitmap_; |
| 1222 | }; |
| 1223 | |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1224 | bool ClassLinker::UpdateAppImageClassLoadersAndDexCaches( |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1225 | gc::space::ImageSpace* space, |
| 1226 | Handle<mirror::ClassLoader> class_loader, |
| 1227 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches, |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1228 | ClassTable::ClassSet* new_class_set, |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1229 | bool* out_forward_dex_cache_array, |
| 1230 | std::string* out_error_msg) { |
| 1231 | DCHECK(out_forward_dex_cache_array != nullptr); |
| 1232 | DCHECK(out_error_msg != nullptr); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1233 | Thread* const self = Thread::Current(); |
| 1234 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
| 1235 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 0c344f2 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1236 | { |
| 1237 | // Add image classes into the class table for the class loader, and fixup the dex caches and |
| 1238 | // class loader fields. |
| 1239 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 1240 | ClassTable* table = InsertClassTableForClassLoader(class_loader.Get()); |
| 1241 | // Dex cache array fixup is all or nothing, we must reject app images that have mixed since we |
| 1242 | // rely on clobering the dex cache arrays in the image to forward to bss. |
| 1243 | size_t num_dex_caches_with_bss_arrays = 0; |
| 1244 | const size_t num_dex_caches = dex_caches->GetLength(); |
| 1245 | for (size_t i = 0; i < num_dex_caches; i++) { |
| 1246 | mirror::DexCache* const dex_cache = dex_caches->Get(i); |
| 1247 | const DexFile* const dex_file = dex_cache->GetDexFile(); |
| 1248 | const OatFile::OatDexFile* oat_dex_file = dex_file->GetOatDexFile(); |
| 1249 | if (oat_dex_file != nullptr && oat_dex_file->GetDexCacheArrays() != nullptr) { |
| 1250 | ++num_dex_caches_with_bss_arrays; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1251 | } |
| 1252 | } |
Mathieu Chartier | 0c344f2 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1253 | *out_forward_dex_cache_array = num_dex_caches_with_bss_arrays != 0; |
| 1254 | if (*out_forward_dex_cache_array) { |
| 1255 | if (num_dex_caches_with_bss_arrays != num_dex_caches) { |
| 1256 | // Reject application image since we cannot forward only some of the dex cache arrays. |
| 1257 | // TODO: We could get around this by having a dedicated forwarding slot. It should be an |
| 1258 | // uncommon case. |
| 1259 | *out_error_msg = StringPrintf("Dex caches in bss does not match total: %zu vs %zu", |
| 1260 | num_dex_caches_with_bss_arrays, |
| 1261 | num_dex_caches); |
| 1262 | return false; |
| 1263 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1264 | } |
Mathieu Chartier | 0c344f2 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1265 | // Only add the classes to the class loader after the points where we can return false. |
| 1266 | for (size_t i = 0; i < num_dex_caches; i++) { |
| 1267 | mirror::DexCache* const dex_cache = dex_caches->Get(i); |
| 1268 | const DexFile* const dex_file = dex_cache->GetDexFile(); |
| 1269 | const OatFile::OatDexFile* oat_dex_file = dex_file->GetOatDexFile(); |
| 1270 | if (oat_dex_file != nullptr && oat_dex_file->GetDexCacheArrays() != nullptr) { |
| 1271 | // If the oat file expects the dex cache arrays to be in the BSS, then allocate there and |
| 1272 | // copy over the arrays. |
| 1273 | DCHECK(dex_file != nullptr); |
| 1274 | const size_t num_strings = dex_file->NumStringIds(); |
| 1275 | const size_t num_types = dex_file->NumTypeIds(); |
| 1276 | const size_t num_methods = dex_file->NumMethodIds(); |
| 1277 | const size_t num_fields = dex_file->NumFieldIds(); |
| 1278 | CHECK_EQ(num_strings, dex_cache->NumStrings()); |
| 1279 | CHECK_EQ(num_types, dex_cache->NumResolvedTypes()); |
| 1280 | CHECK_EQ(num_methods, dex_cache->NumResolvedMethods()); |
| 1281 | CHECK_EQ(num_fields, dex_cache->NumResolvedFields()); |
| 1282 | DexCacheArraysLayout layout(image_pointer_size_, dex_file); |
| 1283 | uint8_t* const raw_arrays = oat_dex_file->GetDexCacheArrays(); |
| 1284 | // The space is not yet visible to the GC, we can avoid the read barriers and use |
| 1285 | // std::copy_n. |
| 1286 | if (num_strings != 0u) { |
| 1287 | GcRoot<mirror::String>* const image_resolved_strings = dex_cache->GetStrings(); |
| 1288 | GcRoot<mirror::String>* const strings = |
| 1289 | reinterpret_cast<GcRoot<mirror::String>*>(raw_arrays + layout.StringsOffset()); |
| 1290 | for (size_t j = 0; kIsDebugBuild && j < num_strings; ++j) { |
| 1291 | DCHECK(strings[j].IsNull()); |
Mathieu Chartier | 9b1c9b7 | 2016-02-02 10:09:58 -0800 | [diff] [blame] | 1292 | } |
Mathieu Chartier | 0c344f2 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1293 | std::copy_n(image_resolved_strings, num_strings, strings); |
| 1294 | dex_cache->SetStrings(strings); |
| 1295 | } |
| 1296 | if (num_types != 0u) { |
| 1297 | GcRoot<mirror::Class>* const image_resolved_types = dex_cache->GetResolvedTypes(); |
| 1298 | GcRoot<mirror::Class>* const types = |
| 1299 | reinterpret_cast<GcRoot<mirror::Class>*>(raw_arrays + layout.TypesOffset()); |
| 1300 | for (size_t j = 0; kIsDebugBuild && j < num_types; ++j) { |
| 1301 | DCHECK(types[j].IsNull()); |
| 1302 | } |
| 1303 | std::copy_n(image_resolved_types, num_types, types); |
| 1304 | // Store a pointer to the new location for fast ArtMethod patching without requiring map. |
| 1305 | // This leaves random garbage at the start of the dex cache array, but nobody should ever |
| 1306 | // read from it again. |
| 1307 | *reinterpret_cast<GcRoot<mirror::Class>**>(image_resolved_types) = types; |
| 1308 | dex_cache->SetResolvedTypes(types); |
| 1309 | } |
| 1310 | if (num_methods != 0u) { |
| 1311 | ArtMethod** const methods = reinterpret_cast<ArtMethod**>( |
| 1312 | raw_arrays + layout.MethodsOffset()); |
| 1313 | ArtMethod** const image_resolved_methods = dex_cache->GetResolvedMethods(); |
| 1314 | for (size_t j = 0; kIsDebugBuild && j < num_methods; ++j) { |
| 1315 | DCHECK(methods[j] == nullptr); |
| 1316 | } |
| 1317 | std::copy_n(image_resolved_methods, num_methods, methods); |
| 1318 | // Store a pointer to the new location for fast ArtMethod patching without requiring map. |
| 1319 | *reinterpret_cast<ArtMethod***>(image_resolved_methods) = methods; |
| 1320 | dex_cache->SetResolvedMethods(methods); |
| 1321 | } |
| 1322 | if (num_fields != 0u) { |
| 1323 | ArtField** const fields = |
| 1324 | reinterpret_cast<ArtField**>(raw_arrays + layout.FieldsOffset()); |
| 1325 | for (size_t j = 0; kIsDebugBuild && j < num_fields; ++j) { |
| 1326 | DCHECK(fields[j] == nullptr); |
| 1327 | } |
| 1328 | std::copy_n(dex_cache->GetResolvedFields(), num_fields, fields); |
| 1329 | dex_cache->SetResolvedFields(fields); |
| 1330 | } |
| 1331 | } |
| 1332 | { |
| 1333 | WriterMutexLock mu2(self, dex_lock_); |
| 1334 | // Make sure to do this after we update the arrays since we store the resolved types array |
| 1335 | // in DexCacheData in RegisterDexFileLocked. We need the array pointer to be the one in the |
| 1336 | // BSS. |
| 1337 | mirror::DexCache* existing_dex_cache = FindDexCacheLocked(self, |
| 1338 | *dex_file, |
| 1339 | /*allow_failure*/true); |
| 1340 | CHECK(existing_dex_cache == nullptr); |
| 1341 | StackHandleScope<1> hs3(self); |
| 1342 | RegisterDexFileLocked(*dex_file, hs3.NewHandle(dex_cache)); |
| 1343 | } |
| 1344 | GcRoot<mirror::Class>* const types = dex_cache->GetResolvedTypes(); |
| 1345 | const size_t num_types = dex_cache->NumResolvedTypes(); |
| 1346 | if (new_class_set == nullptr) { |
| 1347 | for (int32_t j = 0; j < static_cast<int32_t>(num_types); j++) { |
| 1348 | // The image space is not yet added to the heap, avoid read barriers. |
| 1349 | mirror::Class* klass = types[j].Read(); |
Mathieu Chartier | 394fac5 | 2016-05-24 17:41:21 -0700 | [diff] [blame] | 1350 | // There may also be boot image classes, |
| 1351 | if (space->HasAddress(klass)) { |
Mathieu Chartier | 0c344f2 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1352 | DCHECK_NE(klass->GetStatus(), mirror::Class::kStatusError); |
| 1353 | // Update the class loader from the one in the image class loader to the one that loaded |
| 1354 | // the app image. |
| 1355 | klass->SetClassLoader(class_loader.Get()); |
| 1356 | // The resolved type could be from another dex cache, go through the dex cache just in |
| 1357 | // case. May be null for array classes. |
| 1358 | if (klass->GetDexCacheStrings() != nullptr) { |
| 1359 | DCHECK(!klass->IsArrayClass()); |
| 1360 | klass->SetDexCacheStrings(klass->GetDexCache()->GetStrings()); |
| 1361 | } |
| 1362 | // If there are multiple dex caches, there may be the same class multiple times |
| 1363 | // in different dex caches. Check for this since inserting will add duplicates |
| 1364 | // otherwise. |
| 1365 | if (num_dex_caches > 1) { |
| 1366 | mirror::Class* existing = table->LookupByDescriptor(klass); |
| 1367 | if (existing != nullptr) { |
| 1368 | DCHECK_EQ(existing, klass) << PrettyClass(klass); |
| 1369 | } else { |
| 1370 | table->Insert(klass); |
| 1371 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1372 | } else { |
| 1373 | table->Insert(klass); |
| 1374 | } |
Mathieu Chartier | 0c344f2 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1375 | // Double checked VLOG to avoid overhead. |
| 1376 | if (VLOG_IS_ON(image)) { |
| 1377 | VLOG(image) << PrettyClass(klass) << " " << klass->GetStatus(); |
| 1378 | if (!klass->IsArrayClass()) { |
| 1379 | VLOG(image) << "From " << klass->GetDexCache()->GetDexFile()->GetBaseLocation(); |
| 1380 | } |
| 1381 | VLOG(image) << "Direct methods"; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1382 | for (ArtMethod& m : klass->GetDirectMethods(kRuntimePointerSize)) { |
Mathieu Chartier | 0c344f2 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1383 | VLOG(image) << PrettyMethod(&m); |
| 1384 | } |
| 1385 | VLOG(image) << "Virtual methods"; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1386 | for (ArtMethod& m : klass->GetVirtualMethods(kRuntimePointerSize)) { |
Mathieu Chartier | 0c344f2 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1387 | VLOG(image) << PrettyMethod(&m); |
| 1388 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1389 | } |
Mathieu Chartier | 394fac5 | 2016-05-24 17:41:21 -0700 | [diff] [blame] | 1390 | } else { |
| 1391 | DCHECK(klass == nullptr || heap->ObjectIsInBootImageSpace(klass)) |
| 1392 | << klass << " " << PrettyClass(klass); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1393 | } |
| 1394 | } |
| 1395 | } |
Mathieu Chartier | 0c344f2 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1396 | if (kIsDebugBuild) { |
| 1397 | for (int32_t j = 0; j < static_cast<int32_t>(num_types); j++) { |
| 1398 | // The image space is not yet added to the heap, avoid read barriers. |
| 1399 | mirror::Class* klass = types[j].Read(); |
Mathieu Chartier | 394fac5 | 2016-05-24 17:41:21 -0700 | [diff] [blame] | 1400 | if (space->HasAddress(klass)) { |
Mathieu Chartier | 0c344f2 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1401 | DCHECK_NE(klass->GetStatus(), mirror::Class::kStatusError); |
| 1402 | if (kIsDebugBuild) { |
Mathieu Chartier | 394fac5 | 2016-05-24 17:41:21 -0700 | [diff] [blame] | 1403 | if (new_class_set != nullptr) { |
Mathieu Chartier | 0c344f2 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1404 | auto it = new_class_set->Find(GcRoot<mirror::Class>(klass)); |
| 1405 | DCHECK(it != new_class_set->end()); |
| 1406 | DCHECK_EQ(it->Read(), klass); |
| 1407 | mirror::Class* super_class = klass->GetSuperClass(); |
| 1408 | if (super_class != nullptr && !heap->ObjectIsInBootImageSpace(super_class)) { |
| 1409 | auto it2 = new_class_set->Find(GcRoot<mirror::Class>(super_class)); |
| 1410 | DCHECK(it2 != new_class_set->end()); |
| 1411 | DCHECK_EQ(it2->Read(), super_class); |
| 1412 | } |
| 1413 | } else { |
| 1414 | DCHECK_EQ(table->LookupByDescriptor(klass), klass); |
| 1415 | mirror::Class* super_class = klass->GetSuperClass(); |
| 1416 | if (super_class != nullptr && !heap->ObjectIsInBootImageSpace(super_class)) { |
| 1417 | CHECK_EQ(table->LookupByDescriptor(super_class), super_class); |
| 1418 | } |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1419 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1420 | } |
Mathieu Chartier | 0c344f2 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1421 | if (kIsDebugBuild) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1422 | for (ArtMethod& m : klass->GetDirectMethods(kRuntimePointerSize)) { |
Mathieu Chartier | 0c344f2 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1423 | const void* code = m.GetEntryPointFromQuickCompiledCode(); |
| 1424 | const void* oat_code = m.IsInvokable() ? GetQuickOatCodeFor(&m) : code; |
| 1425 | if (!IsQuickResolutionStub(code) && |
| 1426 | !IsQuickGenericJniStub(code) && |
| 1427 | !IsQuickToInterpreterBridge(code) && |
| 1428 | !m.IsNative()) { |
| 1429 | DCHECK_EQ(code, oat_code) << PrettyMethod(&m); |
| 1430 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1431 | } |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1432 | for (ArtMethod& m : klass->GetVirtualMethods(kRuntimePointerSize)) { |
Mathieu Chartier | 0c344f2 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1433 | const void* code = m.GetEntryPointFromQuickCompiledCode(); |
| 1434 | const void* oat_code = m.IsInvokable() ? GetQuickOatCodeFor(&m) : code; |
| 1435 | if (!IsQuickResolutionStub(code) && |
| 1436 | !IsQuickGenericJniStub(code) && |
| 1437 | !IsQuickToInterpreterBridge(code) && |
| 1438 | !m.IsNative()) { |
| 1439 | DCHECK_EQ(code, oat_code) << PrettyMethod(&m); |
| 1440 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | } |
| 1445 | } |
| 1446 | } |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1447 | } |
| 1448 | if (*out_forward_dex_cache_array) { |
| 1449 | ScopedTrace timing("Fixup ArtMethod dex cache arrays"); |
| 1450 | FixupArtMethodArrayVisitor visitor(header); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1451 | header.VisitPackedArtMethods(&visitor, space->Begin(), kRuntimePointerSize); |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1452 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader.Get()); |
| 1453 | } |
| 1454 | if (kVerifyArtMethodDeclaringClasses) { |
| 1455 | ScopedTrace timing("Verify declaring classes"); |
| 1456 | ReaderMutexLock rmu(self, *Locks::heap_bitmap_lock_); |
| 1457 | VerifyDeclaringClassVisitor visitor; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1458 | header.VisitPackedArtMethods(&visitor, space->Begin(), kRuntimePointerSize); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1459 | } |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1460 | return true; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1461 | } |
| 1462 | |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1463 | // Update the class loader and resolved string dex cache array of classes. Should only be used on |
| 1464 | // classes in the image space. |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1465 | class UpdateClassLoaderAndResolvedStringsVisitor { |
| 1466 | public: |
| 1467 | UpdateClassLoaderAndResolvedStringsVisitor(gc::space::ImageSpace* space, |
| 1468 | mirror::ClassLoader* class_loader, |
| 1469 | bool forward_strings) |
| 1470 | : space_(space), |
| 1471 | class_loader_(class_loader), |
| 1472 | forward_strings_(forward_strings) {} |
| 1473 | |
| 1474 | bool operator()(mirror::Class* klass) const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1475 | if (forward_strings_) { |
| 1476 | GcRoot<mirror::String>* strings = klass->GetDexCacheStrings(); |
| 1477 | if (strings != nullptr) { |
Vladimir Marko | 69a0405 | 2016-02-02 14:43:28 +0000 | [diff] [blame] | 1478 | DCHECK( |
| 1479 | space_->GetImageHeader().GetImageSection(ImageHeader::kSectionDexCacheArrays).Contains( |
| 1480 | reinterpret_cast<uint8_t*>(strings) - space_->Begin())) |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1481 | << "String dex cache array for " << PrettyClass(klass) << " is not in app image"; |
Vladimir Marko | 69a0405 | 2016-02-02 14:43:28 +0000 | [diff] [blame] | 1482 | // Dex caches have already been updated, so take the strings pointer from there. |
| 1483 | GcRoot<mirror::String>* new_strings = klass->GetDexCache()->GetStrings(); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1484 | DCHECK_NE(strings, new_strings); |
| 1485 | klass->SetDexCacheStrings(new_strings); |
| 1486 | } |
| 1487 | } |
| 1488 | // Finally, update class loader. |
| 1489 | klass->SetClassLoader(class_loader_); |
| 1490 | return true; |
| 1491 | } |
| 1492 | |
| 1493 | gc::space::ImageSpace* const space_; |
| 1494 | mirror::ClassLoader* const class_loader_; |
| 1495 | const bool forward_strings_; |
| 1496 | }; |
| 1497 | |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1498 | static std::unique_ptr<const DexFile> OpenOatDexFile(const OatFile* oat_file, |
| 1499 | const char* location, |
| 1500 | std::string* error_msg) |
| 1501 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1502 | DCHECK(error_msg != nullptr); |
| 1503 | std::unique_ptr<const DexFile> dex_file; |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 1504 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(location, nullptr, error_msg); |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1505 | if (oat_dex_file == nullptr) { |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1506 | return std::unique_ptr<const DexFile>(); |
| 1507 | } |
| 1508 | std::string inner_error_msg; |
| 1509 | dex_file = oat_dex_file->OpenDexFile(&inner_error_msg); |
| 1510 | if (dex_file == nullptr) { |
| 1511 | *error_msg = StringPrintf("Failed to open dex file %s from within oat file %s error '%s'", |
| 1512 | location, |
| 1513 | oat_file->GetLocation().c_str(), |
| 1514 | inner_error_msg.c_str()); |
| 1515 | return std::unique_ptr<const DexFile>(); |
| 1516 | } |
| 1517 | |
| 1518 | if (dex_file->GetLocationChecksum() != oat_dex_file->GetDexFileLocationChecksum()) { |
| 1519 | *error_msg = StringPrintf("Checksums do not match for %s: %x vs %x", |
| 1520 | location, |
| 1521 | dex_file->GetLocationChecksum(), |
| 1522 | oat_dex_file->GetDexFileLocationChecksum()); |
| 1523 | return std::unique_ptr<const DexFile>(); |
| 1524 | } |
| 1525 | return dex_file; |
| 1526 | } |
| 1527 | |
| 1528 | bool ClassLinker::OpenImageDexFiles(gc::space::ImageSpace* space, |
| 1529 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1530 | std::string* error_msg) { |
| 1531 | ScopedAssertNoThreadSuspension nts(Thread::Current(), __FUNCTION__); |
| 1532 | const ImageHeader& header = space->GetImageHeader(); |
| 1533 | mirror::Object* dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
| 1534 | DCHECK(dex_caches_object != nullptr); |
| 1535 | mirror::ObjectArray<mirror::DexCache>* dex_caches = |
| 1536 | dex_caches_object->AsObjectArray<mirror::DexCache>(); |
| 1537 | const OatFile* oat_file = space->GetOatFile(); |
| 1538 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 1539 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 1540 | std::string dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
| 1541 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 1542 | dex_file_location.c_str(), |
| 1543 | error_msg); |
| 1544 | if (dex_file == nullptr) { |
| 1545 | return false; |
| 1546 | } |
| 1547 | dex_cache->SetDexFile(dex_file.get()); |
| 1548 | out_dex_files->push_back(std::move(dex_file)); |
| 1549 | } |
| 1550 | return true; |
| 1551 | } |
| 1552 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1553 | bool ClassLinker::AddImageSpace( |
| 1554 | gc::space::ImageSpace* space, |
| 1555 | Handle<mirror::ClassLoader> class_loader, |
| 1556 | jobjectArray dex_elements, |
| 1557 | const char* dex_location, |
| 1558 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1559 | std::string* error_msg) { |
| 1560 | DCHECK(out_dex_files != nullptr); |
| 1561 | DCHECK(error_msg != nullptr); |
| 1562 | const uint64_t start_time = NanoTime(); |
| 1563 | const bool app_image = class_loader.Get() != nullptr; |
| 1564 | const ImageHeader& header = space->GetImageHeader(); |
| 1565 | mirror::Object* dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
| 1566 | DCHECK(dex_caches_object != nullptr); |
| 1567 | Runtime* const runtime = Runtime::Current(); |
| 1568 | gc::Heap* const heap = runtime->GetHeap(); |
| 1569 | Thread* const self = Thread::Current(); |
| 1570 | StackHandleScope<2> hs(self); |
| 1571 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches( |
| 1572 | hs.NewHandle(dex_caches_object->AsObjectArray<mirror::DexCache>())); |
| 1573 | Handle<mirror::ObjectArray<mirror::Class>> class_roots(hs.NewHandle( |
| 1574 | header.GetImageRoot(ImageHeader::kClassRoots)->AsObjectArray<mirror::Class>())); |
| 1575 | const OatFile* oat_file = space->GetOatFile(); |
| 1576 | std::unordered_set<mirror::ClassLoader*> image_class_loaders; |
| 1577 | // Check that the image is what we are expecting. |
| 1578 | if (image_pointer_size_ != space->GetImageHeader().GetPointerSize()) { |
| 1579 | *error_msg = StringPrintf("Application image pointer size does not match runtime: %zu vs %zu", |
| 1580 | static_cast<size_t>(space->GetImageHeader().GetPointerSize()), |
| 1581 | image_pointer_size_); |
| 1582 | return false; |
| 1583 | } |
| 1584 | DCHECK(class_roots.Get() != nullptr); |
| 1585 | if (class_roots->GetLength() != static_cast<int32_t>(kClassRootsMax)) { |
| 1586 | *error_msg = StringPrintf("Expected %d class roots but got %d", |
| 1587 | class_roots->GetLength(), |
| 1588 | static_cast<int32_t>(kClassRootsMax)); |
| 1589 | return false; |
| 1590 | } |
| 1591 | // Check against existing class roots to make sure they match the ones in the boot image. |
| 1592 | for (size_t i = 0; i < kClassRootsMax; i++) { |
| 1593 | if (class_roots->Get(i) != GetClassRoot(static_cast<ClassRoot>(i))) { |
| 1594 | *error_msg = "App image class roots must have pointer equality with runtime ones."; |
| 1595 | return false; |
| 1596 | } |
| 1597 | } |
| 1598 | if (oat_file->GetOatHeader().GetDexFileCount() != |
| 1599 | static_cast<uint32_t>(dex_caches->GetLength())) { |
| 1600 | *error_msg = "Dex cache count and dex file count mismatch while trying to initialize from " |
| 1601 | "image"; |
| 1602 | return false; |
| 1603 | } |
| 1604 | |
| 1605 | StackHandleScope<1> hs2(self); |
| 1606 | MutableHandle<mirror::DexCache> h_dex_cache(hs2.NewHandle<mirror::DexCache>(nullptr)); |
| 1607 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 1608 | h_dex_cache.Assign(dex_caches->Get(i)); |
| 1609 | std::string dex_file_location(h_dex_cache->GetLocation()->ToModifiedUtf8()); |
| 1610 | // TODO: Only store qualified paths. |
| 1611 | // If non qualified, qualify it. |
| 1612 | if (dex_file_location.find('/') == std::string::npos) { |
| 1613 | std::string dex_location_path = dex_location; |
| 1614 | const size_t pos = dex_location_path.find_last_of('/'); |
| 1615 | CHECK_NE(pos, std::string::npos); |
| 1616 | dex_location_path = dex_location_path.substr(0, pos + 1); // Keep trailing '/' |
| 1617 | dex_file_location = dex_location_path + dex_file_location; |
| 1618 | } |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1619 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 1620 | dex_file_location.c_str(), |
| 1621 | error_msg); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1622 | if (dex_file == nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1623 | return false; |
| 1624 | } |
| 1625 | |
| 1626 | if (app_image) { |
| 1627 | // The current dex file field is bogus, overwrite it so that we can get the dex file in the |
| 1628 | // loop below. |
| 1629 | h_dex_cache->SetDexFile(dex_file.get()); |
| 1630 | // Check that each class loader resolved the same way. |
| 1631 | // TODO: Store image class loaders as image roots. |
| 1632 | GcRoot<mirror::Class>* const types = h_dex_cache->GetResolvedTypes(); |
| 1633 | for (int32_t j = 0, num_types = h_dex_cache->NumResolvedTypes(); j < num_types; j++) { |
| 1634 | mirror::Class* klass = types[j].Read(); |
| 1635 | if (klass != nullptr) { |
| 1636 | DCHECK_NE(klass->GetStatus(), mirror::Class::kStatusError); |
| 1637 | mirror::ClassLoader* image_class_loader = klass->GetClassLoader(); |
| 1638 | image_class_loaders.insert(image_class_loader); |
| 1639 | } |
| 1640 | } |
| 1641 | } else { |
| 1642 | if (kSanityCheckObjects) { |
| 1643 | SanityCheckArtMethodPointerArray(h_dex_cache->GetResolvedMethods(), |
| 1644 | h_dex_cache->NumResolvedMethods(), |
| 1645 | image_pointer_size_, |
| 1646 | heap->GetBootImageSpaces()); |
| 1647 | } |
| 1648 | // Register dex files, keep track of existing ones that are conflicts. |
| 1649 | AppendToBootClassPath(*dex_file.get(), h_dex_cache); |
| 1650 | } |
| 1651 | out_dex_files->push_back(std::move(dex_file)); |
| 1652 | } |
| 1653 | |
| 1654 | if (app_image) { |
| 1655 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1656 | // Check that the class loader resolves the same way as the ones in the image. |
| 1657 | // Image class loader [A][B][C][image dex files] |
| 1658 | // Class loader = [???][dex_elements][image dex files] |
| 1659 | // Need to ensure that [???][dex_elements] == [A][B][C]. |
| 1660 | // For each class loader, PathClassLoader, the laoder checks the parent first. Also the logic |
| 1661 | // for PathClassLoader does this by looping through the array of dex files. To ensure they |
| 1662 | // resolve the same way, simply flatten the hierarchy in the way the resolution order would be, |
| 1663 | // and check that the dex file names are the same. |
| 1664 | for (mirror::ClassLoader* image_class_loader : image_class_loaders) { |
Nicolas Geoffray | d0668f2 | 2016-04-26 18:30:31 +0100 | [diff] [blame] | 1665 | if (IsBootClassLoader(soa, image_class_loader)) { |
| 1666 | // The dex cache can reference types from the boot class loader. |
| 1667 | continue; |
| 1668 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1669 | std::list<mirror::String*> image_dex_file_names; |
| 1670 | std::string temp_error_msg; |
| 1671 | if (!FlattenPathClassLoader(image_class_loader, &image_dex_file_names, &temp_error_msg)) { |
| 1672 | *error_msg = StringPrintf("Failed to flatten image class loader hierarchy '%s'", |
| 1673 | temp_error_msg.c_str()); |
| 1674 | return false; |
| 1675 | } |
| 1676 | std::list<mirror::String*> loader_dex_file_names; |
| 1677 | if (!FlattenPathClassLoader(class_loader.Get(), &loader_dex_file_names, &temp_error_msg)) { |
| 1678 | *error_msg = StringPrintf("Failed to flatten class loader hierarchy '%s'", |
| 1679 | temp_error_msg.c_str()); |
| 1680 | return false; |
| 1681 | } |
| 1682 | // Add the temporary dex path list elements at the end. |
| 1683 | auto* elements = soa.Decode<mirror::ObjectArray<mirror::Object>*>(dex_elements); |
| 1684 | for (size_t i = 0, num_elems = elements->GetLength(); i < num_elems; ++i) { |
| 1685 | mirror::Object* element = elements->GetWithoutChecks(i); |
| 1686 | if (element != nullptr) { |
| 1687 | // If we are somewhere in the middle of the array, there may be nulls at the end. |
| 1688 | loader_dex_file_names.push_back(GetDexPathListElementName(soa, element)); |
| 1689 | } |
| 1690 | } |
| 1691 | // Ignore the number of image dex files since we are adding those to the class loader anyways. |
| 1692 | CHECK_GE(static_cast<size_t>(image_dex_file_names.size()), |
| 1693 | static_cast<size_t>(dex_caches->GetLength())); |
| 1694 | size_t image_count = image_dex_file_names.size() - dex_caches->GetLength(); |
| 1695 | // Check that the dex file names match. |
| 1696 | bool equal = image_count == loader_dex_file_names.size(); |
| 1697 | if (equal) { |
| 1698 | auto it1 = image_dex_file_names.begin(); |
| 1699 | auto it2 = loader_dex_file_names.begin(); |
| 1700 | for (size_t i = 0; equal && i < image_count; ++i, ++it1, ++it2) { |
| 1701 | equal = equal && (*it1)->Equals(*it2); |
| 1702 | } |
| 1703 | } |
| 1704 | if (!equal) { |
Mathieu Chartier | d329a3b | 2016-01-27 15:30:10 -0800 | [diff] [blame] | 1705 | VLOG(image) << "Image dex files " << image_dex_file_names.size(); |
| 1706 | for (mirror::String* name : image_dex_file_names) { |
| 1707 | VLOG(image) << name->ToModifiedUtf8(); |
| 1708 | } |
| 1709 | VLOG(image) << "Loader dex files " << loader_dex_file_names.size(); |
| 1710 | for (mirror::String* name : loader_dex_file_names) { |
| 1711 | VLOG(image) << name->ToModifiedUtf8(); |
| 1712 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1713 | *error_msg = "Rejecting application image due to class loader mismatch"; |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 1714 | // Ignore class loader mismatch for now since these would just use possibly incorrect |
| 1715 | // oat code anyways. The structural class check should be done in the parent. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1716 | } |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | if (kSanityCheckObjects) { |
| 1721 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 1722 | auto* dex_cache = dex_caches->Get(i); |
| 1723 | for (size_t j = 0; j < dex_cache->NumResolvedFields(); ++j) { |
| 1724 | auto* field = dex_cache->GetResolvedField(j, image_pointer_size_); |
| 1725 | if (field != nullptr) { |
| 1726 | CHECK(field->GetDeclaringClass()->GetClass() != nullptr); |
| 1727 | } |
| 1728 | } |
| 1729 | } |
| 1730 | if (!app_image) { |
| 1731 | heap->VisitObjects(SanityCheckObjectsCallback, nullptr); |
| 1732 | } |
| 1733 | } |
| 1734 | |
| 1735 | // Set entry point to interpreter if in InterpretOnly mode. |
| 1736 | if (!runtime->IsAotCompiler() && runtime->GetInstrumentation()->InterpretOnly()) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1737 | SetInterpreterEntrypointArtMethodVisitor visitor(image_pointer_size_); |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 1738 | header.VisitPackedArtMethods(&visitor, space->Begin(), image_pointer_size_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1739 | } |
| 1740 | |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1741 | ClassTable* class_table = nullptr; |
| 1742 | { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1743 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1744 | class_table = InsertClassTableForClassLoader(class_loader.Get()); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1745 | } |
| 1746 | // If we have a class table section, read it and use it for verification in |
| 1747 | // UpdateAppImageClassLoadersAndDexCaches. |
| 1748 | ClassTable::ClassSet temp_set; |
| 1749 | const ImageSection& class_table_section = header.GetImageSection(ImageHeader::kSectionClassTable); |
| 1750 | const bool added_class_table = class_table_section.Size() > 0u; |
| 1751 | if (added_class_table) { |
| 1752 | const uint64_t start_time2 = NanoTime(); |
| 1753 | size_t read_count = 0; |
| 1754 | temp_set = ClassTable::ClassSet(space->Begin() + class_table_section.Offset(), |
| 1755 | /*make copy*/false, |
| 1756 | &read_count); |
| 1757 | if (!app_image) { |
| 1758 | dex_cache_boot_image_class_lookup_required_ = false; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1759 | } |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1760 | VLOG(image) << "Adding class table classes took " << PrettyDuration(NanoTime() - start_time2); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1761 | } |
| 1762 | if (app_image) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1763 | bool forward_dex_cache_arrays = false; |
| 1764 | if (!UpdateAppImageClassLoadersAndDexCaches(space, |
| 1765 | class_loader, |
| 1766 | dex_caches, |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1767 | added_class_table ? &temp_set : nullptr, |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1768 | /*out*/&forward_dex_cache_arrays, |
| 1769 | /*out*/error_msg)) { |
| 1770 | return false; |
| 1771 | } |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1772 | // Update class loader and resolved strings. If added_class_table is false, the resolved |
| 1773 | // strings were forwarded UpdateAppImageClassLoadersAndDexCaches. |
| 1774 | UpdateClassLoaderAndResolvedStringsVisitor visitor(space, |
| 1775 | class_loader.Get(), |
| 1776 | forward_dex_cache_arrays); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1777 | if (added_class_table) { |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1778 | for (GcRoot<mirror::Class>& root : temp_set) { |
| 1779 | visitor(root.Read()); |
| 1780 | } |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1781 | } |
Mathieu Chartier | 8c4f041 | 2016-02-03 16:40:20 -0800 | [diff] [blame] | 1782 | // forward_dex_cache_arrays is true iff we copied all of the dex cache arrays into the .bss. |
| 1783 | // In this case, madvise away the dex cache arrays section of the image to reduce RAM usage and |
| 1784 | // mark as PROT_NONE to catch any invalid accesses. |
| 1785 | if (forward_dex_cache_arrays) { |
| 1786 | const ImageSection& dex_cache_section = header.GetImageSection( |
| 1787 | ImageHeader::kSectionDexCacheArrays); |
| 1788 | uint8_t* section_begin = AlignUp(space->Begin() + dex_cache_section.Offset(), kPageSize); |
| 1789 | uint8_t* section_end = AlignDown(space->Begin() + dex_cache_section.End(), kPageSize); |
| 1790 | if (section_begin < section_end) { |
| 1791 | madvise(section_begin, section_end - section_begin, MADV_DONTNEED); |
| 1792 | mprotect(section_begin, section_end - section_begin, PROT_NONE); |
| 1793 | VLOG(image) << "Released and protected dex cache array image section from " |
| 1794 | << reinterpret_cast<const void*>(section_begin) << "-" |
| 1795 | << reinterpret_cast<const void*>(section_end); |
| 1796 | } |
| 1797 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1798 | } |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1799 | if (added_class_table) { |
| 1800 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 1801 | class_table->AddClassSet(std::move(temp_set)); |
| 1802 | } |
| 1803 | if (kIsDebugBuild && app_image) { |
| 1804 | // This verification needs to happen after the classes have been added to the class loader. |
| 1805 | // Since it ensures classes are in the class table. |
| 1806 | VerifyClassInTableArtMethodVisitor visitor2(class_table); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1807 | header.VisitPackedArtMethods(&visitor2, space->Begin(), kRuntimePointerSize); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1808 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1809 | VLOG(class_linker) << "Adding image space took " << PrettyDuration(NanoTime() - start_time); |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 1810 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1811 | } |
| 1812 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1813 | bool ClassLinker::ClassInClassTable(mirror::Class* klass) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1814 | ClassTable* const class_table = ClassTableForClassLoader(klass->GetClassLoader()); |
| 1815 | return class_table != nullptr && class_table->Contains(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1816 | } |
| 1817 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1818 | void ClassLinker::VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 1819 | // Acquire tracing_enabled before locking class linker lock to prevent lock order violation. Since |
| 1820 | // enabling tracing requires the mutator lock, there are no race conditions here. |
| 1821 | const bool tracing_enabled = Trace::IsTracingEnabled(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 1822 | Thread* const self = Thread::Current(); |
| 1823 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1824 | BufferedRootVisitor<kDefaultBufferedRootCount> buffered_visitor( |
| 1825 | visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1826 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1827 | // Argument for how root visiting deals with ArtField and ArtMethod roots. |
| 1828 | // There is 3 GC cases to handle: |
| 1829 | // Non moving concurrent: |
| 1830 | // This case is easy to handle since the reference members of ArtMethod and ArtFields are held |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1831 | // live by the class and class roots. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1832 | // |
| 1833 | // Moving non-concurrent: |
| 1834 | // This case needs to call visit VisitNativeRoots in case the classes or dex cache arrays move. |
| 1835 | // To prevent missing roots, this case needs to ensure that there is no |
| 1836 | // suspend points between the point which we allocate ArtMethod arrays and place them in a |
| 1837 | // class which is in the class table. |
| 1838 | // |
| 1839 | // Moving concurrent: |
| 1840 | // Need to make sure to not copy ArtMethods without doing read barriers since the roots are |
| 1841 | // marked concurrently and we don't hold the classlinker_classes_lock_ when we do the copy. |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 1842 | boot_class_table_.VisitRoots(buffered_visitor); |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 1843 | |
| 1844 | // If tracing is enabled, then mark all the class loaders to prevent unloading. |
| 1845 | if (tracing_enabled) { |
| 1846 | for (const ClassLoaderData& data : class_loaders_) { |
| 1847 | GcRoot<mirror::Object> root(GcRoot<mirror::Object>(self->DecodeJObject(data.weak_root))); |
| 1848 | root.VisitRoot(visitor, RootInfo(kRootVMInternal)); |
| 1849 | } |
| 1850 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1851 | } else if ((flags & kVisitRootFlagNewRoots) != 0) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1852 | for (auto& root : new_class_roots_) { |
| 1853 | mirror::Class* old_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1854 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1855 | mirror::Class* new_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1856 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 1857 | CHECK_EQ(new_ref, old_ref); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1858 | } |
| 1859 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1860 | buffered_visitor.Flush(); // Flush before clearing new_class_roots_. |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1861 | if ((flags & kVisitRootFlagClearRootLog) != 0) { |
| 1862 | new_class_roots_.clear(); |
| 1863 | } |
| 1864 | if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
| 1865 | log_new_class_table_roots_ = true; |
| 1866 | } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
| 1867 | log_new_class_table_roots_ = false; |
| 1868 | } |
| 1869 | // We deliberately ignore the class roots in the image since we |
| 1870 | // handle image roots by using the MS/CMS rescanning of dirty cards. |
| 1871 | } |
| 1872 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1873 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 1874 | // reinit references to when reinitializing a ClassLinker from a |
| 1875 | // mapped image. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1876 | void ClassLinker::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 1877 | class_roots_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1878 | VisitClassRoots(visitor, flags); |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 1879 | array_iftable_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); |
Mathieu Chartier | 6cfc2c0 | 2015-10-12 15:06:16 -0700 | [diff] [blame] | 1880 | // Instead of visiting the find_array_class_cache_ drop it so that it doesn't prevent class |
| 1881 | // unloading if we are marking roots. |
| 1882 | DropFindArrayClassCache(); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1883 | } |
| 1884 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 1885 | class VisitClassLoaderClassesVisitor : public ClassLoaderVisitor { |
| 1886 | public: |
| 1887 | explicit VisitClassLoaderClassesVisitor(ClassVisitor* visitor) |
| 1888 | : visitor_(visitor), |
| 1889 | done_(false) {} |
| 1890 | |
| 1891 | void Visit(mirror::ClassLoader* class_loader) |
| 1892 | SHARED_REQUIRES(Locks::classlinker_classes_lock_, Locks::mutator_lock_) OVERRIDE { |
| 1893 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1894 | if (!done_ && class_table != nullptr && !class_table->Visit(*visitor_)) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 1895 | // If the visitor ClassTable returns false it means that we don't need to continue. |
| 1896 | done_ = true; |
| 1897 | } |
| 1898 | } |
| 1899 | |
| 1900 | private: |
| 1901 | ClassVisitor* const visitor_; |
| 1902 | // If done is true then we don't need to do any more visiting. |
| 1903 | bool done_; |
| 1904 | }; |
| 1905 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1906 | void ClassLinker::VisitClassesInternal(ClassVisitor* visitor) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1907 | if (boot_class_table_.Visit(*visitor)) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 1908 | VisitClassLoaderClassesVisitor loader_visitor(visitor); |
| 1909 | VisitClassLoaders(&loader_visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1910 | } |
| 1911 | } |
| 1912 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1913 | void ClassLinker::VisitClasses(ClassVisitor* visitor) { |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 1914 | if (dex_cache_boot_image_class_lookup_required_) { |
| 1915 | AddBootImageClassesToClassTable(); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1916 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1917 | Thread* const self = Thread::Current(); |
| 1918 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 1919 | // Not safe to have thread suspension when we are holding a lock. |
| 1920 | if (self != nullptr) { |
| 1921 | ScopedAssertNoThreadSuspension nts(self, __FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1922 | VisitClassesInternal(visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1923 | } else { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1924 | VisitClassesInternal(visitor); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1925 | } |
| 1926 | } |
| 1927 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1928 | class GetClassesInToVector : public ClassVisitor { |
| 1929 | public: |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1930 | bool operator()(mirror::Class* klass) OVERRIDE { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1931 | classes_.push_back(klass); |
| 1932 | return true; |
| 1933 | } |
| 1934 | std::vector<mirror::Class*> classes_; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1935 | }; |
| 1936 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1937 | class GetClassInToObjectArray : public ClassVisitor { |
| 1938 | public: |
| 1939 | explicit GetClassInToObjectArray(mirror::ObjectArray<mirror::Class>* arr) |
| 1940 | : arr_(arr), index_(0) {} |
| 1941 | |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1942 | bool operator()(mirror::Class* klass) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1943 | ++index_; |
| 1944 | if (index_ <= arr_->GetLength()) { |
| 1945 | arr_->Set(index_ - 1, klass); |
| 1946 | return true; |
| 1947 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1948 | return false; |
| 1949 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1950 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1951 | bool Succeeded() const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1952 | return index_ <= arr_->GetLength(); |
| 1953 | } |
| 1954 | |
| 1955 | private: |
| 1956 | mirror::ObjectArray<mirror::Class>* const arr_; |
| 1957 | int32_t index_; |
| 1958 | }; |
| 1959 | |
| 1960 | void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1961 | // TODO: it may be possible to avoid secondary storage if we iterate over dex caches. The problem |
| 1962 | // is avoiding duplicates. |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1963 | Thread* const self = Thread::Current(); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1964 | if (!kMovingClasses) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1965 | ScopedAssertNoThreadSuspension nts(self, __FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1966 | GetClassesInToVector accumulator; |
| 1967 | VisitClasses(&accumulator); |
| 1968 | for (mirror::Class* klass : accumulator.classes_) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1969 | if (!visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1970 | return; |
| 1971 | } |
| 1972 | } |
| 1973 | } else { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1974 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1975 | auto classes = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1976 | // We size the array assuming classes won't be added to the class table during the visit. |
| 1977 | // If this assumption fails we iterate again. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1978 | while (true) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1979 | size_t class_table_size; |
| 1980 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1981 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1982 | // Add 100 in case new classes get loaded when we are filling in the object array. |
| 1983 | class_table_size = NumZygoteClasses() + NumNonZygoteClasses() + 100; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1984 | } |
| 1985 | mirror::Class* class_type = mirror::Class::GetJavaLangClass(); |
| 1986 | mirror::Class* array_of_class = FindArrayClass(self, &class_type); |
| 1987 | classes.Assign( |
| 1988 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, class_table_size)); |
| 1989 | CHECK(classes.Get() != nullptr); // OOME. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 1990 | GetClassInToObjectArray accumulator(classes.Get()); |
| 1991 | VisitClasses(&accumulator); |
| 1992 | if (accumulator.Succeeded()) { |
| 1993 | break; |
| 1994 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1995 | } |
| 1996 | for (int32_t i = 0; i < classes->GetLength(); ++i) { |
| 1997 | // If the class table shrank during creation of the clases array we expect null elements. If |
| 1998 | // the class table grew then the loop repeats. If classes are created after the loop has |
| 1999 | // finished then we don't visit. |
| 2000 | mirror::Class* klass = classes->Get(i); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2001 | if (klass != nullptr && !visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2002 | return; |
| 2003 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2004 | } |
| 2005 | } |
| 2006 | } |
| 2007 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2008 | ClassLinker::~ClassLinker() { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2009 | mirror::Class::ResetClass(); |
| 2010 | mirror::Constructor::ResetClass(); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 2011 | mirror::Field::ResetClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2012 | mirror::Method::ResetClass(); |
| 2013 | mirror::Reference::ResetClass(); |
| 2014 | mirror::StackTraceElement::ResetClass(); |
| 2015 | mirror::String::ResetClass(); |
| 2016 | mirror::Throwable::ResetClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2017 | mirror::BooleanArray::ResetArrayClass(); |
| 2018 | mirror::ByteArray::ResetArrayClass(); |
| 2019 | mirror::CharArray::ResetArrayClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2020 | mirror::Constructor::ResetArrayClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2021 | mirror::DoubleArray::ResetArrayClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2022 | mirror::Field::ResetArrayClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2023 | mirror::FloatArray::ResetArrayClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2024 | mirror::Method::ResetArrayClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2025 | mirror::IntArray::ResetArrayClass(); |
| 2026 | mirror::LongArray::ResetArrayClass(); |
| 2027 | mirror::ShortArray::ResetArrayClass(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2028 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 2029 | for (const ClassLoaderData& data : class_loaders_) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2030 | DeleteClassLoader(self, data); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 2031 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2032 | class_loaders_.clear(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2033 | } |
| 2034 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2035 | void ClassLinker::DeleteClassLoader(Thread* self, const ClassLoaderData& data) { |
| 2036 | Runtime* const runtime = Runtime::Current(); |
| 2037 | JavaVMExt* const vm = runtime->GetJavaVM(); |
| 2038 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 2039 | // Notify the JIT that we need to remove the methods and/or profiling info. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2040 | if (runtime->GetJit() != nullptr) { |
| 2041 | jit::JitCodeCache* code_cache = runtime->GetJit()->GetCodeCache(); |
| 2042 | if (code_cache != nullptr) { |
| 2043 | code_cache->RemoveMethodsIn(self, *data.allocator); |
| 2044 | } |
| 2045 | } |
| 2046 | delete data.allocator; |
| 2047 | delete data.class_table; |
| 2048 | } |
| 2049 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2050 | mirror::PointerArray* ClassLinker::AllocPointerArray(Thread* self, size_t length) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2051 | return down_cast<mirror::PointerArray*>( |
| 2052 | image_pointer_size_ == PointerSize::k64 |
| 2053 | ? static_cast<mirror::Array*>(mirror::LongArray::Alloc(self, length)) |
| 2054 | : static_cast<mirror::Array*>(mirror::IntArray::Alloc(self, length))); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2055 | } |
| 2056 | |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 2057 | mirror::DexCache* ClassLinker::AllocDexCache(Thread* self, |
| 2058 | const DexFile& dex_file, |
| 2059 | LinearAlloc* linear_alloc) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2060 | StackHandleScope<6> hs(self); |
| 2061 | auto dex_cache(hs.NewHandle(down_cast<mirror::DexCache*>( |
| 2062 | GetClassRoot(kJavaLangDexCache)->AllocObject(self)))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2063 | if (dex_cache.Get() == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2064 | self->AssertPendingOOMException(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2065 | return nullptr; |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 2066 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2067 | auto location(hs.NewHandle(intern_table_->InternStrong(dex_file.GetLocation().c_str()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2068 | if (location.Get() == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2069 | self->AssertPendingOOMException(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2070 | return nullptr; |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 2071 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2072 | DexCacheArraysLayout layout(image_pointer_size_, &dex_file); |
| 2073 | uint8_t* raw_arrays = nullptr; |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 2074 | if (dex_file.GetOatDexFile() != nullptr && |
| 2075 | dex_file.GetOatDexFile()->GetDexCacheArrays() != nullptr) { |
Vladimir Marko | 06d7aaa | 2015-10-16 11:23:41 +0100 | [diff] [blame] | 2076 | raw_arrays = dex_file.GetOatDexFile()->GetDexCacheArrays(); |
Vladimir Marko | 09d0943 | 2015-09-08 13:47:48 +0100 | [diff] [blame] | 2077 | } else if (dex_file.NumStringIds() != 0u || dex_file.NumTypeIds() != 0u || |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2078 | dex_file.NumMethodIds() != 0u || dex_file.NumFieldIds() != 0u) { |
Mathieu Chartier | 32cc9ee | 2015-10-15 09:19:15 -0700 | [diff] [blame] | 2079 | // Zero-initialized. |
| 2080 | raw_arrays = reinterpret_cast<uint8_t*>(linear_alloc->Alloc(self, layout.Size())); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 2081 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2082 | GcRoot<mirror::String>* strings = (dex_file.NumStringIds() == 0u) ? nullptr : |
| 2083 | reinterpret_cast<GcRoot<mirror::String>*>(raw_arrays + layout.StringsOffset()); |
| 2084 | GcRoot<mirror::Class>* types = (dex_file.NumTypeIds() == 0u) ? nullptr : |
| 2085 | reinterpret_cast<GcRoot<mirror::Class>*>(raw_arrays + layout.TypesOffset()); |
| 2086 | ArtMethod** methods = (dex_file.NumMethodIds() == 0u) ? nullptr : |
| 2087 | reinterpret_cast<ArtMethod**>(raw_arrays + layout.MethodsOffset()); |
| 2088 | ArtField** fields = (dex_file.NumFieldIds() == 0u) ? nullptr : |
| 2089 | reinterpret_cast<ArtField**>(raw_arrays + layout.FieldsOffset()); |
Mathieu Chartier | 05005f7 | 2016-06-06 13:10:50 -0700 | [diff] [blame] | 2090 | if (kIsDebugBuild) { |
Mathieu Chartier | c7d3f4b | 2016-06-01 10:48:19 -0700 | [diff] [blame] | 2091 | // Sanity check to make sure all the dex cache arrays are empty. b/28992179 |
| 2092 | for (size_t i = 0; i < dex_file.NumStringIds(); ++i) { |
| 2093 | CHECK(strings[i].Read<kWithoutReadBarrier>() == nullptr); |
| 2094 | } |
| 2095 | for (size_t i = 0; i < dex_file.NumTypeIds(); ++i) { |
| 2096 | CHECK(types[i].Read<kWithoutReadBarrier>() == nullptr); |
| 2097 | } |
| 2098 | for (size_t i = 0; i < dex_file.NumMethodIds(); ++i) { |
| 2099 | CHECK(mirror::DexCache::GetElementPtrSize(methods, i, image_pointer_size_) == nullptr); |
| 2100 | } |
| 2101 | for (size_t i = 0; i < dex_file.NumFieldIds(); ++i) { |
| 2102 | CHECK(mirror::DexCache::GetElementPtrSize(fields, i, image_pointer_size_) == nullptr); |
| 2103 | } |
| 2104 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2105 | dex_cache->Init(&dex_file, |
| 2106 | location.Get(), |
| 2107 | strings, |
| 2108 | dex_file.NumStringIds(), |
| 2109 | types, |
| 2110 | dex_file.NumTypeIds(), |
| 2111 | methods, |
| 2112 | dex_file.NumMethodIds(), |
| 2113 | fields, |
| 2114 | dex_file.NumFieldIds(), |
| 2115 | image_pointer_size_); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2116 | return dex_cache.Get(); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2117 | } |
| 2118 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2119 | mirror::Class* ClassLinker::AllocClass(Thread* self, mirror::Class* java_lang_Class, |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2120 | uint32_t class_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2121 | DCHECK_GE(class_size, sizeof(mirror::Class)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2122 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2123 | mirror::Class::InitializeClassVisitor visitor(class_size); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2124 | mirror::Object* k = kMovingClasses ? |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 2125 | heap->AllocObject<true>(self, java_lang_Class, class_size, visitor) : |
| 2126 | heap->AllocNonMovableObject<true>(self, java_lang_Class, class_size, visitor); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2127 | if (UNLIKELY(k == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2128 | self->AssertPendingOOMException(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2129 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2130 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2131 | return k->AsClass(); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 2132 | } |
| 2133 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2134 | mirror::Class* ClassLinker::AllocClass(Thread* self, uint32_t class_size) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2135 | return AllocClass(self, GetClassRoot(kJavaLangClass), class_size); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2136 | } |
| 2137 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2138 | mirror::ObjectArray<mirror::StackTraceElement>* ClassLinker::AllocStackTraceElementArray( |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2139 | Thread* self, |
| 2140 | size_t length) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2141 | return mirror::ObjectArray<mirror::StackTraceElement>::Alloc( |
| 2142 | self, GetClassRoot(kJavaLangStackTraceElementArrayClass), length); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2143 | } |
| 2144 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2145 | mirror::Class* ClassLinker::EnsureResolved(Thread* self, |
| 2146 | const char* descriptor, |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2147 | mirror::Class* klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2148 | DCHECK(klass != nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2149 | |
| 2150 | // For temporary classes we must wait for them to be retired. |
| 2151 | if (init_done_ && klass->IsTemp()) { |
| 2152 | CHECK(!klass->IsResolved()); |
| 2153 | if (klass->IsErroneous()) { |
| 2154 | ThrowEarlierClassFailure(klass); |
| 2155 | return nullptr; |
| 2156 | } |
| 2157 | StackHandleScope<1> hs(self); |
| 2158 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 2159 | ObjectLock<mirror::Class> lock(self, h_class); |
| 2160 | // Loop and wait for the resolving thread to retire this class. |
| 2161 | while (!h_class->IsRetired() && !h_class->IsErroneous()) { |
| 2162 | lock.WaitIgnoringInterrupts(); |
| 2163 | } |
| 2164 | if (h_class->IsErroneous()) { |
| 2165 | ThrowEarlierClassFailure(h_class.Get()); |
| 2166 | return nullptr; |
| 2167 | } |
| 2168 | CHECK(h_class->IsRetired()); |
| 2169 | // Get the updated class from class table. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2170 | klass = LookupClass(self, descriptor, ComputeModifiedUtf8Hash(descriptor), |
| 2171 | h_class.Get()->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2172 | } |
| 2173 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2174 | // Wait for the class if it has not already been linked. |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2175 | size_t index = 0; |
| 2176 | // Maximum number of yield iterations until we start sleeping. |
| 2177 | static const size_t kNumYieldIterations = 1000; |
| 2178 | // How long each sleep is in us. |
| 2179 | static const size_t kSleepDurationUS = 1000; // 1 ms. |
| 2180 | while (!klass->IsResolved() && !klass->IsErroneous()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2181 | StackHandleScope<1> hs(self); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 2182 | HandleWrapper<mirror::Class> h_class(hs.NewHandleWrapper(&klass)); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2183 | { |
| 2184 | ObjectTryLock<mirror::Class> lock(self, h_class); |
| 2185 | // Can not use a monitor wait here since it may block when returning and deadlock if another |
| 2186 | // thread has locked klass. |
| 2187 | if (lock.Acquired()) { |
| 2188 | // Check for circular dependencies between classes, the lock is required for SetStatus. |
| 2189 | if (!h_class->IsResolved() && h_class->GetClinitThreadId() == self->GetTid()) { |
| 2190 | ThrowClassCircularityError(h_class.Get()); |
| 2191 | mirror::Class::SetStatus(h_class, mirror::Class::kStatusError, self); |
| 2192 | return nullptr; |
| 2193 | } |
| 2194 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2195 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2196 | { |
| 2197 | // Handle wrapper deals with klass moving. |
| 2198 | ScopedThreadSuspension sts(self, kSuspended); |
| 2199 | if (index < kNumYieldIterations) { |
| 2200 | sched_yield(); |
| 2201 | } else { |
| 2202 | usleep(kSleepDurationUS); |
| 2203 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2204 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2205 | ++index; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2206 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2207 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2208 | if (klass->IsErroneous()) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2209 | ThrowEarlierClassFailure(klass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2210 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2211 | } |
| 2212 | // Return the loaded class. No exceptions should be pending. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2213 | CHECK(klass->IsResolved()) << PrettyClass(klass); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2214 | self->AssertNoPendingException(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2215 | return klass; |
| 2216 | } |
| 2217 | |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2218 | typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry; |
| 2219 | |
| 2220 | // Search a collection of DexFiles for a descriptor |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2221 | ClassPathEntry FindInClassPath(const char* descriptor, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2222 | size_t hash, const std::vector<const DexFile*>& class_path) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2223 | for (const DexFile* dex_file : class_path) { |
| 2224 | const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2225 | if (dex_class_def != nullptr) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2226 | return ClassPathEntry(dex_file, dex_class_def); |
| 2227 | } |
| 2228 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2229 | return ClassPathEntry(nullptr, nullptr); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2230 | } |
| 2231 | |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2232 | bool ClassLinker::FindClassInPathClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2233 | Thread* self, |
| 2234 | const char* descriptor, |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2235 | size_t hash, |
| 2236 | Handle<mirror::ClassLoader> class_loader, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2237 | mirror::Class** result) { |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2238 | // Termination case: boot class-loader. |
| 2239 | if (IsBootClassLoader(soa, class_loader.Get())) { |
| 2240 | // The boot class loader, search the boot class path. |
| 2241 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
| 2242 | if (pair.second != nullptr) { |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2243 | mirror::Class* klass = LookupClass(self, descriptor, hash, nullptr); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2244 | if (klass != nullptr) { |
| 2245 | *result = EnsureResolved(self, descriptor, klass); |
| 2246 | } else { |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 2247 | *result = DefineClass(self, |
| 2248 | descriptor, |
| 2249 | hash, |
| 2250 | ScopedNullHandle<mirror::ClassLoader>(), |
| 2251 | *pair.first, |
| 2252 | *pair.second); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2253 | } |
| 2254 | if (*result == nullptr) { |
| 2255 | CHECK(self->IsExceptionPending()) << descriptor; |
| 2256 | self->ClearException(); |
| 2257 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2258 | } else { |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2259 | *result = nullptr; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2260 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2261 | return true; |
| 2262 | } |
| 2263 | |
| 2264 | // Unsupported class-loader? |
| 2265 | if (class_loader->GetClass() != |
| 2266 | soa.Decode<mirror::Class*>(WellKnownClasses::dalvik_system_PathClassLoader)) { |
| 2267 | *result = nullptr; |
| 2268 | return false; |
| 2269 | } |
| 2270 | |
| 2271 | // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension). |
| 2272 | StackHandleScope<4> hs(self); |
| 2273 | Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent())); |
| 2274 | bool recursive_result = FindClassInPathClassLoader(soa, self, descriptor, hash, h_parent, result); |
| 2275 | |
| 2276 | if (!recursive_result) { |
| 2277 | // Something wrong up the chain. |
| 2278 | return false; |
| 2279 | } |
| 2280 | |
| 2281 | if (*result != nullptr) { |
| 2282 | // Found the class up the chain. |
| 2283 | return true; |
| 2284 | } |
| 2285 | |
| 2286 | // Handle this step. |
| 2287 | // Handle as if this is the child PathClassLoader. |
| 2288 | // The class loader is a PathClassLoader which inherits from BaseDexClassLoader. |
| 2289 | // We need to get the DexPathList and loop through it. |
| 2290 | ArtField* const cookie_field = soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_cookie); |
| 2291 | ArtField* const dex_file_field = |
| 2292 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
| 2293 | mirror::Object* dex_path_list = |
| 2294 | soa.DecodeField(WellKnownClasses::dalvik_system_PathClassLoader_pathList)-> |
| 2295 | GetObject(class_loader.Get()); |
| 2296 | if (dex_path_list != nullptr && dex_file_field != nullptr && cookie_field != nullptr) { |
| 2297 | // DexPathList has an array dexElements of Elements[] which each contain a dex file. |
| 2298 | mirror::Object* dex_elements_obj = |
| 2299 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList_dexElements)-> |
| 2300 | GetObject(dex_path_list); |
| 2301 | // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look |
| 2302 | // at the mCookie which is a DexFile vector. |
| 2303 | if (dex_elements_obj != nullptr) { |
| 2304 | Handle<mirror::ObjectArray<mirror::Object>> dex_elements = |
| 2305 | hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>()); |
| 2306 | for (int32_t i = 0; i < dex_elements->GetLength(); ++i) { |
| 2307 | mirror::Object* element = dex_elements->GetWithoutChecks(i); |
| 2308 | if (element == nullptr) { |
| 2309 | // Should never happen, fall back to java code to throw a NPE. |
| 2310 | break; |
| 2311 | } |
| 2312 | mirror::Object* dex_file = dex_file_field->GetObject(element); |
| 2313 | if (dex_file != nullptr) { |
| 2314 | mirror::LongArray* long_array = cookie_field->GetObject(dex_file)->AsLongArray(); |
| 2315 | if (long_array == nullptr) { |
| 2316 | // This should never happen so log a warning. |
| 2317 | LOG(WARNING) << "Null DexFile::mCookie for " << descriptor; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2318 | break; |
| 2319 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2320 | int32_t long_array_size = long_array->GetLength(); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 2321 | // First element is the oat file. |
| 2322 | for (int32_t j = kDexFileIndexStart; j < long_array_size; ++j) { |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2323 | const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>( |
| 2324 | long_array->GetWithoutChecks(j))); |
| 2325 | const DexFile::ClassDef* dex_class_def = cp_dex_file->FindClassDef(descriptor, hash); |
| 2326 | if (dex_class_def != nullptr) { |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 2327 | mirror::Class* klass = DefineClass(self, |
| 2328 | descriptor, |
| 2329 | hash, |
| 2330 | class_loader, |
| 2331 | *cp_dex_file, |
| 2332 | *dex_class_def); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2333 | if (klass == nullptr) { |
| 2334 | CHECK(self->IsExceptionPending()) << descriptor; |
| 2335 | self->ClearException(); |
| 2336 | // TODO: Is it really right to break here, and not check the other dex files? |
| 2337 | return true; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2338 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2339 | *result = klass; |
| 2340 | return true; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2341 | } |
| 2342 | } |
| 2343 | } |
| 2344 | } |
| 2345 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2346 | self->AssertNoPendingException(); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2347 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2348 | |
| 2349 | // Result is still null from the parent call, no need to set it again... |
| 2350 | return true; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2351 | } |
| 2352 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2353 | mirror::Class* ClassLinker::FindClass(Thread* self, |
| 2354 | const char* descriptor, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2355 | Handle<mirror::ClassLoader> class_loader) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 2356 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2357 | DCHECK(self != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2358 | self->AssertNoPendingException(); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 2359 | if (descriptor[1] == '\0') { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2360 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 2361 | // for primitive classes that aren't backed by dex files. |
| 2362 | return FindPrimitiveClass(descriptor[0]); |
| 2363 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2364 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2365 | // Find the class in the loaded classes table. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2366 | mirror::Class* klass = LookupClass(self, descriptor, hash, class_loader.Get()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2367 | if (klass != nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2368 | return EnsureResolved(self, descriptor, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2369 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2370 | // Class is not yet loaded. |
| 2371 | if (descriptor[0] == '[') { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2372 | return CreateArrayClass(self, descriptor, hash, class_loader); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2373 | } else if (class_loader.Get() == nullptr) { |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2374 | // The boot class loader, search the boot class path. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2375 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2376 | if (pair.second != nullptr) { |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 2377 | return DefineClass(self, |
| 2378 | descriptor, |
| 2379 | hash, |
| 2380 | ScopedNullHandle<mirror::ClassLoader>(), |
| 2381 | *pair.first, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2382 | *pair.second); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2383 | } else { |
| 2384 | // The boot class loader is searched ahead of the application class loader, failures are |
| 2385 | // expected and will be wrapped in a ClassNotFoundException. Use the pre-allocated error to |
| 2386 | // trigger the chaining with a proper stack trace. |
| 2387 | mirror::Throwable* pre_allocated = Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2388 | self->SetException(pre_allocated); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2389 | return nullptr; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2390 | } |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2391 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2392 | ScopedObjectAccessUnchecked soa(self); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2393 | mirror::Class* cp_klass; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2394 | if (FindClassInPathClassLoader(soa, self, descriptor, hash, class_loader, &cp_klass)) { |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2395 | // The chain was understood. So the value in cp_klass is either the class we were looking |
| 2396 | // for, or not found. |
| 2397 | if (cp_klass != nullptr) { |
| 2398 | return cp_klass; |
| 2399 | } |
| 2400 | // TODO: We handle the boot classpath loader in FindClassInPathClassLoader. Try to unify this |
| 2401 | // and the branch above. TODO: throw the right exception here. |
| 2402 | |
| 2403 | // We'll let the Java-side rediscover all this and throw the exception with the right stack |
| 2404 | // trace. |
Mathieu Chartier | 6bcae8f | 2014-09-04 18:33:17 -0700 | [diff] [blame] | 2405 | } |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 2406 | |
| 2407 | if (Runtime::Current()->IsAotCompiler()) { |
| 2408 | // Oops, compile-time, can't run actual class-loader code. |
| 2409 | mirror::Throwable* pre_allocated = Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 2410 | self->SetException(pre_allocated); |
| 2411 | return nullptr; |
| 2412 | } |
| 2413 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2414 | ScopedLocalRef<jobject> class_loader_object(soa.Env(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2415 | soa.AddLocalReference<jobject>(class_loader.Get())); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 2416 | std::string class_name_string(DescriptorToDot(descriptor)); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2417 | ScopedLocalRef<jobject> result(soa.Env(), nullptr); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 2418 | { |
| 2419 | ScopedThreadStateChange tsc(self, kNative); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2420 | ScopedLocalRef<jobject> class_name_object(soa.Env(), |
| 2421 | soa.Env()->NewStringUTF(class_name_string.c_str())); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2422 | if (class_name_object.get() == nullptr) { |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2423 | DCHECK(self->IsExceptionPending()); // OOME. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2424 | return nullptr; |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 2425 | } |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2426 | CHECK(class_loader_object.get() != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2427 | result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(), |
| 2428 | WellKnownClasses::java_lang_ClassLoader_loadClass, |
| 2429 | class_name_object.get())); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2430 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2431 | if (self->IsExceptionPending()) { |
Elliott Hughes | 748382f | 2012-01-26 18:07:38 -0800 | [diff] [blame] | 2432 | // If the ClassLoader threw, pass that exception up. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2433 | return nullptr; |
| 2434 | } else if (result.get() == nullptr) { |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 2435 | // broken loader - throw NPE to be compatible with Dalvik |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 2436 | ThrowNullPointerException(StringPrintf("ClassLoader.loadClass returned null for %s", |
| 2437 | class_name_string.c_str()).c_str()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2438 | return nullptr; |
Ian Rogers | 761bfa8 | 2012-01-11 10:14:05 -0800 | [diff] [blame] | 2439 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2440 | // success, return mirror::Class* |
| 2441 | return soa.Decode<mirror::Class*>(result.get()); |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 2442 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2443 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 2444 | UNREACHABLE(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2445 | } |
| 2446 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2447 | mirror::Class* ClassLinker::DefineClass(Thread* self, |
| 2448 | const char* descriptor, |
| 2449 | size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2450 | Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2451 | const DexFile& dex_file, |
| 2452 | const DexFile::ClassDef& dex_class_def) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2453 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2454 | auto klass = hs.NewHandle<mirror::Class>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2455 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2456 | // Load the class from the dex file. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2457 | if (UNLIKELY(!init_done_)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2458 | // finish up init of hand crafted class_roots_ |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2459 | if (strcmp(descriptor, "Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2460 | klass.Assign(GetClassRoot(kJavaLangObject)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2461 | } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2462 | klass.Assign(GetClassRoot(kJavaLangClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2463 | } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2464 | klass.Assign(GetClassRoot(kJavaLangString)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 2465 | } else if (strcmp(descriptor, "Ljava/lang/ref/Reference;") == 0) { |
| 2466 | klass.Assign(GetClassRoot(kJavaLangRefReference)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2467 | } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2468 | klass.Assign(GetClassRoot(kJavaLangDexCache)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2469 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2470 | } |
| 2471 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2472 | if (klass.Get() == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2473 | // Allocate a class with the status of not ready. |
| 2474 | // Interface object should get the right size here. Regular class will |
| 2475 | // figure out the right size later and be replaced with one of the right |
| 2476 | // size when the class becomes resolved. |
| 2477 | klass.Assign(AllocClass(self, SizeOfClassWithoutEmbeddedTables(dex_file, dex_class_def))); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2478 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2479 | if (UNLIKELY(klass.Get() == nullptr)) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2480 | self->AssertPendingOOMException(); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2481 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2482 | } |
Mathieu Chartier | 696632e | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 2483 | mirror::DexCache* dex_cache = RegisterDexFile(dex_file, class_loader.Get()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2484 | if (dex_cache == nullptr) { |
| 2485 | self->AssertPendingOOMException(); |
| 2486 | return nullptr; |
| 2487 | } |
| 2488 | klass->SetDexCache(dex_cache); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2489 | SetupClass(dex_file, dex_class_def, klass, class_loader.Get()); |
| 2490 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 2491 | // Mark the string class by setting its access flag. |
| 2492 | if (UNLIKELY(!init_done_)) { |
| 2493 | if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
| 2494 | klass->SetStringClass(); |
| 2495 | } |
| 2496 | } |
| 2497 | |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 2498 | ObjectLock<mirror::Class> lock(self, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2499 | klass->SetClinitThreadId(self->GetTid()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2500 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2501 | // Add the newly loaded class to the loaded classes table. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2502 | mirror::Class* existing = InsertClass(descriptor, klass.Get(), hash); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2503 | if (existing != nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2504 | // We failed to insert because we raced with another thread. Calling EnsureResolved may cause |
| 2505 | // this thread to block. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2506 | return EnsureResolved(self, descriptor, existing); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2507 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2508 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2509 | // Load the fields and other things after we are inserted in the table. This is so that we don't |
| 2510 | // end up allocating unfree-able linear alloc resources and then lose the race condition. The |
| 2511 | // other reason is that the field roots are only visited from the class table. So we need to be |
| 2512 | // inserted before we allocate / fill in these fields. |
| 2513 | LoadClass(self, dex_file, dex_class_def, klass); |
| 2514 | if (self->IsExceptionPending()) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2515 | VLOG(class_linker) << self->GetException()->Dump(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2516 | // An exception occured during load, set status to erroneous while holding klass' lock in case |
| 2517 | // notification is necessary. |
| 2518 | if (!klass->IsErroneous()) { |
| 2519 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
| 2520 | } |
| 2521 | return nullptr; |
| 2522 | } |
| 2523 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2524 | // Finish loading (if necessary) by finding parents |
| 2525 | CHECK(!klass->IsLoaded()); |
| 2526 | if (!LoadSuperAndInterfaces(klass, dex_file)) { |
| 2527 | // Loading failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2528 | if (!klass->IsErroneous()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2529 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2530 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2531 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2532 | } |
| 2533 | CHECK(klass->IsLoaded()); |
| 2534 | // Link the class (if necessary) |
| 2535 | CHECK(!klass->IsResolved()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2536 | // TODO: Use fast jobjects? |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2537 | auto interfaces = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2538 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 2539 | MutableHandle<mirror::Class> h_new_class = hs.NewHandle<mirror::Class>(nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2540 | if (!LinkClass(self, descriptor, klass, interfaces, &h_new_class)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2541 | // Linking failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2542 | if (!klass->IsErroneous()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2543 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2544 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2545 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2546 | } |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 2547 | self->AssertNoPendingException(); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 2548 | CHECK(h_new_class.Get() != nullptr) << descriptor; |
| 2549 | CHECK(h_new_class->IsResolved()) << descriptor; |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2550 | |
Nicolas Geoffray | c6df1e3 | 2016-07-04 10:15:47 +0100 | [diff] [blame] | 2551 | // Update the dex cache of where the class is defined. Inlining depends on having |
| 2552 | // this filled. |
| 2553 | h_new_class->GetDexCache()->SetResolvedType(h_new_class->GetDexTypeIndex(), h_new_class.Get()); |
| 2554 | |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 2555 | // Instrumentation may have updated entrypoints for all methods of all |
| 2556 | // classes. However it could not update methods of this class while we |
| 2557 | // were loading it. Now the class is resolved, we can update entrypoints |
| 2558 | // as required by instrumentation. |
| 2559 | if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) { |
| 2560 | // We must be in the kRunnable state to prevent instrumentation from |
| 2561 | // suspending all threads to update entrypoints while we are doing it |
| 2562 | // for this class. |
| 2563 | DCHECK_EQ(self->GetState(), kRunnable); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 2564 | Runtime::Current()->GetInstrumentation()->InstallStubsForClass(h_new_class.Get()); |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 2565 | } |
| 2566 | |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2567 | /* |
| 2568 | * We send CLASS_PREPARE events to the debugger from here. The |
| 2569 | * definition of "preparation" is creating the static fields for a |
| 2570 | * class and initializing them to the standard default values, but not |
| 2571 | * executing any code (that comes later, during "initialization"). |
| 2572 | * |
| 2573 | * We did the static preparation in LinkClass. |
| 2574 | * |
| 2575 | * The class has been prepared and resolved but possibly not yet verified |
| 2576 | * at this point. |
| 2577 | */ |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 2578 | Dbg::PostClassPrepare(h_new_class.Get()); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2579 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 2580 | // Notify native debugger of the new class and its layout. |
| 2581 | jit::Jit::NewTypeLoadedIfUsingJit(h_new_class.Get()); |
| 2582 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 2583 | return h_new_class.Get(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2584 | } |
| 2585 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2586 | uint32_t ClassLinker::SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, |
| 2587 | const DexFile::ClassDef& dex_class_def) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2588 | const uint8_t* class_data = dex_file.GetClassData(dex_class_def); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2589 | size_t num_ref = 0; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2590 | size_t num_8 = 0; |
| 2591 | size_t num_16 = 0; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2592 | size_t num_32 = 0; |
| 2593 | size_t num_64 = 0; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2594 | if (class_data != nullptr) { |
Vladimir Marko | 879d27b | 2016-03-15 20:31:50 +0000 | [diff] [blame] | 2595 | // We allow duplicate definitions of the same field in a class_data_item |
| 2596 | // but ignore the repeated indexes here, b/21868015. |
| 2597 | uint32_t last_field_idx = DexFile::kDexNoIndex; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2598 | for (ClassDataItemIterator it(dex_file, class_data); it.HasNextStaticField(); it.Next()) { |
Vladimir Marko | 879d27b | 2016-03-15 20:31:50 +0000 | [diff] [blame] | 2599 | uint32_t field_idx = it.GetMemberIndex(); |
| 2600 | // Ordering enforced by DexFileVerifier. |
| 2601 | DCHECK(last_field_idx == DexFile::kDexNoIndex || last_field_idx <= field_idx); |
| 2602 | if (UNLIKELY(field_idx == last_field_idx)) { |
| 2603 | continue; |
| 2604 | } |
| 2605 | last_field_idx = field_idx; |
| 2606 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2607 | const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2608 | char c = descriptor[0]; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2609 | switch (c) { |
| 2610 | case 'L': |
| 2611 | case '[': |
| 2612 | num_ref++; |
| 2613 | break; |
| 2614 | case 'J': |
| 2615 | case 'D': |
| 2616 | num_64++; |
| 2617 | break; |
| 2618 | case 'I': |
| 2619 | case 'F': |
| 2620 | num_32++; |
| 2621 | break; |
| 2622 | case 'S': |
| 2623 | case 'C': |
| 2624 | num_16++; |
| 2625 | break; |
| 2626 | case 'B': |
| 2627 | case 'Z': |
| 2628 | num_8++; |
| 2629 | break; |
| 2630 | default: |
| 2631 | LOG(FATAL) << "Unknown descriptor: " << c; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 2632 | UNREACHABLE(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2633 | } |
| 2634 | } |
| 2635 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2636 | return mirror::Class::ComputeClassSize(false, |
| 2637 | 0, |
| 2638 | num_8, |
| 2639 | num_16, |
| 2640 | num_32, |
| 2641 | num_64, |
| 2642 | num_ref, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2643 | image_pointer_size_); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2644 | } |
| 2645 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2646 | OatFile::OatClass ClassLinker::FindOatClass(const DexFile& dex_file, |
| 2647 | uint16_t class_def_idx, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2648 | bool* found) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2649 | DCHECK_NE(class_def_idx, DexFile::kDexNoIndex16); |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 2650 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 2651 | if (oat_dex_file == nullptr) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2652 | *found = false; |
| 2653 | return OatFile::OatClass::Invalid(); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2654 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2655 | *found = true; |
| 2656 | return oat_dex_file->GetOatClass(class_def_idx); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2657 | } |
| 2658 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2659 | static uint32_t GetOatMethodIndexFromMethodIndex(const DexFile& dex_file, |
| 2660 | uint16_t class_def_idx, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2661 | uint32_t method_idx) { |
| 2662 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_idx); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2663 | const uint8_t* class_data = dex_file.GetClassData(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2664 | CHECK(class_data != nullptr); |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2665 | ClassDataItemIterator it(dex_file, class_data); |
| 2666 | // Skip fields |
| 2667 | while (it.HasNextStaticField()) { |
| 2668 | it.Next(); |
| 2669 | } |
| 2670 | while (it.HasNextInstanceField()) { |
| 2671 | it.Next(); |
| 2672 | } |
| 2673 | // Process methods |
| 2674 | size_t class_def_method_index = 0; |
| 2675 | while (it.HasNextDirectMethod()) { |
| 2676 | if (it.GetMemberIndex() == method_idx) { |
| 2677 | return class_def_method_index; |
| 2678 | } |
| 2679 | class_def_method_index++; |
| 2680 | it.Next(); |
| 2681 | } |
| 2682 | while (it.HasNextVirtualMethod()) { |
| 2683 | if (it.GetMemberIndex() == method_idx) { |
| 2684 | return class_def_method_index; |
| 2685 | } |
| 2686 | class_def_method_index++; |
| 2687 | it.Next(); |
| 2688 | } |
| 2689 | DCHECK(!it.HasNext()); |
| 2690 | LOG(FATAL) << "Failed to find method index " << method_idx << " in " << dex_file.GetLocation(); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 2691 | UNREACHABLE(); |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2692 | } |
| 2693 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2694 | const OatFile::OatMethod ClassLinker::FindOatMethodFor(ArtMethod* method, bool* found) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2695 | // 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] | 2696 | // method for direct methods (or virtual methods made direct). |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2697 | mirror::Class* declaring_class = method->GetDeclaringClass(); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2698 | size_t oat_method_index; |
| 2699 | if (method->IsStatic() || method->IsDirect()) { |
| 2700 | // Simple case where the oat method index was stashed at load time. |
| 2701 | oat_method_index = method->GetMethodIndex(); |
| 2702 | } else { |
| 2703 | // We're invoking a virtual method directly (thanks to sharpening), compute the oat_method_index |
| 2704 | // by search for its position in the declared virtual methods. |
| 2705 | oat_method_index = declaring_class->NumDirectMethods(); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2706 | bool found_virtual = false; |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 2707 | for (ArtMethod& art_method : declaring_class->GetVirtualMethods(image_pointer_size_)) { |
Jeff Hao | 68caf9e | 2014-09-03 13:48:16 -0700 | [diff] [blame] | 2708 | // Check method index instead of identity in case of duplicate method definitions. |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 2709 | if (method->GetDexMethodIndex() == art_method.GetDexMethodIndex()) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2710 | found_virtual = true; |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2711 | break; |
| 2712 | } |
Ian Rogers | f320b63 | 2012-03-13 18:47:47 -0700 | [diff] [blame] | 2713 | oat_method_index++; |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2714 | } |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2715 | CHECK(found_virtual) << "Didn't find oat method index for virtual method: " |
| 2716 | << PrettyMethod(method); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2717 | } |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2718 | DCHECK_EQ(oat_method_index, |
| 2719 | GetOatMethodIndexFromMethodIndex(*declaring_class->GetDexCache()->GetDexFile(), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2720 | method->GetDeclaringClass()->GetDexClassDefIndex(), |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2721 | method->GetDexMethodIndex())); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2722 | OatFile::OatClass oat_class = FindOatClass(*declaring_class->GetDexCache()->GetDexFile(), |
| 2723 | declaring_class->GetDexClassDefIndex(), |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2724 | found); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2725 | if (!(*found)) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2726 | return OatFile::OatMethod::Invalid(); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2727 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2728 | return oat_class.GetOatMethod(oat_method_index); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2729 | } |
| 2730 | |
| 2731 | // Special case to get oat code without overwriting a trampoline. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2732 | const void* ClassLinker::GetQuickOatCodeFor(ArtMethod* method) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 2733 | CHECK(method->IsInvokable()) << PrettyMethod(method); |
Nicolas Geoffray | a7a4759 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 2734 | if (method->IsProxyMethod()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2735 | return GetQuickProxyInvokeHandler(); |
Jeff Hao | 8df6cea | 2013-07-29 13:54:48 -0700 | [diff] [blame] | 2736 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2737 | bool found; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2738 | OatFile::OatMethod oat_method = FindOatMethodFor(method, &found); |
Mathieu Chartier | c0d5f89 | 2015-02-25 13:22:57 -0800 | [diff] [blame] | 2739 | if (found) { |
| 2740 | auto* code = oat_method.GetQuickCode(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2741 | if (code != nullptr) { |
| 2742 | return code; |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame] | 2743 | } |
| 2744 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2745 | if (method->IsNative()) { |
| 2746 | // No code and native? Use generic trampoline. |
| 2747 | return GetQuickGenericJniStub(); |
| 2748 | } |
| 2749 | return GetQuickToInterpreterBridge(); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2750 | } |
| 2751 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2752 | const void* ClassLinker::GetOatMethodQuickCodeFor(ArtMethod* method) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 2753 | if (method->IsNative() || !method->IsInvokable() || method->IsProxyMethod()) { |
Hiroshi Yamauchi | 9bdec88 | 2014-08-15 17:11:12 -0700 | [diff] [blame] | 2754 | return nullptr; |
| 2755 | } |
Mathieu Chartier | c0d5f89 | 2015-02-25 13:22:57 -0800 | [diff] [blame] | 2756 | bool found; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2757 | OatFile::OatMethod oat_method = FindOatMethodFor(method, &found); |
Mathieu Chartier | c0d5f89 | 2015-02-25 13:22:57 -0800 | [diff] [blame] | 2758 | if (found) { |
| 2759 | return oat_method.GetQuickCode(); |
| 2760 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2761 | return nullptr; |
Hiroshi Yamauchi | 9bdec88 | 2014-08-15 17:11:12 -0700 | [diff] [blame] | 2762 | } |
| 2763 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2764 | bool ClassLinker::ShouldUseInterpreterEntrypoint(ArtMethod* method, const void* quick_code) { |
| 2765 | if (UNLIKELY(method->IsNative() || method->IsProxyMethod())) { |
| 2766 | return false; |
| 2767 | } |
| 2768 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2769 | if (quick_code == nullptr) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2770 | return true; |
| 2771 | } |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2772 | |
| 2773 | Runtime* runtime = Runtime::Current(); |
| 2774 | instrumentation::Instrumentation* instr = runtime->GetInstrumentation(); |
| 2775 | if (instr->InterpretOnly()) { |
| 2776 | return true; |
| 2777 | } |
| 2778 | |
| 2779 | if (runtime->GetClassLinker()->IsQuickToInterpreterBridge(quick_code)) { |
| 2780 | // Doing this check avoids doing compiled/interpreter transitions. |
| 2781 | return true; |
| 2782 | } |
| 2783 | |
| 2784 | if (Dbg::IsForcedInterpreterNeededForCalling(Thread::Current(), method)) { |
| 2785 | // Force the use of interpreter when it is required by the debugger. |
| 2786 | return true; |
| 2787 | } |
| 2788 | |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 2789 | if (runtime->IsNativeDebuggable()) { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 2790 | DCHECK(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse()); |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 2791 | // If we are doing native debugging, ignore application's AOT code, |
| 2792 | // since we want to JIT it with extra stackmaps for native debugging. |
| 2793 | // On the other hand, keep all AOT code from the boot image, since the |
| 2794 | // blocking JIT would results in non-negligible performance impact. |
| 2795 | return !runtime->GetHeap()->IsInBootImageOatFile(quick_code); |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 2798 | if (Dbg::IsDebuggerActive()) { |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 2799 | // Boot image classes may be AOT-compiled as non-debuggable. |
| 2800 | // This is not suitable for the Java debugger, so ignore the AOT code. |
Mingyao Yang | 6ea1a0e | 2016-01-29 12:12:49 -0800 | [diff] [blame] | 2801 | return runtime->GetHeap()->IsInBootImageOatFile(quick_code); |
| 2802 | } |
| 2803 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2804 | return false; |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2805 | } |
| 2806 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2807 | void ClassLinker::FixupStaticTrampolines(mirror::Class* klass) { |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 2808 | DCHECK(klass->IsInitialized()) << PrettyDescriptor(klass); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2809 | if (klass->NumDirectMethods() == 0) { |
| 2810 | return; // No direct methods => no static methods. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2811 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2812 | Runtime* runtime = Runtime::Current(); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 2813 | if (!runtime->IsStarted()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2814 | if (runtime->IsAotCompiler() || runtime->GetHeap()->HasBootImageSpace()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 2815 | return; // OAT file unavailable. |
| 2816 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2817 | } |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 2818 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 2819 | const DexFile& dex_file = klass->GetDexFile(); |
| 2820 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2821 | CHECK(dex_class_def != nullptr); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2822 | const uint8_t* class_data = dex_file.GetClassData(*dex_class_def); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2823 | // There should always be class data if there were direct methods. |
| 2824 | CHECK(class_data != nullptr) << PrettyDescriptor(klass); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2825 | ClassDataItemIterator it(dex_file, class_data); |
| 2826 | // Skip fields |
| 2827 | while (it.HasNextStaticField()) { |
| 2828 | it.Next(); |
| 2829 | } |
| 2830 | while (it.HasNextInstanceField()) { |
| 2831 | it.Next(); |
| 2832 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2833 | bool has_oat_class; |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2834 | OatFile::OatClass oat_class = FindOatClass(dex_file, |
| 2835 | klass->GetDexClassDefIndex(), |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2836 | &has_oat_class); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2837 | // Link the code of methods skipped by LinkCode. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2838 | for (size_t method_index = 0; it.HasNextDirectMethod(); ++method_index, it.Next()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2839 | ArtMethod* method = klass->GetDirectMethod(method_index, image_pointer_size_); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2840 | if (!method->IsStatic()) { |
| 2841 | // Only update static methods. |
| 2842 | continue; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2843 | } |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2844 | const void* quick_code = nullptr; |
| 2845 | if (has_oat_class) { |
| 2846 | OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2847 | quick_code = oat_method.GetQuickCode(); |
| 2848 | } |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2849 | // Check whether the method is native, in which case it's generic JNI. |
| 2850 | if (quick_code == nullptr && method->IsNative()) { |
| 2851 | quick_code = GetQuickGenericJniStub(); |
| 2852 | } else if (ShouldUseInterpreterEntrypoint(method, quick_code)) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2853 | // Use interpreter entry point. |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2854 | quick_code = GetQuickToInterpreterBridge(); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2855 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2856 | runtime->GetInstrumentation()->UpdateMethodsCode(method, quick_code); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2857 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2858 | // Ignore virtual methods on the iterator. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2859 | } |
| 2860 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 2861 | void ClassLinker::EnsureThrowsInvocationError(ArtMethod* method) { |
| 2862 | DCHECK(method != nullptr); |
| 2863 | DCHECK(!method->IsInvokable()); |
| 2864 | method->SetEntryPointFromQuickCompiledCodePtrSize(quick_to_interpreter_bridge_trampoline_, |
| 2865 | image_pointer_size_); |
| 2866 | } |
| 2867 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2868 | void ClassLinker::LinkCode(ArtMethod* method, const OatFile::OatClass* oat_class, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2869 | uint32_t class_def_method_index) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2870 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2871 | if (runtime->IsAotCompiler()) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2872 | // The following code only applies to a non-compiler runtime. |
| 2873 | return; |
| 2874 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2875 | // Method shouldn't have already been linked. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2876 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2877 | if (oat_class != nullptr) { |
| 2878 | // Every kind of method should at least get an invoke stub from the oat_method. |
| 2879 | // non-abstract methods also get their code pointers. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2880 | const OatFile::OatMethod oat_method = oat_class->GetOatMethod(class_def_method_index); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2881 | oat_method.LinkMethod(method); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2882 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2883 | |
Tamas Berghammer | 3a98aae | 2016-02-08 20:21:54 +0000 | [diff] [blame] | 2884 | // Install entry point from interpreter. |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2885 | const void* quick_code = method->GetEntryPointFromQuickCompiledCode(); |
| 2886 | bool enter_interpreter = ShouldUseInterpreterEntrypoint(method, quick_code); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 2887 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 2888 | if (!method->IsInvokable()) { |
| 2889 | EnsureThrowsInvocationError(method); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2890 | return; |
| 2891 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2892 | |
| 2893 | if (method->IsStatic() && !method->IsConstructor()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2894 | // For static methods excluding the class initializer, install the trampoline. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2895 | // It will be replaced by the proper entry point by ClassLinker::FixupStaticTrampolines |
| 2896 | // after initializing class (see ClassLinker::InitializeClass method). |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2897 | method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionStub()); |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2898 | } else if (quick_code == nullptr && method->IsNative()) { |
| 2899 | method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub()); |
Tamas Berghammer | 3a98aae | 2016-02-08 20:21:54 +0000 | [diff] [blame] | 2900 | } else if (enter_interpreter) { |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2901 | // Set entry point from compiled code if there's no code or in interpreter only mode. |
| 2902 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
Ian Rogers | 0d6de04 | 2012-02-29 08:50:26 -0800 | [diff] [blame] | 2903 | } |
jeffhao | 26c0a1a | 2012-01-17 16:28:33 -0800 | [diff] [blame] | 2904 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2905 | if (method->IsNative()) { |
| 2906 | // Unregistering restores the dlsym lookup stub. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2907 | method->UnregisterNative(); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2908 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2909 | if (enter_interpreter || quick_code == nullptr) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2910 | // We have a native method here without code. Then it should have either the generic JNI |
| 2911 | // trampoline as entrypoint (non-static), or the resolution trampoline (static). |
| 2912 | // TODO: this doesn't handle all the cases where trampolines may be installed. |
| 2913 | const void* entry_point = method->GetEntryPointFromQuickCompiledCode(); |
| 2914 | DCHECK(IsQuickGenericJniStub(entry_point) || IsQuickResolutionStub(entry_point)); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2915 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2916 | } |
| 2917 | } |
| 2918 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2919 | void ClassLinker::SetupClass(const DexFile& dex_file, |
| 2920 | const DexFile::ClassDef& dex_class_def, |
| 2921 | Handle<mirror::Class> klass, |
| 2922 | mirror::ClassLoader* class_loader) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2923 | CHECK(klass.Get() != nullptr); |
| 2924 | CHECK(klass->GetDexCache() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2925 | CHECK_EQ(mirror::Class::kStatusNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 2926 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2927 | CHECK(descriptor != nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2928 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2929 | klass->SetClass(GetClassRoot(kJavaLangClass)); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 2930 | uint32_t access_flags = dex_class_def.GetJavaAccessFlags(); |
Brian Carlstrom | 8e3fb14 | 2013-10-09 21:00:27 -0700 | [diff] [blame] | 2931 | CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2932 | klass->SetAccessFlags(access_flags); |
| 2933 | klass->SetClassLoader(class_loader); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2934 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 2935 | mirror::Class::SetStatus(klass, mirror::Class::kStatusIdx, nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2936 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2937 | klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2938 | klass->SetDexTypeIndex(dex_class_def.class_idx_); |
Mathieu Chartier | 91a6dc4 | 2014-12-01 10:31:15 -0800 | [diff] [blame] | 2939 | CHECK(klass->GetDexCacheStrings() != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2940 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2941 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2942 | void ClassLinker::LoadClass(Thread* self, |
| 2943 | const DexFile& dex_file, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2944 | const DexFile::ClassDef& dex_class_def, |
| 2945 | Handle<mirror::Class> klass) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2946 | const uint8_t* class_data = dex_file.GetClassData(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2947 | if (class_data == nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2948 | return; // no fields or methods - for example a marker interface |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2949 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2950 | bool has_oat_class = false; |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 2951 | if (Runtime::Current()->IsStarted() && !Runtime::Current()->IsAotCompiler()) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2952 | OatFile::OatClass oat_class = FindOatClass(dex_file, klass->GetDexClassDefIndex(), |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2953 | &has_oat_class); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2954 | if (has_oat_class) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2955 | LoadClassMembers(self, dex_file, class_data, klass, &oat_class); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2956 | } |
| 2957 | } |
| 2958 | if (!has_oat_class) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2959 | LoadClassMembers(self, dex_file, class_data, klass, nullptr); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2960 | } |
| 2961 | } |
| 2962 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 2963 | LengthPrefixedArray<ArtField>* ClassLinker::AllocArtFieldArray(Thread* self, |
| 2964 | LinearAlloc* allocator, |
| 2965 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2966 | if (length == 0) { |
| 2967 | return nullptr; |
| 2968 | } |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 2969 | // If the ArtField alignment changes, review all uses of LengthPrefixedArray<ArtField>. |
| 2970 | static_assert(alignof(ArtField) == 4, "ArtField alignment is expected to be 4."); |
| 2971 | size_t storage_size = LengthPrefixedArray<ArtField>::ComputeSize(length); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 2972 | void* array_storage = allocator->Alloc(self, storage_size); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 2973 | auto* ret = new(array_storage) LengthPrefixedArray<ArtField>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2974 | CHECK(ret != nullptr); |
| 2975 | std::uninitialized_fill_n(&ret->At(0), length, ArtField()); |
| 2976 | return ret; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2977 | } |
| 2978 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 2979 | LengthPrefixedArray<ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self, |
| 2980 | LinearAlloc* allocator, |
| 2981 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2982 | if (length == 0) { |
| 2983 | return nullptr; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2984 | } |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 2985 | const size_t method_alignment = ArtMethod::Alignment(image_pointer_size_); |
| 2986 | const size_t method_size = ArtMethod::Size(image_pointer_size_); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 2987 | const size_t storage_size = |
| 2988 | LengthPrefixedArray<ArtMethod>::ComputeSize(length, method_size, method_alignment); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 2989 | void* array_storage = allocator->Alloc(self, storage_size); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 2990 | auto* ret = new (array_storage) LengthPrefixedArray<ArtMethod>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2991 | CHECK(ret != nullptr); |
| 2992 | for (size_t i = 0; i < length; ++i) { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 2993 | new(reinterpret_cast<void*>(&ret->At(i, method_size, method_alignment))) ArtMethod; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2994 | } |
| 2995 | return ret; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2996 | } |
| 2997 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 2998 | LinearAlloc* ClassLinker::GetAllocatorForClassLoader(mirror::ClassLoader* class_loader) { |
| 2999 | if (class_loader == nullptr) { |
| 3000 | return Runtime::Current()->GetLinearAlloc(); |
| 3001 | } |
| 3002 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3003 | DCHECK(allocator != nullptr); |
| 3004 | return allocator; |
| 3005 | } |
| 3006 | |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3007 | LinearAlloc* ClassLinker::GetOrCreateAllocatorForClassLoader(mirror::ClassLoader* class_loader) { |
| 3008 | if (class_loader == nullptr) { |
| 3009 | return Runtime::Current()->GetLinearAlloc(); |
| 3010 | } |
| 3011 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3012 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3013 | if (allocator == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 3014 | RegisterClassLoader(class_loader); |
| 3015 | allocator = class_loader->GetAllocator(); |
| 3016 | CHECK(allocator != nullptr); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3017 | } |
| 3018 | return allocator; |
| 3019 | } |
| 3020 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3021 | void ClassLinker::LoadClassMembers(Thread* self, |
| 3022 | const DexFile& dex_file, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 3023 | const uint8_t* class_data, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3024 | Handle<mirror::Class> klass, |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 3025 | const OatFile::OatClass* oat_class) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3026 | { |
| 3027 | // Note: We cannot have thread suspension until the field and method arrays are setup or else |
| 3028 | // Class::VisitFieldRoots may miss some fields or methods. |
| 3029 | ScopedAssertNoThreadSuspension nts(self, __FUNCTION__); |
| 3030 | // Load static fields. |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3031 | // We allow duplicate definitions of the same field in a class_data_item |
| 3032 | // but ignore the repeated indexes here, b/21868015. |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3033 | LinearAlloc* const allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3034 | ClassDataItemIterator it(dex_file, class_data); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3035 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, |
| 3036 | allocator, |
| 3037 | it.NumStaticFields()); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3038 | size_t num_sfields = 0; |
| 3039 | uint32_t last_field_idx = 0u; |
| 3040 | for (; it.HasNextStaticField(); it.Next()) { |
| 3041 | uint32_t field_idx = it.GetMemberIndex(); |
| 3042 | DCHECK_GE(field_idx, last_field_idx); // Ordering enforced by DexFileVerifier. |
| 3043 | if (num_sfields == 0 || LIKELY(field_idx > last_field_idx)) { |
| 3044 | DCHECK_LT(num_sfields, it.NumStaticFields()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3045 | LoadField(it, klass, &sfields->At(num_sfields)); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3046 | ++num_sfields; |
| 3047 | last_field_idx = field_idx; |
| 3048 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3049 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3050 | // Load instance fields. |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3051 | LengthPrefixedArray<ArtField>* ifields = AllocArtFieldArray(self, |
| 3052 | allocator, |
| 3053 | it.NumInstanceFields()); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3054 | size_t num_ifields = 0u; |
| 3055 | last_field_idx = 0u; |
| 3056 | for (; it.HasNextInstanceField(); it.Next()) { |
| 3057 | uint32_t field_idx = it.GetMemberIndex(); |
| 3058 | DCHECK_GE(field_idx, last_field_idx); // Ordering enforced by DexFileVerifier. |
| 3059 | if (num_ifields == 0 || LIKELY(field_idx > last_field_idx)) { |
| 3060 | DCHECK_LT(num_ifields, it.NumInstanceFields()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3061 | LoadField(it, klass, &ifields->At(num_ifields)); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3062 | ++num_ifields; |
| 3063 | last_field_idx = field_idx; |
| 3064 | } |
| 3065 | } |
| 3066 | if (UNLIKELY(num_sfields != it.NumStaticFields()) || |
| 3067 | UNLIKELY(num_ifields != it.NumInstanceFields())) { |
| 3068 | LOG(WARNING) << "Duplicate fields in class " << PrettyDescriptor(klass.Get()) |
| 3069 | << " (unique static fields: " << num_sfields << "/" << it.NumStaticFields() |
| 3070 | << ", unique instance fields: " << num_ifields << "/" << it.NumInstanceFields() << ")"; |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 3071 | // NOTE: Not shrinking the over-allocated sfields/ifields, just setting size. |
| 3072 | if (sfields != nullptr) { |
| 3073 | sfields->SetSize(num_sfields); |
| 3074 | } |
| 3075 | if (ifields != nullptr) { |
| 3076 | ifields->SetSize(num_ifields); |
| 3077 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3078 | } |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 3079 | // Set the field arrays. |
| 3080 | klass->SetSFieldsPtr(sfields); |
| 3081 | DCHECK_EQ(klass->NumStaticFields(), num_sfields); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3082 | klass->SetIFieldsPtr(ifields); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3083 | DCHECK_EQ(klass->NumInstanceFields(), num_ifields); |
| 3084 | // Load methods. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 3085 | klass->SetMethodsPtr( |
| 3086 | AllocArtMethodArray(self, allocator, it.NumDirectMethods() + it.NumVirtualMethods()), |
| 3087 | it.NumDirectMethods(), |
| 3088 | it.NumVirtualMethods()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3089 | size_t class_def_method_index = 0; |
| 3090 | uint32_t last_dex_method_index = DexFile::kDexNoIndex; |
| 3091 | size_t last_class_def_method_index = 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 3092 | // TODO These should really use the iterators. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3093 | for (size_t i = 0; it.HasNextDirectMethod(); i++, it.Next()) { |
| 3094 | ArtMethod* method = klass->GetDirectMethodUnchecked(i, image_pointer_size_); |
| 3095 | LoadMethod(self, dex_file, it, klass, method); |
| 3096 | LinkCode(method, oat_class, class_def_method_index); |
| 3097 | uint32_t it_method_index = it.GetMemberIndex(); |
| 3098 | if (last_dex_method_index == it_method_index) { |
| 3099 | // duplicate case |
| 3100 | method->SetMethodIndex(last_class_def_method_index); |
| 3101 | } else { |
| 3102 | method->SetMethodIndex(class_def_method_index); |
| 3103 | last_dex_method_index = it_method_index; |
| 3104 | last_class_def_method_index = class_def_method_index; |
| 3105 | } |
| 3106 | class_def_method_index++; |
| 3107 | } |
| 3108 | for (size_t i = 0; it.HasNextVirtualMethod(); i++, it.Next()) { |
| 3109 | ArtMethod* method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
| 3110 | LoadMethod(self, dex_file, it, klass, method); |
| 3111 | DCHECK_EQ(class_def_method_index, it.NumDirectMethods() + i); |
| 3112 | LinkCode(method, oat_class, class_def_method_index); |
| 3113 | class_def_method_index++; |
| 3114 | } |
| 3115 | DCHECK(!it.HasNext()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3116 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 3117 | // Ensure that the card is marked so that remembered sets pick up native roots. |
| 3118 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(klass.Get()); |
Mathieu Chartier | f3f2a7a | 2015-04-14 15:43:10 -0700 | [diff] [blame] | 3119 | self->AllowThreadSuspension(); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3120 | } |
| 3121 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3122 | void ClassLinker::LoadField(const ClassDataItemIterator& it, |
| 3123 | Handle<mirror::Class> klass, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3124 | ArtField* dst) { |
| 3125 | const uint32_t field_idx = it.GetMemberIndex(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3126 | dst->SetDexFieldIndex(field_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3127 | dst->SetDeclaringClass(klass.Get()); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 3128 | dst->SetAccessFlags(it.GetFieldAccessFlags()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3129 | } |
| 3130 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3131 | void ClassLinker::LoadMethod(Thread* self, |
| 3132 | const DexFile& dex_file, |
| 3133 | const ClassDataItemIterator& it, |
| 3134 | Handle<mirror::Class> klass, |
| 3135 | ArtMethod* dst) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3136 | uint32_t dex_method_idx = it.GetMemberIndex(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3137 | const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3138 | const char* method_name = dex_file.StringDataByIdx(method_id.name_idx_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3139 | |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 3140 | ScopedAssertNoThreadSuspension ants(self, "LoadMethod"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3141 | dst->SetDexMethodIndex(dex_method_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3142 | dst->SetDeclaringClass(klass.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3143 | dst->SetCodeItemOffset(it.GetMethodCodeItemOffset()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3144 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 3145 | dst->SetDexCacheResolvedMethods(klass->GetDexCache()->GetResolvedMethods(), image_pointer_size_); |
| 3146 | dst->SetDexCacheResolvedTypes(klass->GetDexCache()->GetResolvedTypes(), image_pointer_size_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3147 | |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 3148 | uint32_t access_flags = it.GetMethodAccessFlags(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3149 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3150 | if (UNLIKELY(strcmp("finalize", method_name) == 0)) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3151 | // Set finalizable flag on declaring class. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3152 | if (strcmp("V", dex_file.GetShorty(method_id.proto_idx_)) == 0) { |
| 3153 | // Void return type. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3154 | if (klass->GetClassLoader() != nullptr) { // All non-boot finalizer methods are flagged. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3155 | klass->SetFinalizable(); |
| 3156 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3157 | std::string temp; |
| 3158 | const char* klass_descriptor = klass->GetDescriptor(&temp); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3159 | // The Enum class declares a "final" finalize() method to prevent subclasses from |
| 3160 | // introducing a finalizer. We don't want to set the finalizable flag for Enum or its |
| 3161 | // subclasses, so we exclude it here. |
| 3162 | // We also want to avoid setting the flag on Object, where we know that finalize() is |
| 3163 | // empty. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3164 | if (strcmp(klass_descriptor, "Ljava/lang/Object;") != 0 && |
| 3165 | strcmp(klass_descriptor, "Ljava/lang/Enum;") != 0) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3166 | klass->SetFinalizable(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3167 | } |
| 3168 | } |
| 3169 | } |
| 3170 | } else if (method_name[0] == '<') { |
| 3171 | // Fix broken access flags for initializers. Bug 11157540. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3172 | bool is_init = (strcmp("<init>", method_name) == 0); |
| 3173 | bool is_clinit = !is_init && (strcmp("<clinit>", method_name) == 0); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3174 | if (UNLIKELY(!is_init && !is_clinit)) { |
| 3175 | LOG(WARNING) << "Unexpected '<' at start of method name " << method_name; |
| 3176 | } else { |
| 3177 | if (UNLIKELY((access_flags & kAccConstructor) == 0)) { |
| 3178 | 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] | 3179 | << PrettyDescriptor(klass.Get()) << " in dex file " << dex_file.GetLocation(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3180 | access_flags |= kAccConstructor; |
| 3181 | } |
| 3182 | } |
| 3183 | } |
| 3184 | dst->SetAccessFlags(access_flags); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3185 | } |
| 3186 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3187 | void ClassLinker::AppendToBootClassPath(Thread* self, const DexFile& dex_file) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3188 | StackHandleScope<1> hs(self); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3189 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(AllocDexCache( |
| 3190 | self, |
| 3191 | dex_file, |
| 3192 | Runtime::Current()->GetLinearAlloc()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3193 | CHECK(dex_cache.Get() != nullptr) << "Failed to allocate dex cache for " |
| 3194 | << dex_file.GetLocation(); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 3195 | AppendToBootClassPath(dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 3196 | } |
| 3197 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3198 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3199 | Handle<mirror::DexCache> dex_cache) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3200 | CHECK(dex_cache.Get() != nullptr) << dex_file.GetLocation(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 3201 | boot_class_path_.push_back(&dex_file); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 3202 | RegisterDexFile(dex_file, dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3203 | } |
| 3204 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3205 | void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3206 | Handle<mirror::DexCache> dex_cache) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3207 | Thread* const self = Thread::Current(); |
| 3208 | dex_lock_.AssertExclusiveHeld(self); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3209 | CHECK(dex_cache.Get() != nullptr) << dex_file.GetLocation(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3210 | // For app images, the dex cache location may be a suffix of the dex file location since the |
| 3211 | // dex file location is an absolute path. |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3212 | const std::string dex_cache_location = dex_cache->GetLocation()->ToModifiedUtf8(); |
| 3213 | const size_t dex_cache_length = dex_cache_location.length(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3214 | CHECK_GT(dex_cache_length, 0u) << dex_file.GetLocation(); |
| 3215 | std::string dex_file_location = dex_file.GetLocation(); |
| 3216 | CHECK_GE(dex_file_location.length(), dex_cache_length) |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3217 | << dex_cache_location << " " << dex_file.GetLocation(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3218 | // Take suffix. |
| 3219 | const std::string dex_file_suffix = dex_file_location.substr( |
| 3220 | dex_file_location.length() - dex_cache_length, |
| 3221 | dex_cache_length); |
| 3222 | // Example dex_cache location is SettingsProvider.apk and |
| 3223 | // dex file location is /system/priv-app/SettingsProvider/SettingsProvider.apk |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3224 | CHECK_EQ(dex_cache_location, dex_file_suffix); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3225 | // Clean up pass to remove null dex caches. |
| 3226 | // Null dex caches can occur due to class unloading and we are lazily removing null entries. |
| 3227 | JavaVMExt* const vm = self->GetJniEnv()->vm; |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3228 | for (auto it = dex_caches_.begin(); it != dex_caches_.end(); ) { |
| 3229 | DexCacheData data = *it; |
| 3230 | if (self->IsJWeakCleared(data.weak_root)) { |
| 3231 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3232 | it = dex_caches_.erase(it); |
| 3233 | } else { |
| 3234 | ++it; |
| 3235 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3236 | } |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3237 | jweak dex_cache_jweak = vm->AddWeakGlobalRef(self, dex_cache.Get()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3238 | dex_cache->SetDexFile(&dex_file); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3239 | DexCacheData data; |
| 3240 | data.weak_root = dex_cache_jweak; |
| 3241 | data.dex_file = dex_cache->GetDexFile(); |
| 3242 | data.resolved_types = dex_cache->GetResolvedTypes(); |
| 3243 | dex_caches_.push_back(data); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3244 | } |
| 3245 | |
Mathieu Chartier | 696632e | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3246 | mirror::DexCache* ClassLinker::RegisterDexFile(const DexFile& dex_file, |
| 3247 | mirror::ClassLoader* class_loader) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3248 | Thread* self = Thread::Current(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3249 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3250 | ReaderMutexLock mu(self, dex_lock_); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3251 | mirror::DexCache* dex_cache = FindDexCacheLocked(self, dex_file, true); |
| 3252 | if (dex_cache != nullptr) { |
| 3253 | return dex_cache; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3254 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3255 | } |
Mathieu Chartier | 696632e | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3256 | LinearAlloc* const linear_alloc = GetOrCreateAllocatorForClassLoader(class_loader); |
| 3257 | DCHECK(linear_alloc != nullptr); |
| 3258 | ClassTable* table; |
| 3259 | { |
| 3260 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3261 | table = InsertClassTableForClassLoader(class_loader); |
| 3262 | } |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3263 | // Don't alloc while holding the lock, since allocation may need to |
| 3264 | // suspend all threads and another thread may need the dex_lock_ to |
| 3265 | // get to a suspend point. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3266 | StackHandleScope<1> hs(self); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3267 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(AllocDexCache(self, dex_file, linear_alloc))); |
Mathieu Chartier | 696632e | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3268 | { |
| 3269 | WriterMutexLock mu(self, dex_lock_); |
| 3270 | mirror::DexCache* dex_cache = FindDexCacheLocked(self, dex_file, true); |
| 3271 | if (dex_cache != nullptr) { |
| 3272 | return dex_cache; |
| 3273 | } |
| 3274 | if (h_dex_cache.Get() == nullptr) { |
| 3275 | self->AssertPendingOOMException(); |
| 3276 | return nullptr; |
| 3277 | } |
| 3278 | RegisterDexFileLocked(dex_file, h_dex_cache); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3279 | } |
Mathieu Chartier | 696632e | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3280 | table->InsertStrongRoot(h_dex_cache.Get()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3281 | return h_dex_cache.Get(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3282 | } |
| 3283 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3284 | void ClassLinker::RegisterDexFile(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3285 | Handle<mirror::DexCache> dex_cache) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3286 | WriterMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3287 | RegisterDexFileLocked(dex_file, dex_cache); |
| 3288 | } |
| 3289 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3290 | mirror::DexCache* ClassLinker::FindDexCache(Thread* self, |
| 3291 | const DexFile& dex_file, |
| 3292 | bool allow_failure) { |
| 3293 | ReaderMutexLock mu(self, dex_lock_); |
| 3294 | return FindDexCacheLocked(self, dex_file, allow_failure); |
| 3295 | } |
| 3296 | |
| 3297 | mirror::DexCache* ClassLinker::FindDexCacheLocked(Thread* self, |
| 3298 | const DexFile& dex_file, |
| 3299 | bool allow_failure) { |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3300 | // Search assuming unique-ness of dex file. |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3301 | for (const DexCacheData& data : dex_caches_) { |
| 3302 | // Avoid decoding (and read barriers) other unrelated dex caches. |
| 3303 | if (data.dex_file == &dex_file) { |
| 3304 | mirror::DexCache* dex_cache = |
| 3305 | down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root)); |
| 3306 | if (dex_cache != nullptr) { |
| 3307 | return dex_cache; |
| 3308 | } else { |
| 3309 | break; |
| 3310 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3311 | } |
| 3312 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3313 | if (allow_failure) { |
| 3314 | return nullptr; |
| 3315 | } |
Mathieu Chartier | ac8f439 | 2015-08-27 13:54:20 -0700 | [diff] [blame] | 3316 | std::string location(dex_file.GetLocation()); |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3317 | // Failure, dump diagnostic and abort. |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3318 | for (const DexCacheData& data : dex_caches_) { |
| 3319 | mirror::DexCache* dex_cache = down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3320 | if (dex_cache != nullptr) { |
| 3321 | LOG(ERROR) << "Registered dex file " << dex_cache->GetDexFile()->GetLocation(); |
| 3322 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3323 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3324 | LOG(FATAL) << "Failed to find DexCache for DexFile " << location; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 3325 | UNREACHABLE(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3326 | } |
| 3327 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3328 | void ClassLinker::FixupDexCaches(ArtMethod* resolution_method) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3329 | Thread* const self = Thread::Current(); |
| 3330 | ReaderMutexLock mu(self, dex_lock_); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3331 | for (const DexCacheData& data : dex_caches_) { |
| 3332 | if (!self->IsJWeakCleared(data.weak_root)) { |
| 3333 | mirror::DexCache* dex_cache = down_cast<mirror::DexCache*>( |
| 3334 | self->DecodeJObject(data.weak_root)); |
| 3335 | if (dex_cache != nullptr) { |
| 3336 | dex_cache->Fixup(resolution_method, image_pointer_size_); |
| 3337 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3338 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3339 | } |
| 3340 | } |
| 3341 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3342 | mirror::Class* ClassLinker::CreatePrimitiveClass(Thread* self, Primitive::Type type) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3343 | mirror::Class* klass = AllocClass(self, mirror::Class::PrimitiveClassSize(image_pointer_size_)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3344 | if (UNLIKELY(klass == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3345 | self->AssertPendingOOMException(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3346 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3347 | } |
| 3348 | return InitializePrimitiveClass(klass, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3349 | } |
| 3350 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3351 | mirror::Class* ClassLinker::InitializePrimitiveClass(mirror::Class* primitive_class, |
| 3352 | Primitive::Type type) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3353 | CHECK(primitive_class != nullptr); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3354 | // Must hold lock on object when initializing. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3355 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3356 | StackHandleScope<1> hs(self); |
| 3357 | Handle<mirror::Class> h_class(hs.NewHandle(primitive_class)); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3358 | ObjectLock<mirror::Class> lock(self, h_class); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3359 | h_class->SetAccessFlags(kAccPublic | kAccFinal | kAccAbstract); |
| 3360 | h_class->SetPrimitiveType(type); |
| 3361 | mirror::Class::SetStatus(h_class, mirror::Class::kStatusInitialized, self); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3362 | const char* descriptor = Primitive::Descriptor(type); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3363 | mirror::Class* existing = InsertClass(descriptor, h_class.Get(), |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3364 | ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3365 | CHECK(existing == nullptr) << "InitPrimitiveClass(" << type << ") failed"; |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3366 | return h_class.Get(); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 3367 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3368 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3369 | // Create an array class (i.e. the class object for the array, not the |
| 3370 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 3371 | // "[Ljava/lang/String;". |
| 3372 | // |
| 3373 | // If "descriptor" refers to an array of primitives, look up the |
| 3374 | // primitive type's internally-generated class object. |
| 3375 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3376 | // "class_loader" is the class loader of the class that's referring to |
| 3377 | // us. It's used to ensure that we're looking for the element type in |
| 3378 | // the right context. It does NOT become the class loader for the |
| 3379 | // array class; that always comes from the base element class. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3380 | // |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 3381 | // Returns null with an exception raised on failure. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 3382 | mirror::Class* ClassLinker::CreateArrayClass(Thread* self, const char* descriptor, size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3383 | Handle<mirror::ClassLoader> class_loader) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3384 | // Identify the underlying component type |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3385 | CHECK_EQ('[', descriptor[0]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3386 | StackHandleScope<2> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3387 | MutableHandle<mirror::Class> component_type(hs.NewHandle(FindClass(self, descriptor + 1, |
| 3388 | class_loader))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3389 | if (component_type.Get() == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3390 | DCHECK(self->IsExceptionPending()); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 3391 | // We need to accept erroneous classes as component types. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3392 | const size_t component_hash = ComputeModifiedUtf8Hash(descriptor + 1); |
| 3393 | component_type.Assign(LookupClass(self, descriptor + 1, component_hash, class_loader.Get())); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 3394 | if (component_type.Get() == nullptr) { |
| 3395 | DCHECK(self->IsExceptionPending()); |
| 3396 | return nullptr; |
| 3397 | } else { |
| 3398 | self->ClearException(); |
| 3399 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3400 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 3401 | if (UNLIKELY(component_type->IsPrimitiveVoid())) { |
| 3402 | ThrowNoClassDefFoundError("Attempt to create array of void primitive type"); |
| 3403 | return nullptr; |
| 3404 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3405 | // See if the component type is already loaded. Array classes are |
| 3406 | // always associated with the class loader of their underlying |
| 3407 | // element type -- an array of Strings goes with the loader for |
| 3408 | // java/lang/String -- so we need to look for it there. (The |
| 3409 | // caller should have checked for the existence of the class |
| 3410 | // before calling here, but they did so with *their* class loader, |
| 3411 | // not the component type's loader.) |
| 3412 | // |
| 3413 | // If we find it, the caller adds "loader" to the class' initiating |
| 3414 | // loader list, which should prevent us from going through this again. |
| 3415 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3416 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3417 | // are the same, because our caller (FindClass) just did the |
| 3418 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 3419 | // because we effectively do this lookup again when we add the new |
| 3420 | // class to the hash table --- necessary because of possible races with |
| 3421 | // other threads.) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3422 | if (class_loader.Get() != component_type->GetClassLoader()) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3423 | mirror::Class* new_class = LookupClass(self, descriptor, hash, component_type->GetClassLoader()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3424 | if (new_class != nullptr) { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3425 | return new_class; |
| 3426 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3427 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3428 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3429 | // Fill out the fields in the Class. |
| 3430 | // |
| 3431 | // It is possible to execute some methods against arrays, because |
| 3432 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 3433 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 3434 | // |
| 3435 | // Array classes are simple enough that we don't need to do a full |
| 3436 | // link step. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3437 | auto new_class = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3438 | if (UNLIKELY(!init_done_)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3439 | // Classes that were hand created, ie not by FindSystemClass |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3440 | if (strcmp(descriptor, "[Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3441 | new_class.Assign(GetClassRoot(kClassArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3442 | } else if (strcmp(descriptor, "[Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3443 | new_class.Assign(GetClassRoot(kObjectArrayClass)); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3444 | } else if (strcmp(descriptor, GetClassRootDescriptor(kJavaLangStringArrayClass)) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3445 | new_class.Assign(GetClassRoot(kJavaLangStringArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3446 | } else if (strcmp(descriptor, "[C") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3447 | new_class.Assign(GetClassRoot(kCharArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3448 | } else if (strcmp(descriptor, "[I") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3449 | new_class.Assign(GetClassRoot(kIntArrayClass)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3450 | } else if (strcmp(descriptor, "[J") == 0) { |
| 3451 | new_class.Assign(GetClassRoot(kLongArrayClass)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3452 | } |
| 3453 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3454 | if (new_class.Get() == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3455 | new_class.Assign(AllocClass(self, mirror::Array::ClassSize(image_pointer_size_))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3456 | if (new_class.Get() == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3457 | self->AssertPendingOOMException(); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3458 | return nullptr; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3459 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3460 | new_class->SetComponentType(component_type.Get()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3461 | } |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3462 | ObjectLock<mirror::Class> lock(self, new_class); // Must hold lock on object when initializing. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3463 | DCHECK(new_class->GetComponentType() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3464 | mirror::Class* java_lang_Object = GetClassRoot(kJavaLangObject); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3465 | new_class->SetSuperClass(java_lang_Object); |
| 3466 | new_class->SetVTable(java_lang_Object->GetVTable()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3467 | new_class->SetPrimitiveType(Primitive::kPrimNot); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3468 | new_class->SetClassLoader(component_type->GetClassLoader()); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 3469 | if (component_type->IsPrimitive()) { |
| 3470 | new_class->SetClassFlags(mirror::kClassFlagNoReferenceFields); |
| 3471 | } else { |
| 3472 | new_class->SetClassFlags(mirror::kClassFlagObjectArray); |
| 3473 | } |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3474 | mirror::Class::SetStatus(new_class, mirror::Class::kStatusLoaded, self); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 3475 | new_class->PopulateEmbeddedVTable(image_pointer_size_); |
| 3476 | ImTable* object_imt = java_lang_Object->GetImt(image_pointer_size_); |
| 3477 | new_class->SetImt(object_imt, image_pointer_size_); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3478 | mirror::Class::SetStatus(new_class, mirror::Class::kStatusInitialized, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3479 | // don't need to set new_class->SetObjectSize(..) |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 3480 | // because Object::SizeOf delegates to Array::SizeOf |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3481 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3482 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 3483 | // interfaces. We need to set that up here, so that stuff like |
| 3484 | // "instanceof" works right. |
| 3485 | // |
| 3486 | // Note: The GC could run during the call to FindSystemClass, |
| 3487 | // so we need to make sure the class object is GC-valid while we're in |
| 3488 | // there. Do this by clearing the interface list so the GC will just |
| 3489 | // think that the entries are null. |
| 3490 | |
| 3491 | |
| 3492 | // Use the single, global copies of "interfaces" and "iftable" |
| 3493 | // (remember not to free them for arrays). |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3494 | { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 3495 | mirror::IfTable* array_iftable = array_iftable_.Read(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3496 | CHECK(array_iftable != nullptr); |
| 3497 | new_class->SetIfTable(array_iftable); |
| 3498 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3499 | |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 3500 | // Inherit access flags from the component type. |
| 3501 | int access_flags = new_class->GetComponentType()->GetAccessFlags(); |
| 3502 | // Lose any implementation detail flags; in particular, arrays aren't finalizable. |
| 3503 | access_flags &= kAccJavaFlagsMask; |
| 3504 | // 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] | 3505 | // and remove "interface". |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 3506 | access_flags |= kAccAbstract | kAccFinal; |
| 3507 | access_flags &= ~kAccInterface; |
| 3508 | |
| 3509 | new_class->SetAccessFlags(access_flags); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3510 | |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3511 | mirror::Class* existing = InsertClass(descriptor, new_class.Get(), hash); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3512 | if (existing == nullptr) { |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 3513 | jit::Jit::NewTypeLoadedIfUsingJit(new_class.Get()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3514 | return new_class.Get(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3515 | } |
| 3516 | // Another thread must have loaded the class after we |
| 3517 | // started but before we finished. Abandon what we've |
| 3518 | // done. |
| 3519 | // |
| 3520 | // (Yes, this happens.) |
| 3521 | |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 3522 | return existing; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3523 | } |
| 3524 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3525 | mirror::Class* ClassLinker::FindPrimitiveClass(char type) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3526 | switch (type) { |
| 3527 | case 'B': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3528 | return GetClassRoot(kPrimitiveByte); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3529 | case 'C': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3530 | return GetClassRoot(kPrimitiveChar); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3531 | case 'D': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3532 | return GetClassRoot(kPrimitiveDouble); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3533 | case 'F': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3534 | return GetClassRoot(kPrimitiveFloat); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3535 | case 'I': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3536 | return GetClassRoot(kPrimitiveInt); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3537 | case 'J': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3538 | return GetClassRoot(kPrimitiveLong); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3539 | case 'S': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3540 | return GetClassRoot(kPrimitiveShort); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3541 | case 'Z': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3542 | return GetClassRoot(kPrimitiveBoolean); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3543 | case 'V': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3544 | return GetClassRoot(kPrimitiveVoid); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3545 | default: |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3546 | break; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 3547 | } |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 3548 | std::string printable_type(PrintableChar(type)); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 3549 | ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3550 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3551 | } |
| 3552 | |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 3553 | mirror::Class* ClassLinker::InsertClass(const char* descriptor, mirror::Class* klass, size_t hash) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3554 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3555 | mirror::DexCache* dex_cache = klass->GetDexCache(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3556 | std::string source; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3557 | if (dex_cache != nullptr) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3558 | source += " from "; |
| 3559 | source += dex_cache->GetLocation()->ToModifiedUtf8(); |
| 3560 | } |
| 3561 | LOG(INFO) << "Loaded class " << descriptor << source; |
| 3562 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame^] | 3563 | { |
| 3564 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3565 | mirror::ClassLoader* const class_loader = klass->GetClassLoader(); |
| 3566 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader); |
| 3567 | mirror::Class* existing = class_table->Lookup(descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3568 | if (existing != nullptr) { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame^] | 3569 | return existing; |
| 3570 | } |
| 3571 | if (kIsDebugBuild && |
| 3572 | !klass->IsTemp() && |
| 3573 | class_loader == nullptr && |
| 3574 | dex_cache_boot_image_class_lookup_required_) { |
| 3575 | // Check a class loaded with the system class loader matches one in the image if the class |
| 3576 | // is in the image. |
| 3577 | existing = LookupClassFromBootImage(descriptor); |
| 3578 | if (existing != nullptr) { |
| 3579 | CHECK_EQ(klass, existing); |
| 3580 | } |
| 3581 | } |
| 3582 | VerifyObject(klass); |
| 3583 | class_table->InsertWithHash(klass, hash); |
| 3584 | if (class_loader != nullptr) { |
| 3585 | // This is necessary because we need to have the card dirtied for remembered sets. |
| 3586 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader); |
| 3587 | } |
| 3588 | if (log_new_class_table_roots_) { |
| 3589 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3590 | } |
| 3591 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame^] | 3592 | if (kIsDebugBuild) { |
| 3593 | // Test that copied methods correctly can find their holder. |
| 3594 | for (ArtMethod& method : klass->GetCopiedMethods(image_pointer_size_)) { |
| 3595 | CHECK_EQ(GetHoldingClassOfCopiedMethod(&method), klass); |
| 3596 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3597 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3598 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3599 | } |
| 3600 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 3601 | // TODO This should really be in mirror::Class. |
| 3602 | void ClassLinker::UpdateClassMethods(mirror::Class* klass, |
| 3603 | LengthPrefixedArray<ArtMethod>* new_methods) { |
| 3604 | klass->SetMethodsPtrUnchecked(new_methods, |
| 3605 | klass->NumDirectMethods(), |
| 3606 | klass->NumDeclaredVirtualMethods()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3607 | // Need to mark the card so that the remembered sets and mod union tables get updated. |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 3608 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3609 | } |
| 3610 | |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3611 | bool ClassLinker::RemoveClass(const char* descriptor, mirror::ClassLoader* class_loader) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3612 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 3613 | ClassTable* const class_table = ClassTableForClassLoader(class_loader); |
| 3614 | return class_table != nullptr && class_table->Remove(descriptor); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3615 | } |
| 3616 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3617 | mirror::Class* ClassLinker::LookupClass(Thread* self, |
| 3618 | const char* descriptor, |
| 3619 | size_t hash, |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3620 | mirror::ClassLoader* class_loader) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3621 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3622 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 3623 | ClassTable* const class_table = ClassTableForClassLoader(class_loader); |
| 3624 | if (class_table != nullptr) { |
| 3625 | mirror::Class* result = class_table->Lookup(descriptor, hash); |
| 3626 | if (result != nullptr) { |
| 3627 | return result; |
| 3628 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3629 | } |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 3630 | } |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 3631 | if (class_loader != nullptr || !dex_cache_boot_image_class_lookup_required_) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3632 | return nullptr; |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 3633 | } |
| 3634 | // Lookup failed but need to search dex_caches_. |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 3635 | mirror::Class* result = LookupClassFromBootImage(descriptor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 3636 | if (result != nullptr) { |
| 3637 | result = InsertClass(descriptor, result, hash); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3638 | } else { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 3639 | // Searching the image dex files/caches failed, we don't want to get into this situation |
| 3640 | // often as map searches are faster, so after kMaxFailedDexCacheLookups move all image |
| 3641 | // classes into the class table. |
| 3642 | constexpr uint32_t kMaxFailedDexCacheLookups = 1000; |
| 3643 | if (++failed_dex_cache_class_lookups_ > kMaxFailedDexCacheLookups) { |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 3644 | AddBootImageClassesToClassTable(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 3645 | } |
| 3646 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 3647 | return result; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3648 | } |
| 3649 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 3650 | static std::vector<mirror::ObjectArray<mirror::DexCache>*> GetImageDexCaches( |
| 3651 | std::vector<gc::space::ImageSpace*> image_spaces) SHARED_REQUIRES(Locks::mutator_lock_) { |
| 3652 | CHECK(!image_spaces.empty()); |
| 3653 | std::vector<mirror::ObjectArray<mirror::DexCache>*> dex_caches_vector; |
| 3654 | for (gc::space::ImageSpace* image_space : image_spaces) { |
| 3655 | mirror::Object* root = image_space->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
| 3656 | DCHECK(root != nullptr); |
| 3657 | dex_caches_vector.push_back(root->AsObjectArray<mirror::DexCache>()); |
| 3658 | } |
| 3659 | return dex_caches_vector; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3660 | } |
| 3661 | |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 3662 | void ClassLinker::AddBootImageClassesToClassTable() { |
| 3663 | if (dex_cache_boot_image_class_lookup_required_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 3664 | AddImageClassesToClassTable(Runtime::Current()->GetHeap()->GetBootImageSpaces(), |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 3665 | /*class_loader*/nullptr); |
| 3666 | dex_cache_boot_image_class_lookup_required_ = false; |
| 3667 | } |
| 3668 | } |
| 3669 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 3670 | void ClassLinker::AddImageClassesToClassTable(std::vector<gc::space::ImageSpace*> image_spaces, |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 3671 | mirror::ClassLoader* class_loader) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3672 | Thread* self = Thread::Current(); |
| 3673 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 3674 | ScopedAssertNoThreadSuspension ants(self, "Moving image classes to class table"); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 3675 | |
| 3676 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader); |
| 3677 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3678 | std::string temp; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 3679 | std::vector<mirror::ObjectArray<mirror::DexCache>*> dex_caches_vector = |
| 3680 | GetImageDexCaches(image_spaces); |
| 3681 | for (mirror::ObjectArray<mirror::DexCache>* dex_caches : dex_caches_vector) { |
| 3682 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 3683 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 3684 | GcRoot<mirror::Class>* types = dex_cache->GetResolvedTypes(); |
| 3685 | for (int32_t j = 0, num_types = dex_cache->NumResolvedTypes(); j < num_types; j++) { |
| 3686 | mirror::Class* klass = types[j].Read(); |
| 3687 | if (klass != nullptr) { |
| 3688 | DCHECK_EQ(klass->GetClassLoader(), class_loader); |
| 3689 | const char* descriptor = klass->GetDescriptor(&temp); |
| 3690 | size_t hash = ComputeModifiedUtf8Hash(descriptor); |
| 3691 | mirror::Class* existing = class_table->Lookup(descriptor, hash); |
| 3692 | if (existing != nullptr) { |
| 3693 | CHECK_EQ(existing, klass) << PrettyClassAndClassLoader(existing) << " != " |
| 3694 | << PrettyClassAndClassLoader(klass); |
| 3695 | } else { |
| 3696 | class_table->Insert(klass); |
| 3697 | if (log_new_class_table_roots_) { |
| 3698 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
| 3699 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3700 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3701 | } |
| 3702 | } |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 3703 | } |
| 3704 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3705 | } |
| 3706 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3707 | class MoveClassTableToPreZygoteVisitor : public ClassLoaderVisitor { |
| 3708 | public: |
| 3709 | explicit MoveClassTableToPreZygoteVisitor() {} |
| 3710 | |
| 3711 | void Visit(mirror::ClassLoader* class_loader) |
| 3712 | REQUIRES(Locks::classlinker_classes_lock_) |
| 3713 | SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE { |
| 3714 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3715 | if (class_table != nullptr) { |
| 3716 | class_table->FreezeSnapshot(); |
| 3717 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 3718 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3719 | }; |
| 3720 | |
| 3721 | void ClassLinker::MoveClassTableToPreZygote() { |
| 3722 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3723 | boot_class_table_.FreezeSnapshot(); |
| 3724 | MoveClassTableToPreZygoteVisitor visitor; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3725 | VisitClassLoaders(&visitor); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3726 | } |
| 3727 | |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 3728 | mirror::Class* ClassLinker::LookupClassFromBootImage(const char* descriptor) { |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 3729 | ScopedAssertNoThreadSuspension ants(Thread::Current(), "Image class lookup"); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 3730 | std::vector<mirror::ObjectArray<mirror::DexCache>*> dex_caches_vector = |
| 3731 | GetImageDexCaches(Runtime::Current()->GetHeap()->GetBootImageSpaces()); |
| 3732 | for (mirror::ObjectArray<mirror::DexCache>* dex_caches : dex_caches_vector) { |
| 3733 | for (int32_t i = 0; i < dex_caches->GetLength(); ++i) { |
| 3734 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 3735 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 3736 | // Try binary searching the type index by descriptor. |
| 3737 | const DexFile::TypeId* type_id = dex_file->FindTypeId(descriptor); |
| 3738 | if (type_id != nullptr) { |
| 3739 | uint16_t type_idx = dex_file->GetIndexForTypeId(*type_id); |
| 3740 | mirror::Class* klass = dex_cache->GetResolvedType(type_idx); |
| 3741 | if (klass != nullptr) { |
| 3742 | return klass; |
| 3743 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3744 | } |
| 3745 | } |
| 3746 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3747 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3748 | } |
| 3749 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3750 | // Look up classes by hash and descriptor and put all matching ones in the result array. |
| 3751 | class LookupClassesVisitor : public ClassLoaderVisitor { |
| 3752 | public: |
| 3753 | LookupClassesVisitor(const char* descriptor, size_t hash, std::vector<mirror::Class*>* result) |
| 3754 | : descriptor_(descriptor), |
| 3755 | hash_(hash), |
| 3756 | result_(result) {} |
| 3757 | |
| 3758 | void Visit(mirror::ClassLoader* class_loader) |
| 3759 | SHARED_REQUIRES(Locks::classlinker_classes_lock_, Locks::mutator_lock_) OVERRIDE { |
| 3760 | ClassTable* const class_table = class_loader->GetClassTable(); |
| 3761 | mirror::Class* klass = class_table->Lookup(descriptor_, hash_); |
| 3762 | if (klass != nullptr) { |
| 3763 | result_->push_back(klass); |
| 3764 | } |
| 3765 | } |
| 3766 | |
| 3767 | private: |
| 3768 | const char* const descriptor_; |
| 3769 | const size_t hash_; |
| 3770 | std::vector<mirror::Class*>* const result_; |
| 3771 | }; |
| 3772 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 3773 | void ClassLinker::LookupClasses(const char* descriptor, std::vector<mirror::Class*>& result) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3774 | result.clear(); |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 3775 | if (dex_cache_boot_image_class_lookup_required_) { |
| 3776 | AddBootImageClassesToClassTable(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3777 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3778 | Thread* const self = Thread::Current(); |
| 3779 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3780 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
| 3781 | mirror::Class* klass = boot_class_table_.Lookup(descriptor, hash); |
| 3782 | if (klass != nullptr) { |
| 3783 | result.push_back(klass); |
| 3784 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3785 | LookupClassesVisitor visitor(descriptor, hash, &result); |
| 3786 | VisitClassLoaders(&visitor); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 3787 | } |
| 3788 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3789 | bool ClassLinker::AttemptSupertypeVerification(Thread* self, |
| 3790 | Handle<mirror::Class> klass, |
| 3791 | Handle<mirror::Class> supertype) { |
| 3792 | DCHECK(self != nullptr); |
| 3793 | DCHECK(klass.Get() != nullptr); |
| 3794 | DCHECK(supertype.Get() != nullptr); |
| 3795 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3796 | if (!supertype->IsVerified() && !supertype->IsErroneous()) { |
| 3797 | VerifyClass(self, supertype); |
| 3798 | } |
| 3799 | if (supertype->IsCompileTimeVerified()) { |
| 3800 | // Either we are verified or we soft failed and need to retry at runtime. |
| 3801 | return true; |
| 3802 | } |
| 3803 | // If we got this far then we have a hard failure. |
| 3804 | std::string error_msg = |
| 3805 | StringPrintf("Rejecting class %s that attempts to sub-type erroneous class %s", |
| 3806 | PrettyDescriptor(klass.Get()).c_str(), |
| 3807 | PrettyDescriptor(supertype.Get()).c_str()); |
| 3808 | LOG(WARNING) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3809 | StackHandleScope<1> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3810 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
| 3811 | if (cause.Get() != nullptr) { |
| 3812 | // Set during VerifyClass call (if at all). |
| 3813 | self->ClearException(); |
| 3814 | } |
| 3815 | // Change into a verify error. |
| 3816 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
| 3817 | if (cause.Get() != nullptr) { |
| 3818 | self->GetException()->SetCause(cause.Get()); |
| 3819 | } |
| 3820 | ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex()); |
| 3821 | if (Runtime::Current()->IsAotCompiler()) { |
| 3822 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
| 3823 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3824 | // Need to grab the lock to change status. |
| 3825 | ObjectLock<mirror::Class> super_lock(self, klass); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3826 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
| 3827 | return false; |
| 3828 | } |
| 3829 | |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 3830 | void ClassLinker::VerifyClass(Thread* self, Handle<mirror::Class> klass, LogSeverity log_level) { |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3831 | { |
| 3832 | // TODO: assert that the monitor on the Class is held |
| 3833 | ObjectLock<mirror::Class> lock(self, klass); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 3834 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3835 | // Is somebody verifying this now? |
| 3836 | mirror::Class::Status old_status = klass->GetStatus(); |
| 3837 | while (old_status == mirror::Class::kStatusVerifying || |
| 3838 | old_status == mirror::Class::kStatusVerifyingAtRuntime) { |
| 3839 | lock.WaitIgnoringInterrupts(); |
Alex Light | 06039ba | 2016-04-04 13:23:14 -0700 | [diff] [blame] | 3840 | CHECK(klass->IsErroneous() || (klass->GetStatus() > old_status)) |
| 3841 | << "Class '" << PrettyClass(klass.Get()) << "' performed an illegal verification state " |
| 3842 | << "transition from " << old_status << " to " << klass->GetStatus(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3843 | old_status = klass->GetStatus(); |
| 3844 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3845 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3846 | // The class might already be erroneous, for example at compile time if we attempted to verify |
| 3847 | // this class as a parent to another. |
| 3848 | if (klass->IsErroneous()) { |
| 3849 | ThrowEarlierClassFailure(klass.Get()); |
| 3850 | return; |
| 3851 | } |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 3852 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3853 | // Don't attempt to re-verify if already sufficiently verified. |
| 3854 | if (klass->IsVerified()) { |
| 3855 | EnsureSkipAccessChecksMethods(klass); |
| 3856 | return; |
| 3857 | } |
| 3858 | if (klass->IsCompileTimeVerified() && Runtime::Current()->IsAotCompiler()) { |
| 3859 | return; |
| 3860 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3861 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3862 | if (klass->GetStatus() == mirror::Class::kStatusResolved) { |
| 3863 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerifying, self); |
| 3864 | } else { |
| 3865 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime) |
| 3866 | << PrettyClass(klass.Get()); |
| 3867 | CHECK(!Runtime::Current()->IsAotCompiler()); |
| 3868 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerifyingAtRuntime, self); |
| 3869 | } |
| 3870 | |
| 3871 | // Skip verification if disabled. |
| 3872 | if (!Runtime::Current()->IsVerificationEnabled()) { |
| 3873 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, self); |
| 3874 | EnsureSkipAccessChecksMethods(klass); |
| 3875 | return; |
| 3876 | } |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 3877 | } |
| 3878 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3879 | // Verify super class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3880 | StackHandleScope<2> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3881 | MutableHandle<mirror::Class> supertype(hs.NewHandle(klass->GetSuperClass())); |
| 3882 | // If we have a superclass and we get a hard verification failure we can return immediately. |
| 3883 | if (supertype.Get() != nullptr && !AttemptSupertypeVerification(self, klass, supertype)) { |
| 3884 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
| 3885 | return; |
| 3886 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3887 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3888 | // Verify all default super-interfaces. |
| 3889 | // |
| 3890 | // (1) Don't bother if the superclass has already had a soft verification failure. |
| 3891 | // |
| 3892 | // (2) Interfaces shouldn't bother to do this recursive verification because they cannot cause |
| 3893 | // recursive initialization by themselves. This is because when an interface is initialized |
| 3894 | // directly it must not initialize its superinterfaces. We are allowed to verify regardless |
| 3895 | // but choose not to for an optimization. If the interfaces is being verified due to a class |
| 3896 | // initialization (which would need all the default interfaces to be verified) the class code |
| 3897 | // will trigger the recursive verification anyway. |
| 3898 | if ((supertype.Get() == nullptr || supertype->IsVerified()) // See (1) |
| 3899 | && !klass->IsInterface()) { // See (2) |
| 3900 | int32_t iftable_count = klass->GetIfTableCount(); |
| 3901 | MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 3902 | // Loop through all interfaces this class has defined. It doesn't matter the order. |
| 3903 | for (int32_t i = 0; i < iftable_count; i++) { |
| 3904 | iface.Assign(klass->GetIfTable()->GetInterface(i)); |
| 3905 | DCHECK(iface.Get() != nullptr); |
| 3906 | // We only care if we have default interfaces and can skip if we are already verified... |
| 3907 | if (LIKELY(!iface->HasDefaultMethods() || iface->IsVerified())) { |
| 3908 | continue; |
| 3909 | } else if (UNLIKELY(!AttemptSupertypeVerification(self, klass, iface))) { |
| 3910 | // We had a hard failure while verifying this interface. Just return immediately. |
| 3911 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
| 3912 | return; |
| 3913 | } else if (UNLIKELY(!iface->IsVerified())) { |
| 3914 | // We softly failed to verify the iface. Stop checking and clean up. |
| 3915 | // Put the iface into the supertype handle so we know what caused us to fail. |
| 3916 | supertype.Assign(iface.Get()); |
| 3917 | break; |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3918 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3919 | } |
| 3920 | } |
| 3921 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3922 | // At this point if verification failed, then supertype is the "first" supertype that failed |
| 3923 | // verification (without a specific order). If verification succeeded, then supertype is either |
| 3924 | // null or the original superclass of klass and is verified. |
| 3925 | DCHECK(supertype.Get() == nullptr || |
| 3926 | supertype.Get() == klass->GetSuperClass() || |
| 3927 | !supertype->IsVerified()); |
| 3928 | |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 3929 | // 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] | 3930 | const DexFile& dex_file = *klass->GetDexCache()->GetDexFile(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3931 | mirror::Class::Status oat_file_class_status(mirror::Class::kStatusNotReady); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3932 | bool preverified = VerifyClassUsingOatFile(dex_file, klass.Get(), oat_file_class_status); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 3933 | // If the oat file says the class had an error, re-run the verifier. That way we will get a |
| 3934 | // precise error message. To ensure a rerun, test: |
| 3935 | // oat_file_class_status == mirror::Class::kStatusError => !preverified |
| 3936 | DCHECK(!(oat_file_class_status == mirror::Class::kStatusError) || !preverified); |
| 3937 | |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3938 | verifier::MethodVerifier::FailureKind verifier_failure = verifier::MethodVerifier::kNoFailure; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3939 | std::string error_msg; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 3940 | if (!preverified) { |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 3941 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3942 | verifier_failure = verifier::MethodVerifier::VerifyClass(self, |
| 3943 | klass.Get(), |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 3944 | runtime->GetCompilerCallbacks(), |
| 3945 | runtime->IsAotCompiler(), |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 3946 | log_level, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3947 | &error_msg); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 3948 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3949 | |
| 3950 | // Verification is done, grab the lock again. |
| 3951 | ObjectLock<mirror::Class> lock(self, klass); |
| 3952 | |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 3953 | if (preverified || verifier_failure != verifier::MethodVerifier::kHardFailure) { |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 3954 | if (!preverified && verifier_failure != verifier::MethodVerifier::kNoFailure) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3955 | VLOG(class_linker) << "Soft verification failure in class " << PrettyDescriptor(klass.Get()) |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 3956 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 3957 | << " because: " << error_msg; |
| 3958 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3959 | self->AssertNoPendingException(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3960 | // Make sure all classes referenced by catch blocks are resolved. |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 3961 | ResolveClassExceptionHandlerTypes(klass); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3962 | if (verifier_failure == verifier::MethodVerifier::kNoFailure) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3963 | // Even though there were no verifier failures we need to respect whether the super-class and |
| 3964 | // super-default-interfaces were verified or requiring runtime reverification. |
| 3965 | if (supertype.Get() == nullptr || supertype->IsVerified()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3966 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3967 | } else { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3968 | CHECK_EQ(supertype->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3969 | mirror::Class::SetStatus(klass, mirror::Class::kStatusRetryVerificationAtRuntime, self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3970 | // Pretend a soft failure occurred so that we don't consider the class verified below. |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 3971 | verifier_failure = verifier::MethodVerifier::kSoftFailure; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3972 | } |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3973 | } else { |
| 3974 | CHECK_EQ(verifier_failure, verifier::MethodVerifier::kSoftFailure); |
| 3975 | // Soft failures at compile time should be retried at runtime. Soft |
| 3976 | // failures at runtime will be handled by slow paths in the generated |
| 3977 | // code. Set status accordingly. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3978 | if (Runtime::Current()->IsAotCompiler()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3979 | mirror::Class::SetStatus(klass, mirror::Class::kStatusRetryVerificationAtRuntime, self); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3980 | } else { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3981 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, self); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 3982 | // As this is a fake verified status, make sure the methods are _not_ marked |
| 3983 | // kAccSkipAccessChecks later. |
| 3984 | klass->SetVerificationAttempted(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3985 | } |
| 3986 | } |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 3987 | } else { |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 3988 | VLOG(verifier) << "Verification failed on class " << PrettyDescriptor(klass.Get()) |
| 3989 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 3990 | << " because: " << error_msg; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3991 | self->AssertNoPendingException(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3992 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3993 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 3994 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3995 | if (preverified || verifier_failure == verifier::MethodVerifier::kNoFailure) { |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 3996 | // Class is verified so we don't need to do any access check on its methods. |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 3997 | // Let the interpreter know it by setting the kAccSkipAccessChecks flag onto each |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3998 | // method. |
| 3999 | // Note: we're going here during compilation and at runtime. When we set the |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4000 | // kAccSkipAccessChecks flag when compiling image classes, the flag is recorded |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 4001 | // in the image and is set when loading the image. |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4002 | |
| 4003 | if (UNLIKELY(Runtime::Current()->IsVerificationSoftFail())) { |
| 4004 | // Never skip access checks if the verification soft fail is forced. |
| 4005 | // Mark the class as having a verification attempt to avoid re-running the verifier. |
| 4006 | klass->SetVerificationAttempted(); |
| 4007 | } else { |
| 4008 | EnsureSkipAccessChecksMethods(klass); |
| 4009 | } |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4010 | } |
| 4011 | } |
| 4012 | |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4013 | void ClassLinker::EnsureSkipAccessChecksMethods(Handle<mirror::Class> klass) { |
| 4014 | if (!klass->WasVerificationAttempted()) { |
| 4015 | klass->SetSkipAccessChecksFlagOnAllMethods(image_pointer_size_); |
| 4016 | klass->SetVerificationAttempted(); |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 4017 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 4018 | } |
| 4019 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4020 | bool ClassLinker::VerifyClassUsingOatFile(const DexFile& dex_file, |
| 4021 | mirror::Class* klass, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4022 | mirror::Class::Status& oat_file_class_status) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4023 | // If we're compiling, we can only verify the class using the oat file if |
| 4024 | // we are not compiling the image or if the class we're verifying is not part of |
| 4025 | // the app. In other words, we will only check for preverification of bootclasspath |
| 4026 | // classes. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4027 | if (Runtime::Current()->IsAotCompiler()) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4028 | // Are we compiling the bootclasspath? |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 4029 | if (Runtime::Current()->GetCompilerCallbacks()->IsBootImage()) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4030 | return false; |
| 4031 | } |
| 4032 | // We are compiling an app (not the image). |
| 4033 | |
| 4034 | // Is this an app class? (I.e. not a bootclasspath class) |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4035 | if (klass->GetClassLoader() != nullptr) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4036 | return false; |
| 4037 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4038 | } |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4039 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 4040 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4041 | // In case we run without an image there won't be a backing oat file. |
| 4042 | if (oat_dex_file == nullptr) { |
Anwar Ghuloum | ad256bb | 2013-07-18 14:58:55 -0700 | [diff] [blame] | 4043 | return false; |
| 4044 | } |
| 4045 | |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 4046 | // We may be running with a preopted oat file but without image. In this case, |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4047 | // we don't skip verification of skip_access_checks classes to ensure we initialize |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 4048 | // dex caches with all types resolved during verification. |
| 4049 | // We need to trust image classes, as these might be coming out of a pre-opted, quickened boot |
| 4050 | // image (that we just failed loading), and the verifier can't be run on quickened opcodes when |
| 4051 | // the runtime isn't started. On the other hand, app classes can be re-verified even if they are |
| 4052 | // already pre-opted, as then the runtime is started. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4053 | if (!Runtime::Current()->IsAotCompiler() && |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 4054 | !Runtime::Current()->GetHeap()->HasBootImageSpace() && |
Andreas Gampe | 76bd880 | 2014-12-10 16:43:58 -0800 | [diff] [blame] | 4055 | klass->GetClassLoader() != nullptr) { |
| 4056 | return false; |
| 4057 | } |
| 4058 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4059 | uint16_t class_def_index = klass->GetDexClassDefIndex(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 4060 | oat_file_class_status = oat_dex_file->GetOatClass(class_def_index).GetStatus(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4061 | if (oat_file_class_status == mirror::Class::kStatusVerified || |
| 4062 | oat_file_class_status == mirror::Class::kStatusInitialized) { |
Mathieu Chartier | a807780 | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4063 | return true; |
| 4064 | } |
| 4065 | // If we only verified a subset of the classes at compile time, we can end up with classes that |
| 4066 | // were resolved by the verifier. |
| 4067 | if (oat_file_class_status == mirror::Class::kStatusResolved) { |
| 4068 | return false; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4069 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4070 | if (oat_file_class_status == mirror::Class::kStatusRetryVerificationAtRuntime) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 4071 | // Compile time verification failed with a soft error. Compile time verification can fail |
| 4072 | // because we have incomplete type information. Consider the following: |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 4073 | // class ... { |
| 4074 | // Foo x; |
| 4075 | // .... () { |
| 4076 | // if (...) { |
| 4077 | // v1 gets assigned a type of resolved class Foo |
| 4078 | // } else { |
| 4079 | // v1 gets assigned a type of unresolved class Bar |
| 4080 | // } |
| 4081 | // iput x = v1 |
| 4082 | // } } |
| 4083 | // when we merge v1 following the if-the-else it results in Conflict |
| 4084 | // (see verifier::RegType::Merge) as we can't know the type of Bar and we could possibly be |
| 4085 | // allowing an unsafe assignment to the field x in the iput (javac may have compiled this as |
| 4086 | // it knew Bar was a sub-class of Foo, but for us this may have been moved into a separate apk |
| 4087 | // at compile time). |
| 4088 | return false; |
| 4089 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4090 | if (oat_file_class_status == mirror::Class::kStatusError) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 4091 | // Compile time verification failed with a hard error. This is caused by invalid instructions |
| 4092 | // in the class. These errors are unrecoverable. |
| 4093 | return false; |
| 4094 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4095 | if (oat_file_class_status == mirror::Class::kStatusNotReady) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 4096 | // Status is uninitialized if we couldn't determine the status at compile time, for example, |
| 4097 | // not loading the class. |
| 4098 | // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy |
| 4099 | // isn't a problem and this case shouldn't occur |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4100 | return false; |
| 4101 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4102 | std::string temp; |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 4103 | LOG(FATAL) << "Unexpected class status: " << oat_file_class_status |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 4104 | << " " << dex_file.GetLocation() << " " << PrettyClass(klass) << " " |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4105 | << klass->GetDescriptor(&temp); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4106 | UNREACHABLE(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4107 | } |
| 4108 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4109 | void ClassLinker::ResolveClassExceptionHandlerTypes(Handle<mirror::Class> klass) { |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 4110 | for (ArtMethod& method : klass->GetMethods(image_pointer_size_)) { |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4111 | ResolveMethodExceptionHandlerTypes(&method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4112 | } |
| 4113 | } |
| 4114 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4115 | void ClassLinker::ResolveMethodExceptionHandlerTypes(ArtMethod* method) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4116 | // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod. |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4117 | const DexFile::CodeItem* code_item = |
| 4118 | method->GetDexFile()->GetCodeItem(method->GetCodeItemOffset()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4119 | if (code_item == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4120 | return; // native or abstract method |
| 4121 | } |
| 4122 | if (code_item->tries_size_ == 0) { |
| 4123 | return; // nothing to process |
| 4124 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 4125 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item, 0); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4126 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4127 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 4128 | CatchHandlerIterator iterator(handlers_ptr); |
| 4129 | for (; iterator.HasNext(); iterator.Next()) { |
| 4130 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 4131 | // unresolved exception types will be ignored by exception delivery |
| 4132 | if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) { |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 4133 | mirror::Class* exception_type = ResolveType(iterator.GetHandlerTypeIndex(), method); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4134 | if (exception_type == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4135 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4136 | Thread::Current()->ClearException(); |
| 4137 | } |
| 4138 | } |
| 4139 | } |
| 4140 | handlers_ptr = iterator.EndDataPointer(); |
| 4141 | } |
| 4142 | } |
| 4143 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4144 | mirror::Class* ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, |
| 4145 | jstring name, |
| 4146 | jobjectArray interfaces, |
| 4147 | jobject loader, |
| 4148 | jobjectArray methods, |
| 4149 | jobjectArray throws) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4150 | Thread* self = soa.Self(); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4151 | StackHandleScope<10> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4152 | MutableHandle<mirror::Class> klass(hs.NewHandle( |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4153 | AllocClass(self, GetClassRoot(kJavaLangClass), sizeof(mirror::Class)))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4154 | if (klass.Get() == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4155 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4156 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4157 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4158 | DCHECK(klass->GetClass() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4159 | klass->SetObjectSize(sizeof(mirror::Proxy)); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4160 | // Set the class access flags incl. VerificationAttempted, so we do not try to set the flag on |
| 4161 | // the methods. |
| 4162 | klass->SetAccessFlags(kAccClassIsProxy | kAccPublic | kAccFinal | kAccVerificationAttempted); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4163 | klass->SetClassLoader(soa.Decode<mirror::ClassLoader*>(loader)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4164 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4165 | klass->SetName(soa.Decode<mirror::String*>(name)); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4166 | klass->SetDexCache(GetClassRoot(kJavaLangReflectProxy)->GetDexCache()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4167 | mirror::Class::SetStatus(klass, mirror::Class::kStatusIdx, self); |
Nicolas Geoffray | a7a4759 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 4168 | std::string descriptor(GetDescriptorForProxy(klass.Get())); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 4169 | const size_t hash = ComputeModifiedUtf8Hash(descriptor.c_str()); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4170 | |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 4171 | // Needs to be before we insert the class so that the allocator field is set. |
| 4172 | LinearAlloc* const allocator = GetOrCreateAllocatorForClassLoader(klass->GetClassLoader()); |
| 4173 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4174 | // Insert the class before loading the fields as the field roots |
| 4175 | // (ArtField::declaring_class_) are only visited from the class |
| 4176 | // table. There can't be any suspend points between inserting the |
| 4177 | // class and setting the field arrays below. |
| 4178 | mirror::Class* existing = InsertClass(descriptor.c_str(), klass.Get(), hash); |
| 4179 | CHECK(existing == nullptr); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4180 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4181 | // Instance fields are inherited, but we add a couple of static fields... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4182 | const size_t num_fields = 2; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 4183 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, allocator, num_fields); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4184 | klass->SetSFieldsPtr(sfields); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4185 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4186 | // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by |
| 4187 | // our proxy, so Class.getInterfaces doesn't return the flattened set. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4188 | ArtField& interfaces_sfield = sfields->At(0); |
| 4189 | interfaces_sfield.SetDexFieldIndex(0); |
| 4190 | interfaces_sfield.SetDeclaringClass(klass.Get()); |
| 4191 | interfaces_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4192 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4193 | // 2. Create a static field 'throws' that holds exceptions thrown by our methods. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4194 | ArtField& throws_sfield = sfields->At(1); |
| 4195 | throws_sfield.SetDexFieldIndex(1); |
| 4196 | throws_sfield.SetDeclaringClass(klass.Get()); |
| 4197 | throws_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4198 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4199 | // Proxies have 1 direct method, the constructor |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4200 | const size_t num_direct_methods = 1; |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4201 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4202 | // They have as many virtual methods as the array |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 4203 | auto h_methods = hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Method>*>(methods)); |
| 4204 | DCHECK_EQ(h_methods->GetClass(), mirror::Method::ArrayClass()) |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4205 | << PrettyClass(h_methods->GetClass()); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 4206 | const size_t num_virtual_methods = h_methods->GetLength(); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4207 | |
| 4208 | // Create the methods array. |
| 4209 | LengthPrefixedArray<ArtMethod>* proxy_class_methods = AllocArtMethodArray( |
| 4210 | self, allocator, num_direct_methods + num_virtual_methods); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4211 | // Currently AllocArtMethodArray cannot return null, but the OOM logic is left there in case we |
| 4212 | // want to throw OOM in the future. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4213 | if (UNLIKELY(proxy_class_methods == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4214 | self->AssertPendingOOMException(); |
| 4215 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4216 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4217 | klass->SetMethodsPtr(proxy_class_methods, num_direct_methods, num_virtual_methods); |
| 4218 | |
| 4219 | // Create the single direct method. |
| 4220 | CreateProxyConstructor(klass, klass->GetDirectMethodUnchecked(0, image_pointer_size_)); |
| 4221 | |
| 4222 | // Create virtual method using specified prototypes. |
| 4223 | // TODO These should really use the iterators. |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4224 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4225 | auto* virtual_method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
| 4226 | auto* prototype = h_methods->Get(i)->GetArtMethod(); |
| 4227 | CreateProxyMethod(klass, prototype, virtual_method); |
| 4228 | DCHECK(virtual_method->GetDeclaringClass() != nullptr); |
| 4229 | DCHECK(prototype->GetDeclaringClass() != nullptr); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4230 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4231 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4232 | // The super class is java.lang.reflect.Proxy |
| 4233 | klass->SetSuperClass(GetClassRoot(kJavaLangReflectProxy)); |
| 4234 | // Now effectively in the loaded state. |
| 4235 | mirror::Class::SetStatus(klass, mirror::Class::kStatusLoaded, self); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4236 | self->AssertNoPendingException(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4237 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4238 | MutableHandle<mirror::Class> new_class = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 4239 | { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4240 | // Must hold lock on object when resolved. |
| 4241 | ObjectLock<mirror::Class> resolution_lock(self, klass); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4242 | // Link the fields and virtual methods, creating vtable and iftables. |
| 4243 | // The new class will replace the old one in the class table. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4244 | Handle<mirror::ObjectArray<mirror::Class>> h_interfaces( |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4245 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces))); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4246 | if (!LinkClass(self, descriptor.c_str(), klass, h_interfaces, &new_class)) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4247 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4248 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4249 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4250 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4251 | CHECK(klass->IsRetired()); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4252 | CHECK_NE(klass.Get(), new_class.Get()); |
| 4253 | klass.Assign(new_class.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4254 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4255 | CHECK_EQ(interfaces_sfield.GetDeclaringClass(), klass.Get()); |
| 4256 | interfaces_sfield.SetObject<false>(klass.Get(), |
| 4257 | soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces)); |
| 4258 | CHECK_EQ(throws_sfield.GetDeclaringClass(), klass.Get()); |
| 4259 | throws_sfield.SetObject<false>( |
| 4260 | klass.Get(), soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class> >*>(throws)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4261 | |
| 4262 | { |
| 4263 | // Lock on klass is released. Lock new class object. |
| 4264 | ObjectLock<mirror::Class> initialization_lock(self, klass); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4265 | mirror::Class::SetStatus(klass, mirror::Class::kStatusInitialized, self); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 4266 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4267 | |
| 4268 | // sanity checks |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 4269 | if (kIsDebugBuild) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4270 | CHECK(klass->GetIFieldsPtr() == nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4271 | CheckProxyConstructor(klass->GetDirectMethod(0, image_pointer_size_)); |
| 4272 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4273 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4274 | auto* virtual_method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
| 4275 | auto* prototype = h_methods->Get(i++)->GetArtMethod(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4276 | CheckProxyMethod(virtual_method, prototype); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4277 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4278 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4279 | StackHandleScope<1> hs2(self); |
| 4280 | Handle<mirror::String> decoded_name = hs2.NewHandle(soa.Decode<mirror::String*>(name)); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4281 | std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4282 | decoded_name->ToModifiedUtf8().c_str())); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4283 | CHECK_EQ(PrettyField(klass->GetStaticField(0)), interfaces_field_name); |
| 4284 | |
| 4285 | std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4286 | decoded_name->ToModifiedUtf8().c_str())); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4287 | CHECK_EQ(PrettyField(klass->GetStaticField(1)), throws_field_name); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4288 | |
Nicolas Geoffray | a7a4759 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 4289 | CHECK_EQ(klass.Get()->GetInterfaces(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4290 | soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces)); |
Nicolas Geoffray | a7a4759 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 4291 | CHECK_EQ(klass.Get()->GetThrows(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4292 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>*>(throws)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4293 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4294 | return klass.Get(); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4295 | } |
| 4296 | |
Nicolas Geoffray | a7a4759 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 4297 | std::string ClassLinker::GetDescriptorForProxy(mirror::Class* proxy_class) { |
| 4298 | DCHECK(proxy_class->IsProxyClass()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4299 | mirror::String* name = proxy_class->GetName(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4300 | DCHECK(name != nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4301 | return DotToDescriptor(name->ToModifiedUtf8().c_str()); |
| 4302 | } |
| 4303 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4304 | void ClassLinker::CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out) { |
| 4305 | // Create constructor for Proxy that must initialize the method. |
Igor Murashkin | e3d4ff5 | 2016-02-02 10:46:42 -0800 | [diff] [blame] | 4306 | CHECK_EQ(GetClassRoot(kJavaLangReflectProxy)->NumDirectMethods(), 18u); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4307 | ArtMethod* proxy_constructor = GetClassRoot(kJavaLangReflectProxy)->GetDirectMethodUnchecked( |
| 4308 | 2, image_pointer_size_); |
Igor Murashkin | e3d4ff5 | 2016-02-02 10:46:42 -0800 | [diff] [blame] | 4309 | DCHECK_EQ(std::string(proxy_constructor->GetName()), "<init>"); |
Sebastien Hertz | ae94e35 | 2014-08-27 15:32:56 +0200 | [diff] [blame] | 4310 | // Ensure constructor is in dex cache so that we can use the dex cache to look up the overridden |
| 4311 | // constructor method. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4312 | GetClassRoot(kJavaLangReflectProxy)->GetDexCache()->SetResolvedMethod( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4313 | proxy_constructor->GetDexMethodIndex(), proxy_constructor, image_pointer_size_); |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 4314 | // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its |
| 4315 | // code_ too) |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4316 | DCHECK(out != nullptr); |
| 4317 | out->CopyFrom(proxy_constructor, image_pointer_size_); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4318 | // Make this constructor public and fix the class to be our Proxy version |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4319 | out->SetAccessFlags((out->GetAccessFlags() & ~kAccProtected) | kAccPublic); |
| 4320 | out->SetDeclaringClass(klass.Get()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4321 | } |
| 4322 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4323 | void ClassLinker::CheckProxyConstructor(ArtMethod* constructor) const { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4324 | CHECK(constructor->IsConstructor()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4325 | auto* np = constructor->GetInterfaceMethodIfProxy(image_pointer_size_); |
| 4326 | CHECK_STREQ(np->GetName(), "<init>"); |
| 4327 | CHECK_STREQ(np->GetSignature().ToString().c_str(), "(Ljava/lang/reflect/InvocationHandler;)V"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4328 | DCHECK(constructor->IsPublic()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4329 | } |
| 4330 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4331 | void ClassLinker::CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4332 | ArtMethod* out) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4333 | // Ensure prototype is in dex cache so that we can use the dex cache to look up the overridden |
| 4334 | // prototype method |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 4335 | auto* dex_cache = prototype->GetDeclaringClass()->GetDexCache(); |
| 4336 | // Avoid dirtying the dex cache unless we need to. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4337 | if (dex_cache->GetResolvedMethod(prototype->GetDexMethodIndex(), image_pointer_size_) != |
| 4338 | prototype) { |
| 4339 | dex_cache->SetResolvedMethod( |
| 4340 | prototype->GetDexMethodIndex(), prototype, image_pointer_size_); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 4341 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4342 | // 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] | 4343 | // as necessary |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4344 | DCHECK(out != nullptr); |
| 4345 | out->CopyFrom(prototype, image_pointer_size_); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4346 | |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 4347 | // Set class to be the concrete proxy class. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4348 | out->SetDeclaringClass(klass.Get()); |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 4349 | // Clear the abstract, default and conflict flags to ensure that defaults aren't picked in |
| 4350 | // preference to the invocation handler. |
| 4351 | const uint32_t kRemoveFlags = kAccAbstract | kAccDefault | kAccDefaultConflict; |
| 4352 | // Make the method final. |
| 4353 | const uint32_t kAddFlags = kAccFinal; |
| 4354 | out->SetAccessFlags((out->GetAccessFlags() & ~kRemoveFlags) | kAddFlags); |
| 4355 | |
| 4356 | // Clear the dex_code_item_offset_. It needs to be 0 since proxy methods have no CodeItems but the |
| 4357 | // method they copy might (if it's a default method). |
| 4358 | out->SetCodeItemOffset(0); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4359 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4360 | // At runtime the method looks like a reference and argument saving method, clone the code |
| 4361 | // related parameters from this method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4362 | out->SetEntryPointFromQuickCompiledCode(GetQuickProxyInvokeHandler()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4363 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4364 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4365 | void ClassLinker::CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4366 | // Basic sanity |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4367 | CHECK(!prototype->IsFinal()); |
| 4368 | CHECK(method->IsFinal()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 4369 | CHECK(method->IsInvokable()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 4370 | |
| 4371 | // The proxy method doesn't have its own dex cache or dex file and so it steals those of its |
| 4372 | // interface prototype. The exception to this are Constructors and the Class of the Proxy itself. |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 4373 | CHECK(prototype->HasSameDexCacheResolvedMethods(method, image_pointer_size_)); |
| 4374 | CHECK(prototype->HasSameDexCacheResolvedTypes(method, image_pointer_size_)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4375 | auto* np = method->GetInterfaceMethodIfProxy(image_pointer_size_); |
| 4376 | CHECK_EQ(prototype->GetDeclaringClass()->GetDexCache(), np->GetDexCache()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 4377 | CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex()); |
| 4378 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4379 | CHECK_STREQ(np->GetName(), prototype->GetName()); |
| 4380 | CHECK_STREQ(np->GetShorty(), prototype->GetShorty()); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4381 | // More complex sanity - via dex cache |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 4382 | CHECK_EQ(np->GetReturnType(true /* resolve */, image_pointer_size_), |
| 4383 | prototype->GetReturnType(true /* resolve */, image_pointer_size_)); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4384 | } |
| 4385 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4386 | bool ClassLinker::CanWeInitializeClass(mirror::Class* klass, bool can_init_statics, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4387 | bool can_init_parents) { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 4388 | if (can_init_statics && can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4389 | return true; |
| 4390 | } |
| 4391 | if (!can_init_statics) { |
| 4392 | // Check if there's a class initializer. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4393 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4394 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4395 | return false; |
| 4396 | } |
| 4397 | // Check if there are encoded static values needing initialization. |
| 4398 | if (klass->NumStaticFields() != 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4399 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4400 | DCHECK(dex_class_def != nullptr); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4401 | if (dex_class_def->static_values_off_ != 0) { |
| 4402 | return false; |
| 4403 | } |
| 4404 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4405 | // If we are a class we need to initialize all interfaces with default methods when we are |
| 4406 | // initialized. Check all of them. |
| 4407 | if (!klass->IsInterface()) { |
| 4408 | size_t num_interfaces = klass->GetIfTableCount(); |
| 4409 | for (size_t i = 0; i < num_interfaces; i++) { |
| 4410 | mirror::Class* iface = klass->GetIfTable()->GetInterface(i); |
| 4411 | if (iface->HasDefaultMethods() && |
| 4412 | !CanWeInitializeClass(iface, can_init_statics, can_init_parents)) { |
| 4413 | return false; |
| 4414 | } |
| 4415 | } |
| 4416 | } |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4417 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4418 | if (klass->IsInterface() || !klass->HasSuperClass()) { |
| 4419 | return true; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4420 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4421 | mirror::Class* super_class = klass->GetSuperClass(); |
| 4422 | if (!can_init_parents && !super_class->IsInitialized()) { |
| 4423 | return false; |
| 4424 | } |
| 4425 | return CanWeInitializeClass(super_class, can_init_statics, can_init_parents); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4426 | } |
| 4427 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4428 | bool ClassLinker::InitializeClass(Thread* self, Handle<mirror::Class> klass, |
| 4429 | bool can_init_statics, bool can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4430 | // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol |
| 4431 | |
| 4432 | // Are we already initialized and therefore done? |
| 4433 | // Note: we differ from the JLS here as we don't do this under the lock, this is benign as |
| 4434 | // an initialized class will never change its state. |
| 4435 | if (klass->IsInitialized()) { |
| 4436 | return true; |
| 4437 | } |
| 4438 | |
| 4439 | // 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] | 4440 | if (!CanWeInitializeClass(klass.Get(), can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4441 | return false; |
| 4442 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4443 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4444 | self->AllowThreadSuspension(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4445 | uint64_t t0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4446 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4447 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4448 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4449 | // Re-check under the lock in case another thread initialized ahead of us. |
| 4450 | if (klass->IsInitialized()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4451 | return true; |
| 4452 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4453 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4454 | // 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] | 4455 | if (klass->IsErroneous()) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 4456 | ThrowEarlierClassFailure(klass.Get(), true); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4457 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4458 | return false; |
| 4459 | } |
| 4460 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4461 | CHECK(klass->IsResolved()) << PrettyClass(klass.Get()) << ": state=" << klass->GetStatus(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4462 | |
| 4463 | if (!klass->IsVerified()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4464 | VerifyClass(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4465 | if (!klass->IsVerified()) { |
| 4466 | // We failed to verify, expect either the klass to be erroneous or verification failed at |
| 4467 | // compile time. |
| 4468 | if (klass->IsErroneous()) { |
Andreas Gampe | 53954b9 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 4469 | // The class is erroneous. This may be a verifier error, or another thread attempted |
| 4470 | // verification and/or initialization and failed. We can distinguish those cases by |
| 4471 | // whether an exception is already pending. |
| 4472 | if (self->IsExceptionPending()) { |
| 4473 | // Check that it's a VerifyError. |
| 4474 | DCHECK_EQ("java.lang.Class<java.lang.VerifyError>", |
| 4475 | PrettyClass(self->GetException()->GetClass())); |
| 4476 | } else { |
| 4477 | // Check that another thread attempted initialization. |
| 4478 | DCHECK_NE(0, klass->GetClinitThreadId()); |
| 4479 | DCHECK_NE(self->GetTid(), klass->GetClinitThreadId()); |
| 4480 | // Need to rethrow the previous failure now. |
| 4481 | ThrowEarlierClassFailure(klass.Get(), true); |
| 4482 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4483 | VlogClassInitializationFailure(klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4484 | } else { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4485 | CHECK(Runtime::Current()->IsAotCompiler()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4486 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 4487 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4488 | return false; |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4489 | } else { |
| 4490 | self->AssertNoPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4491 | } |
Andreas Gampe | 53954b9 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 4492 | |
| 4493 | // A separate thread could have moved us all the way to initialized. A "simple" example |
| 4494 | // involves a subclass of the current class being initialized at the same time (which |
| 4495 | // will implicitly initialize the superclass, if scheduled that way). b/28254258 |
| 4496 | DCHECK_NE(mirror::Class::kStatusError, klass->GetStatus()); |
| 4497 | if (klass->IsInitialized()) { |
| 4498 | return true; |
| 4499 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4500 | } |
| 4501 | |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4502 | // If the class is kStatusInitializing, either this thread is |
| 4503 | // initializing higher up the stack or another thread has beat us |
| 4504 | // to initializing and we need to wait. Either way, this |
| 4505 | // invocation of InitializeClass will not be responsible for |
| 4506 | // running <clinit> and will return. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4507 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4508 | // Could have got an exception during verification. |
| 4509 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4510 | VlogClassInitializationFailure(klass); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4511 | return false; |
| 4512 | } |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 4513 | // We caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 4514 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4515 | // Yes. That's fine. Return so we can continue initializing. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4516 | return true; |
| 4517 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4518 | // No. That's fine. Wait for another thread to finish initializing. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4519 | return WaitForInitializeClass(klass, self, lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4520 | } |
| 4521 | |
| 4522 | if (!ValidateSuperClassDescriptors(klass)) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4523 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4524 | return false; |
| 4525 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4526 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4527 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4528 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusVerified) << PrettyClass(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4529 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4530 | // From here out other threads may observe that we're initializing and so changes of state |
| 4531 | // require the a notification. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 4532 | klass->SetClinitThreadId(self->GetTid()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4533 | mirror::Class::SetStatus(klass, mirror::Class::kStatusInitializing, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4534 | |
| 4535 | t0 = NanoTime(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4536 | } |
| 4537 | |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 4538 | // Initialize super classes, must be done while initializing for the JLS. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4539 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
| 4540 | mirror::Class* super_class = klass->GetSuperClass(); |
| 4541 | if (!super_class->IsInitialized()) { |
| 4542 | CHECK(!super_class->IsInterface()); |
| 4543 | CHECK(can_init_parents); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4544 | StackHandleScope<1> hs(self); |
| 4545 | Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4546 | bool super_initialized = InitializeClass(self, handle_scope_super, can_init_statics, true); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4547 | if (!super_initialized) { |
| 4548 | // The super class was verified ahead of entering initializing, we should only be here if |
| 4549 | // the super class became erroneous due to initialization. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4550 | CHECK(handle_scope_super->IsErroneous() && self->IsExceptionPending()) |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4551 | << "Super class initialization failed for " |
| 4552 | << PrettyDescriptor(handle_scope_super.Get()) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4553 | << " that has unexpected status " << handle_scope_super->GetStatus() |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4554 | << "\nPending exception:\n" |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 4555 | << (self->GetException() != nullptr ? self->GetException()->Dump() : ""); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4556 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4557 | // Initialization failed because the super-class is erroneous. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4558 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4559 | return false; |
| 4560 | } |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 4561 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4562 | } |
| 4563 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4564 | if (!klass->IsInterface()) { |
| 4565 | // Initialize interfaces with default methods for the JLS. |
| 4566 | size_t num_direct_interfaces = klass->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4567 | // Only setup the (expensive) handle scope if we actually need to. |
| 4568 | if (UNLIKELY(num_direct_interfaces > 0)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4569 | StackHandleScope<1> hs_iface(self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4570 | MutableHandle<mirror::Class> handle_scope_iface(hs_iface.NewHandle<mirror::Class>(nullptr)); |
| 4571 | for (size_t i = 0; i < num_direct_interfaces; i++) { |
| 4572 | handle_scope_iface.Assign(mirror::Class::GetDirectInterface(self, klass, i)); |
| 4573 | CHECK(handle_scope_iface.Get() != nullptr); |
| 4574 | CHECK(handle_scope_iface->IsInterface()); |
| 4575 | if (handle_scope_iface->HasBeenRecursivelyInitialized()) { |
| 4576 | // We have already done this for this interface. Skip it. |
| 4577 | continue; |
| 4578 | } |
| 4579 | // We cannot just call initialize class directly because we need to ensure that ALL |
| 4580 | // interfaces with default methods are initialized. Non-default interface initialization |
| 4581 | // will not affect other non-default super-interfaces. |
| 4582 | bool iface_initialized = InitializeDefaultInterfaceRecursive(self, |
| 4583 | handle_scope_iface, |
| 4584 | can_init_statics, |
| 4585 | can_init_parents); |
| 4586 | if (!iface_initialized) { |
| 4587 | ObjectLock<mirror::Class> lock(self, klass); |
| 4588 | // Initialization failed because one of our interfaces with default methods is erroneous. |
| 4589 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
| 4590 | return false; |
| 4591 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4592 | } |
| 4593 | } |
| 4594 | } |
| 4595 | |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4596 | const size_t num_static_fields = klass->NumStaticFields(); |
| 4597 | if (num_static_fields > 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4598 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4599 | CHECK(dex_class_def != nullptr); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4600 | const DexFile& dex_file = klass->GetDexFile(); |
Hiroshi Yamauchi | 67ef46a | 2014-08-21 15:59:43 -0700 | [diff] [blame] | 4601 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4602 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4603 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4604 | |
| 4605 | // Eagerly fill in static fields so that the we don't have to do as many expensive |
| 4606 | // Class::FindStaticField in ResolveField. |
| 4607 | for (size_t i = 0; i < num_static_fields; ++i) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4608 | ArtField* field = klass->GetStaticField(i); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4609 | const uint32_t field_idx = field->GetDexFieldIndex(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4610 | ArtField* resolved_field = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4611 | if (resolved_field == nullptr) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4612 | dex_cache->SetResolvedField(field_idx, field, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4613 | } else { |
| 4614 | DCHECK_EQ(field, resolved_field); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4615 | } |
| 4616 | } |
| 4617 | |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4618 | EncodedStaticFieldValueIterator value_it(dex_file, &dex_cache, &class_loader, |
| 4619 | this, *dex_class_def); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 4620 | const uint8_t* class_data = dex_file.GetClassData(*dex_class_def); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4621 | ClassDataItemIterator field_it(dex_file, class_data); |
| 4622 | if (value_it.HasNext()) { |
| 4623 | DCHECK(field_it.HasNextStaticField()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4624 | CHECK(can_init_statics); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4625 | for ( ; value_it.HasNext(); value_it.Next(), field_it.Next()) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4626 | ArtField* field = ResolveField( |
| 4627 | dex_file, field_it.GetMemberIndex(), dex_cache, class_loader, true); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4628 | if (Runtime::Current()->IsActiveTransaction()) { |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4629 | value_it.ReadValueToField<true>(field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4630 | } else { |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4631 | value_it.ReadValueToField<false>(field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4632 | } |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4633 | DCHECK(!value_it.HasNext() || field_it.HasNextStaticField()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4634 | } |
| 4635 | } |
| 4636 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4637 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4638 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4639 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4640 | CHECK(can_init_statics); |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 4641 | JValue result; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4642 | clinit->Invoke(self, nullptr, 0, &result, "V"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4643 | } |
| 4644 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4645 | self->AllowThreadSuspension(); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 4646 | uint64_t t1 = NanoTime(); |
| 4647 | |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4648 | bool success = true; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4649 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4650 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4651 | |
| 4652 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4653 | WrapExceptionInInitializer(klass); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4654 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4655 | success = false; |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 4656 | } else if (Runtime::Current()->IsTransactionAborted()) { |
| 4657 | // The exception thrown when the transaction aborted has been caught and cleared |
| 4658 | // so we need to throw it again now. |
Sebastien Hertz | bd9cf9f | 2015-03-03 12:16:13 +0100 | [diff] [blame] | 4659 | VLOG(compiler) << "Return from class initializer of " << PrettyDescriptor(klass.Get()) |
| 4660 | << " without exception while transaction was aborted: re-throw it now."; |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 4661 | Runtime::Current()->ThrowTransactionAbortError(self); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4662 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 4663 | success = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4664 | } else { |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 4665 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 4666 | RuntimeStats* thread_stats = self->GetStats(); |
| 4667 | ++global_stats->class_init_count; |
| 4668 | ++thread_stats->class_init_count; |
| 4669 | global_stats->class_init_time_ns += (t1 - t0); |
| 4670 | thread_stats->class_init_time_ns += (t1 - t0); |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 4671 | // Set the class as initialized except if failed to initialize static fields. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4672 | mirror::Class::SetStatus(klass, mirror::Class::kStatusInitialized, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4673 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4674 | std::string temp; |
| 4675 | LOG(INFO) << "Initialized class " << klass->GetDescriptor(&temp) << " from " << |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4676 | klass->GetLocation(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 4677 | } |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 4678 | // Opportunistically set static method trampolines to their destination. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4679 | FixupStaticTrampolines(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4680 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4681 | } |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4682 | return success; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4683 | } |
| 4684 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4685 | // We recursively run down the tree of interfaces. We need to do this in the order they are declared |
| 4686 | // and perform the initialization only on those interfaces that contain default methods. |
| 4687 | bool ClassLinker::InitializeDefaultInterfaceRecursive(Thread* self, |
| 4688 | Handle<mirror::Class> iface, |
| 4689 | bool can_init_statics, |
| 4690 | bool can_init_parents) { |
| 4691 | CHECK(iface->IsInterface()); |
| 4692 | size_t num_direct_ifaces = iface->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4693 | // Only create the (expensive) handle scope if we need it. |
| 4694 | if (UNLIKELY(num_direct_ifaces > 0)) { |
| 4695 | StackHandleScope<1> hs(self); |
| 4696 | MutableHandle<mirror::Class> handle_super_iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 4697 | // First we initialize all of iface's super-interfaces recursively. |
| 4698 | for (size_t i = 0; i < num_direct_ifaces; i++) { |
| 4699 | mirror::Class* super_iface = mirror::Class::GetDirectInterface(self, iface, i); |
| 4700 | if (!super_iface->HasBeenRecursivelyInitialized()) { |
| 4701 | // Recursive step |
| 4702 | handle_super_iface.Assign(super_iface); |
| 4703 | if (!InitializeDefaultInterfaceRecursive(self, |
| 4704 | handle_super_iface, |
| 4705 | can_init_statics, |
| 4706 | can_init_parents)) { |
| 4707 | return false; |
| 4708 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4709 | } |
| 4710 | } |
| 4711 | } |
| 4712 | |
| 4713 | bool result = true; |
| 4714 | // Then we initialize 'iface' if it has default methods. We do not need to (and in fact must not) |
| 4715 | // initialize if we don't have default methods. |
| 4716 | if (iface->HasDefaultMethods()) { |
| 4717 | result = EnsureInitialized(self, iface, can_init_statics, can_init_parents); |
| 4718 | } |
| 4719 | |
| 4720 | // Mark that this interface has undergone recursive default interface initialization so we know we |
| 4721 | // can skip it on any later class initializations. We do this even if we are not a default |
| 4722 | // interface since we can still avoid the traversal. This is purely a performance optimization. |
| 4723 | if (result) { |
| 4724 | // TODO This should be done in a better way |
| 4725 | ObjectLock<mirror::Class> lock(self, iface); |
| 4726 | iface->SetRecursivelyInitialized(); |
| 4727 | } |
| 4728 | return result; |
| 4729 | } |
| 4730 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4731 | bool ClassLinker::WaitForInitializeClass(Handle<mirror::Class> klass, |
| 4732 | Thread* self, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4733 | ObjectLock<mirror::Class>& lock) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 4734 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4735 | while (true) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4736 | self->AssertNoPendingException(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4737 | CHECK(!klass->IsInitialized()); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4738 | lock.WaitIgnoringInterrupts(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4739 | |
| 4740 | // When we wake up, repeat the test for init-in-progress. If |
| 4741 | // there's an exception pending (only possible if |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4742 | // we were not using WaitIgnoringInterrupts), bail out. |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4743 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4744 | WrapExceptionInInitializer(klass); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4745 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4746 | return false; |
| 4747 | } |
| 4748 | // Spurious wakeup? Go back to waiting. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4749 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4750 | continue; |
| 4751 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4752 | if (klass->GetStatus() == mirror::Class::kStatusVerified && |
| 4753 | Runtime::Current()->IsAotCompiler()) { |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 4754 | // Compile time initialization failed. |
| 4755 | return false; |
| 4756 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4757 | if (klass->IsErroneous()) { |
| 4758 | // The caller wants an exception, but it was thrown in a |
| 4759 | // different thread. Synthesize one here. |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 4760 | ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread", |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4761 | PrettyDescriptor(klass.Get()).c_str()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4762 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4763 | return false; |
| 4764 | } |
| 4765 | if (klass->IsInitialized()) { |
| 4766 | return true; |
| 4767 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4768 | LOG(FATAL) << "Unexpected class status. " << PrettyClass(klass.Get()) << " is " |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4769 | << klass->GetStatus(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4770 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 4771 | UNREACHABLE(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4772 | } |
| 4773 | |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4774 | static void ThrowSignatureCheckResolveReturnTypeException(Handle<mirror::Class> klass, |
| 4775 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4776 | ArtMethod* method, |
| 4777 | ArtMethod* m) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 4778 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4779 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4780 | DCHECK(!m->IsProxyMethod()); |
| 4781 | const DexFile* dex_file = m->GetDexFile(); |
| 4782 | const DexFile::MethodId& method_id = dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 4783 | const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id); |
| 4784 | uint16_t return_type_idx = proto_id.return_type_idx_; |
| 4785 | std::string return_type = PrettyType(return_type_idx, *dex_file); |
| 4786 | std::string class_loader = PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
| 4787 | ThrowWrappedLinkageError(klass.Get(), |
| 4788 | "While checking class %s method %s signature against %s %s: " |
| 4789 | "Failed to resolve return type %s with %s", |
| 4790 | PrettyDescriptor(klass.Get()).c_str(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4791 | PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4792 | super_klass->IsInterface() ? "interface" : "superclass", |
| 4793 | PrettyDescriptor(super_klass.Get()).c_str(), |
| 4794 | return_type.c_str(), class_loader.c_str()); |
| 4795 | } |
| 4796 | |
| 4797 | static void ThrowSignatureCheckResolveArgException(Handle<mirror::Class> klass, |
| 4798 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4799 | ArtMethod* method, |
| 4800 | ArtMethod* m, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4801 | uint32_t index, |
| 4802 | uint32_t arg_type_idx) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 4803 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4804 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4805 | DCHECK(!m->IsProxyMethod()); |
| 4806 | const DexFile* dex_file = m->GetDexFile(); |
| 4807 | std::string arg_type = PrettyType(arg_type_idx, *dex_file); |
| 4808 | std::string class_loader = PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
| 4809 | ThrowWrappedLinkageError(klass.Get(), |
| 4810 | "While checking class %s method %s signature against %s %s: " |
| 4811 | "Failed to resolve arg %u type %s with %s", |
| 4812 | PrettyDescriptor(klass.Get()).c_str(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4813 | PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4814 | super_klass->IsInterface() ? "interface" : "superclass", |
| 4815 | PrettyDescriptor(super_klass.Get()).c_str(), |
| 4816 | index, arg_type.c_str(), class_loader.c_str()); |
| 4817 | } |
| 4818 | |
| 4819 | static void ThrowSignatureMismatch(Handle<mirror::Class> klass, |
| 4820 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4821 | ArtMethod* method, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4822 | const std::string& error_msg) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 4823 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4824 | ThrowLinkageError(klass.Get(), |
| 4825 | "Class %s method %s resolves differently in %s %s: %s", |
| 4826 | PrettyDescriptor(klass.Get()).c_str(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4827 | PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4828 | super_klass->IsInterface() ? "interface" : "superclass", |
| 4829 | PrettyDescriptor(super_klass.Get()).c_str(), |
| 4830 | error_msg.c_str()); |
| 4831 | } |
| 4832 | |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4833 | static bool HasSameSignatureWithDifferentClassLoaders(Thread* self, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 4834 | PointerSize pointer_size, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4835 | Handle<mirror::Class> klass, |
| 4836 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4837 | ArtMethod* method1, |
| 4838 | ArtMethod* method2) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 4839 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4840 | { |
| 4841 | StackHandleScope<1> hs(self); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 4842 | Handle<mirror::Class> return_type(hs.NewHandle(method1->GetReturnType(true /* resolve */, |
| 4843 | pointer_size))); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4844 | if (UNLIKELY(return_type.Get() == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4845 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method1); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4846 | return false; |
| 4847 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 4848 | mirror::Class* other_return_type = method2->GetReturnType(true /* resolve */, |
| 4849 | pointer_size); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4850 | if (UNLIKELY(other_return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4851 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method2); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4852 | return false; |
| 4853 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 4854 | if (UNLIKELY(other_return_type != return_type.Get())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4855 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 4856 | StringPrintf("Return types mismatch: %s(%p) vs %s(%p)", |
| 4857 | PrettyClassAndClassLoader(return_type.Get()).c_str(), |
| 4858 | return_type.Get(), |
| 4859 | PrettyClassAndClassLoader(other_return_type).c_str(), |
| 4860 | other_return_type)); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4861 | return false; |
| 4862 | } |
| 4863 | } |
| 4864 | const DexFile::TypeList* types1 = method1->GetParameterTypeList(); |
| 4865 | const DexFile::TypeList* types2 = method2->GetParameterTypeList(); |
| 4866 | if (types1 == nullptr) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 4867 | if (types2 != nullptr && types2->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4868 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 4869 | StringPrintf("Type list mismatch with %s", |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4870 | PrettyMethod(method2, true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 4871 | return false; |
| 4872 | } |
| 4873 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4874 | } else if (UNLIKELY(types2 == nullptr)) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 4875 | if (types1->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4876 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 4877 | StringPrintf("Type list mismatch with %s", |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4878 | PrettyMethod(method2, true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 4879 | return false; |
| 4880 | } |
| 4881 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4882 | } |
| 4883 | uint32_t num_types = types1->Size(); |
| 4884 | if (UNLIKELY(num_types != types2->Size())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4885 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 4886 | StringPrintf("Type list mismatch with %s", |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4887 | PrettyMethod(method2, true).c_str())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4888 | return false; |
| 4889 | } |
| 4890 | for (uint32_t i = 0; i < num_types; ++i) { |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 4891 | StackHandleScope<1> hs(self); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4892 | uint32_t param_type_idx = types1->GetTypeItem(i).type_idx_; |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 4893 | Handle<mirror::Class> param_type(hs.NewHandle( |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 4894 | method1->GetClassFromTypeIndex(param_type_idx, true /* resolve */, pointer_size))); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4895 | if (UNLIKELY(param_type.Get() == nullptr)) { |
| 4896 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4897 | method1, i, param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4898 | return false; |
| 4899 | } |
| 4900 | uint32_t other_param_type_idx = types2->GetTypeItem(i).type_idx_; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4901 | mirror::Class* other_param_type = |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 4902 | method2->GetClassFromTypeIndex(other_param_type_idx, true /* resolve */, pointer_size); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4903 | if (UNLIKELY(other_param_type == nullptr)) { |
| 4904 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4905 | method2, i, other_param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4906 | return false; |
| 4907 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 4908 | if (UNLIKELY(param_type.Get() != other_param_type)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4909 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 4910 | StringPrintf("Parameter %u type mismatch: %s(%p) vs %s(%p)", |
| 4911 | i, |
| 4912 | PrettyClassAndClassLoader(param_type.Get()).c_str(), |
| 4913 | param_type.Get(), |
| 4914 | PrettyClassAndClassLoader(other_param_type).c_str(), |
| 4915 | other_param_type)); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4916 | return false; |
| 4917 | } |
| 4918 | } |
| 4919 | return true; |
| 4920 | } |
| 4921 | |
| 4922 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4923 | bool ClassLinker::ValidateSuperClassDescriptors(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4924 | if (klass->IsInterface()) { |
| 4925 | return true; |
| 4926 | } |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 4927 | // Begin with the methods local to the superclass. |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 4928 | Thread* self = Thread::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4929 | StackHandleScope<1> hs(self); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4930 | MutableHandle<mirror::Class> super_klass(hs.NewHandle<mirror::Class>(nullptr)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4931 | if (klass->HasSuperClass() && |
| 4932 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4933 | super_klass.Assign(klass->GetSuperClass()); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4934 | for (int i = klass->GetSuperClass()->GetVTableLength() - 1; i >= 0; --i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4935 | auto* m = klass->GetVTableEntry(i, image_pointer_size_); |
| 4936 | auto* super_m = klass->GetSuperClass()->GetVTableEntry(i, image_pointer_size_); |
| 4937 | if (m != super_m) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 4938 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, image_pointer_size_, |
| 4939 | klass, super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4940 | m, super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4941 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 4942 | return false; |
| 4943 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4944 | } |
| 4945 | } |
| 4946 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4947 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4948 | super_klass.Assign(klass->GetIfTable()->GetInterface(i)); |
| 4949 | if (klass->GetClassLoader() != super_klass->GetClassLoader()) { |
| 4950 | uint32_t num_methods = super_klass->NumVirtualMethods(); |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 4951 | for (uint32_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4952 | auto* m = klass->GetIfTable()->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>( |
| 4953 | j, image_pointer_size_); |
| 4954 | auto* super_m = super_klass->GetVirtualMethod(j, image_pointer_size_); |
| 4955 | if (m != super_m) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 4956 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, image_pointer_size_, |
| 4957 | klass, super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4958 | m, super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4959 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 4960 | return false; |
| 4961 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4962 | } |
| 4963 | } |
| 4964 | } |
| 4965 | } |
| 4966 | return true; |
| 4967 | } |
| 4968 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4969 | bool ClassLinker::EnsureInitialized(Thread* self, Handle<mirror::Class> c, bool can_init_fields, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4970 | bool can_init_parents) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4971 | DCHECK(c.Get() != nullptr); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4972 | if (c->IsInitialized()) { |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4973 | EnsureSkipAccessChecksMethods(c); |
Mathieu Chartier | 8502f72 | 2016-06-08 15:09:08 -0700 | [diff] [blame] | 4974 | self->AssertNoPendingException(); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4975 | return true; |
| 4976 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4977 | const bool success = InitializeClass(self, c, can_init_fields, can_init_parents); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4978 | if (!success) { |
| 4979 | if (can_init_fields && can_init_parents) { |
| 4980 | CHECK(self->IsExceptionPending()) << PrettyClass(c.Get()); |
| 4981 | } |
| 4982 | } else { |
| 4983 | self->AssertNoPendingException(); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 4984 | } |
| 4985 | return success; |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 4986 | } |
| 4987 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4988 | void ClassLinker::FixupTemporaryDeclaringClass(mirror::Class* temp_class, |
| 4989 | mirror::Class* new_class) { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 4990 | DCHECK_EQ(temp_class->NumInstanceFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4991 | for (ArtField& field : new_class->GetIFields()) { |
| 4992 | if (field.GetDeclaringClass() == temp_class) { |
| 4993 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4994 | } |
| 4995 | } |
| 4996 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 4997 | DCHECK_EQ(temp_class->NumStaticFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4998 | for (ArtField& field : new_class->GetSFields()) { |
| 4999 | if (field.GetDeclaringClass() == temp_class) { |
| 5000 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5001 | } |
| 5002 | } |
| 5003 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5004 | DCHECK_EQ(temp_class->NumDirectMethods(), 0u); |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5005 | DCHECK_EQ(temp_class->NumVirtualMethods(), 0u); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5006 | for (auto& method : new_class->GetMethods(image_pointer_size_)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5007 | if (method.GetDeclaringClass() == temp_class) { |
| 5008 | method.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5009 | } |
| 5010 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5011 | |
| 5012 | // Make sure the remembered set and mod-union tables know that we updated some of the native |
| 5013 | // roots. |
| 5014 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5015 | } |
| 5016 | |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5017 | void ClassLinker::RegisterClassLoader(mirror::ClassLoader* class_loader) { |
| 5018 | CHECK(class_loader->GetAllocator() == nullptr); |
| 5019 | CHECK(class_loader->GetClassTable() == nullptr); |
| 5020 | Thread* const self = Thread::Current(); |
| 5021 | ClassLoaderData data; |
| 5022 | data.weak_root = self->GetJniEnv()->vm->AddWeakGlobalRef(self, class_loader); |
| 5023 | // Create and set the class table. |
| 5024 | data.class_table = new ClassTable; |
| 5025 | class_loader->SetClassTable(data.class_table); |
| 5026 | // Create and set the linear allocator. |
| 5027 | data.allocator = Runtime::Current()->CreateLinearAlloc(); |
| 5028 | class_loader->SetAllocator(data.allocator); |
| 5029 | // Add to the list so that we know to free the data later. |
| 5030 | class_loaders_.push_back(data); |
| 5031 | } |
| 5032 | |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5033 | ClassTable* ClassLinker::InsertClassTableForClassLoader(mirror::ClassLoader* class_loader) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5034 | if (class_loader == nullptr) { |
| 5035 | return &boot_class_table_; |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5036 | } |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5037 | ClassTable* class_table = class_loader->GetClassTable(); |
| 5038 | if (class_table == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5039 | RegisterClassLoader(class_loader); |
| 5040 | class_table = class_loader->GetClassTable(); |
| 5041 | DCHECK(class_table != nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5042 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5043 | return class_table; |
| 5044 | } |
| 5045 | |
| 5046 | ClassTable* ClassLinker::ClassTableForClassLoader(mirror::ClassLoader* class_loader) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5047 | return class_loader == nullptr ? &boot_class_table_ : class_loader->GetClassTable(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5048 | } |
| 5049 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5050 | static ImTable* FindSuperImt(mirror::Class* klass, PointerSize pointer_size) |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5051 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 5052 | while (klass->HasSuperClass()) { |
| 5053 | klass = klass->GetSuperClass(); |
| 5054 | if (klass->ShouldHaveImt()) { |
| 5055 | return klass->GetImt(pointer_size); |
| 5056 | } |
| 5057 | } |
| 5058 | return nullptr; |
| 5059 | } |
| 5060 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5061 | bool ClassLinker::LinkClass(Thread* self, |
| 5062 | const char* descriptor, |
| 5063 | Handle<mirror::Class> klass, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5064 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5065 | MutableHandle<mirror::Class>* h_new_class_out) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5066 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5067 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5068 | if (!LinkSuperClass(klass)) { |
| 5069 | return false; |
| 5070 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5071 | ArtMethod* imt_data[ImTable::kSize]; |
| 5072 | // If there are any new conflicts compared to super class. |
| 5073 | bool new_conflict = false; |
| 5074 | std::fill_n(imt_data, arraysize(imt_data), Runtime::Current()->GetImtUnimplementedMethod()); |
| 5075 | if (!LinkMethods(self, klass, interfaces, &new_conflict, imt_data)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5076 | return false; |
| 5077 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5078 | if (!LinkInstanceFields(self, klass)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5079 | return false; |
| 5080 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5081 | size_t class_size; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5082 | if (!LinkStaticFields(self, klass, &class_size)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5083 | return false; |
| 5084 | } |
| 5085 | CreateReferenceInstanceOffsets(klass); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5086 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5087 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5088 | ImTable* imt = nullptr; |
| 5089 | if (klass->ShouldHaveImt()) { |
| 5090 | // If there are any new conflicts compared to the super class we can not make a copy. There |
| 5091 | // can be cases where both will have a conflict method at the same slot without having the same |
| 5092 | // set of conflicts. In this case, we can not share the IMT since the conflict table slow path |
| 5093 | // will possibly create a table that is incorrect for either of the classes. |
| 5094 | // Same IMT with new_conflict does not happen very often. |
| 5095 | if (!new_conflict) { |
| 5096 | ImTable* super_imt = FindSuperImt(klass.Get(), image_pointer_size_); |
| 5097 | if (super_imt != nullptr) { |
| 5098 | bool imt_equals = true; |
| 5099 | for (size_t i = 0; i < ImTable::kSize && imt_equals; ++i) { |
| 5100 | imt_equals = imt_equals && (super_imt->Get(i, image_pointer_size_) == imt_data[i]); |
| 5101 | } |
| 5102 | if (imt_equals) { |
| 5103 | imt = super_imt; |
| 5104 | } |
| 5105 | } |
| 5106 | } |
| 5107 | if (imt == nullptr) { |
| 5108 | LinearAlloc* allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
| 5109 | imt = reinterpret_cast<ImTable*>( |
| 5110 | allocator->Alloc(self, ImTable::SizeInBytes(image_pointer_size_))); |
| 5111 | if (imt == nullptr) { |
| 5112 | return false; |
| 5113 | } |
| 5114 | imt->Populate(imt_data, image_pointer_size_); |
| 5115 | } |
| 5116 | } |
| 5117 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5118 | if (!klass->IsTemp() || (!init_done_ && klass->GetClassSize() == class_size)) { |
| 5119 | // We don't need to retire this class as it has no embedded tables or it was created the |
| 5120 | // correct size during class linker initialization. |
| 5121 | CHECK_EQ(klass->GetClassSize(), class_size) << PrettyDescriptor(klass.Get()); |
| 5122 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5123 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 5124 | klass->PopulateEmbeddedVTable(image_pointer_size_); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5125 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5126 | if (klass->ShouldHaveImt()) { |
| 5127 | klass->SetImt(imt, image_pointer_size_); |
| 5128 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5129 | // This will notify waiters on klass that saw the not yet resolved |
| 5130 | // class in the class_table_ during EnsureResolved. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 5131 | mirror::Class::SetStatus(klass, mirror::Class::kStatusResolved, self); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5132 | h_new_class_out->Assign(klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5133 | } else { |
| 5134 | CHECK(!klass->IsResolved()); |
| 5135 | // Retire the temporary class and create the correctly sized resolved class. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5136 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5137 | auto h_new_class = hs.NewHandle(klass->CopyOf(self, class_size, imt, image_pointer_size_)); |
Mathieu Chartier | 3ee25bb | 2015-08-10 10:13:02 -0700 | [diff] [blame] | 5138 | // Set arrays to null since we don't want to have multiple classes with the same ArtField or |
| 5139 | // ArtMethod array pointers. If this occurs, it causes bugs in remembered sets since the GC |
| 5140 | // may not see any references to the target space and clean the card for a class if another |
| 5141 | // class had the same array pointer. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5142 | klass->SetMethodsPtrUnchecked(nullptr, 0, 0); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5143 | klass->SetSFieldsPtrUnchecked(nullptr); |
| 5144 | klass->SetIFieldsPtrUnchecked(nullptr); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5145 | if (UNLIKELY(h_new_class.Get() == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5146 | self->AssertPendingOOMException(); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 5147 | mirror::Class::SetStatus(klass, mirror::Class::kStatusError, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5148 | return false; |
| 5149 | } |
| 5150 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5151 | CHECK_EQ(h_new_class->GetClassSize(), class_size); |
| 5152 | ObjectLock<mirror::Class> lock(self, h_new_class); |
| 5153 | FixupTemporaryDeclaringClass(klass.Get(), h_new_class.Get()); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5154 | |
| 5155 | { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5156 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5157 | mirror::ClassLoader* const class_loader = h_new_class.Get()->GetClassLoader(); |
| 5158 | ClassTable* const table = InsertClassTableForClassLoader(class_loader); |
| 5159 | mirror::Class* existing = table->UpdateClass(descriptor, h_new_class.Get(), |
| 5160 | ComputeModifiedUtf8Hash(descriptor)); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 5161 | if (class_loader != nullptr) { |
| 5162 | // We updated the class in the class table, perform the write barrier so that the GC knows |
| 5163 | // about the change. |
| 5164 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader); |
| 5165 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5166 | CHECK_EQ(existing, klass.Get()); |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 5167 | if (kIsDebugBuild && class_loader == nullptr && dex_cache_boot_image_class_lookup_required_) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5168 | // Check a class loaded with the system class loader matches one in the image if the class |
| 5169 | // is in the image. |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 5170 | mirror::Class* const image_class = LookupClassFromBootImage(descriptor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5171 | if (image_class != nullptr) { |
| 5172 | CHECK_EQ(klass.Get(), existing) << descriptor; |
| 5173 | } |
| 5174 | } |
| 5175 | if (log_new_class_table_roots_) { |
| 5176 | new_class_roots_.push_back(GcRoot<mirror::Class>(h_new_class.Get())); |
| 5177 | } |
| 5178 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5179 | |
| 5180 | // This will notify waiters on temp class that saw the not yet resolved class in the |
| 5181 | // class_table_ during EnsureResolved. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 5182 | mirror::Class::SetStatus(klass, mirror::Class::kStatusRetired, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5183 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5184 | CHECK_EQ(h_new_class->GetStatus(), mirror::Class::kStatusResolving); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5185 | // This will notify waiters on new_class that saw the not yet resolved |
| 5186 | // class in the class_table_ during EnsureResolved. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5187 | mirror::Class::SetStatus(h_new_class, mirror::Class::kStatusResolved, self); |
| 5188 | // Return the new class. |
| 5189 | h_new_class_out->Assign(h_new_class.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5190 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5191 | return true; |
| 5192 | } |
| 5193 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5194 | static void CountMethodsAndFields(ClassDataItemIterator& dex_data, |
| 5195 | size_t* virtual_methods, |
| 5196 | size_t* direct_methods, |
| 5197 | size_t* static_fields, |
| 5198 | size_t* instance_fields) { |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5199 | *virtual_methods = *direct_methods = *static_fields = *instance_fields = 0; |
| 5200 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5201 | while (dex_data.HasNextStaticField()) { |
| 5202 | dex_data.Next(); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5203 | (*static_fields)++; |
| 5204 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5205 | while (dex_data.HasNextInstanceField()) { |
| 5206 | dex_data.Next(); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5207 | (*instance_fields)++; |
| 5208 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5209 | while (dex_data.HasNextDirectMethod()) { |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5210 | (*direct_methods)++; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5211 | dex_data.Next(); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5212 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5213 | while (dex_data.HasNextVirtualMethod()) { |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5214 | (*virtual_methods)++; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5215 | dex_data.Next(); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5216 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5217 | DCHECK(!dex_data.HasNext()); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5218 | } |
| 5219 | |
| 5220 | static void DumpClass(std::ostream& os, |
| 5221 | const DexFile& dex_file, const DexFile::ClassDef& dex_class_def, |
| 5222 | const char* suffix) { |
| 5223 | ClassDataItemIterator dex_data(dex_file, dex_file.GetClassData(dex_class_def)); |
| 5224 | os << dex_file.GetClassDescriptor(dex_class_def) << suffix << ":\n"; |
| 5225 | os << " Static fields:\n"; |
| 5226 | while (dex_data.HasNextStaticField()) { |
| 5227 | const DexFile::FieldId& id = dex_file.GetFieldId(dex_data.GetMemberIndex()); |
| 5228 | os << " " << dex_file.GetFieldTypeDescriptor(id) << " " << dex_file.GetFieldName(id) << "\n"; |
| 5229 | dex_data.Next(); |
| 5230 | } |
| 5231 | os << " Instance fields:\n"; |
| 5232 | while (dex_data.HasNextInstanceField()) { |
| 5233 | const DexFile::FieldId& id = dex_file.GetFieldId(dex_data.GetMemberIndex()); |
| 5234 | os << " " << dex_file.GetFieldTypeDescriptor(id) << " " << dex_file.GetFieldName(id) << "\n"; |
| 5235 | dex_data.Next(); |
| 5236 | } |
| 5237 | os << " Direct methods:\n"; |
| 5238 | while (dex_data.HasNextDirectMethod()) { |
| 5239 | const DexFile::MethodId& id = dex_file.GetMethodId(dex_data.GetMemberIndex()); |
| 5240 | os << " " << dex_file.GetMethodName(id) << dex_file.GetMethodSignature(id).ToString() << "\n"; |
| 5241 | dex_data.Next(); |
| 5242 | } |
| 5243 | os << " Virtual methods:\n"; |
| 5244 | while (dex_data.HasNextVirtualMethod()) { |
| 5245 | const DexFile::MethodId& id = dex_file.GetMethodId(dex_data.GetMemberIndex()); |
| 5246 | os << " " << dex_file.GetMethodName(id) << dex_file.GetMethodSignature(id).ToString() << "\n"; |
| 5247 | dex_data.Next(); |
| 5248 | } |
| 5249 | } |
| 5250 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5251 | static std::string DumpClasses(const DexFile& dex_file1, |
| 5252 | const DexFile::ClassDef& dex_class_def1, |
| 5253 | const DexFile& dex_file2, |
| 5254 | const DexFile::ClassDef& dex_class_def2) { |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5255 | std::ostringstream os; |
| 5256 | DumpClass(os, dex_file1, dex_class_def1, " (Compile time)"); |
| 5257 | DumpClass(os, dex_file2, dex_class_def2, " (Runtime)"); |
| 5258 | return os.str(); |
| 5259 | } |
| 5260 | |
| 5261 | |
| 5262 | // Very simple structural check on whether the classes match. Only compares the number of |
| 5263 | // methods and fields. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5264 | static bool SimpleStructuralCheck(const DexFile& dex_file1, |
| 5265 | const DexFile::ClassDef& dex_class_def1, |
| 5266 | const DexFile& dex_file2, |
| 5267 | const DexFile::ClassDef& dex_class_def2, |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5268 | std::string* error_msg) { |
| 5269 | ClassDataItemIterator dex_data1(dex_file1, dex_file1.GetClassData(dex_class_def1)); |
| 5270 | ClassDataItemIterator dex_data2(dex_file2, dex_file2.GetClassData(dex_class_def2)); |
| 5271 | |
| 5272 | // Counters for current dex file. |
| 5273 | size_t dex_virtual_methods1, dex_direct_methods1, dex_static_fields1, dex_instance_fields1; |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5274 | CountMethodsAndFields(dex_data1, |
| 5275 | &dex_virtual_methods1, |
| 5276 | &dex_direct_methods1, |
| 5277 | &dex_static_fields1, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5278 | &dex_instance_fields1); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5279 | // Counters for compile-time dex file. |
| 5280 | size_t dex_virtual_methods2, dex_direct_methods2, dex_static_fields2, dex_instance_fields2; |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5281 | CountMethodsAndFields(dex_data2, |
| 5282 | &dex_virtual_methods2, |
| 5283 | &dex_direct_methods2, |
| 5284 | &dex_static_fields2, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5285 | &dex_instance_fields2); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5286 | |
| 5287 | if (dex_virtual_methods1 != dex_virtual_methods2) { |
| 5288 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5289 | *error_msg = StringPrintf("Virtual method count off: %zu vs %zu\n%s", |
| 5290 | dex_virtual_methods1, |
| 5291 | dex_virtual_methods2, |
| 5292 | class_dump.c_str()); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5293 | return false; |
| 5294 | } |
| 5295 | if (dex_direct_methods1 != dex_direct_methods2) { |
| 5296 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5297 | *error_msg = StringPrintf("Direct method count off: %zu vs %zu\n%s", |
| 5298 | dex_direct_methods1, |
| 5299 | dex_direct_methods2, |
| 5300 | class_dump.c_str()); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5301 | return false; |
| 5302 | } |
| 5303 | if (dex_static_fields1 != dex_static_fields2) { |
| 5304 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5305 | *error_msg = StringPrintf("Static field count off: %zu vs %zu\n%s", |
| 5306 | dex_static_fields1, |
| 5307 | dex_static_fields2, |
| 5308 | class_dump.c_str()); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5309 | return false; |
| 5310 | } |
| 5311 | if (dex_instance_fields1 != dex_instance_fields2) { |
| 5312 | std::string class_dump = DumpClasses(dex_file1, dex_class_def1, dex_file2, dex_class_def2); |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5313 | *error_msg = StringPrintf("Instance field count off: %zu vs %zu\n%s", |
| 5314 | dex_instance_fields1, |
| 5315 | dex_instance_fields2, |
| 5316 | class_dump.c_str()); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5317 | return false; |
| 5318 | } |
| 5319 | |
| 5320 | return true; |
| 5321 | } |
| 5322 | |
| 5323 | // Checks whether a the super-class changed from what we had at compile-time. This would |
| 5324 | // invalidate quickening. |
| 5325 | static bool CheckSuperClassChange(Handle<mirror::Class> klass, |
| 5326 | const DexFile& dex_file, |
| 5327 | const DexFile::ClassDef& class_def, |
| 5328 | mirror::Class* super_class) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 5329 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5330 | // Check for unexpected changes in the superclass. |
| 5331 | // Quick check 1) is the super_class class-loader the boot class loader? This always has |
| 5332 | // precedence. |
| 5333 | if (super_class->GetClassLoader() != nullptr && |
| 5334 | // Quick check 2) different dex cache? Breaks can only occur for different dex files, |
| 5335 | // which is implied by different dex cache. |
| 5336 | klass->GetDexCache() != super_class->GetDexCache()) { |
| 5337 | // Now comes the expensive part: things can be broken if (a) the klass' dex file has a |
| 5338 | // definition for the super-class, and (b) the files are in separate oat files. The oat files |
| 5339 | // are referenced from the dex file, so do (b) first. Only relevant if we have oat files. |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 5340 | const OatDexFile* class_oat_dex_file = dex_file.GetOatDexFile(); |
| 5341 | const OatFile* class_oat_file = nullptr; |
| 5342 | if (class_oat_dex_file != nullptr) { |
| 5343 | class_oat_file = class_oat_dex_file->GetOatFile(); |
| 5344 | } |
| 5345 | |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5346 | if (class_oat_file != nullptr) { |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 5347 | const OatDexFile* loaded_super_oat_dex_file = super_class->GetDexFile().GetOatDexFile(); |
| 5348 | const OatFile* loaded_super_oat_file = nullptr; |
| 5349 | if (loaded_super_oat_dex_file != nullptr) { |
| 5350 | loaded_super_oat_file = loaded_super_oat_dex_file->GetOatFile(); |
| 5351 | } |
| 5352 | |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5353 | if (loaded_super_oat_file != nullptr && class_oat_file != loaded_super_oat_file) { |
| 5354 | // Now check (a). |
| 5355 | const DexFile::ClassDef* super_class_def = dex_file.FindClassDef(class_def.superclass_idx_); |
| 5356 | if (super_class_def != nullptr) { |
| 5357 | // Uh-oh, we found something. Do our check. |
| 5358 | std::string error_msg; |
| 5359 | if (!SimpleStructuralCheck(dex_file, *super_class_def, |
| 5360 | super_class->GetDexFile(), *super_class->GetClassDef(), |
| 5361 | &error_msg)) { |
| 5362 | // Print a warning to the log. This exception might be caught, e.g., as common in test |
| 5363 | // drivers. When the class is later tried to be used, we re-throw a new instance, as we |
| 5364 | // only save the type of the exception. |
| 5365 | LOG(WARNING) << "Incompatible structural change detected: " << |
| 5366 | StringPrintf( |
| 5367 | "Structural change of %s is hazardous (%s at compile time, %s at runtime): %s", |
| 5368 | PrettyType(super_class_def->class_idx_, dex_file).c_str(), |
| 5369 | class_oat_file->GetLocation().c_str(), |
| 5370 | loaded_super_oat_file->GetLocation().c_str(), |
| 5371 | error_msg.c_str()); |
| 5372 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 5373 | "Structural change of %s is hazardous (%s at compile time, %s at runtime): %s", |
| 5374 | PrettyType(super_class_def->class_idx_, dex_file).c_str(), |
| 5375 | class_oat_file->GetLocation().c_str(), |
| 5376 | loaded_super_oat_file->GetLocation().c_str(), |
| 5377 | error_msg.c_str()); |
| 5378 | return false; |
| 5379 | } |
| 5380 | } |
| 5381 | } |
| 5382 | } |
| 5383 | } |
| 5384 | return true; |
| 5385 | } |
| 5386 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5387 | bool ClassLinker::LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5388 | CHECK_EQ(mirror::Class::kStatusIdx, klass->GetStatus()); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 5389 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
| 5390 | uint16_t super_class_idx = class_def.superclass_idx_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5391 | if (super_class_idx != DexFile::kDexNoIndex16) { |
Roland Levillain | 989ab3b | 2016-05-18 15:52:54 +0100 | [diff] [blame] | 5392 | // Check that a class does not inherit from itself directly. |
| 5393 | // |
| 5394 | // TODO: This is a cheap check to detect the straightforward case |
| 5395 | // of a class extending itself (b/28685551), but we should do a |
| 5396 | // proper cycle detection on loaded classes, to detect all cases |
| 5397 | // of class circularity errors (b/28830038). |
| 5398 | if (super_class_idx == class_def.class_idx_) { |
| 5399 | ThrowClassCircularityError(klass.Get(), |
| 5400 | "Class %s extends itself", |
| 5401 | PrettyDescriptor(klass.Get()).c_str()); |
| 5402 | return false; |
| 5403 | } |
| 5404 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5405 | mirror::Class* super_class = ResolveType(dex_file, super_class_idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5406 | if (super_class == nullptr) { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 5407 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5408 | return false; |
| 5409 | } |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 5410 | // Verify |
| 5411 | if (!klass->CanAccess(super_class)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5412 | ThrowIllegalAccessError(klass.Get(), "Class %s extended by class %s is inaccessible", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5413 | PrettyDescriptor(super_class).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5414 | PrettyDescriptor(klass.Get()).c_str()); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 5415 | return false; |
| 5416 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5417 | CHECK(super_class->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5418 | klass->SetSuperClass(super_class); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5419 | |
| 5420 | if (!CheckSuperClassChange(klass, dex_file, class_def, super_class)) { |
| 5421 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5422 | return false; |
| 5423 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5424 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 5425 | const DexFile::TypeList* interfaces = dex_file.GetInterfacesList(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5426 | if (interfaces != nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5427 | for (size_t i = 0; i < interfaces->Size(); i++) { |
| 5428 | uint16_t idx = interfaces->GetTypeItem(i).type_idx_; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5429 | mirror::Class* interface = ResolveType(dex_file, idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5430 | if (interface == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5431 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5432 | return false; |
| 5433 | } |
| 5434 | // Verify |
| 5435 | if (!klass->CanAccess(interface)) { |
| 5436 | // TODO: the RI seemed to ignore this in my testing. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5437 | ThrowIllegalAccessError(klass.Get(), |
| 5438 | "Interface %s implemented by class %s is inaccessible", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5439 | PrettyDescriptor(interface).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5440 | PrettyDescriptor(klass.Get()).c_str()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5441 | return false; |
| 5442 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5443 | } |
| 5444 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 5445 | // Mark the class as loaded. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 5446 | mirror::Class::SetStatus(klass, mirror::Class::kStatusLoaded, nullptr); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5447 | return true; |
| 5448 | } |
| 5449 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5450 | bool ClassLinker::LinkSuperClass(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5451 | CHECK(!klass->IsPrimitive()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5452 | mirror::Class* super = klass->GetSuperClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5453 | if (klass.Get() == GetClassRoot(kJavaLangObject)) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5454 | if (super != nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5455 | ThrowClassFormatError(klass.Get(), "java.lang.Object must not have a superclass"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5456 | return false; |
| 5457 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5458 | return true; |
| 5459 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5460 | if (super == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5461 | ThrowLinkageError(klass.Get(), "No superclass defined for class %s", |
| 5462 | PrettyDescriptor(klass.Get()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5463 | return false; |
| 5464 | } |
| 5465 | // Verify |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 5466 | if (super->IsFinal() || super->IsInterface()) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5467 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 5468 | "Superclass %s of %s is %s", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5469 | PrettyDescriptor(super).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5470 | PrettyDescriptor(klass.Get()).c_str(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5471 | super->IsFinal() ? "declared final" : "an interface"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5472 | return false; |
| 5473 | } |
| 5474 | if (!klass->CanAccess(super)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5475 | ThrowIllegalAccessError(klass.Get(), "Superclass %s is inaccessible to class %s", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5476 | PrettyDescriptor(super).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5477 | PrettyDescriptor(klass.Get()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5478 | return false; |
| 5479 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 5480 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5481 | // Inherit kAccClassIsFinalizable from the superclass in case this |
| 5482 | // class doesn't override finalize. |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 5483 | if (super->IsFinalizable()) { |
| 5484 | klass->SetFinalizable(); |
| 5485 | } |
| 5486 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 5487 | // Inherit class loader flag form super class. |
| 5488 | if (super->IsClassLoaderClass()) { |
| 5489 | klass->SetClassLoaderClass(); |
| 5490 | } |
| 5491 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5492 | // Inherit reference flags (if any) from the superclass. |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 5493 | uint32_t reference_flags = (super->GetClassFlags() & mirror::kClassFlagReference); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5494 | if (reference_flags != 0) { |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 5495 | CHECK_EQ(klass->GetClassFlags(), 0u); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 5496 | klass->SetClassFlags(klass->GetClassFlags() | reference_flags); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5497 | } |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 5498 | // Disallow custom direct subclasses of java.lang.ref.Reference. |
Elliott Hughes | bf61ba3 | 2011-10-11 10:53:09 -0700 | [diff] [blame] | 5499 | if (init_done_ && super == GetClassRoot(kJavaLangRefReference)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5500 | ThrowLinkageError(klass.Get(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5501 | "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] | 5502 | PrettyDescriptor(klass.Get()).c_str()); |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 5503 | return false; |
| 5504 | } |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5505 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5506 | if (kIsDebugBuild) { |
| 5507 | // Ensure super classes are fully resolved prior to resolving fields.. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5508 | while (super != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5509 | CHECK(super->IsResolved()); |
| 5510 | super = super->GetSuperClass(); |
| 5511 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5512 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5513 | return true; |
| 5514 | } |
| 5515 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5516 | // Populate the class vtable and itable. Compute return type indices. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5517 | bool ClassLinker::LinkMethods(Thread* self, |
| 5518 | Handle<mirror::Class> klass, |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5519 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5520 | bool* out_new_conflict, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5521 | ArtMethod** out_imt) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5522 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5523 | // A map from vtable indexes to the method they need to be updated to point to. Used because we |
| 5524 | // need to have default methods be in the virtuals array of each class but we don't set that up |
| 5525 | // until LinkInterfaceMethods. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5526 | std::unordered_map<size_t, ClassLinker::MethodTranslation> default_translations; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5527 | // Link virtual methods then interface methods. |
| 5528 | // We set up the interface lookup table first because we need it to determine if we need to update |
| 5529 | // any vtable entries with new default method implementations. |
| 5530 | return SetupInterfaceLookupTable(self, klass, interfaces) |
| 5531 | && LinkVirtualMethods(self, klass, /*out*/ &default_translations) |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5532 | && LinkInterfaceMethods(self, klass, default_translations, out_new_conflict, out_imt); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5533 | } |
| 5534 | |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5535 | // Comparator for name and signature of a method, used in finding overriding methods. Implementation |
| 5536 | // avoids the use of handles, if it didn't then rather than compare dex files we could compare dex |
| 5537 | // caches in the implementation below. |
| 5538 | class MethodNameAndSignatureComparator FINAL : public ValueObject { |
| 5539 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5540 | explicit MethodNameAndSignatureComparator(ArtMethod* method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 5541 | SHARED_REQUIRES(Locks::mutator_lock_) : |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5542 | dex_file_(method->GetDexFile()), mid_(&dex_file_->GetMethodId(method->GetDexMethodIndex())), |
| 5543 | name_(nullptr), name_len_(0) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5544 | DCHECK(!method->IsProxyMethod()) << PrettyMethod(method); |
| 5545 | } |
| 5546 | |
| 5547 | const char* GetName() { |
| 5548 | if (name_ == nullptr) { |
| 5549 | name_ = dex_file_->StringDataAndUtf16LengthByIdx(mid_->name_idx_, &name_len_); |
| 5550 | } |
| 5551 | return name_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5552 | } |
| 5553 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5554 | bool HasSameNameAndSignature(ArtMethod* other) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 5555 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5556 | DCHECK(!other->IsProxyMethod()) << PrettyMethod(other); |
| 5557 | const DexFile* other_dex_file = other->GetDexFile(); |
| 5558 | const DexFile::MethodId& other_mid = other_dex_file->GetMethodId(other->GetDexMethodIndex()); |
| 5559 | if (dex_file_ == other_dex_file) { |
| 5560 | return mid_->name_idx_ == other_mid.name_idx_ && mid_->proto_idx_ == other_mid.proto_idx_; |
| 5561 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5562 | GetName(); // Only used to make sure its calculated. |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5563 | uint32_t other_name_len; |
| 5564 | const char* other_name = other_dex_file->StringDataAndUtf16LengthByIdx(other_mid.name_idx_, |
| 5565 | &other_name_len); |
| 5566 | if (name_len_ != other_name_len || strcmp(name_, other_name) != 0) { |
| 5567 | return false; |
| 5568 | } |
| 5569 | return dex_file_->GetMethodSignature(*mid_) == other_dex_file->GetMethodSignature(other_mid); |
| 5570 | } |
| 5571 | |
| 5572 | private: |
| 5573 | // Dex file for the method to compare against. |
| 5574 | const DexFile* const dex_file_; |
| 5575 | // MethodId for the method to compare against. |
| 5576 | const DexFile::MethodId* const mid_; |
| 5577 | // Lazily computed name from the dex file's strings. |
| 5578 | const char* name_; |
| 5579 | // Lazily computed name length. |
| 5580 | uint32_t name_len_; |
| 5581 | }; |
| 5582 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5583 | class LinkVirtualHashTable { |
| 5584 | public: |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5585 | LinkVirtualHashTable(Handle<mirror::Class> klass, |
| 5586 | size_t hash_size, |
| 5587 | uint32_t* hash_table, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5588 | PointerSize image_pointer_size) |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5589 | : klass_(klass), |
| 5590 | hash_size_(hash_size), |
| 5591 | hash_table_(hash_table), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5592 | image_pointer_size_(image_pointer_size) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5593 | std::fill(hash_table_, hash_table_ + hash_size_, invalid_index_); |
| 5594 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5595 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 5596 | void Add(uint32_t virtual_method_index) SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5597 | ArtMethod* local_method = klass_->GetVirtualMethodDuringLinking( |
| 5598 | virtual_method_index, image_pointer_size_); |
| 5599 | const char* name = local_method->GetInterfaceMethodIfProxy(image_pointer_size_)->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 5600 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5601 | uint32_t index = hash % hash_size_; |
| 5602 | // Linear probe until we have an empty slot. |
| 5603 | while (hash_table_[index] != invalid_index_) { |
| 5604 | if (++index == hash_size_) { |
| 5605 | index = 0; |
| 5606 | } |
| 5607 | } |
| 5608 | hash_table_[index] = virtual_method_index; |
| 5609 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5610 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5611 | uint32_t FindAndRemove(MethodNameAndSignatureComparator* comparator) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 5612 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5613 | const char* name = comparator->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 5614 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5615 | size_t index = hash % hash_size_; |
| 5616 | while (true) { |
| 5617 | const uint32_t value = hash_table_[index]; |
| 5618 | // Since linear probe makes continuous blocks, hitting an invalid index means we are done |
| 5619 | // the block and can safely assume not found. |
| 5620 | if (value == invalid_index_) { |
| 5621 | break; |
| 5622 | } |
| 5623 | if (value != removed_index_) { // This signifies not already overriden. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5624 | ArtMethod* virtual_method = |
| 5625 | klass_->GetVirtualMethodDuringLinking(value, image_pointer_size_); |
| 5626 | if (comparator->HasSameNameAndSignature( |
| 5627 | virtual_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5628 | hash_table_[index] = removed_index_; |
| 5629 | return value; |
| 5630 | } |
| 5631 | } |
| 5632 | if (++index == hash_size_) { |
| 5633 | index = 0; |
| 5634 | } |
| 5635 | } |
| 5636 | return GetNotFoundIndex(); |
| 5637 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5638 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5639 | static uint32_t GetNotFoundIndex() { |
| 5640 | return invalid_index_; |
| 5641 | } |
| 5642 | |
| 5643 | private: |
| 5644 | static const uint32_t invalid_index_; |
| 5645 | static const uint32_t removed_index_; |
| 5646 | |
| 5647 | Handle<mirror::Class> klass_; |
| 5648 | const size_t hash_size_; |
| 5649 | uint32_t* const hash_table_; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5650 | const PointerSize image_pointer_size_; |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5651 | }; |
| 5652 | |
| 5653 | const uint32_t LinkVirtualHashTable::invalid_index_ = std::numeric_limits<uint32_t>::max(); |
| 5654 | const uint32_t LinkVirtualHashTable::removed_index_ = std::numeric_limits<uint32_t>::max() - 1; |
| 5655 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5656 | // b/30419309 |
| 5657 | #if defined(__i386__) |
| 5658 | #define X86_OPTNONE __attribute__((optnone)) |
| 5659 | #else |
| 5660 | #define X86_OPTNONE |
| 5661 | #endif |
| 5662 | |
| 5663 | X86_OPTNONE bool ClassLinker::LinkVirtualMethods( |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5664 | Thread* self, |
| 5665 | Handle<mirror::Class> klass, |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5666 | /*out*/std::unordered_map<size_t, ClassLinker::MethodTranslation>* default_translations) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5667 | const size_t num_virtual_methods = klass->NumVirtualMethods(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5668 | if (klass->IsInterface()) { |
| 5669 | // No vtable. |
| 5670 | if (!IsUint<16>(num_virtual_methods)) { |
| 5671 | ThrowClassFormatError(klass.Get(), "Too many methods on interface: %zu", num_virtual_methods); |
| 5672 | return false; |
| 5673 | } |
| 5674 | bool has_defaults = false; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5675 | // Assign each method an IMT index and set the default flag. |
| 5676 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
| 5677 | ArtMethod* m = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
| 5678 | m->SetMethodIndex(i); |
| 5679 | if (!m->IsAbstract()) { |
| 5680 | m->SetAccessFlags(m->GetAccessFlags() | kAccDefault); |
| 5681 | has_defaults = true; |
| 5682 | } |
| 5683 | } |
| 5684 | // Mark that we have default methods so that we won't need to scan the virtual_methods_ array |
| 5685 | // during initialization. This is a performance optimization. We could simply traverse the |
| 5686 | // virtual_methods_ array again during initialization. |
| 5687 | if (has_defaults) { |
| 5688 | klass->SetHasDefaultMethods(); |
| 5689 | } |
| 5690 | return true; |
| 5691 | } else if (klass->HasSuperClass()) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5692 | const size_t super_vtable_length = klass->GetSuperClass()->GetVTableLength(); |
| 5693 | const size_t max_count = num_virtual_methods + super_vtable_length; |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5694 | StackHandleScope<2> hs(self); |
Mingyao Yang | 38eecb0 | 2014-08-13 14:51:03 -0700 | [diff] [blame] | 5695 | Handle<mirror::Class> super_class(hs.NewHandle(klass->GetSuperClass())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5696 | MutableHandle<mirror::PointerArray> vtable; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5697 | if (super_class->ShouldHaveEmbeddedVTable()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5698 | vtable = hs.NewHandle(AllocPointerArray(self, max_count)); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5699 | if (UNLIKELY(vtable.Get() == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5700 | self->AssertPendingOOMException(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5701 | return false; |
| 5702 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5703 | for (size_t i = 0; i < super_vtable_length; i++) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5704 | vtable->SetElementPtrSize( |
| 5705 | i, super_class->GetEmbeddedVTableEntry(i, image_pointer_size_), image_pointer_size_); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5706 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5707 | // We might need to change vtable if we have new virtual methods or new interfaces (since that |
| 5708 | // might give us new default methods). If no new interfaces then we can skip the rest since |
| 5709 | // the class cannot override any of the super-class's methods. This is required for |
| 5710 | // correctness since without it we might not update overridden default method vtable entries |
| 5711 | // correctly. |
| 5712 | if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5713 | klass->SetVTable(vtable.Get()); |
| 5714 | return true; |
| 5715 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5716 | } else { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5717 | DCHECK(super_class->IsAbstract() && !super_class->IsArrayClass()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5718 | auto* super_vtable = super_class->GetVTable(); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5719 | CHECK(super_vtable != nullptr) << PrettyClass(super_class.Get()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5720 | // We might need to change vtable if we have new virtual methods or new interfaces (since that |
| 5721 | // might give us new default methods). See comment above. |
| 5722 | if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5723 | klass->SetVTable(super_vtable); |
| 5724 | return true; |
| 5725 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5726 | vtable = hs.NewHandle(down_cast<mirror::PointerArray*>( |
| 5727 | super_vtable->CopyOf(self, max_count))); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5728 | if (UNLIKELY(vtable.Get() == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5729 | self->AssertPendingOOMException(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5730 | return false; |
| 5731 | } |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5732 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5733 | // How the algorithm works: |
| 5734 | // 1. Populate hash table by adding num_virtual_methods from klass. The values in the hash |
| 5735 | // table are: invalid_index for unused slots, index super_vtable_length + i for a virtual |
| 5736 | // method which has not been matched to a vtable method, and j if the virtual method at the |
| 5737 | // index overrode the super virtual method at index j. |
| 5738 | // 2. Loop through super virtual methods, if they overwrite, update hash table to j |
| 5739 | // (j < super_vtable_length) to avoid redundant checks. (TODO maybe use this info for reducing |
| 5740 | // the need for the initial vtable which we later shrink back down). |
| 5741 | // 3. Add non overridden methods to the end of the vtable. |
| 5742 | static constexpr size_t kMaxStackHash = 250; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5743 | // + 1 so that even if we only have new default methods we will still be able to use this hash |
| 5744 | // table (i.e. it will never have 0 size). |
| 5745 | const size_t hash_table_size = num_virtual_methods * 3 + 1; |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5746 | uint32_t* hash_table_ptr; |
| 5747 | std::unique_ptr<uint32_t[]> hash_heap_storage; |
| 5748 | if (hash_table_size <= kMaxStackHash) { |
| 5749 | hash_table_ptr = reinterpret_cast<uint32_t*>( |
| 5750 | alloca(hash_table_size * sizeof(*hash_table_ptr))); |
| 5751 | } else { |
| 5752 | hash_heap_storage.reset(new uint32_t[hash_table_size]); |
| 5753 | hash_table_ptr = hash_heap_storage.get(); |
| 5754 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5755 | LinkVirtualHashTable hash_table(klass, hash_table_size, hash_table_ptr, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5756 | // Add virtual methods to the hash table. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5757 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5758 | DCHECK(klass->GetVirtualMethodDuringLinking( |
| 5759 | i, image_pointer_size_)->GetDeclaringClass() != nullptr); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5760 | hash_table.Add(i); |
| 5761 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5762 | // Loop through each super vtable method and see if they are overridden by a method we added to |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5763 | // the hash table. |
| 5764 | for (size_t j = 0; j < super_vtable_length; ++j) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5765 | // Search the hash table to see if we are overridden by any method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5766 | ArtMethod* super_method = vtable->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5767 | MethodNameAndSignatureComparator super_method_name_comparator( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5768 | super_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5769 | uint32_t hash_index = hash_table.FindAndRemove(&super_method_name_comparator); |
| 5770 | if (hash_index != hash_table.GetNotFoundIndex()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5771 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking( |
| 5772 | hash_index, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5773 | if (klass->CanAccessMember(super_method->GetDeclaringClass(), |
| 5774 | super_method->GetAccessFlags())) { |
| 5775 | if (super_method->IsFinal()) { |
| 5776 | ThrowLinkageError(klass.Get(), "Method %s overrides final method in class %s", |
| 5777 | PrettyMethod(virtual_method).c_str(), |
| 5778 | super_method->GetDeclaringClassDescriptor()); |
| 5779 | return false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5780 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5781 | vtable->SetElementPtrSize(j, virtual_method, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5782 | virtual_method->SetMethodIndex(j); |
| 5783 | } else { |
| 5784 | LOG(WARNING) << "Before Android 4.1, method " << PrettyMethod(virtual_method) |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5785 | << " would have incorrectly overridden the package-private method in " |
| 5786 | << PrettyDescriptor(super_method->GetDeclaringClassDescriptor()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5787 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5788 | } else if (super_method->IsOverridableByDefaultMethod()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5789 | // We didn't directly override this method but we might through default methods... |
| 5790 | // Check for default method update. |
| 5791 | ArtMethod* default_method = nullptr; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5792 | switch (FindDefaultMethodImplementation(self, |
| 5793 | super_method, |
| 5794 | klass, |
| 5795 | /*out*/&default_method)) { |
| 5796 | case DefaultMethodSearchResult::kDefaultConflict: { |
| 5797 | // A conflict was found looking for default methods. Note this (assuming it wasn't |
| 5798 | // pre-existing) in the translations map. |
| 5799 | if (UNLIKELY(!super_method->IsDefaultConflicting())) { |
| 5800 | // Don't generate another conflict method to reduce memory use as an optimization. |
| 5801 | default_translations->insert( |
| 5802 | {j, ClassLinker::MethodTranslation::CreateConflictingMethod()}); |
| 5803 | } |
| 5804 | break; |
| 5805 | } |
| 5806 | case DefaultMethodSearchResult::kAbstractFound: { |
| 5807 | // No conflict but method is abstract. |
| 5808 | // We note that this vtable entry must be made abstract. |
| 5809 | if (UNLIKELY(!super_method->IsAbstract())) { |
| 5810 | default_translations->insert( |
| 5811 | {j, ClassLinker::MethodTranslation::CreateAbstractMethod()}); |
| 5812 | } |
| 5813 | break; |
| 5814 | } |
| 5815 | case DefaultMethodSearchResult::kDefaultFound: { |
| 5816 | if (UNLIKELY(super_method->IsDefaultConflicting() || |
| 5817 | default_method->GetDeclaringClass() != super_method->GetDeclaringClass())) { |
| 5818 | // Found a default method implementation that is new. |
| 5819 | // TODO Refactor this add default methods to virtuals here and not in |
| 5820 | // LinkInterfaceMethods maybe. |
| 5821 | // The problem is default methods might override previously present |
| 5822 | // default-method or miranda-method vtable entries from the superclass. |
| 5823 | // Unfortunately we need these to be entries in this class's virtuals. We do not |
| 5824 | // give these entries there until LinkInterfaceMethods so we pass this map around |
| 5825 | // to let it know which vtable entries need to be updated. |
| 5826 | // Make a note that vtable entry j must be updated, store what it needs to be updated |
| 5827 | // to. We will allocate a virtual method slot in LinkInterfaceMethods and fix it up |
| 5828 | // then. |
| 5829 | default_translations->insert( |
| 5830 | {j, ClassLinker::MethodTranslation::CreateTranslatedMethod(default_method)}); |
| 5831 | VLOG(class_linker) << "Method " << PrettyMethod(super_method) |
| 5832 | << " overridden by default " << PrettyMethod(default_method) |
| 5833 | << " in " << PrettyClass(klass.Get()); |
| 5834 | } |
| 5835 | break; |
| 5836 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5837 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5838 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5839 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5840 | size_t actual_count = super_vtable_length; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5841 | // Add the non-overridden methods at the end. |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5842 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5843 | ArtMethod* local_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5844 | size_t method_idx = local_method->GetMethodIndexDuringLinking(); |
| 5845 | if (method_idx < super_vtable_length && |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5846 | local_method == vtable->GetElementPtrSize<ArtMethod*>(method_idx, image_pointer_size_)) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5847 | continue; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5848 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5849 | vtable->SetElementPtrSize(actual_count, local_method, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5850 | local_method->SetMethodIndex(actual_count); |
| 5851 | ++actual_count; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5852 | } |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 5853 | if (!IsUint<16>(actual_count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5854 | ThrowClassFormatError(klass.Get(), "Too many methods defined on class: %zd", actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5855 | return false; |
| 5856 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5857 | // Shrink vtable if possible |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5858 | CHECK_LE(actual_count, max_count); |
| 5859 | if (actual_count < max_count) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5860 | vtable.Assign(down_cast<mirror::PointerArray*>(vtable->CopyOf(self, actual_count))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5861 | if (UNLIKELY(vtable.Get() == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5862 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5863 | return false; |
| 5864 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5865 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5866 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5867 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 5868 | CHECK_EQ(klass.Get(), GetClassRoot(kJavaLangObject)); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 5869 | if (!IsUint<16>(num_virtual_methods)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5870 | ThrowClassFormatError(klass.Get(), "Too many methods: %d", |
| 5871 | static_cast<int>(num_virtual_methods)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5872 | return false; |
| 5873 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5874 | auto* vtable = AllocPointerArray(self, num_virtual_methods); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5875 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5876 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5877 | return false; |
| 5878 | } |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 5879 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5880 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
| 5881 | vtable->SetElementPtrSize(i, virtual_method, image_pointer_size_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5882 | virtual_method->SetMethodIndex(i & 0xFFFF); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5883 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5884 | klass->SetVTable(vtable); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5885 | } |
| 5886 | return true; |
| 5887 | } |
| 5888 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5889 | // Determine if the given iface has any subinterface in the given list that declares the method |
| 5890 | // specified by 'target'. |
| 5891 | // |
| 5892 | // Arguments |
| 5893 | // - self: The thread we are running on |
| 5894 | // - target: A comparator that will match any method that overrides the method we are checking for |
| 5895 | // - iftable: The iftable we are searching for an overriding method on. |
| 5896 | // - ifstart: The index of the interface we are checking to see if anything overrides |
| 5897 | // - iface: The interface we are checking to see if anything overrides. |
| 5898 | // - image_pointer_size: |
| 5899 | // The image pointer size. |
| 5900 | // |
| 5901 | // Returns |
| 5902 | // - True: There is some method that matches the target comparator defined in an interface that |
| 5903 | // is a subtype of iface. |
| 5904 | // - False: There is no method that matches the target comparator in any interface that is a subtype |
| 5905 | // of iface. |
| 5906 | static bool ContainsOverridingMethodOf(Thread* self, |
| 5907 | MethodNameAndSignatureComparator& target, |
| 5908 | Handle<mirror::IfTable> iftable, |
| 5909 | size_t ifstart, |
| 5910 | Handle<mirror::Class> iface, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5911 | PointerSize image_pointer_size) |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5912 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 5913 | DCHECK(self != nullptr); |
| 5914 | DCHECK(iface.Get() != nullptr); |
| 5915 | DCHECK(iftable.Get() != nullptr); |
| 5916 | DCHECK_GE(ifstart, 0u); |
| 5917 | DCHECK_LT(ifstart, iftable->Count()); |
| 5918 | DCHECK_EQ(iface.Get(), iftable->GetInterface(ifstart)); |
| 5919 | DCHECK(iface->IsInterface()); |
| 5920 | |
| 5921 | size_t iftable_count = iftable->Count(); |
| 5922 | StackHandleScope<1> hs(self); |
| 5923 | MutableHandle<mirror::Class> current_iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 5924 | for (size_t k = ifstart + 1; k < iftable_count; k++) { |
| 5925 | // Skip ifstart since our current interface obviously cannot override itself. |
| 5926 | current_iface.Assign(iftable->GetInterface(k)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5927 | // Iterate through every method on this interface. The order does not matter. |
| 5928 | for (ArtMethod& current_method : current_iface->GetDeclaredVirtualMethods(image_pointer_size)) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5929 | if (UNLIKELY(target.HasSameNameAndSignature( |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5930 | current_method.GetInterfaceMethodIfProxy(image_pointer_size)))) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5931 | // Check if the i'th interface is a subtype of this one. |
| 5932 | if (iface->IsAssignableFrom(current_iface.Get())) { |
| 5933 | return true; |
| 5934 | } |
| 5935 | break; |
| 5936 | } |
| 5937 | } |
| 5938 | } |
| 5939 | return false; |
| 5940 | } |
| 5941 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5942 | // Find the default method implementation for 'interface_method' in 'klass'. Stores it into |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5943 | // out_default_method and returns kDefaultFound on success. If no default method was found return |
| 5944 | // kAbstractFound and store nullptr into out_default_method. If an error occurs (such as a |
| 5945 | // default_method conflict) it will return kDefaultConflict. |
| 5946 | ClassLinker::DefaultMethodSearchResult ClassLinker::FindDefaultMethodImplementation( |
| 5947 | Thread* self, |
| 5948 | ArtMethod* target_method, |
| 5949 | Handle<mirror::Class> klass, |
| 5950 | /*out*/ArtMethod** out_default_method) const { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5951 | DCHECK(self != nullptr); |
| 5952 | DCHECK(target_method != nullptr); |
| 5953 | DCHECK(out_default_method != nullptr); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5954 | |
| 5955 | *out_default_method = nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5956 | |
| 5957 | // We organize the interface table so that, for interface I any subinterfaces J follow it in the |
| 5958 | // table. This lets us walk the table backwards when searching for default methods. The first one |
| 5959 | // we encounter is the best candidate since it is the most specific. Once we have found it we keep |
| 5960 | // track of it and then continue checking all other interfaces, since we need to throw an error if |
| 5961 | // we encounter conflicting default method implementations (one is not a subtype of the other). |
| 5962 | // |
| 5963 | // The order of unrelated interfaces does not matter and is not defined. |
| 5964 | size_t iftable_count = klass->GetIfTableCount(); |
| 5965 | if (iftable_count == 0) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5966 | // No interfaces. We have already reset out to null so just return kAbstractFound. |
| 5967 | return DefaultMethodSearchResult::kAbstractFound; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5968 | } |
| 5969 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5970 | StackHandleScope<3> hs(self); |
| 5971 | MutableHandle<mirror::Class> chosen_iface(hs.NewHandle<mirror::Class>(nullptr)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5972 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable())); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5973 | MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5974 | MethodNameAndSignatureComparator target_name_comparator( |
| 5975 | target_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
| 5976 | // Iterates over the klass's iftable in reverse |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5977 | for (size_t k = iftable_count; k != 0; ) { |
| 5978 | --k; |
| 5979 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5980 | DCHECK_LT(k, iftable->Count()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5981 | |
| 5982 | iface.Assign(iftable->GetInterface(k)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5983 | // Iterate through every declared method on this interface. The order does not matter. |
| 5984 | for (auto& method_iter : iface->GetDeclaredVirtualMethods(image_pointer_size_)) { |
| 5985 | ArtMethod* current_method = &method_iter; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5986 | // Skip abstract methods and methods with different names. |
| 5987 | if (current_method->IsAbstract() || |
| 5988 | !target_name_comparator.HasSameNameAndSignature( |
| 5989 | current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
| 5990 | continue; |
Alex Light | d7c10c2 | 2016-03-31 10:03:07 -0700 | [diff] [blame] | 5991 | } else if (!current_method->IsPublic()) { |
| 5992 | // The verifier should have caught the non-public method for dex version 37. Just warn and |
| 5993 | // skip it since this is from before default-methods so we don't really need to care that it |
| 5994 | // has code. |
| 5995 | LOG(WARNING) << "Interface method " << PrettyMethod(current_method) << " is not public! " |
| 5996 | << "This will be a fatal error in subsequent versions of android. " |
| 5997 | << "Continuing anyway."; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5998 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5999 | if (UNLIKELY(chosen_iface.Get() != nullptr)) { |
| 6000 | // We have multiple default impls of the same method. This is a potential default conflict. |
| 6001 | // We need to check if this possibly conflicting method is either a superclass of the chosen |
| 6002 | // default implementation or is overridden by a non-default interface method. In either case |
| 6003 | // there is no conflict. |
| 6004 | if (!iface->IsAssignableFrom(chosen_iface.Get()) && |
| 6005 | !ContainsOverridingMethodOf(self, |
| 6006 | target_name_comparator, |
| 6007 | iftable, |
| 6008 | k, |
| 6009 | iface, |
| 6010 | image_pointer_size_)) { |
Nicolas Geoffray | 7f3e0db | 2016-01-28 09:29:31 +0000 | [diff] [blame] | 6011 | VLOG(class_linker) << "Conflicting default method implementations found: " |
| 6012 | << PrettyMethod(current_method) << " and " |
| 6013 | << PrettyMethod(*out_default_method) << " in class " |
| 6014 | << PrettyClass(klass.Get()) << " conflict."; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6015 | *out_default_method = nullptr; |
| 6016 | return DefaultMethodSearchResult::kDefaultConflict; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6017 | } else { |
| 6018 | break; // Continue checking at the next interface. |
| 6019 | } |
| 6020 | } else { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6021 | // chosen_iface == null |
| 6022 | if (!ContainsOverridingMethodOf(self, |
| 6023 | target_name_comparator, |
| 6024 | iftable, |
| 6025 | k, |
| 6026 | iface, |
| 6027 | image_pointer_size_)) { |
| 6028 | // Don't set this as the chosen interface if something else is overriding it (because that |
| 6029 | // other interface would be potentially chosen instead if it was default). If the other |
| 6030 | // interface was abstract then we wouldn't select this interface as chosen anyway since |
| 6031 | // the abstract method masks it. |
| 6032 | *out_default_method = current_method; |
| 6033 | chosen_iface.Assign(iface.Get()); |
| 6034 | // We should now finish traversing the graph to find if we have default methods that |
| 6035 | // conflict. |
| 6036 | } else { |
| 6037 | VLOG(class_linker) << "A default method '" << PrettyMethod(current_method) << "' was " |
| 6038 | << "skipped because it was overridden by an abstract method in a " |
| 6039 | << "subinterface on class '" << PrettyClass(klass.Get()) << "'"; |
| 6040 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6041 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6042 | break; |
| 6043 | } |
| 6044 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6045 | if (*out_default_method != nullptr) { |
| 6046 | VLOG(class_linker) << "Default method '" << PrettyMethod(*out_default_method) << "' selected " |
| 6047 | << "as the implementation for '" << PrettyMethod(target_method) << "' " |
| 6048 | << "in '" << PrettyClass(klass.Get()) << "'"; |
| 6049 | return DefaultMethodSearchResult::kDefaultFound; |
| 6050 | } else { |
| 6051 | return DefaultMethodSearchResult::kAbstractFound; |
| 6052 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6053 | } |
| 6054 | |
Mathieu Chartier | 7f98c9a | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6055 | ArtMethod* ClassLinker::AddMethodToConflictTable(mirror::Class* klass, |
| 6056 | ArtMethod* conflict_method, |
| 6057 | ArtMethod* interface_method, |
| 6058 | ArtMethod* method, |
| 6059 | bool force_new_conflict_method) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6060 | ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize); |
Mathieu Chartier | 7f98c9a | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6061 | Runtime* const runtime = Runtime::Current(); |
| 6062 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
| 6063 | bool new_entry = conflict_method == runtime->GetImtConflictMethod() || force_new_conflict_method; |
| 6064 | |
| 6065 | // Create a new entry if the existing one is the shared conflict method. |
| 6066 | ArtMethod* new_conflict_method = new_entry |
| 6067 | ? runtime->CreateImtConflictMethod(linear_alloc) |
| 6068 | : conflict_method; |
| 6069 | |
| 6070 | // Allocate a new table. Note that we will leak this table at the next conflict, |
| 6071 | // but that's a tradeoff compared to making the table fixed size. |
| 6072 | void* data = linear_alloc->Alloc( |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6073 | Thread::Current(), ImtConflictTable::ComputeSizeWithOneMoreEntry(current_table, |
| 6074 | image_pointer_size_)); |
Mathieu Chartier | 7f98c9a | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6075 | if (data == nullptr) { |
| 6076 | LOG(ERROR) << "Failed to allocate conflict table"; |
| 6077 | return conflict_method; |
| 6078 | } |
| 6079 | ImtConflictTable* new_table = new (data) ImtConflictTable(current_table, |
| 6080 | interface_method, |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6081 | method, |
| 6082 | image_pointer_size_); |
Mathieu Chartier | 7f98c9a | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6083 | |
| 6084 | // Do a fence to ensure threads see the data in the table before it is assigned |
| 6085 | // to the conflict method. |
| 6086 | // Note that there is a race in the presence of multiple threads and we may leak |
| 6087 | // memory from the LinearAlloc, but that's a tradeoff compared to using |
| 6088 | // atomic operations. |
| 6089 | QuasiAtomic::ThreadFenceRelease(); |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6090 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
Mathieu Chartier | 7f98c9a | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6091 | return new_conflict_method; |
| 6092 | } |
| 6093 | |
| 6094 | void ClassLinker::SetIMTRef(ArtMethod* unimplemented_method, |
| 6095 | ArtMethod* imt_conflict_method, |
| 6096 | ArtMethod* current_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6097 | /*out*/bool* new_conflict, |
Mathieu Chartier | 7f98c9a | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6098 | /*out*/ArtMethod** imt_ref) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6099 | // Place method in imt if entry is empty, place conflict otherwise. |
| 6100 | if (*imt_ref == unimplemented_method) { |
| 6101 | *imt_ref = current_method; |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6102 | } else if (!(*imt_ref)->IsRuntimeMethod()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6103 | // If we are not a conflict and we have the same signature and name as the imt |
| 6104 | // entry, it must be that we overwrote a superclass vtable entry. |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6105 | // Note that we have checked IsRuntimeMethod, as there may be multiple different |
| 6106 | // conflict methods. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6107 | MethodNameAndSignatureComparator imt_comparator( |
Mathieu Chartier | 7f98c9a | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6108 | (*imt_ref)->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6109 | if (imt_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 7f98c9a | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6110 | current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6111 | *imt_ref = current_method; |
| 6112 | } else { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6113 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6114 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6115 | } |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6116 | } else { |
| 6117 | // Place the default conflict method. Note that there may be an existing conflict |
| 6118 | // method in the IMT, but it could be one tailored to the super class, with a |
| 6119 | // specific ImtConflictTable. |
| 6120 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6121 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6122 | } |
| 6123 | } |
| 6124 | |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6125 | void ClassLinker::FillIMTAndConflictTables(mirror::Class* klass) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6126 | DCHECK(klass->ShouldHaveImt()) << PrettyClass(klass); |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6127 | DCHECK(!klass->IsTemp()) << PrettyClass(klass); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6128 | ArtMethod* imt_data[ImTable::kSize]; |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6129 | Runtime* const runtime = Runtime::Current(); |
| 6130 | ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod(); |
| 6131 | ArtMethod* const conflict_method = runtime->GetImtConflictMethod(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6132 | std::fill_n(imt_data, arraysize(imt_data), unimplemented_method); |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6133 | if (klass->GetIfTable() != nullptr) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6134 | bool new_conflict = false; |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6135 | FillIMTFromIfTable(klass->GetIfTable(), |
| 6136 | unimplemented_method, |
| 6137 | conflict_method, |
| 6138 | klass, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6139 | /*create_conflict_tables*/true, |
| 6140 | /*ignore_copied_methods*/false, |
| 6141 | &new_conflict, |
| 6142 | &imt_data[0]); |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6143 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6144 | if (!klass->ShouldHaveImt()) { |
| 6145 | return; |
| 6146 | } |
| 6147 | // Compare the IMT with the super class including the conflict methods. If they are equivalent, |
| 6148 | // we can just use the same pointer. |
| 6149 | ImTable* imt = nullptr; |
| 6150 | mirror::Class* super_class = klass->GetSuperClass(); |
| 6151 | if (super_class != nullptr && super_class->ShouldHaveImt()) { |
| 6152 | ImTable* super_imt = super_class->GetImt(image_pointer_size_); |
| 6153 | bool same = true; |
| 6154 | for (size_t i = 0; same && i < ImTable::kSize; ++i) { |
| 6155 | ArtMethod* method = imt_data[i]; |
| 6156 | ArtMethod* super_method = super_imt->Get(i, image_pointer_size_); |
| 6157 | if (method != super_method) { |
| 6158 | bool is_conflict_table = method->IsRuntimeMethod() && |
| 6159 | method != unimplemented_method && |
| 6160 | method != conflict_method; |
| 6161 | // Verify conflict contents. |
| 6162 | bool super_conflict_table = super_method->IsRuntimeMethod() && |
| 6163 | super_method != unimplemented_method && |
| 6164 | super_method != conflict_method; |
| 6165 | if (!is_conflict_table || !super_conflict_table) { |
| 6166 | same = false; |
| 6167 | } else { |
| 6168 | ImtConflictTable* table1 = method->GetImtConflictTable(image_pointer_size_); |
| 6169 | ImtConflictTable* table2 = super_method->GetImtConflictTable(image_pointer_size_); |
| 6170 | same = same && table1->Equals(table2, image_pointer_size_); |
| 6171 | } |
| 6172 | } |
| 6173 | } |
| 6174 | if (same) { |
| 6175 | imt = super_imt; |
| 6176 | } |
| 6177 | } |
| 6178 | if (imt == nullptr) { |
| 6179 | imt = klass->GetImt(image_pointer_size_); |
| 6180 | DCHECK(imt != nullptr); |
| 6181 | imt->Populate(imt_data, image_pointer_size_); |
| 6182 | } else { |
| 6183 | klass->SetImt(imt, image_pointer_size_); |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6184 | } |
| 6185 | } |
| 6186 | |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6187 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, |
| 6188 | LinearAlloc* linear_alloc, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6189 | PointerSize image_pointer_size) { |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6190 | void* data = linear_alloc->Alloc(Thread::Current(), |
| 6191 | ImtConflictTable::ComputeSize(count, |
| 6192 | image_pointer_size)); |
| 6193 | return (data != nullptr) ? new (data) ImtConflictTable(count, image_pointer_size) : nullptr; |
| 6194 | } |
| 6195 | |
| 6196 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, LinearAlloc* linear_alloc) { |
| 6197 | return CreateImtConflictTable(count, linear_alloc, image_pointer_size_); |
| 6198 | } |
| 6199 | |
| 6200 | void ClassLinker::FillIMTFromIfTable(mirror::IfTable* if_table, |
| 6201 | ArtMethod* unimplemented_method, |
| 6202 | ArtMethod* imt_conflict_method, |
| 6203 | mirror::Class* klass, |
| 6204 | bool create_conflict_tables, |
| 6205 | bool ignore_copied_methods, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6206 | /*out*/bool* new_conflict, |
| 6207 | /*out*/ArtMethod** imt) { |
| 6208 | uint32_t conflict_counts[ImTable::kSize] = {}; |
Mathieu Chartier | 7f98c9a | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6209 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
| 6210 | mirror::Class* interface = if_table->GetInterface(i); |
| 6211 | const size_t num_virtuals = interface->NumVirtualMethods(); |
| 6212 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 6213 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 6214 | DCHECK_GE(num_virtuals, method_array_count); |
| 6215 | if (kIsDebugBuild) { |
| 6216 | if (klass->IsInterface()) { |
| 6217 | DCHECK_EQ(method_array_count, 0u); |
| 6218 | } else { |
| 6219 | DCHECK_EQ(interface->NumDeclaredVirtualMethods(), method_array_count); |
| 6220 | } |
| 6221 | } |
| 6222 | if (method_array_count == 0) { |
| 6223 | continue; |
| 6224 | } |
| 6225 | auto* method_array = if_table->GetMethodArray(i); |
| 6226 | for (size_t j = 0; j < method_array_count; ++j) { |
| 6227 | ArtMethod* implementation_method = |
| 6228 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 6229 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 6230 | continue; |
| 6231 | } |
| 6232 | DCHECK(implementation_method != nullptr); |
| 6233 | // Miranda methods cannot be used to implement an interface method, but they are safe to put |
| 6234 | // in the IMT since their entrypoint is the interface trampoline. If we put any copied methods |
| 6235 | // or interface methods in the IMT here they will not create extra conflicts since we compare |
| 6236 | // names and signatures in SetIMTRef. |
| 6237 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
Matthew Gharrity | 465ecc8 | 2016-07-19 21:32:52 +0000 | [diff] [blame] | 6238 | const uint32_t imt_index = interface_method->GetImtIndex(); |
Mathieu Chartier | 7f98c9a | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6239 | |
| 6240 | // There is only any conflicts if all of the interface methods for an IMT slot don't have |
| 6241 | // the same implementation method, keep track of this to avoid creating a conflict table in |
| 6242 | // this case. |
| 6243 | |
| 6244 | // Conflict table size for each IMT slot. |
| 6245 | ++conflict_counts[imt_index]; |
| 6246 | |
| 6247 | SetIMTRef(unimplemented_method, |
| 6248 | imt_conflict_method, |
| 6249 | implementation_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6250 | /*out*/new_conflict, |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6251 | /*out*/&imt[imt_index]); |
Mathieu Chartier | 7f98c9a | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6252 | } |
| 6253 | } |
| 6254 | |
| 6255 | if (create_conflict_tables) { |
| 6256 | // Create the conflict tables. |
| 6257 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6258 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
Mathieu Chartier | 7f98c9a | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6259 | size_t conflicts = conflict_counts[i]; |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6260 | if (imt[i] == imt_conflict_method) { |
| 6261 | ImtConflictTable* new_table = CreateImtConflictTable(conflicts, linear_alloc); |
| 6262 | if (new_table != nullptr) { |
| 6263 | ArtMethod* new_conflict_method = |
| 6264 | Runtime::Current()->CreateImtConflictMethod(linear_alloc); |
| 6265 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
| 6266 | imt[i] = new_conflict_method; |
Mathieu Chartier | 7f98c9a | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6267 | } else { |
| 6268 | LOG(ERROR) << "Failed to allocate conflict table"; |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6269 | imt[i] = imt_conflict_method; |
Mathieu Chartier | 7f98c9a | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6270 | } |
| 6271 | } else { |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6272 | DCHECK_NE(imt[i], imt_conflict_method); |
Mathieu Chartier | 7f98c9a | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6273 | } |
| 6274 | } |
| 6275 | |
Mathieu Chartier | 7f98c9a | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6276 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
| 6277 | mirror::Class* interface = if_table->GetInterface(i); |
| 6278 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 6279 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 6280 | if (method_array_count == 0) { |
| 6281 | continue; |
| 6282 | } |
| 6283 | auto* method_array = if_table->GetMethodArray(i); |
| 6284 | for (size_t j = 0; j < method_array_count; ++j) { |
| 6285 | ArtMethod* implementation_method = |
| 6286 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 6287 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 6288 | continue; |
| 6289 | } |
| 6290 | DCHECK(implementation_method != nullptr); |
| 6291 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
Matthew Gharrity | 465ecc8 | 2016-07-19 21:32:52 +0000 | [diff] [blame] | 6292 | const uint32_t imt_index = interface_method->GetImtIndex(); |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6293 | if (!imt[imt_index]->IsRuntimeMethod() || |
| 6294 | imt[imt_index] == unimplemented_method || |
| 6295 | imt[imt_index] == imt_conflict_method) { |
| 6296 | continue; |
Mathieu Chartier | 7f98c9a | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6297 | } |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6298 | ImtConflictTable* table = imt[imt_index]->GetImtConflictTable(image_pointer_size_); |
| 6299 | const size_t num_entries = table->NumEntries(image_pointer_size_); |
| 6300 | table->SetInterfaceMethod(num_entries, image_pointer_size_, interface_method); |
| 6301 | table->SetImplementationMethod(num_entries, image_pointer_size_, implementation_method); |
Mathieu Chartier | 7f98c9a | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6302 | } |
| 6303 | } |
| 6304 | } |
| 6305 | } |
| 6306 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6307 | // Simple helper function that checks that no subtypes of 'val' are contained within the 'classes' |
| 6308 | // set. |
| 6309 | static bool NotSubinterfaceOfAny(const std::unordered_set<mirror::Class*>& classes, |
| 6310 | mirror::Class* val) |
| 6311 | REQUIRES(Roles::uninterruptible_) |
| 6312 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 6313 | DCHECK(val != nullptr); |
| 6314 | for (auto c : classes) { |
| 6315 | if (val->IsAssignableFrom(&*c)) { |
| 6316 | return false; |
| 6317 | } |
| 6318 | } |
| 6319 | return true; |
| 6320 | } |
| 6321 | |
| 6322 | // Fills in and flattens the interface inheritance hierarchy. |
| 6323 | // |
| 6324 | // By the end of this function all interfaces in the transitive closure of to_process are added to |
| 6325 | // the iftable and every interface precedes all of its sub-interfaces in this list. |
| 6326 | // |
| 6327 | // all I, J: Interface | I <: J implies J precedes I |
| 6328 | // |
| 6329 | // (note A <: B means that A is a subtype of B) |
| 6330 | // |
| 6331 | // This returns the total number of items in the iftable. The iftable might be resized down after |
| 6332 | // this call. |
| 6333 | // |
| 6334 | // We order this backwards so that we do not need to reorder superclass interfaces when new |
| 6335 | // interfaces are added in subclass's interface tables. |
| 6336 | // |
| 6337 | // Upon entry into this function iftable is a copy of the superclass's iftable with the first |
| 6338 | // super_ifcount entries filled in with the transitive closure of the interfaces of the superclass. |
| 6339 | // The other entries are uninitialized. We will fill in the remaining entries in this function. The |
| 6340 | // iftable must be large enough to hold all interfaces without changing its size. |
| 6341 | static size_t FillIfTable(mirror::IfTable* iftable, |
| 6342 | size_t super_ifcount, |
| 6343 | std::vector<mirror::Class*> to_process) |
| 6344 | REQUIRES(Roles::uninterruptible_) |
| 6345 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 6346 | // This is the set of all class's already in the iftable. Used to make checking if a class has |
| 6347 | // already been added quicker. |
| 6348 | std::unordered_set<mirror::Class*> classes_in_iftable; |
| 6349 | // The first super_ifcount elements are from the superclass. We note that they are already added. |
| 6350 | for (size_t i = 0; i < super_ifcount; i++) { |
| 6351 | mirror::Class* iface = iftable->GetInterface(i); |
| 6352 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, iface)) << "Bad ordering."; |
| 6353 | classes_in_iftable.insert(iface); |
| 6354 | } |
| 6355 | size_t filled_ifcount = super_ifcount; |
| 6356 | for (mirror::Class* interface : to_process) { |
| 6357 | // Let us call the first filled_ifcount elements of iftable the current-iface-list. |
| 6358 | // At this point in the loop current-iface-list has the invariant that: |
| 6359 | // for every pair of interfaces I,J within it: |
| 6360 | // if index_of(I) < index_of(J) then I is not a subtype of J |
| 6361 | |
| 6362 | // If we have already seen this element then all of its super-interfaces must already be in the |
| 6363 | // current-iface-list so we can skip adding it. |
| 6364 | if (!ContainsElement(classes_in_iftable, interface)) { |
| 6365 | // We haven't seen this interface so add all of its super-interfaces onto the |
| 6366 | // current-iface-list, skipping those already on it. |
| 6367 | int32_t ifcount = interface->GetIfTableCount(); |
| 6368 | for (int32_t j = 0; j < ifcount; j++) { |
| 6369 | mirror::Class* super_interface = interface->GetIfTable()->GetInterface(j); |
| 6370 | if (!ContainsElement(classes_in_iftable, super_interface)) { |
| 6371 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, super_interface)) << "Bad ordering."; |
| 6372 | classes_in_iftable.insert(super_interface); |
| 6373 | iftable->SetInterface(filled_ifcount, super_interface); |
| 6374 | filled_ifcount++; |
| 6375 | } |
| 6376 | } |
| 6377 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, interface)) << "Bad ordering"; |
| 6378 | // Place this interface onto the current-iface-list after all of its super-interfaces. |
| 6379 | classes_in_iftable.insert(interface); |
| 6380 | iftable->SetInterface(filled_ifcount, interface); |
| 6381 | filled_ifcount++; |
| 6382 | } else if (kIsDebugBuild) { |
| 6383 | // Check all super-interfaces are already in the list. |
| 6384 | int32_t ifcount = interface->GetIfTableCount(); |
| 6385 | for (int32_t j = 0; j < ifcount; j++) { |
| 6386 | mirror::Class* super_interface = interface->GetIfTable()->GetInterface(j); |
| 6387 | DCHECK(ContainsElement(classes_in_iftable, super_interface)) |
| 6388 | << "Iftable does not contain " << PrettyClass(super_interface) |
| 6389 | << ", a superinterface of " << PrettyClass(interface); |
| 6390 | } |
| 6391 | } |
| 6392 | } |
| 6393 | if (kIsDebugBuild) { |
| 6394 | // Check that the iftable is ordered correctly. |
| 6395 | for (size_t i = 0; i < filled_ifcount; i++) { |
| 6396 | mirror::Class* if_a = iftable->GetInterface(i); |
| 6397 | for (size_t j = i + 1; j < filled_ifcount; j++) { |
| 6398 | mirror::Class* if_b = iftable->GetInterface(j); |
| 6399 | // !(if_a <: if_b) |
| 6400 | CHECK(!if_b->IsAssignableFrom(if_a)) |
| 6401 | << "Bad interface order: " << PrettyClass(if_a) << " (index " << i << ") extends " |
| 6402 | << PrettyClass(if_b) << " (index " << j << ") and so should be after it in the " |
| 6403 | << "interface list."; |
| 6404 | } |
| 6405 | } |
| 6406 | } |
| 6407 | return filled_ifcount; |
| 6408 | } |
| 6409 | |
| 6410 | bool ClassLinker::SetupInterfaceLookupTable(Thread* self, Handle<mirror::Class> klass, |
| 6411 | Handle<mirror::ObjectArray<mirror::Class>> interfaces) { |
| 6412 | StackHandleScope<1> hs(self); |
| 6413 | const size_t super_ifcount = |
| 6414 | klass->HasSuperClass() ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6415 | const bool have_interfaces = interfaces.Get() != nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6416 | const size_t num_interfaces = |
| 6417 | have_interfaces ? interfaces->GetLength() : klass->NumDirectInterfaces(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6418 | if (num_interfaces == 0) { |
| 6419 | if (super_ifcount == 0) { |
| 6420 | // Class implements no interfaces. |
| 6421 | DCHECK_EQ(klass->GetIfTableCount(), 0); |
| 6422 | DCHECK(klass->GetIfTable() == nullptr); |
| 6423 | return true; |
| 6424 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6425 | // Class implements same interfaces as parent, are any of these not marker interfaces? |
| 6426 | bool has_non_marker_interface = false; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 6427 | mirror::IfTable* super_iftable = klass->GetSuperClass()->GetIfTable(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6428 | for (size_t i = 0; i < super_ifcount; ++i) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6429 | if (super_iftable->GetMethodArrayCount(i) > 0) { |
| 6430 | has_non_marker_interface = true; |
| 6431 | break; |
| 6432 | } |
| 6433 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6434 | // Class just inherits marker interfaces from parent so recycle parent's iftable. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6435 | if (!has_non_marker_interface) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6436 | klass->SetIfTable(super_iftable); |
| 6437 | return true; |
| 6438 | } |
| 6439 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6440 | size_t ifcount = super_ifcount + num_interfaces; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6441 | // Check that every class being implemented is an interface. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6442 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6443 | mirror::Class* interface = have_interfaces |
| 6444 | ? interfaces->GetWithoutChecks(i) |
| 6445 | : mirror::Class::GetDirectInterface(self, klass, i); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6446 | DCHECK(interface != nullptr); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6447 | if (UNLIKELY(!interface->IsInterface())) { |
| 6448 | std::string temp; |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6449 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 6450 | "Class %s implements non-interface class %s", |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6451 | PrettyDescriptor(klass.Get()).c_str(), |
| 6452 | PrettyDescriptor(interface->GetDescriptor(&temp)).c_str()); |
| 6453 | return false; |
| 6454 | } |
| 6455 | ifcount += interface->GetIfTableCount(); |
| 6456 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6457 | // Create the interface function table. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 6458 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(AllocIfTable(self, ifcount))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6459 | if (UNLIKELY(iftable.Get() == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6460 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6461 | return false; |
| 6462 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6463 | // Fill in table with superclass's iftable. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6464 | if (super_ifcount != 0) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 6465 | mirror::IfTable* super_iftable = klass->GetSuperClass()->GetIfTable(); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 6466 | for (size_t i = 0; i < super_ifcount; i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 6467 | mirror::Class* super_interface = super_iftable->GetInterface(i); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6468 | iftable->SetInterface(i, super_interface); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 6469 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6470 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6471 | |
| 6472 | // Note that AllowThreadSuspension is to thread suspension as pthread_testcancel is to pthread |
| 6473 | // cancellation. That is it will suspend if one has a pending suspend request but otherwise |
| 6474 | // doesn't really do anything. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 6475 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6476 | |
| 6477 | size_t new_ifcount; |
| 6478 | { |
| 6479 | ScopedAssertNoThreadSuspension nts(self, "Copying mirror::Class*'s for FillIfTable"); |
| 6480 | std::vector<mirror::Class*> to_add; |
| 6481 | for (size_t i = 0; i < num_interfaces; i++) { |
| 6482 | mirror::Class* interface = have_interfaces ? interfaces->Get(i) : |
| 6483 | mirror::Class::GetDirectInterface(self, klass, i); |
| 6484 | to_add.push_back(interface); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6485 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6486 | |
| 6487 | new_ifcount = FillIfTable(iftable.Get(), super_ifcount, std::move(to_add)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6488 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6489 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 6490 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6491 | |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6492 | // Shrink iftable in case duplicates were found |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6493 | if (new_ifcount < ifcount) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6494 | DCHECK_NE(num_interfaces, 0U); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6495 | iftable.Assign(down_cast<mirror::IfTable*>( |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6496 | iftable->CopyOf(self, new_ifcount * mirror::IfTable::kMax))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6497 | if (UNLIKELY(iftable.Get() == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6498 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6499 | return false; |
| 6500 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6501 | ifcount = new_ifcount; |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6502 | } else { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6503 | DCHECK_EQ(new_ifcount, ifcount); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6504 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6505 | klass->SetIfTable(iftable.Get()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6506 | return true; |
| 6507 | } |
| 6508 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6509 | // Finds the method with a name/signature that matches cmp in the given list of methods. The list of |
| 6510 | // methods must be unique. |
| 6511 | static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp, |
| 6512 | const ScopedArenaVector<ArtMethod*>& list) |
| 6513 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 6514 | for (ArtMethod* method : list) { |
| 6515 | if (cmp.HasSameNameAndSignature(method)) { |
| 6516 | return method; |
| 6517 | } |
| 6518 | } |
| 6519 | return nullptr; |
| 6520 | } |
| 6521 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6522 | static void SanityCheckVTable(Handle<mirror::Class> klass, PointerSize pointer_size) |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6523 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 6524 | mirror::PointerArray* check_vtable = klass->GetVTableDuringLinking(); |
| 6525 | mirror::Class* superclass = (klass->HasSuperClass()) ? klass->GetSuperClass() : nullptr; |
| 6526 | int32_t super_vtable_length = (superclass != nullptr) ? superclass->GetVTableLength() : 0; |
| 6527 | for (int32_t i = 0; i < check_vtable->GetLength(); ++i) { |
| 6528 | ArtMethod* m = check_vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 6529 | CHECK(m != nullptr); |
| 6530 | |
| 6531 | ArraySlice<ArtMethod> virtuals = klass->GetVirtualMethodsSliceUnchecked(pointer_size); |
| 6532 | auto is_same_method = [m] (const ArtMethod& meth) { |
| 6533 | return &meth == m; |
| 6534 | }; |
| 6535 | CHECK((super_vtable_length > i && superclass->GetVTableEntry(i, pointer_size) == m) || |
| 6536 | std::find_if(virtuals.begin(), virtuals.end(), is_same_method) != virtuals.end()) |
| 6537 | << "While linking class '" << PrettyClass(klass.Get()) << "' unable to find owning class " |
| 6538 | << "of '" << PrettyMethod(m) << "' (vtable index: " << i << ")."; |
| 6539 | } |
| 6540 | } |
| 6541 | |
Mathieu Chartier | 7f98c9a | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6542 | void ClassLinker::FillImtFromSuperClass(Handle<mirror::Class> klass, |
| 6543 | ArtMethod* unimplemented_method, |
| 6544 | ArtMethod* imt_conflict_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6545 | bool* new_conflict, |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6546 | ArtMethod** imt) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6547 | DCHECK(klass->HasSuperClass()); |
| 6548 | mirror::Class* super_class = klass->GetSuperClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6549 | if (super_class->ShouldHaveImt()) { |
| 6550 | ImTable* super_imt = super_class->GetImt(image_pointer_size_); |
| 6551 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 6552 | imt[i] = super_imt->Get(i, image_pointer_size_); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6553 | } |
| 6554 | } else { |
| 6555 | // No imt in the super class, need to reconstruct from the iftable. |
| 6556 | mirror::IfTable* if_table = super_class->GetIfTable(); |
Mathieu Chartier | 7f98c9a | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6557 | if (if_table != nullptr) { |
| 6558 | // Ignore copied methods since we will handle these in LinkInterfaceMethods. |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6559 | FillIMTFromIfTable(if_table, |
| 6560 | unimplemented_method, |
| 6561 | imt_conflict_method, |
| 6562 | klass.Get(), |
| 6563 | /*create_conflict_table*/false, |
| 6564 | /*ignore_copied_methods*/true, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6565 | /*out*/new_conflict, |
Mathieu Chartier | 91288d8 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6566 | /*out*/imt); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6567 | } |
| 6568 | } |
| 6569 | } |
| 6570 | |
| 6571 | // TODO This method needs to be split up into several smaller methods. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6572 | bool ClassLinker::LinkInterfaceMethods( |
| 6573 | Thread* self, |
| 6574 | Handle<mirror::Class> klass, |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6575 | const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6576 | bool* out_new_conflict, |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6577 | ArtMethod** out_imt) { |
| 6578 | StackHandleScope<3> hs(self); |
| 6579 | Runtime* const runtime = Runtime::Current(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6580 | |
| 6581 | const bool is_interface = klass->IsInterface(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6582 | const bool has_superclass = klass->HasSuperClass(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6583 | const bool fill_tables = !is_interface; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6584 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
| 6585 | const size_t method_alignment = ArtMethod::Alignment(image_pointer_size_); |
| 6586 | const size_t method_size = ArtMethod::Size(image_pointer_size_); |
| 6587 | const size_t ifcount = klass->GetIfTableCount(); |
| 6588 | |
| 6589 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable())); |
| 6590 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6591 | // These are allocated on the heap to begin, we then transfer to linear alloc when we re-create |
| 6592 | // the virtual methods array. |
| 6593 | // Need to use low 4GB arenas for compiler or else the pointers wont fit in 32 bit method array |
| 6594 | // during cross compilation. |
| 6595 | // Use the linear alloc pool since this one is in the low 4gb for the compiler. |
| 6596 | ArenaStack stack(runtime->GetLinearAlloc()->GetArenaPool()); |
| 6597 | ScopedArenaAllocator allocator(&stack); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6598 | |
| 6599 | ScopedArenaVector<ArtMethod*> default_conflict_methods(allocator.Adapter()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6600 | ScopedArenaVector<ArtMethod*> miranda_methods(allocator.Adapter()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6601 | ScopedArenaVector<ArtMethod*> default_methods(allocator.Adapter()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6602 | |
| 6603 | MutableHandle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
| 6604 | ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod(); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6605 | ArtMethod* const imt_conflict_method = runtime->GetImtConflictMethod(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6606 | // Copy the IMT from the super class if possible. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6607 | const bool extend_super_iftable = has_superclass; |
| 6608 | if (has_superclass && fill_tables) { |
| 6609 | FillImtFromSuperClass(klass, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6610 | unimplemented_method, |
| 6611 | imt_conflict_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6612 | out_new_conflict, |
Mathieu Chartier | 7f98c9a | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6613 | out_imt); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6614 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6615 | // Allocate method arrays before since we don't want miss visiting miranda method roots due to |
| 6616 | // thread suspension. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6617 | if (fill_tables) { |
| 6618 | for (size_t i = 0; i < ifcount; ++i) { |
| 6619 | size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods(); |
| 6620 | if (num_methods > 0) { |
| 6621 | const bool is_super = i < super_ifcount; |
| 6622 | // This is an interface implemented by a super-class. Therefore we can just copy the method |
| 6623 | // array from the superclass. |
| 6624 | const bool super_interface = is_super && extend_super_iftable; |
| 6625 | mirror::PointerArray* method_array; |
| 6626 | if (super_interface) { |
| 6627 | mirror::IfTable* if_table = klass->GetSuperClass()->GetIfTable(); |
| 6628 | DCHECK(if_table != nullptr); |
| 6629 | DCHECK(if_table->GetMethodArray(i) != nullptr); |
| 6630 | // If we are working on a super interface, try extending the existing method array. |
| 6631 | method_array = down_cast<mirror::PointerArray*>(if_table->GetMethodArray(i)->Clone(self)); |
| 6632 | } else { |
| 6633 | method_array = AllocPointerArray(self, num_methods); |
| 6634 | } |
| 6635 | if (UNLIKELY(method_array == nullptr)) { |
| 6636 | self->AssertPendingOOMException(); |
| 6637 | return false; |
| 6638 | } |
| 6639 | iftable->SetMethodArray(i, method_array); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6640 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6641 | } |
| 6642 | } |
| 6643 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 6644 | auto* old_cause = self->StartAssertNoThreadSuspension( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6645 | "Copying ArtMethods for LinkInterfaceMethods"); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6646 | // Going in reverse to ensure that we will hit abstract methods that override defaults before the |
| 6647 | // defaults. This means we don't need to do any trickery when creating the Miranda methods, since |
| 6648 | // they will already be null. This has the additional benefit that the declarer of a miranda |
| 6649 | // method will actually declare an abstract method. |
| 6650 | for (size_t i = ifcount; i != 0; ) { |
| 6651 | --i; |
| 6652 | |
| 6653 | DCHECK_GE(i, 0u); |
| 6654 | DCHECK_LT(i, ifcount); |
| 6655 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6656 | size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6657 | if (num_methods > 0) { |
| 6658 | StackHandleScope<2> hs2(self); |
| 6659 | const bool is_super = i < super_ifcount; |
| 6660 | const bool super_interface = is_super && extend_super_iftable; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6661 | // We don't actually create or fill these tables for interfaces, we just copy some methods for |
| 6662 | // conflict methods. Just set this as nullptr in those cases. |
| 6663 | Handle<mirror::PointerArray> method_array(fill_tables |
| 6664 | ? hs2.NewHandle(iftable->GetMethodArray(i)) |
| 6665 | : hs2.NewHandle<mirror::PointerArray>(nullptr)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6666 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6667 | ArraySlice<ArtMethod> input_virtual_methods; |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 6668 | ScopedNullHandle<mirror::PointerArray> null_handle; |
| 6669 | Handle<mirror::PointerArray> input_vtable_array(null_handle); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6670 | int32_t input_array_length = 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6671 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6672 | // TODO Cleanup Needed: In the presence of default methods this optimization is rather dirty |
| 6673 | // and confusing. Default methods should always look through all the superclasses |
| 6674 | // because they are the last choice of an implementation. We get around this by looking |
| 6675 | // at the super-classes iftable methods (copied into method_array previously) when we are |
| 6676 | // looking for the implementation of a super-interface method but that is rather dirty. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6677 | bool using_virtuals; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6678 | if (super_interface || is_interface) { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6679 | // If we are overwriting a super class interface, try to only virtual methods instead of the |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6680 | // whole vtable. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6681 | using_virtuals = true; |
| 6682 | input_virtual_methods = klass->GetDeclaredMethodsSlice(image_pointer_size_); |
| 6683 | input_array_length = input_virtual_methods.size(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6684 | } else { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6685 | // For a new interface, however, we need the whole vtable in case a new |
| 6686 | // interface method is implemented in the whole superclass. |
| 6687 | using_virtuals = false; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6688 | DCHECK(vtable.Get() != nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6689 | input_vtable_array = vtable; |
| 6690 | input_array_length = input_vtable_array->GetLength(); |
| 6691 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6692 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6693 | // For each method in interface |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 6694 | for (size_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6695 | auto* interface_method = iftable->GetInterface(i)->GetVirtualMethod(j, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6696 | MethodNameAndSignatureComparator interface_name_comparator( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6697 | interface_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Matthew Gharrity | 465ecc8 | 2016-07-19 21:32:52 +0000 | [diff] [blame] | 6698 | uint32_t imt_index = interface_method->GetImtIndex(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6699 | ArtMethod** imt_ptr = &out_imt[imt_index]; |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6700 | // For each method listed in the interface's method list, find the |
| 6701 | // matching method in our class's method list. We want to favor the |
| 6702 | // subclass over the superclass, which just requires walking |
| 6703 | // back from the end of the vtable. (This only matters if the |
| 6704 | // superclass defines a private method and this class redefines |
| 6705 | // it -- otherwise it would use the same vtable slot. In .dex files |
| 6706 | // those don't end up in the virtual method table, so it shouldn't |
| 6707 | // matter which direction we go. We walk it backward anyway.) |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6708 | // |
| 6709 | // To find defaults we need to do the same but also go over interfaces. |
| 6710 | bool found_impl = false; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6711 | ArtMethod* vtable_impl = nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6712 | for (int32_t k = input_array_length - 1; k >= 0; --k) { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6713 | ArtMethod* vtable_method = using_virtuals ? |
| 6714 | &input_virtual_methods[k] : |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6715 | input_vtable_array->GetElementPtrSize<ArtMethod*>(k, image_pointer_size_); |
| 6716 | ArtMethod* vtable_method_for_name_comparison = |
| 6717 | vtable_method->GetInterfaceMethodIfProxy(image_pointer_size_); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6718 | if (interface_name_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6719 | vtable_method_for_name_comparison)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6720 | if (!vtable_method->IsAbstract() && !vtable_method->IsPublic()) { |
Mathieu Chartier | 4d122c1 | 2015-06-17 14:14:36 -0700 | [diff] [blame] | 6721 | // Must do EndAssertNoThreadSuspension before throw since the throw can cause |
| 6722 | // allocations. |
| 6723 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6724 | ThrowIllegalAccessError(klass.Get(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 6725 | "Method '%s' implementing interface method '%s' is not public", |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6726 | PrettyMethod(vtable_method).c_str(), PrettyMethod(interface_method).c_str()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6727 | return false; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6728 | } else if (UNLIKELY(vtable_method->IsOverridableByDefaultMethod())) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6729 | // We might have a newer, better, default method for this, so we just skip it. If we |
| 6730 | // are still using this we will select it again when scanning for default methods. To |
| 6731 | // obviate the need to copy the method again we will make a note that we already found |
| 6732 | // a default here. |
| 6733 | // TODO This should be much cleaner. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6734 | vtable_impl = vtable_method; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6735 | break; |
| 6736 | } else { |
| 6737 | found_impl = true; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6738 | if (LIKELY(fill_tables)) { |
| 6739 | method_array->SetElementPtrSize(j, vtable_method, image_pointer_size_); |
| 6740 | // Place method in imt if entry is empty, place conflict otherwise. |
| 6741 | SetIMTRef(unimplemented_method, |
| 6742 | imt_conflict_method, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6743 | vtable_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6744 | /*out*/out_new_conflict, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6745 | /*out*/imt_ptr); |
| 6746 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6747 | break; |
| 6748 | } |
| 6749 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6750 | } |
| 6751 | // Continue on to the next method if we are done. |
| 6752 | if (LIKELY(found_impl)) { |
| 6753 | continue; |
| 6754 | } else if (LIKELY(super_interface)) { |
| 6755 | // Don't look for a default implementation when the super-method is implemented directly |
| 6756 | // by the class. |
| 6757 | // |
| 6758 | // See if we can use the superclasses method and skip searching everything else. |
| 6759 | // Note: !found_impl && super_interface |
| 6760 | CHECK(extend_super_iftable); |
| 6761 | // If this is a super_interface method it is possible we shouldn't override it because a |
| 6762 | // superclass could have implemented it directly. We get the method the superclass used |
| 6763 | // to implement this to know if we can override it with a default method. Doing this is |
| 6764 | // safe since we know that the super_iftable is filled in so we can simply pull it from |
| 6765 | // there. We don't bother if this is not a super-classes interface since in that case we |
| 6766 | // have scanned the entire vtable anyway and would have found it. |
| 6767 | // TODO This is rather dirty but it is faster than searching through the entire vtable |
| 6768 | // every time. |
| 6769 | ArtMethod* supers_method = |
| 6770 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 6771 | DCHECK(supers_method != nullptr); |
| 6772 | DCHECK(interface_name_comparator.HasSameNameAndSignature(supers_method)); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6773 | if (LIKELY(!supers_method->IsOverridableByDefaultMethod())) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6774 | // The method is not overridable by a default method (i.e. it is directly implemented |
| 6775 | // in some class). Therefore move onto the next interface method. |
| 6776 | continue; |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 6777 | } else { |
| 6778 | // If the super-classes method is override-able by a default method we need to keep |
| 6779 | // track of it since though it is override-able it is not guaranteed to be 'overridden'. |
| 6780 | // If it turns out not to be overridden and we did not keep track of it we might add it |
Alex Light | 66630be | 2016-05-04 09:23:09 -0700 | [diff] [blame] | 6781 | // to the vtable twice, causing corruption (vtable entries having inconsistent and |
| 6782 | // illegal states, incorrect vtable size, and incorrect or inconsistent iftable entries) |
| 6783 | // in this class and any subclasses. |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 6784 | DCHECK(vtable_impl == nullptr || vtable_impl == supers_method) |
| 6785 | << "vtable_impl was " << PrettyMethod(vtable_impl) << " and not 'nullptr' or " |
| 6786 | << PrettyMethod(supers_method) << " as expected. IFTable appears to be corrupt!"; |
| 6787 | vtable_impl = supers_method; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6788 | } |
| 6789 | } |
| 6790 | // If we haven't found it yet we should search through the interfaces for default methods. |
| 6791 | ArtMethod* current_method = nullptr; |
| 6792 | switch (FindDefaultMethodImplementation(self, |
| 6793 | interface_method, |
| 6794 | klass, |
| 6795 | /*out*/¤t_method)) { |
| 6796 | case DefaultMethodSearchResult::kDefaultConflict: { |
| 6797 | // Default method conflict. |
| 6798 | DCHECK(current_method == nullptr); |
| 6799 | ArtMethod* default_conflict_method = nullptr; |
| 6800 | if (vtable_impl != nullptr && vtable_impl->IsDefaultConflicting()) { |
| 6801 | // We can reuse the method from the superclass, don't bother adding it to virtuals. |
| 6802 | default_conflict_method = vtable_impl; |
| 6803 | } else { |
| 6804 | // See if we already have a conflict method for this method. |
| 6805 | ArtMethod* preexisting_conflict = FindSameNameAndSignature(interface_name_comparator, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6806 | default_conflict_methods); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6807 | if (LIKELY(preexisting_conflict != nullptr)) { |
| 6808 | // We already have another conflict we can reuse. |
| 6809 | default_conflict_method = preexisting_conflict; |
| 6810 | } else { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6811 | // Note that we do this even if we are an interface since we need to create this and |
| 6812 | // cannot reuse another classes. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6813 | // Create a new conflict method for this to use. |
| 6814 | default_conflict_method = |
| 6815 | reinterpret_cast<ArtMethod*>(allocator.Alloc(method_size)); |
| 6816 | new(default_conflict_method) ArtMethod(interface_method, image_pointer_size_); |
| 6817 | default_conflict_methods.push_back(default_conflict_method); |
| 6818 | } |
| 6819 | } |
| 6820 | current_method = default_conflict_method; |
| 6821 | break; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6822 | } // case kDefaultConflict |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6823 | case DefaultMethodSearchResult::kDefaultFound: { |
| 6824 | DCHECK(current_method != nullptr); |
| 6825 | // Found a default method. |
| 6826 | if (vtable_impl != nullptr && |
| 6827 | current_method->GetDeclaringClass() == vtable_impl->GetDeclaringClass()) { |
| 6828 | // We found a default method but it was the same one we already have from our |
| 6829 | // superclass. Don't bother adding it to our vtable again. |
| 6830 | current_method = vtable_impl; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6831 | } else if (LIKELY(fill_tables)) { |
| 6832 | // Interfaces don't need to copy default methods since they don't have vtables. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6833 | // Only record this default method if it is new to save space. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6834 | // TODO It might be worthwhile to copy default methods on interfaces anyway since it |
| 6835 | // would make lookup for interface super much faster. (We would only need to scan |
| 6836 | // the iftable to find if there is a NSME or AME.) |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6837 | ArtMethod* old = FindSameNameAndSignature(interface_name_comparator, default_methods); |
| 6838 | if (old == nullptr) { |
| 6839 | // We found a default method implementation and there were no conflicts. |
| 6840 | // Save the default method. We need to add it to the vtable. |
| 6841 | default_methods.push_back(current_method); |
| 6842 | } else { |
| 6843 | CHECK(old == current_method) << "Multiple default implementations selected!"; |
| 6844 | } |
| 6845 | } |
| 6846 | break; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6847 | } // case kDefaultFound |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6848 | case DefaultMethodSearchResult::kAbstractFound: { |
| 6849 | DCHECK(current_method == nullptr); |
| 6850 | // Abstract method masks all defaults. |
| 6851 | if (vtable_impl != nullptr && |
| 6852 | vtable_impl->IsAbstract() && |
| 6853 | !vtable_impl->IsDefaultConflicting()) { |
| 6854 | // We need to make this an abstract method but the version in the vtable already is so |
| 6855 | // don't do anything. |
| 6856 | current_method = vtable_impl; |
| 6857 | } |
| 6858 | break; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6859 | } // case kAbstractFound |
| 6860 | } |
| 6861 | if (LIKELY(fill_tables)) { |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 6862 | if (current_method == nullptr && !super_interface) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6863 | // We could not find an implementation for this method and since it is a brand new |
| 6864 | // interface we searched the entire vtable (and all default methods) for an |
| 6865 | // implementation but couldn't find one. We therefore need to make a miranda method. |
| 6866 | // |
| 6867 | // Find out if there is already a miranda method we can use. |
| 6868 | ArtMethod* miranda_method = FindSameNameAndSignature(interface_name_comparator, |
| 6869 | miranda_methods); |
| 6870 | if (miranda_method == nullptr) { |
| 6871 | DCHECK(interface_method->IsAbstract()) << PrettyMethod(interface_method); |
| 6872 | miranda_method = reinterpret_cast<ArtMethod*>(allocator.Alloc(method_size)); |
| 6873 | CHECK(miranda_method != nullptr); |
| 6874 | // Point the interface table at a phantom slot. |
| 6875 | new(miranda_method) ArtMethod(interface_method, image_pointer_size_); |
| 6876 | miranda_methods.push_back(miranda_method); |
| 6877 | } |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 6878 | current_method = miranda_method; |
| 6879 | } |
| 6880 | |
| 6881 | if (current_method != nullptr) { |
| 6882 | // We found a default method implementation. Record it in the iftable and IMT. |
| 6883 | method_array->SetElementPtrSize(j, current_method, image_pointer_size_); |
| 6884 | SetIMTRef(unimplemented_method, |
| 6885 | imt_conflict_method, |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 6886 | current_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6887 | /*out*/out_new_conflict, |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 6888 | /*out*/imt_ptr); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6889 | } |
| 6890 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6891 | } // For each method in interface end. |
| 6892 | } // if (num_methods > 0) |
| 6893 | } // For each interface. |
| 6894 | const bool has_new_virtuals = !(miranda_methods.empty() && |
| 6895 | default_methods.empty() && |
| 6896 | default_conflict_methods.empty()); |
| 6897 | // TODO don't extend virtuals of interface unless necessary (when is it?). |
| 6898 | if (has_new_virtuals) { |
| 6899 | DCHECK(!is_interface || (default_methods.empty() && miranda_methods.empty())) |
| 6900 | << "Interfaces should only have default-conflict methods appended to them."; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6901 | VLOG(class_linker) << PrettyClass(klass.Get()) << ": miranda_methods=" << miranda_methods.size() |
| 6902 | << " default_methods=" << default_methods.size() |
| 6903 | << " default_conflict_methods=" << default_conflict_methods.size(); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6904 | const size_t old_method_count = klass->NumMethods(); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6905 | const size_t new_method_count = old_method_count + |
| 6906 | miranda_methods.size() + |
| 6907 | default_methods.size() + |
| 6908 | default_conflict_methods.size(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6909 | // Attempt to realloc to save RAM if possible. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6910 | LengthPrefixedArray<ArtMethod>* old_methods = klass->GetMethodsPtr(); |
| 6911 | // The Realloced virtual methods aren't visible from the class roots, so there is no issue |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6912 | // where GCs could attempt to mark stale pointers due to memcpy. And since we overwrite the |
| 6913 | // realloced memory with out->CopyFrom, we are guaranteed to have objects in the to space since |
| 6914 | // CopyFrom has internal read barriers. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6915 | // |
| 6916 | // TODO We should maybe move some of this into mirror::Class or at least into another method. |
| 6917 | const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_method_count, |
| 6918 | method_size, |
| 6919 | method_alignment); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 6920 | const size_t new_size = LengthPrefixedArray<ArtMethod>::ComputeSize(new_method_count, |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 6921 | method_size, |
| 6922 | method_alignment); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6923 | const size_t old_methods_ptr_size = (old_methods != nullptr) ? old_size : 0; |
| 6924 | auto* methods = reinterpret_cast<LengthPrefixedArray<ArtMethod>*>( |
| 6925 | runtime->GetLinearAlloc()->Realloc(self, old_methods, old_methods_ptr_size, new_size)); |
| 6926 | if (UNLIKELY(methods == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6927 | self->AssertPendingOOMException(); |
Mathieu Chartier | 4e2cb09 | 2015-07-22 16:17:51 -0700 | [diff] [blame] | 6928 | self->EndAssertNoThreadSuspension(old_cause); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6929 | return false; |
| 6930 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6931 | ScopedArenaUnorderedMap<ArtMethod*, ArtMethod*> move_table(allocator.Adapter()); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6932 | if (methods != old_methods) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6933 | // Maps from heap allocated miranda method to linear alloc miranda method. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6934 | StrideIterator<ArtMethod> out = methods->begin(method_size, method_alignment); |
| 6935 | // Copy over the old methods. |
| 6936 | for (auto& m : klass->GetMethods(image_pointer_size_)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6937 | move_table.emplace(&m, &*out); |
| 6938 | // The CopyFrom is only necessary to not miss read barriers since Realloc won't do read |
| 6939 | // barriers when it copies. |
| 6940 | out->CopyFrom(&m, image_pointer_size_); |
| 6941 | ++out; |
| 6942 | } |
| 6943 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6944 | StrideIterator<ArtMethod> out(methods->begin(method_size, method_alignment) + old_method_count); |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 6945 | // Copy over miranda methods before copying vtable since CopyOf may cause thread suspension and |
| 6946 | // we want the roots of the miranda methods to get visited. |
| 6947 | for (ArtMethod* mir_method : miranda_methods) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6948 | ArtMethod& new_method = *out; |
| 6949 | new_method.CopyFrom(mir_method, image_pointer_size_); |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 6950 | new_method.SetAccessFlags(new_method.GetAccessFlags() | kAccMiranda | kAccCopied); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6951 | DCHECK_NE(new_method.GetAccessFlags() & kAccAbstract, 0u) |
| 6952 | << "Miranda method should be abstract!"; |
| 6953 | move_table.emplace(mir_method, &new_method); |
| 6954 | ++out; |
| 6955 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6956 | // We need to copy the default methods into our own method table since the runtime requires that |
| 6957 | // every method on a class's vtable be in that respective class's virtual method table. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6958 | // NOTE This means that two classes might have the same implementation of a method from the same |
| 6959 | // interface but will have different ArtMethod*s for them. This also means we cannot compare a |
| 6960 | // default method found on a class with one found on the declaring interface directly and must |
| 6961 | // look at the declaring class to determine if they are the same. |
| 6962 | for (ArtMethod* def_method : default_methods) { |
| 6963 | ArtMethod& new_method = *out; |
| 6964 | new_method.CopyFrom(def_method, image_pointer_size_); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 6965 | // Clear the kAccSkipAccessChecks flag if it is present. Since this class hasn't been verified |
| 6966 | // yet it shouldn't have methods that are skipping access checks. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6967 | // TODO This is rather arbitrary. We should maybe support classes where only some of its |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 6968 | // methods are skip_access_checks. |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 6969 | constexpr uint32_t kSetFlags = kAccDefault | kAccCopied; |
| 6970 | constexpr uint32_t kMaskFlags = ~kAccSkipAccessChecks; |
| 6971 | new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6972 | move_table.emplace(def_method, &new_method); |
Mathieu Chartier | cf3b1a3 | 2015-06-01 14:30:06 -0700 | [diff] [blame] | 6973 | ++out; |
Mathieu Chartier | cf3b1a3 | 2015-06-01 14:30:06 -0700 | [diff] [blame] | 6974 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6975 | for (ArtMethod* conf_method : default_conflict_methods) { |
| 6976 | ArtMethod& new_method = *out; |
| 6977 | new_method.CopyFrom(conf_method, image_pointer_size_); |
| 6978 | // This is a type of default method (there are default method impls, just a conflict) so mark |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 6979 | // this as a default, non-abstract method, since thats what it is. Also clear the |
| 6980 | // kAccSkipAccessChecks bit since this class hasn't been verified yet it shouldn't have |
| 6981 | // methods that are skipping access checks. |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 6982 | constexpr uint32_t kSetFlags = kAccDefault | kAccDefaultConflict | kAccCopied; |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 6983 | constexpr uint32_t kMaskFlags = ~(kAccAbstract | kAccSkipAccessChecks); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6984 | new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags); |
| 6985 | DCHECK(new_method.IsDefaultConflicting()); |
| 6986 | // The actual method might or might not be marked abstract since we just copied it from a |
| 6987 | // (possibly default) interface method. We need to set it entry point to be the bridge so that |
| 6988 | // the compiler will not invoke the implementation of whatever method we copied from. |
| 6989 | EnsureThrowsInvocationError(&new_method); |
| 6990 | move_table.emplace(conf_method, &new_method); |
| 6991 | ++out; |
| 6992 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6993 | methods->SetSize(new_method_count); |
| 6994 | UpdateClassMethods(klass.Get(), methods); |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 6995 | // Done copying methods, they are all roots in the class now, so we can end the no thread |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6996 | // suspension assert. |
| 6997 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 6998 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6999 | if (fill_tables) { |
| 7000 | // Update the vtable to the new method structures. We can skip this for interfaces since they |
| 7001 | // do not have vtables. |
| 7002 | const size_t old_vtable_count = vtable->GetLength(); |
| 7003 | const size_t new_vtable_count = old_vtable_count + |
| 7004 | miranda_methods.size() + |
| 7005 | default_methods.size() + |
| 7006 | default_conflict_methods.size(); |
| 7007 | vtable.Assign(down_cast<mirror::PointerArray*>(vtable->CopyOf(self, new_vtable_count))); |
| 7008 | if (UNLIKELY(vtable.Get() == nullptr)) { |
| 7009 | self->AssertPendingOOMException(); |
| 7010 | return false; |
| 7011 | } |
| 7012 | out = methods->begin(method_size, method_alignment) + old_method_count; |
| 7013 | size_t vtable_pos = old_vtable_count; |
| 7014 | // Update all the newly copied method's indexes so they denote their placement in the vtable. |
| 7015 | for (size_t i = old_method_count; i < new_method_count; ++i) { |
| 7016 | // Leave the declaring class alone the method's dex_code_item_offset_ and dex_method_index_ |
| 7017 | // fields are references into the dex file the method was defined in. Since the ArtMethod |
| 7018 | // does not store that information it uses declaring_class_->dex_cache_. |
| 7019 | out->SetMethodIndex(0xFFFF & vtable_pos); |
| 7020 | vtable->SetElementPtrSize(vtable_pos, &*out, image_pointer_size_); |
| 7021 | ++out; |
| 7022 | ++vtable_pos; |
| 7023 | } |
| 7024 | CHECK_EQ(vtable_pos, new_vtable_count); |
| 7025 | // Update old vtable methods. We use the default_translations map to figure out what each |
| 7026 | // vtable entry should be updated to, if they need to be at all. |
| 7027 | for (size_t i = 0; i < old_vtable_count; ++i) { |
| 7028 | ArtMethod* translated_method = vtable->GetElementPtrSize<ArtMethod*>( |
| 7029 | i, image_pointer_size_); |
| 7030 | // Try and find what we need to change this method to. |
| 7031 | auto translation_it = default_translations.find(i); |
| 7032 | bool found_translation = false; |
| 7033 | if (translation_it != default_translations.end()) { |
| 7034 | if (translation_it->second.IsInConflict()) { |
| 7035 | // Find which conflict method we are to use for this method. |
| 7036 | MethodNameAndSignatureComparator old_method_comparator( |
| 7037 | translated_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
| 7038 | ArtMethod* new_conflict_method = FindSameNameAndSignature(old_method_comparator, |
| 7039 | default_conflict_methods); |
| 7040 | CHECK(new_conflict_method != nullptr) << "Expected a conflict method!"; |
| 7041 | translated_method = new_conflict_method; |
| 7042 | } else if (translation_it->second.IsAbstract()) { |
| 7043 | // Find which miranda method we are to use for this method. |
| 7044 | MethodNameAndSignatureComparator old_method_comparator( |
| 7045 | translated_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
| 7046 | ArtMethod* miranda_method = FindSameNameAndSignature(old_method_comparator, |
| 7047 | miranda_methods); |
| 7048 | DCHECK(miranda_method != nullptr); |
| 7049 | translated_method = miranda_method; |
| 7050 | } else { |
| 7051 | // Normal default method (changed from an older default or abstract interface method). |
| 7052 | DCHECK(translation_it->second.IsTranslation()); |
| 7053 | translated_method = translation_it->second.GetTranslation(); |
| 7054 | } |
| 7055 | found_translation = true; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7056 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7057 | DCHECK(translated_method != nullptr); |
| 7058 | auto it = move_table.find(translated_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7059 | if (it != move_table.end()) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7060 | auto* new_method = it->second; |
| 7061 | DCHECK(new_method != nullptr); |
| 7062 | vtable->SetElementPtrSize(i, new_method, image_pointer_size_); |
| 7063 | } else { |
| 7064 | // If it was not going to be updated we wouldn't have put it into the default_translations |
| 7065 | // map. |
| 7066 | CHECK(!found_translation) << "We were asked to update this vtable entry. Must not fail."; |
| 7067 | } |
| 7068 | } |
| 7069 | klass->SetVTable(vtable.Get()); |
| 7070 | |
| 7071 | // Go fix up all the stale iftable pointers. |
| 7072 | for (size_t i = 0; i < ifcount; ++i) { |
| 7073 | for (size_t j = 0, count = iftable->GetMethodArrayCount(i); j < count; ++j) { |
| 7074 | auto* method_array = iftable->GetMethodArray(i); |
| 7075 | auto* m = method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 7076 | DCHECK(m != nullptr) << PrettyClass(klass.Get()); |
| 7077 | auto it = move_table.find(m); |
| 7078 | if (it != move_table.end()) { |
| 7079 | auto* new_m = it->second; |
| 7080 | DCHECK(new_m != nullptr) << PrettyClass(klass.Get()); |
| 7081 | method_array->SetElementPtrSize(j, new_m, image_pointer_size_); |
| 7082 | } |
| 7083 | } |
| 7084 | } |
| 7085 | |
| 7086 | // Fix up IMT next |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7087 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7088 | auto it = move_table.find(out_imt[i]); |
| 7089 | if (it != move_table.end()) { |
| 7090 | out_imt[i] = it->second; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7091 | } |
| 7092 | } |
| 7093 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7094 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7095 | // Check that there are no stale methods are in the dex cache array. |
| 7096 | if (kIsDebugBuild) { |
| 7097 | auto* resolved_methods = klass->GetDexCache()->GetResolvedMethods(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 7098 | for (size_t i = 0, count = klass->GetDexCache()->NumResolvedMethods(); i < count; ++i) { |
| 7099 | auto* m = mirror::DexCache::GetElementPtrSize(resolved_methods, i, image_pointer_size_); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7100 | CHECK(move_table.find(m) == move_table.end() || |
| 7101 | // The original versions of copied methods will still be present so allow those too. |
| 7102 | // Note that if the first check passes this might fail to GetDeclaringClass(). |
| 7103 | std::find_if(m->GetDeclaringClass()->GetMethods(image_pointer_size_).begin(), |
| 7104 | m->GetDeclaringClass()->GetMethods(image_pointer_size_).end(), |
| 7105 | [m] (ArtMethod& meth) { |
| 7106 | return &meth == m; |
| 7107 | }) != m->GetDeclaringClass()->GetMethods(image_pointer_size_).end()) |
| 7108 | << "Obsolete methods " << PrettyMethod(m) << " is in dex cache!"; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7109 | } |
| 7110 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7111 | // Put some random garbage in old methods to help find stale pointers. |
Mathieu Chartier | 1b1e31f | 2016-05-19 10:13:04 -0700 | [diff] [blame] | 7112 | if (methods != old_methods && old_methods != nullptr && kIsDebugBuild) { |
| 7113 | // Need to make sure the GC is not running since it could be scanning the methods we are |
| 7114 | // about to overwrite. |
| 7115 | ScopedThreadStateChange tsc(self, kSuspended); |
| 7116 | gc::ScopedGCCriticalSection gcs(self, |
| 7117 | gc::kGcCauseClassLinker, |
| 7118 | gc::kCollectorTypeClassLinker); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7119 | memset(old_methods, 0xFEu, old_size); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7120 | } |
| 7121 | } else { |
| 7122 | self->EndAssertNoThreadSuspension(old_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7123 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7124 | if (kIsDebugBuild && !is_interface) { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7125 | SanityCheckVTable(klass, image_pointer_size_); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 7126 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7127 | return true; |
| 7128 | } |
| 7129 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7130 | bool ClassLinker::LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7131 | CHECK(klass.Get() != nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7132 | return LinkFields(self, klass, false, nullptr); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 7133 | } |
| 7134 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7135 | bool ClassLinker::LinkStaticFields(Thread* self, Handle<mirror::Class> klass, size_t* class_size) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7136 | CHECK(klass.Get() != nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7137 | return LinkFields(self, klass, true, class_size); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 7138 | } |
| 7139 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7140 | struct LinkFieldsComparator { |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 7141 | explicit LinkFieldsComparator() SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 7142 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 7143 | // No thread safety analysis as will be called from STL. Checked lock held in constructor. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7144 | bool operator()(ArtField* field1, ArtField* field2) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 7145 | NO_THREAD_SAFETY_ANALYSIS { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 7146 | // First come reference fields, then 64-bit, then 32-bit, and then 16-bit, then finally 8-bit. |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 7147 | Primitive::Type type1 = field1->GetTypeAsPrimitiveType(); |
| 7148 | Primitive::Type type2 = field2->GetTypeAsPrimitiveType(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 7149 | if (type1 != type2) { |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 7150 | if (type1 == Primitive::kPrimNot) { |
| 7151 | // Reference always goes first. |
| 7152 | return true; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 7153 | } |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 7154 | if (type2 == Primitive::kPrimNot) { |
| 7155 | // Reference always goes first. |
| 7156 | return false; |
| 7157 | } |
| 7158 | size_t size1 = Primitive::ComponentSize(type1); |
| 7159 | size_t size2 = Primitive::ComponentSize(type2); |
| 7160 | if (size1 != size2) { |
| 7161 | // Larger primitive types go first. |
| 7162 | return size1 > size2; |
| 7163 | } |
| 7164 | // Primitive types differ but sizes match. Arbitrarily order by primitive type. |
| 7165 | return type1 < type2; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7166 | } |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 7167 | // Same basic group? Then sort by dex field index. This is guaranteed to be sorted |
| 7168 | // by name and for equal names by type id index. |
| 7169 | // NOTE: This works also for proxies. Their static fields are assigned appropriate indexes. |
| 7170 | return field1->GetDexFieldIndex() < field2->GetDexFieldIndex(); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7171 | } |
| 7172 | }; |
| 7173 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7174 | bool ClassLinker::LinkFields(Thread* self, |
| 7175 | Handle<mirror::Class> klass, |
| 7176 | bool is_static, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7177 | size_t* class_size) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 7178 | self->AllowThreadSuspension(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7179 | const size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7180 | LengthPrefixedArray<ArtField>* const fields = is_static ? klass->GetSFieldsPtr() : |
| 7181 | klass->GetIFieldsPtr(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7182 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 7183 | // Initialize field_offset |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 7184 | MemberOffset field_offset(0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7185 | if (is_static) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7186 | field_offset = klass->GetFirstReferenceStaticFieldOffsetDuringLinking(image_pointer_size_); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7187 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 7188 | mirror::Class* super_class = klass->GetSuperClass(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7189 | if (super_class != nullptr) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 7190 | CHECK(super_class->IsResolved()) |
| 7191 | << PrettyClass(klass.Get()) << " " << PrettyClass(super_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7192 | field_offset = MemberOffset(super_class->GetObjectSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7193 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7194 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7195 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7196 | CHECK_EQ(num_fields == 0, fields == nullptr) << PrettyClass(klass.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7197 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7198 | // we want a relatively stable order so that adding new fields |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7199 | // minimizes disruption of C++ version such as Class and Method. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7200 | // |
| 7201 | // The overall sort order order is: |
| 7202 | // 1) All object reference fields, sorted alphabetically. |
| 7203 | // 2) All java long (64-bit) integer fields, sorted alphabetically. |
| 7204 | // 3) All java double (64-bit) floating point fields, sorted alphabetically. |
| 7205 | // 4) All java int (32-bit) integer fields, sorted alphabetically. |
| 7206 | // 5) All java float (32-bit) floating point fields, sorted alphabetically. |
| 7207 | // 6) All java char (16-bit) integer fields, sorted alphabetically. |
| 7208 | // 7) All java short (16-bit) integer fields, sorted alphabetically. |
| 7209 | // 8) All java boolean (8-bit) integer fields, sorted alphabetically. |
| 7210 | // 9) All java byte (8-bit) integer fields, sorted alphabetically. |
| 7211 | // |
| 7212 | // Once the fields are sorted in this order we will attempt to fill any gaps that might be present |
| 7213 | // in the memory layout of the structure. See ShuffleForward for how this is done. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7214 | std::deque<ArtField*> grouped_and_sorted_fields; |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 7215 | const char* old_no_suspend_cause = self->StartAssertNoThreadSuspension( |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 7216 | "Naked ArtField references in deque"); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7217 | for (size_t i = 0; i < num_fields; i++) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7218 | grouped_and_sorted_fields.push_back(&fields->At(i)); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7219 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 7220 | std::sort(grouped_and_sorted_fields.begin(), grouped_and_sorted_fields.end(), |
| 7221 | LinkFieldsComparator()); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7222 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7223 | // References should be at the front. |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7224 | size_t current_field = 0; |
| 7225 | size_t num_reference_fields = 0; |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7226 | FieldGaps gaps; |
| 7227 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7228 | for (; current_field < num_fields; current_field++) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7229 | ArtField* field = grouped_and_sorted_fields.front(); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 7230 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 7231 | bool isPrimitive = type != Primitive::kPrimNot; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7232 | if (isPrimitive) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 7233 | break; // past last reference, move on to the next phase |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7234 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7235 | if (UNLIKELY(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>( |
| 7236 | field_offset.Uint32Value()))) { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7237 | MemberOffset old_offset = field_offset; |
| 7238 | field_offset = MemberOffset(RoundUp(field_offset.Uint32Value(), 4)); |
| 7239 | AddFieldGap(old_offset.Uint32Value(), field_offset.Uint32Value(), &gaps); |
| 7240 | } |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 7241 | DCHECK_ALIGNED(field_offset.Uint32Value(), sizeof(mirror::HeapReference<mirror::Object>)); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7242 | grouped_and_sorted_fields.pop_front(); |
| 7243 | num_reference_fields++; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7244 | field->SetOffset(field_offset); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7245 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 7246 | sizeof(mirror::HeapReference<mirror::Object>)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7247 | } |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7248 | // Gaps are stored as a max heap which means that we must shuffle from largest to smallest |
| 7249 | // otherwise we could end up with suboptimal gap fills. |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7250 | ShuffleForward<8>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 7251 | ShuffleForward<4>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 7252 | ShuffleForward<2>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 7253 | ShuffleForward<1>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 7254 | CHECK(grouped_and_sorted_fields.empty()) << "Missed " << grouped_and_sorted_fields.size() << |
| 7255 | " fields."; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 7256 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7257 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7258 | // 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] | 7259 | if (!is_static && klass->DescriptorEquals("Ljava/lang/ref/Reference;")) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7260 | // We know there are no non-reference fields in the Reference classes, and we know |
| 7261 | // that 'referent' is alphabetically last, so this is easy... |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 7262 | CHECK_EQ(num_reference_fields, num_fields) << PrettyClass(klass.Get()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7263 | CHECK_STREQ(fields->At(num_fields - 1).GetName(), "referent") |
| 7264 | << PrettyClass(klass.Get()); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7265 | --num_reference_fields; |
| 7266 | } |
| 7267 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 7268 | size_t size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7269 | // Update klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7270 | if (is_static) { |
| 7271 | klass->SetNumReferenceStaticFields(num_reference_fields); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 7272 | *class_size = size; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7273 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7274 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 7275 | mirror::Class* super_class = klass->GetSuperClass(); |
| 7276 | if (num_reference_fields == 0 || super_class == nullptr) { |
| 7277 | // object has one reference field, klass, but we ignore it since we always visit the class. |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 7278 | // super_class is null iff the class is java.lang.Object. |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 7279 | if (super_class == nullptr || |
| 7280 | (super_class->GetClassFlags() & mirror::kClassFlagNoReferenceFields) != 0) { |
| 7281 | klass->SetClassFlags(klass->GetClassFlags() | mirror::kClassFlagNoReferenceFields); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 7282 | } |
| 7283 | } |
| 7284 | if (kIsDebugBuild) { |
| 7285 | DCHECK_EQ(super_class == nullptr, klass->DescriptorEquals("Ljava/lang/Object;")); |
| 7286 | size_t total_reference_instance_fields = 0; |
| 7287 | mirror::Class* cur_super = klass.Get(); |
| 7288 | while (cur_super != nullptr) { |
| 7289 | total_reference_instance_fields += cur_super->NumReferenceInstanceFieldsDuringLinking(); |
| 7290 | cur_super = cur_super->GetSuperClass(); |
| 7291 | } |
| 7292 | if (super_class == nullptr) { |
| 7293 | CHECK_EQ(total_reference_instance_fields, 1u) << PrettyDescriptor(klass.Get()); |
| 7294 | } else { |
| 7295 | // Check that there is at least num_reference_fields other than Object.class. |
| 7296 | CHECK_GE(total_reference_instance_fields, 1u + num_reference_fields) |
| 7297 | << PrettyClass(klass.Get()); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 7298 | } |
| 7299 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7300 | if (!klass->IsVariableSize()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7301 | std::string temp; |
| 7302 | DCHECK_GE(size, sizeof(mirror::Object)) << klass->GetDescriptor(&temp); |
| 7303 | size_t previous_size = klass->GetObjectSize(); |
| 7304 | if (previous_size != 0) { |
| 7305 | // Make sure that we didn't originally have an incorrect size. |
| 7306 | CHECK_EQ(previous_size, size) << klass->GetDescriptor(&temp); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 7307 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7308 | klass->SetObjectSize(size); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7309 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7310 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7311 | |
| 7312 | if (kIsDebugBuild) { |
| 7313 | // Make sure that the fields array is ordered by name but all reference |
| 7314 | // offsets are at the beginning as far as alignment allows. |
| 7315 | MemberOffset start_ref_offset = is_static |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7316 | ? klass->GetFirstReferenceStaticFieldOffsetDuringLinking(image_pointer_size_) |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7317 | : klass->GetFirstReferenceInstanceFieldOffset(); |
| 7318 | MemberOffset end_ref_offset(start_ref_offset.Uint32Value() + |
| 7319 | num_reference_fields * |
| 7320 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 7321 | MemberOffset current_ref_offset = start_ref_offset; |
| 7322 | for (size_t i = 0; i < num_fields; i++) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7323 | ArtField* field = &fields->At(i); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7324 | VLOG(class_linker) << "LinkFields: " << (is_static ? "static" : "instance") |
| 7325 | << " class=" << PrettyClass(klass.Get()) << " field=" << PrettyField(field) << " offset=" |
Nicolas Geoffray | 555b3d0 | 2015-06-23 12:11:24 +0100 | [diff] [blame] | 7326 | << field->GetOffsetDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7327 | if (i != 0) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7328 | ArtField* const prev_field = &fields->At(i - 1); |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 7329 | // NOTE: The field names can be the same. This is not possible in the Java language |
| 7330 | // but it's valid Java/dex bytecode and for example proguard can generate such bytecode. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7331 | DCHECK_LE(strcmp(prev_field->GetName(), field->GetName()), 0); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7332 | } |
| 7333 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 7334 | bool is_primitive = type != Primitive::kPrimNot; |
| 7335 | if (klass->DescriptorEquals("Ljava/lang/ref/Reference;") && |
| 7336 | strcmp("referent", field->GetName()) == 0) { |
| 7337 | is_primitive = true; // We lied above, so we have to expect a lie here. |
| 7338 | } |
| 7339 | MemberOffset offset = field->GetOffsetDuringLinking(); |
| 7340 | if (is_primitive) { |
| 7341 | if (offset.Uint32Value() < end_ref_offset.Uint32Value()) { |
| 7342 | // Shuffled before references. |
| 7343 | size_t type_size = Primitive::ComponentSize(type); |
| 7344 | CHECK_LT(type_size, sizeof(mirror::HeapReference<mirror::Object>)); |
| 7345 | CHECK_LT(offset.Uint32Value(), start_ref_offset.Uint32Value()); |
| 7346 | CHECK_LE(offset.Uint32Value() + type_size, start_ref_offset.Uint32Value()); |
| 7347 | CHECK(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(offset.Uint32Value())); |
| 7348 | } |
| 7349 | } else { |
| 7350 | CHECK_EQ(current_ref_offset.Uint32Value(), offset.Uint32Value()); |
| 7351 | current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() + |
| 7352 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 7353 | } |
| 7354 | } |
| 7355 | CHECK_EQ(current_ref_offset.Uint32Value(), end_ref_offset.Uint32Value()); |
| 7356 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7357 | return true; |
| 7358 | } |
| 7359 | |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7360 | // Set the bitmap of reference instance field offsets. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7361 | void ClassLinker::CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7362 | uint32_t reference_offsets = 0; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 7363 | mirror::Class* super_class = klass->GetSuperClass(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7364 | // Leave the reference offsets as 0 for mirror::Object (the class field is handled specially). |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7365 | if (super_class != nullptr) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7366 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7367 | // Compute reference offsets unless our superclass overflowed. |
| 7368 | if (reference_offsets != mirror::Class::kClassWalkSuper) { |
| 7369 | size_t num_reference_fields = klass->NumReferenceInstanceFieldsDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7370 | if (num_reference_fields != 0u) { |
| 7371 | // All of the fields that contain object references are guaranteed be grouped in memory |
| 7372 | // starting at an appropriately aligned address after super class object data. |
| 7373 | uint32_t start_offset = RoundUp(super_class->GetObjectSize(), |
| 7374 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 7375 | uint32_t start_bit = (start_offset - mirror::kObjectHeaderSize) / |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7376 | sizeof(mirror::HeapReference<mirror::Object>); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7377 | if (start_bit + num_reference_fields > 32) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7378 | reference_offsets = mirror::Class::kClassWalkSuper; |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7379 | } else { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7380 | reference_offsets |= (0xffffffffu << start_bit) & |
| 7381 | (0xffffffffu >> (32 - (start_bit + num_reference_fields))); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7382 | } |
| 7383 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 7384 | } |
| 7385 | } |
Mingyao Yang | faff0f0 | 2014-09-10 12:03:22 -0700 | [diff] [blame] | 7386 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7387 | } |
| 7388 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7389 | mirror::String* ClassLinker::ResolveString(const DexFile& dex_file, |
| 7390 | uint32_t string_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7391 | Handle<mirror::DexCache> dex_cache) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 7392 | DCHECK(dex_cache.Get() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 7393 | mirror::String* resolved = dex_cache->GetResolvedString(string_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7394 | if (resolved != nullptr) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7395 | return resolved; |
| 7396 | } |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 7397 | uint32_t utf16_length; |
| 7398 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 7399 | mirror::String* string = intern_table_->InternStrong(utf16_length, utf8_data); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7400 | dex_cache->SetResolvedString(string_idx, string); |
| 7401 | return string; |
| 7402 | } |
| 7403 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7404 | mirror::String* ClassLinker::LookupString(const DexFile& dex_file, |
| 7405 | uint32_t string_idx, |
| 7406 | Handle<mirror::DexCache> dex_cache) { |
| 7407 | DCHECK(dex_cache.Get() != nullptr); |
| 7408 | mirror::String* resolved = dex_cache->GetResolvedString(string_idx); |
| 7409 | if (resolved != nullptr) { |
| 7410 | return resolved; |
| 7411 | } |
| 7412 | uint32_t utf16_length; |
| 7413 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
| 7414 | mirror::String* string = intern_table_->LookupStrong(Thread::Current(), utf16_length, utf8_data); |
| 7415 | if (string != nullptr) { |
| 7416 | dex_cache->SetResolvedString(string_idx, string); |
| 7417 | } |
| 7418 | return string; |
| 7419 | } |
| 7420 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7421 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, |
| 7422 | uint16_t type_idx, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 7423 | mirror::Class* referrer) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 7424 | StackHandleScope<2> hs(Thread::Current()); |
| 7425 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 7426 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 7427 | return ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 7428 | } |
| 7429 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7430 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, |
| 7431 | uint16_t type_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7432 | Handle<mirror::DexCache> dex_cache, |
| 7433 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7434 | DCHECK(dex_cache.Get() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 7435 | mirror::Class* resolved = dex_cache->GetResolvedType(type_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7436 | if (resolved == nullptr) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 7437 | Thread* self = Thread::Current(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 7438 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 7439 | resolved = FindClass(self, descriptor, class_loader); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7440 | if (resolved != nullptr) { |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 7441 | // TODO: we used to throw here if resolved's class loader was not the |
| 7442 | // boot class loader. This was to permit different classes with the |
| 7443 | // same name to be loaded simultaneously by different loaders |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7444 | dex_cache->SetResolvedType(type_idx, resolved); |
| 7445 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 7446 | CHECK(self->IsExceptionPending()) |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 7447 | << "Expected pending exception for failed resolution of: " << descriptor; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 7448 | // Convert a ClassNotFoundException to a NoClassDefFoundError. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 7449 | StackHandleScope<1> hs(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 7450 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 7451 | if (cause->InstanceOf(GetClassRoot(kJavaLangClassNotFoundException))) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7452 | DCHECK(resolved == nullptr); // No Handle needed to preserve resolved. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 7453 | self->ClearException(); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 7454 | ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 7455 | self->GetException()->SetCause(cause.Get()); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 7456 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7457 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7458 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7459 | DCHECK((resolved == nullptr) || resolved->IsResolved() || resolved->IsErroneous()) |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7460 | << PrettyDescriptor(resolved) << " " << resolved->GetStatus(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7461 | return resolved; |
| 7462 | } |
| 7463 | |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 7464 | template <ClassLinker::ResolveMode kResolveMode> |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7465 | ArtMethod* ClassLinker::ResolveMethod(const DexFile& dex_file, |
| 7466 | uint32_t method_idx, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7467 | Handle<mirror::DexCache> dex_cache, |
| 7468 | Handle<mirror::ClassLoader> class_loader, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7469 | ArtMethod* referrer, |
| 7470 | InvokeType type) { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7471 | DCHECK(dex_cache.Get() != nullptr); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7472 | // Check for hit in the dex cache. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7473 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, image_pointer_size_); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 7474 | if (resolved != nullptr && !resolved->IsRuntimeMethod()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7475 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 7476 | if (kResolveMode == ClassLinker::kForceICCECheck) { |
| 7477 | if (resolved->CheckIncompatibleClassChange(type)) { |
| 7478 | ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer); |
| 7479 | return nullptr; |
| 7480 | } |
| 7481 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7482 | return resolved; |
| 7483 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7484 | // Fail, get the declaring class. |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7485 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 7486 | mirror::Class* klass = ResolveType(dex_file, method_id.class_idx_, dex_cache, class_loader); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7487 | if (klass == nullptr) { |
Elliott Hughes | cc5f9a9 | 2011-09-28 19:17:29 -0700 | [diff] [blame] | 7488 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7489 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7490 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7491 | // Scan using method_idx, this saves string compares but will only hit for matching dex |
| 7492 | // caches/files. |
| 7493 | switch (type) { |
| 7494 | case kDirect: // Fall-through. |
| 7495 | case kStatic: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7496 | resolved = klass->FindDirectMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 7497 | DCHECK(resolved == nullptr || resolved->GetDeclaringClassUnchecked() != nullptr); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7498 | break; |
| 7499 | case kInterface: |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 7500 | // We have to check whether the method id really belongs to an interface (dex static bytecode |
| 7501 | // constraint A15). Otherwise you must not invoke-interface on it. |
| 7502 | // |
| 7503 | // This is not symmetric to A12-A14 (direct, static, virtual), as using FindInterfaceMethod |
| 7504 | // assumes that the given type is an interface, and will check the interface table if the |
| 7505 | // method isn't declared in the class. So it may find an interface method (usually by name |
| 7506 | // in the handling below, but we do the constraint check early). In that case, |
| 7507 | // CheckIncompatibleClassChange will succeed (as it is called on an interface method) |
| 7508 | // unexpectedly. |
| 7509 | // Example: |
| 7510 | // interface I { |
| 7511 | // foo() |
| 7512 | // } |
| 7513 | // class A implements I { |
| 7514 | // ... |
| 7515 | // } |
| 7516 | // class B extends A { |
| 7517 | // ... |
| 7518 | // } |
| 7519 | // invoke-interface B.foo |
| 7520 | // -> FindInterfaceMethod finds I.foo (interface method), not A.foo (miranda method) |
| 7521 | if (UNLIKELY(!klass->IsInterface())) { |
| 7522 | ThrowIncompatibleClassChangeError(klass, |
| 7523 | "Found class %s, but interface was expected", |
| 7524 | PrettyDescriptor(klass).c_str()); |
| 7525 | return nullptr; |
| 7526 | } else { |
| 7527 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 7528 | DCHECK(resolved == nullptr || resolved->GetDeclaringClass()->IsInterface()); |
| 7529 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7530 | break; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7531 | case kSuper: |
| 7532 | if (klass->IsInterface()) { |
| 7533 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 7534 | } else { |
| 7535 | resolved = klass->FindVirtualMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 7536 | } |
| 7537 | break; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7538 | case kVirtual: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7539 | resolved = klass->FindVirtualMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7540 | break; |
| 7541 | default: |
| 7542 | LOG(FATAL) << "Unreachable - invocation type: " << type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 7543 | UNREACHABLE(); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 7544 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7545 | if (resolved == nullptr) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7546 | // Search by name, which works across dex files. |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 7547 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 7548 | const Signature signature = dex_file.GetMethodSignature(method_id); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7549 | switch (type) { |
| 7550 | case kDirect: // Fall-through. |
| 7551 | case kStatic: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7552 | resolved = klass->FindDirectMethod(name, signature, image_pointer_size_); |
| 7553 | DCHECK(resolved == nullptr || resolved->GetDeclaringClassUnchecked() != nullptr); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7554 | break; |
| 7555 | case kInterface: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7556 | resolved = klass->FindInterfaceMethod(name, signature, image_pointer_size_); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7557 | DCHECK(resolved == nullptr || resolved->GetDeclaringClass()->IsInterface()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7558 | break; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7559 | case kSuper: |
| 7560 | if (klass->IsInterface()) { |
| 7561 | resolved = klass->FindInterfaceMethod(name, signature, image_pointer_size_); |
| 7562 | } else { |
| 7563 | resolved = klass->FindVirtualMethod(name, signature, image_pointer_size_); |
| 7564 | } |
| 7565 | break; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7566 | case kVirtual: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7567 | resolved = klass->FindVirtualMethod(name, signature, image_pointer_size_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7568 | break; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 7569 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7570 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7571 | // If we found a method, check for incompatible class changes. |
| 7572 | if (LIKELY(resolved != nullptr && !resolved->CheckIncompatibleClassChange(type))) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7573 | // Be a good citizen and update the dex cache to speed subsequent calls. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7574 | dex_cache->SetResolvedMethod(method_idx, resolved, image_pointer_size_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7575 | return resolved; |
| 7576 | } else { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7577 | // If we had a method, it's an incompatible-class-change error. |
| 7578 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7579 | ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7580 | } else { |
| 7581 | // We failed to find the method which means either an access error, an incompatible class |
| 7582 | // change, or no such method. First try to find the method among direct and virtual methods. |
| 7583 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 7584 | const Signature signature = dex_file.GetMethodSignature(method_id); |
| 7585 | switch (type) { |
| 7586 | case kDirect: |
| 7587 | case kStatic: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7588 | resolved = klass->FindVirtualMethod(name, signature, image_pointer_size_); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7589 | // Note: kDirect and kStatic are also mutually exclusive, but in that case we would |
| 7590 | // have had a resolved method before, which triggers the "true" branch above. |
| 7591 | break; |
| 7592 | case kInterface: |
| 7593 | case kVirtual: |
| 7594 | case kSuper: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7595 | resolved = klass->FindDirectMethod(name, signature, image_pointer_size_); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7596 | break; |
| 7597 | } |
| 7598 | |
| 7599 | // If we found something, check that it can be accessed by the referrer. |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 7600 | bool exception_generated = false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7601 | if (resolved != nullptr && referrer != nullptr) { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7602 | mirror::Class* methods_class = resolved->GetDeclaringClass(); |
| 7603 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
| 7604 | if (!referring_class->CanAccess(methods_class)) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7605 | ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, |
| 7606 | methods_class, |
| 7607 | resolved, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7608 | type); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 7609 | exception_generated = true; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7610 | } else if (!referring_class->CanAccessMember(methods_class, resolved->GetAccessFlags())) { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7611 | ThrowIllegalAccessErrorMethod(referring_class, resolved); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 7612 | exception_generated = true; |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7613 | } |
| 7614 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 7615 | if (!exception_generated) { |
| 7616 | // Otherwise, throw an IncompatibleClassChangeError if we found something, and check |
| 7617 | // interface methods and throw if we find the method there. If we find nothing, throw a |
| 7618 | // NoSuchMethodError. |
| 7619 | switch (type) { |
| 7620 | case kDirect: |
| 7621 | case kStatic: |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7622 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7623 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7624 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7625 | resolved = klass->FindInterfaceMethod(name, signature, image_pointer_size_); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 7626 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7627 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 7628 | } else { |
| 7629 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 7630 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7631 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 7632 | break; |
| 7633 | case kInterface: |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7634 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7635 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 7636 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7637 | resolved = klass->FindVirtualMethod(name, signature, image_pointer_size_); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 7638 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7639 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 7640 | } else { |
| 7641 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 7642 | } |
| 7643 | } |
| 7644 | break; |
| 7645 | case kSuper: |
| 7646 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7647 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7648 | } else { |
| 7649 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 7650 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 7651 | break; |
| 7652 | case kVirtual: |
| 7653 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7654 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 7655 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7656 | resolved = klass->FindInterfaceMethod(name, signature, image_pointer_size_); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 7657 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7658 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 7659 | } else { |
| 7660 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 7661 | } |
| 7662 | } |
| 7663 | break; |
| 7664 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7665 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7666 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 7667 | Thread::Current()->AssertPendingException(); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7668 | return nullptr; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7669 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7670 | } |
| 7671 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 7672 | ArtMethod* ClassLinker::ResolveMethodWithoutInvokeType(const DexFile& dex_file, |
| 7673 | uint32_t method_idx, |
| 7674 | Handle<mirror::DexCache> dex_cache, |
| 7675 | Handle<mirror::ClassLoader> class_loader) { |
| 7676 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, image_pointer_size_); |
| 7677 | if (resolved != nullptr && !resolved->IsRuntimeMethod()) { |
| 7678 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
| 7679 | return resolved; |
| 7680 | } |
| 7681 | // Fail, get the declaring class. |
| 7682 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
| 7683 | mirror::Class* klass = ResolveType(dex_file, method_id.class_idx_, dex_cache, class_loader); |
| 7684 | if (klass == nullptr) { |
| 7685 | Thread::Current()->AssertPendingException(); |
| 7686 | return nullptr; |
| 7687 | } |
| 7688 | if (klass->IsInterface()) { |
| 7689 | LOG(FATAL) << "ResolveAmbiguousMethod: unexpected method in interface: " << PrettyClass(klass); |
| 7690 | return nullptr; |
| 7691 | } |
| 7692 | |
| 7693 | // Search both direct and virtual methods |
| 7694 | resolved = klass->FindDirectMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 7695 | if (resolved == nullptr) { |
| 7696 | resolved = klass->FindVirtualMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 7697 | } |
| 7698 | |
| 7699 | return resolved; |
| 7700 | } |
| 7701 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7702 | ArtField* ClassLinker::ResolveField(const DexFile& dex_file, |
| 7703 | uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7704 | Handle<mirror::DexCache> dex_cache, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7705 | Handle<mirror::ClassLoader> class_loader, |
| 7706 | bool is_static) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 7707 | DCHECK(dex_cache.Get() != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7708 | ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 7709 | if (resolved != nullptr) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7710 | return resolved; |
| 7711 | } |
| 7712 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 7713 | Thread* const self = Thread::Current(); |
| 7714 | StackHandleScope<1> hs(self); |
| 7715 | Handle<mirror::Class> klass( |
| 7716 | 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] | 7717 | if (klass.Get() == nullptr) { |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 7718 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 7719 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7720 | } |
| 7721 | |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 7722 | if (is_static) { |
Vladimir Marko | bb268b1 | 2016-06-30 15:52:56 +0100 | [diff] [blame] | 7723 | resolved = mirror::Class::FindStaticField(self, klass.Get(), dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 7724 | } else { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 7725 | resolved = klass->FindInstanceField(dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 7726 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 7727 | |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 7728 | if (resolved == nullptr) { |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 7729 | const char* name = dex_file.GetFieldName(field_id); |
| 7730 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 7731 | if (is_static) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 7732 | resolved = mirror::Class::FindStaticField(self, klass, name, type); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 7733 | } else { |
| 7734 | resolved = klass->FindInstanceField(name, type); |
| 7735 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 7736 | if (resolved == nullptr) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 7737 | ThrowNoSuchFieldError(is_static ? "static " : "instance ", klass.Get(), type, name); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7738 | return nullptr; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 7739 | } |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 7740 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7741 | dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 7742 | return resolved; |
| 7743 | } |
| 7744 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7745 | ArtField* ClassLinker::ResolveFieldJLS(const DexFile& dex_file, |
| 7746 | uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7747 | Handle<mirror::DexCache> dex_cache, |
| 7748 | Handle<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 7749 | DCHECK(dex_cache.Get() != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7750 | ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 7751 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 7752 | return resolved; |
| 7753 | } |
| 7754 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 7755 | Thread* self = Thread::Current(); |
| 7756 | StackHandleScope<1> hs(self); |
| 7757 | Handle<mirror::Class> klass( |
| 7758 | hs.NewHandle(ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7759 | if (klass.Get() == nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 7760 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7761 | return nullptr; |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 7762 | } |
| 7763 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 7764 | StringPiece name(dex_file.StringDataByIdx(field_id.name_idx_)); |
| 7765 | StringPiece type(dex_file.StringDataByIdx( |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 7766 | dex_file.GetTypeId(field_id.type_idx_).descriptor_idx_)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 7767 | resolved = mirror::Class::FindField(self, klass, name, type); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7768 | if (resolved != nullptr) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7769 | dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 7770 | } else { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 7771 | ThrowNoSuchFieldError("", klass.Get(), type, name); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7772 | } |
| 7773 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 7774 | } |
| 7775 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7776 | const char* ClassLinker::MethodShorty(uint32_t method_idx, |
| 7777 | ArtMethod* referrer, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 7778 | uint32_t* length) { |
| 7779 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
| 7780 | mirror::DexCache* dex_cache = declaring_class->GetDexCache(); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 7781 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 7782 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 7783 | return dex_file.GetMethodShorty(method_id, length); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 7784 | } |
| 7785 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 7786 | class DumpClassVisitor : public ClassVisitor { |
| 7787 | public: |
| 7788 | explicit DumpClassVisitor(int flags) : flags_(flags) {} |
| 7789 | |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 7790 | bool operator()(mirror::Class* klass) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 7791 | klass->DumpClass(LOG(ERROR), flags_); |
| 7792 | return true; |
| 7793 | } |
| 7794 | |
| 7795 | private: |
| 7796 | const int flags_; |
| 7797 | }; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 7798 | |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 7799 | void ClassLinker::DumpAllClasses(int flags) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 7800 | DumpClassVisitor visitor(flags); |
| 7801 | VisitClasses(&visitor); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 7802 | } |
| 7803 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 7804 | static OatFile::OatMethod CreateOatMethod(const void* code) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 7805 | CHECK(code != nullptr); |
Mathieu Chartier | 957ca1c | 2014-11-21 16:51:29 -0800 | [diff] [blame] | 7806 | const uint8_t* base = reinterpret_cast<const uint8_t*>(code); // Base of data points at code. |
| 7807 | base -= sizeof(void*); // Move backward so that code_offset != 0. |
| 7808 | const uint32_t code_offset = sizeof(void*); |
| 7809 | return OatFile::OatMethod(base, code_offset); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 7810 | } |
| 7811 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 7812 | bool ClassLinker::IsQuickResolutionStub(const void* entry_point) const { |
| 7813 | return (entry_point == GetQuickResolutionStub()) || |
| 7814 | (quick_resolution_trampoline_ == entry_point); |
| 7815 | } |
| 7816 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 7817 | bool ClassLinker::IsQuickToInterpreterBridge(const void* entry_point) const { |
| 7818 | return (entry_point == GetQuickToInterpreterBridge()) || |
| 7819 | (quick_to_interpreter_bridge_trampoline_ == entry_point); |
| 7820 | } |
| 7821 | |
| 7822 | bool ClassLinker::IsQuickGenericJniStub(const void* entry_point) const { |
| 7823 | return (entry_point == GetQuickGenericJniStub()) || |
| 7824 | (quick_generic_jni_trampoline_ == entry_point); |
| 7825 | } |
| 7826 | |
| 7827 | const void* ClassLinker::GetRuntimeQuickGenericJniStub() const { |
| 7828 | return GetQuickGenericJniStub(); |
| 7829 | } |
| 7830 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7831 | void ClassLinker::SetEntryPointsToCompiledCode(ArtMethod* method, |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 7832 | const void* method_code) const { |
| 7833 | OatFile::OatMethod oat_method = CreateOatMethod(method_code); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 7834 | oat_method.LinkMethod(method); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 7835 | } |
| 7836 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7837 | void ClassLinker::SetEntryPointsToInterpreter(ArtMethod* method) const { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 7838 | if (!method->IsNative()) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 7839 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 7840 | } else { |
| 7841 | const void* quick_method_code = GetQuickGenericJniStub(); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 7842 | OatFile::OatMethod oat_method = CreateOatMethod(quick_method_code); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 7843 | oat_method.LinkMethod(method); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 7844 | } |
| 7845 | } |
| 7846 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 7847 | void ClassLinker::DumpForSigQuit(std::ostream& os) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 7848 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 7849 | if (dex_cache_boot_image_class_lookup_required_) { |
| 7850 | AddBootImageClassesToClassTable(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 7851 | } |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 7852 | ReaderMutexLock mu(soa.Self(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 7853 | os << "Zygote loaded classes=" << NumZygoteClasses() << " post zygote classes=" |
| 7854 | << NumNonZygoteClasses() << "\n"; |
| 7855 | } |
| 7856 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 7857 | class CountClassesVisitor : public ClassLoaderVisitor { |
| 7858 | public: |
| 7859 | CountClassesVisitor() : num_zygote_classes(0), num_non_zygote_classes(0) {} |
| 7860 | |
| 7861 | void Visit(mirror::ClassLoader* class_loader) |
| 7862 | SHARED_REQUIRES(Locks::classlinker_classes_lock_, Locks::mutator_lock_) OVERRIDE { |
| 7863 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 7864 | if (class_table != nullptr) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 7865 | num_zygote_classes += class_table->NumZygoteClasses(); |
| 7866 | num_non_zygote_classes += class_table->NumNonZygoteClasses(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 7867 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 7868 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 7869 | |
| 7870 | size_t num_zygote_classes; |
| 7871 | size_t num_non_zygote_classes; |
| 7872 | }; |
| 7873 | |
| 7874 | size_t ClassLinker::NumZygoteClasses() const { |
| 7875 | CountClassesVisitor visitor; |
| 7876 | VisitClassLoaders(&visitor); |
| 7877 | return visitor.num_zygote_classes + boot_class_table_.NumZygoteClasses(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 7878 | } |
| 7879 | |
| 7880 | size_t ClassLinker::NumNonZygoteClasses() const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 7881 | CountClassesVisitor visitor; |
| 7882 | VisitClassLoaders(&visitor); |
| 7883 | return visitor.num_non_zygote_classes + boot_class_table_.NumNonZygoteClasses(); |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 7884 | } |
| 7885 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 7886 | size_t ClassLinker::NumLoadedClasses() { |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 7887 | if (dex_cache_boot_image_class_lookup_required_) { |
| 7888 | AddBootImageClassesToClassTable(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 7889 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 7890 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 7891 | // Only return non zygote classes since these are the ones which apps which care about. |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 7892 | return NumNonZygoteClasses(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 7893 | } |
| 7894 | |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 7895 | pid_t ClassLinker::GetClassesLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 7896 | return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 7897 | } |
| 7898 | |
| 7899 | pid_t ClassLinker::GetDexLockOwner() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 7900 | return dex_lock_.GetExclusiveOwnerTid(); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 7901 | } |
| 7902 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 7903 | void ClassLinker::SetClassRoot(ClassRoot class_root, mirror::Class* klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 7904 | DCHECK(!init_done_); |
| 7905 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7906 | DCHECK(klass != nullptr); |
| 7907 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 7908 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 7909 | mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7910 | DCHECK(class_roots != nullptr); |
| 7911 | DCHECK(class_roots->Get(class_root) == nullptr); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 7912 | class_roots->Set<false>(class_root, klass); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 7913 | } |
| 7914 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 7915 | const char* ClassLinker::GetClassRootDescriptor(ClassRoot class_root) { |
| 7916 | static const char* class_roots_descriptors[] = { |
| 7917 | "Ljava/lang/Class;", |
| 7918 | "Ljava/lang/Object;", |
| 7919 | "[Ljava/lang/Class;", |
| 7920 | "[Ljava/lang/Object;", |
| 7921 | "Ljava/lang/String;", |
| 7922 | "Ljava/lang/DexCache;", |
| 7923 | "Ljava/lang/ref/Reference;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 7924 | "Ljava/lang/reflect/Constructor;", |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 7925 | "Ljava/lang/reflect/Field;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 7926 | "Ljava/lang/reflect/Method;", |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 7927 | "Ljava/lang/reflect/Proxy;", |
| 7928 | "[Ljava/lang/String;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 7929 | "[Ljava/lang/reflect/Constructor;", |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 7930 | "[Ljava/lang/reflect/Field;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 7931 | "[Ljava/lang/reflect/Method;", |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 7932 | "Ljava/lang/ClassLoader;", |
| 7933 | "Ljava/lang/Throwable;", |
| 7934 | "Ljava/lang/ClassNotFoundException;", |
| 7935 | "Ljava/lang/StackTraceElement;", |
| 7936 | "Z", |
| 7937 | "B", |
| 7938 | "C", |
| 7939 | "D", |
| 7940 | "F", |
| 7941 | "I", |
| 7942 | "J", |
| 7943 | "S", |
| 7944 | "V", |
| 7945 | "[Z", |
| 7946 | "[B", |
| 7947 | "[C", |
| 7948 | "[D", |
| 7949 | "[F", |
| 7950 | "[I", |
| 7951 | "[J", |
| 7952 | "[S", |
| 7953 | "[Ljava/lang/StackTraceElement;", |
| 7954 | }; |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 7955 | static_assert(arraysize(class_roots_descriptors) == size_t(kClassRootsMax), |
| 7956 | "Mismatch between class descriptors and class-root enum"); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 7957 | |
| 7958 | const char* descriptor = class_roots_descriptors[class_root]; |
| 7959 | CHECK(descriptor != nullptr); |
| 7960 | return descriptor; |
| 7961 | } |
| 7962 | |
Jeff Hao | f0192c8 | 2016-03-28 20:39:50 -0700 | [diff] [blame] | 7963 | jobject ClassLinker::CreatePathClassLoader(Thread* self, |
| 7964 | const std::vector<const DexFile*>& dex_files) { |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 7965 | // SOAAlreadyRunnable is protected, and we need something to add a global reference. |
| 7966 | // We could move the jobject to the callers, but all call-sites do this... |
| 7967 | ScopedObjectAccessUnchecked soa(self); |
| 7968 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 7969 | // For now, create a libcore-level DexFile for each ART DexFile. This "explodes" multidex. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7970 | StackHandleScope<10> hs(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 7971 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7972 | ArtField* dex_elements_field = |
| 7973 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList_dexElements); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 7974 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7975 | mirror::Class* dex_elements_class = dex_elements_field->GetType<true>(); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 7976 | DCHECK(dex_elements_class != nullptr); |
| 7977 | DCHECK(dex_elements_class->IsArrayClass()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 7978 | Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements(hs.NewHandle( |
| 7979 | mirror::ObjectArray<mirror::Object>::Alloc(self, dex_elements_class, dex_files.size()))); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 7980 | Handle<mirror::Class> h_dex_element_class = |
| 7981 | hs.NewHandle(dex_elements_class->GetComponentType()); |
| 7982 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7983 | ArtField* element_file_field = |
| 7984 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
| 7985 | DCHECK_EQ(h_dex_element_class.Get(), element_file_field->GetDeclaringClass()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 7986 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7987 | ArtField* cookie_field = soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_cookie); |
| 7988 | DCHECK_EQ(cookie_field->GetDeclaringClass(), element_file_field->GetType<false>()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 7989 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 7990 | ArtField* file_name_field = soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_fileName); |
| 7991 | DCHECK_EQ(file_name_field->GetDeclaringClass(), element_file_field->GetType<false>()); |
| 7992 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 7993 | // Fill the elements array. |
| 7994 | int32_t index = 0; |
| 7995 | for (const DexFile* dex_file : dex_files) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 7996 | StackHandleScope<4> hs2(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 7997 | |
Mathieu Chartier | 966878d | 2016-01-14 14:33:29 -0800 | [diff] [blame] | 7998 | // CreatePathClassLoader is only used by gtests. Index 0 of h_long_array is supposed to be the |
| 7999 | // oat file but we can leave it null. |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 8000 | Handle<mirror::LongArray> h_long_array = hs2.NewHandle(mirror::LongArray::Alloc( |
| 8001 | self, |
| 8002 | kDexFileIndexStart + 1)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8003 | DCHECK(h_long_array.Get() != nullptr); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 8004 | h_long_array->Set(kDexFileIndexStart, reinterpret_cast<intptr_t>(dex_file)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8005 | |
| 8006 | Handle<mirror::Object> h_dex_file = hs2.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8007 | cookie_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8008 | DCHECK(h_dex_file.Get() != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8009 | cookie_field->SetObject<false>(h_dex_file.Get(), h_long_array.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8010 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 8011 | Handle<mirror::String> h_file_name = hs2.NewHandle( |
| 8012 | mirror::String::AllocFromModifiedUtf8(self, dex_file->GetLocation().c_str())); |
| 8013 | DCHECK(h_file_name.Get() != nullptr); |
| 8014 | file_name_field->SetObject<false>(h_dex_file.Get(), h_file_name.Get()); |
| 8015 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8016 | Handle<mirror::Object> h_element = hs2.NewHandle(h_dex_element_class->AllocObject(self)); |
| 8017 | DCHECK(h_element.Get() != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8018 | element_file_field->SetObject<false>(h_element.Get(), h_dex_file.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8019 | |
| 8020 | h_dex_elements->Set(index, h_element.Get()); |
| 8021 | index++; |
| 8022 | } |
| 8023 | DCHECK_EQ(index, h_dex_elements->GetLength()); |
| 8024 | |
| 8025 | // Create DexPathList. |
| 8026 | Handle<mirror::Object> h_dex_path_list = hs.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8027 | dex_elements_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8028 | DCHECK(h_dex_path_list.Get() != nullptr); |
| 8029 | // Set elements. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8030 | dex_elements_field->SetObject<false>(h_dex_path_list.Get(), h_dex_elements.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8031 | |
| 8032 | // Create PathClassLoader. |
| 8033 | Handle<mirror::Class> h_path_class_class = hs.NewHandle( |
| 8034 | soa.Decode<mirror::Class*>(WellKnownClasses::dalvik_system_PathClassLoader)); |
| 8035 | Handle<mirror::Object> h_path_class_loader = hs.NewHandle( |
| 8036 | h_path_class_class->AllocObject(self)); |
| 8037 | DCHECK(h_path_class_loader.Get() != nullptr); |
| 8038 | // Set DexPathList. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8039 | ArtField* path_list_field = |
| 8040 | soa.DecodeField(WellKnownClasses::dalvik_system_PathClassLoader_pathList); |
| 8041 | DCHECK(path_list_field != nullptr); |
| 8042 | path_list_field->SetObject<false>(h_path_class_loader.Get(), h_dex_path_list.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8043 | |
| 8044 | // Make a pretend boot-classpath. |
| 8045 | // TODO: Should we scan the image? |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8046 | ArtField* const parent_field = |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8047 | mirror::Class::FindField(self, hs.NewHandle(h_path_class_loader->GetClass()), "parent", |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8048 | "Ljava/lang/ClassLoader;"); |
Roland Levillain | f39c9eb | 2015-05-26 15:02:07 +0100 | [diff] [blame] | 8049 | DCHECK(parent_field != nullptr); |
Mathieu Chartier | 966878d | 2016-01-14 14:33:29 -0800 | [diff] [blame] | 8050 | mirror::Object* boot_cl = |
| 8051 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_BootClassLoader)->AllocObject(self); |
| 8052 | parent_field->SetObject<false>(h_path_class_loader.Get(), boot_cl); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8053 | |
| 8054 | // Make it a global ref and return. |
| 8055 | ScopedLocalRef<jobject> local_ref( |
| 8056 | soa.Env(), soa.Env()->AddLocalReference<jobject>(h_path_class_loader.Get())); |
| 8057 | return soa.Env()->NewGlobalRef(local_ref.get()); |
| 8058 | } |
| 8059 | |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 8060 | ArtMethod* ClassLinker::CreateRuntimeMethod(LinearAlloc* linear_alloc) { |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 8061 | const size_t method_alignment = ArtMethod::Alignment(image_pointer_size_); |
| 8062 | const size_t method_size = ArtMethod::Size(image_pointer_size_); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 8063 | LengthPrefixedArray<ArtMethod>* method_array = AllocArtMethodArray( |
| 8064 | Thread::Current(), |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 8065 | linear_alloc, |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 8066 | 1); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 8067 | ArtMethod* method = &method_array->At(0, method_size, method_alignment); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8068 | CHECK(method != nullptr); |
| 8069 | method->SetDexMethodIndex(DexFile::kDexNoIndex); |
| 8070 | CHECK(method->IsRuntimeMethod()); |
| 8071 | return method; |
| 8072 | } |
| 8073 | |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 8074 | void ClassLinker::DropFindArrayClassCache() { |
| 8075 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
| 8076 | find_array_class_cache_next_victim_ = 0; |
| 8077 | } |
| 8078 | |
Mathieu Chartier | 696632e | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 8079 | void ClassLinker::ClearClassTableStrongRoots() const { |
| 8080 | Thread* const self = Thread::Current(); |
| 8081 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 8082 | for (const ClassLoaderData& data : class_loaders_) { |
| 8083 | if (data.class_table != nullptr) { |
| 8084 | data.class_table->ClearStrongRoots(); |
| 8085 | } |
| 8086 | } |
| 8087 | } |
| 8088 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 8089 | void ClassLinker::VisitClassLoaders(ClassLoaderVisitor* visitor) const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8090 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 8091 | for (const ClassLoaderData& data : class_loaders_) { |
Mathieu Chartier | 4843bd5 | 2015-10-01 17:08:44 -0700 | [diff] [blame] | 8092 | // Need to use DecodeJObject so that we get null for cleared JNI weak globals. |
| 8093 | auto* const class_loader = down_cast<mirror::ClassLoader*>(self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8094 | if (class_loader != nullptr) { |
| 8095 | visitor->Visit(class_loader); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8096 | } |
| 8097 | } |
| 8098 | } |
| 8099 | |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8100 | void ClassLinker::InsertDexFileInToClassLoader(mirror::Object* dex_file, |
| 8101 | mirror::ClassLoader* class_loader) { |
| 8102 | DCHECK(dex_file != nullptr); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8103 | Thread* const self = Thread::Current(); |
| 8104 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 6d25cf6 | 2016-04-12 16:54:48 -0700 | [diff] [blame] | 8105 | ClassTable* const table = ClassTableForClassLoader(class_loader); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8106 | DCHECK(table != nullptr); |
Mathieu Chartier | 696632e | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 8107 | if (table->InsertStrongRoot(dex_file) && class_loader != nullptr) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8108 | // It was not already inserted, perform the write barrier to let the GC know the class loader's |
| 8109 | // class table was modified. |
| 8110 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader); |
| 8111 | } |
| 8112 | } |
| 8113 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 8114 | void ClassLinker::CleanupClassLoaders() { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8115 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame^] | 8116 | std::vector<ClassLoaderData> to_delete; |
| 8117 | // Do the delete outside the lock to avoid lock violation in jit code cache. |
| 8118 | { |
| 8119 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 8120 | for (auto it = class_loaders_.begin(); it != class_loaders_.end(); ) { |
| 8121 | const ClassLoaderData& data = *it; |
| 8122 | // Need to use DecodeJObject so that we get null for cleared JNI weak globals. |
| 8123 | auto* const class_loader = |
| 8124 | down_cast<mirror::ClassLoader*>(self->DecodeJObject(data.weak_root)); |
| 8125 | if (class_loader != nullptr) { |
| 8126 | ++it; |
| 8127 | } else { |
| 8128 | VLOG(class_linker) << "Freeing class loader"; |
| 8129 | to_delete.push_back(data); |
| 8130 | it = class_loaders_.erase(it); |
| 8131 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8132 | } |
| 8133 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame^] | 8134 | for (ClassLoaderData& data : to_delete) { |
| 8135 | DeleteClassLoader(self, data); |
| 8136 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8137 | } |
| 8138 | |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8139 | std::set<DexCacheResolvedClasses> ClassLinker::GetResolvedClasses(bool ignore_boot_classes) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 8140 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8141 | ScopedObjectAccess soa(Thread::Current()); |
| 8142 | ScopedAssertNoThreadSuspension ants(soa.Self(), __FUNCTION__); |
| 8143 | std::set<DexCacheResolvedClasses> ret; |
| 8144 | VLOG(class_linker) << "Collecting resolved classes"; |
| 8145 | const uint64_t start_time = NanoTime(); |
| 8146 | ReaderMutexLock mu(soa.Self(), *DexLock()); |
| 8147 | // Loop through all the dex caches and inspect resolved classes. |
| 8148 | for (const ClassLinker::DexCacheData& data : GetDexCachesData()) { |
| 8149 | if (soa.Self()->IsJWeakCleared(data.weak_root)) { |
| 8150 | continue; |
| 8151 | } |
| 8152 | mirror::DexCache* dex_cache = |
| 8153 | down_cast<mirror::DexCache*>(soa.Self()->DecodeJObject(data.weak_root)); |
| 8154 | if (dex_cache == nullptr) { |
| 8155 | continue; |
| 8156 | } |
| 8157 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 8158 | const std::string& location = dex_file->GetLocation(); |
| 8159 | const size_t num_class_defs = dex_file->NumClassDefs(); |
| 8160 | // Use the resolved types, this will miss array classes. |
| 8161 | const size_t num_types = dex_file->NumTypeIds(); |
| 8162 | VLOG(class_linker) << "Collecting class profile for dex file " << location |
| 8163 | << " types=" << num_types << " class_defs=" << num_class_defs; |
| 8164 | DexCacheResolvedClasses resolved_classes(dex_file->GetLocation(), |
Mathieu Chartier | b384e5e | 2016-04-29 12:03:56 -0700 | [diff] [blame] | 8165 | dex_file->GetBaseLocation(), |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8166 | dex_file->GetLocationChecksum()); |
| 8167 | size_t num_resolved = 0; |
| 8168 | std::unordered_set<uint16_t> class_set; |
| 8169 | CHECK_EQ(num_types, dex_cache->NumResolvedTypes()); |
| 8170 | for (size_t i = 0; i < num_types; ++i) { |
| 8171 | mirror::Class* klass = dex_cache->GetResolvedType(i); |
| 8172 | // Filter out null class loader since that is the boot class loader. |
| 8173 | if (klass == nullptr || (ignore_boot_classes && klass->GetClassLoader() == nullptr)) { |
| 8174 | continue; |
| 8175 | } |
| 8176 | ++num_resolved; |
| 8177 | DCHECK(!klass->IsProxyClass()); |
Andreas Gampe | 1a7beae | 2016-03-09 15:52:21 -0800 | [diff] [blame] | 8178 | if (!klass->IsResolved()) { |
| 8179 | DCHECK(klass->IsErroneous()); |
| 8180 | continue; |
| 8181 | } |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8182 | mirror::DexCache* klass_dex_cache = klass->GetDexCache(); |
| 8183 | if (klass_dex_cache == dex_cache) { |
| 8184 | const size_t class_def_idx = klass->GetDexClassDefIndex(); |
| 8185 | DCHECK(klass->IsResolved()); |
| 8186 | CHECK_LT(class_def_idx, num_class_defs); |
| 8187 | class_set.insert(class_def_idx); |
| 8188 | } |
| 8189 | } |
| 8190 | |
| 8191 | if (!class_set.empty()) { |
| 8192 | auto it = ret.find(resolved_classes); |
| 8193 | if (it != ret.end()) { |
| 8194 | // Already have the key, union the class def idxs. |
| 8195 | it->AddClasses(class_set.begin(), class_set.end()); |
| 8196 | } else { |
| 8197 | resolved_classes.AddClasses(class_set.begin(), class_set.end()); |
| 8198 | ret.insert(resolved_classes); |
| 8199 | } |
| 8200 | } |
| 8201 | |
| 8202 | VLOG(class_linker) << "Dex location " << location << " has " << num_resolved << " / " |
| 8203 | << num_class_defs << " resolved classes"; |
| 8204 | } |
| 8205 | VLOG(class_linker) << "Collecting class profile took " << PrettyDuration(NanoTime() - start_time); |
| 8206 | return ret; |
| 8207 | } |
| 8208 | |
| 8209 | std::unordered_set<std::string> ClassLinker::GetClassDescriptorsForProfileKeys( |
| 8210 | const std::set<DexCacheResolvedClasses>& classes) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 8211 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8212 | std::unordered_set<std::string> ret; |
| 8213 | Thread* const self = Thread::Current(); |
| 8214 | std::unordered_map<std::string, const DexFile*> location_to_dex_file; |
| 8215 | ScopedObjectAccess soa(self); |
| 8216 | ScopedAssertNoThreadSuspension ants(soa.Self(), __FUNCTION__); |
| 8217 | ReaderMutexLock mu(self, *DexLock()); |
| 8218 | for (const ClassLinker::DexCacheData& data : GetDexCachesData()) { |
| 8219 | if (!self->IsJWeakCleared(data.weak_root)) { |
| 8220 | mirror::DexCache* dex_cache = |
| 8221 | down_cast<mirror::DexCache*>(soa.Self()->DecodeJObject(data.weak_root)); |
| 8222 | if (dex_cache != nullptr) { |
| 8223 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 8224 | // There could be duplicates if two dex files with the same location are mapped. |
| 8225 | location_to_dex_file.emplace( |
| 8226 | ProfileCompilationInfo::GetProfileDexFileKey(dex_file->GetLocation()), dex_file); |
| 8227 | } |
| 8228 | } |
| 8229 | } |
| 8230 | for (const DexCacheResolvedClasses& info : classes) { |
| 8231 | const std::string& profile_key = info.GetDexLocation(); |
| 8232 | auto found = location_to_dex_file.find(profile_key); |
| 8233 | if (found != location_to_dex_file.end()) { |
| 8234 | const DexFile* dex_file = found->second; |
| 8235 | VLOG(profiler) << "Found opened dex file for " << dex_file->GetLocation() << " with " |
| 8236 | << info.GetClasses().size() << " classes"; |
| 8237 | DCHECK_EQ(dex_file->GetLocationChecksum(), info.GetLocationChecksum()); |
| 8238 | for (uint16_t class_def_idx : info.GetClasses()) { |
| 8239 | if (class_def_idx >= dex_file->NumClassDefs()) { |
| 8240 | LOG(WARNING) << "Class def index " << class_def_idx << " >= " << dex_file->NumClassDefs(); |
| 8241 | continue; |
| 8242 | } |
| 8243 | const DexFile::TypeId& type_id = dex_file->GetTypeId( |
| 8244 | dex_file->GetClassDef(class_def_idx).class_idx_); |
| 8245 | const char* descriptor = dex_file->GetTypeDescriptor(type_id); |
| 8246 | ret.insert(descriptor); |
| 8247 | } |
| 8248 | } else { |
| 8249 | VLOG(class_linker) << "Failed to find opened dex file for profile key " << profile_key; |
| 8250 | } |
| 8251 | } |
| 8252 | return ret; |
| 8253 | } |
| 8254 | |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame^] | 8255 | class ClassLinker::FindVirtualMethodHolderVisitor : public ClassVisitor { |
| 8256 | public: |
| 8257 | FindVirtualMethodHolderVisitor(const ArtMethod* method, PointerSize pointer_size) |
| 8258 | : method_(method), |
| 8259 | pointer_size_(pointer_size) {} |
| 8260 | |
| 8261 | bool operator()(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_) OVERRIDE { |
| 8262 | if (klass->GetVirtualMethodsSliceUnchecked(pointer_size_).Contains(method_)) { |
| 8263 | holder_ = klass; |
| 8264 | } |
| 8265 | // Return false to stop searching if holder_ is not null. |
| 8266 | return holder_ == nullptr; |
| 8267 | } |
| 8268 | |
| 8269 | mirror::Class* holder_ = nullptr; |
| 8270 | const ArtMethod* const method_; |
| 8271 | const PointerSize pointer_size_; |
| 8272 | }; |
| 8273 | |
| 8274 | mirror::Class* ClassLinker::GetHoldingClassOfCopiedMethod(ArtMethod* method) { |
| 8275 | ScopedTrace trace(__FUNCTION__); // Since this function is slow, have a trace to notify people. |
| 8276 | CHECK(method->IsCopied()); |
| 8277 | FindVirtualMethodHolderVisitor visitor(method, image_pointer_size_); |
| 8278 | VisitClasses(&visitor); |
| 8279 | return visitor.holder_; |
| 8280 | } |
| 8281 | |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 8282 | // Instantiate ResolveMethod. |
| 8283 | template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::kForceICCECheck>( |
| 8284 | const DexFile& dex_file, |
| 8285 | uint32_t method_idx, |
| 8286 | Handle<mirror::DexCache> dex_cache, |
| 8287 | Handle<mirror::ClassLoader> class_loader, |
| 8288 | ArtMethod* referrer, |
| 8289 | InvokeType type); |
| 8290 | template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::kNoICCECheckForCache>( |
| 8291 | const DexFile& dex_file, |
| 8292 | uint32_t method_idx, |
| 8293 | Handle<mirror::DexCache> dex_cache, |
| 8294 | Handle<mirror::ClassLoader> class_loader, |
| 8295 | ArtMethod* referrer, |
| 8296 | InvokeType type); |
| 8297 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8298 | } // namespace art |