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> |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 22 | #include <map> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 23 | #include <memory> |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 24 | #include <queue> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 25 | #include <string> |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 26 | #include <tuple> |
Andreas Gampe | a696c0a | 2014-12-10 20:51:45 -0800 | [diff] [blame] | 27 | #include <unistd.h> |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 28 | #include <unordered_map> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 29 | #include <utility> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 30 | #include <vector> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 31 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 32 | #include "android-base/stringprintf.h" |
| 33 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 34 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 35 | #include "art_method-inl.h" |
| 36 | #include "base/arena_allocator.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 37 | #include "base/casts.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 38 | #include "base/logging.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 39 | #include "base/scoped_arena_containers.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 40 | #include "base/scoped_flock.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 41 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 42 | #include "base/systrace.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 43 | #include "base/time_utils.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 44 | #include "base/unix_file/fd_file.h" |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 45 | #include "base/value_object.h" |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 46 | #include "cha.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 47 | #include "class_linker-inl.h" |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 48 | #include "class_table-inl.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 49 | #include "compiler_callbacks.h" |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 50 | #include "debugger.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 51 | #include "dex_file-inl.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 52 | #include "entrypoints/entrypoint_utils.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 53 | #include "entrypoints/runtime_asm_entrypoints.h" |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 54 | #include "experimental_flags.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 55 | #include "gc_root-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 56 | #include "gc/accounting/card_table-inl.h" |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 57 | #include "gc/accounting/heap_bitmap-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 58 | #include "gc/heap.h" |
Mathieu Chartier | 1b1e31f | 2016-05-19 10:13:04 -0700 | [diff] [blame] | 59 | #include "gc/scoped_gc_critical_section.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 60 | #include "gc/space/image_space.h" |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 61 | #include "gc/space/space-inl.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 62 | #include "handle_scope-inl.h" |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 63 | #include "image-inl.h" |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 64 | #include "imt_conflict_table.h" |
| 65 | #include "imtable-inl.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 66 | #include "intern_table.h" |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 67 | #include "interpreter/interpreter.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 68 | #include "jit/jit.h" |
| 69 | #include "jit/jit_code_cache.h" |
Calin Juravle | 33083d6 | 2017-01-18 15:29:12 -0800 | [diff] [blame] | 70 | #include "jit/profile_compilation_info.h" |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 71 | #include "jni_internal.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 72 | #include "leb128.h" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 73 | #include "linear_alloc.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 74 | #include "mirror/call_site.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 75 | #include "mirror/class.h" |
| 76 | #include "mirror/class-inl.h" |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 77 | #include "mirror/class_ext.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 78 | #include "mirror/class_loader.h" |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 79 | #include "mirror/dex_cache.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 80 | #include "mirror/dex_cache-inl.h" |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 81 | #include "mirror/emulated_stack_frame.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 82 | #include "mirror/field.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 83 | #include "mirror/iftable-inl.h" |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 84 | #include "mirror/method.h" |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 85 | #include "mirror/method_type.h" |
| 86 | #include "mirror/method_handle_impl.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 87 | #include "mirror/method_handles_lookup.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 88 | #include "mirror/object-inl.h" |
| 89 | #include "mirror/object_array-inl.h" |
| 90 | #include "mirror/proxy.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 91 | #include "mirror/reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 92 | #include "mirror/stack_trace_element.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 93 | #include "mirror/string-inl.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 94 | #include "native/dalvik_system_DexFile.h" |
| 95 | #include "oat.h" |
| 96 | #include "oat_file.h" |
| 97 | #include "oat_file-inl.h" |
| 98 | #include "oat_file_assistant.h" |
| 99 | #include "oat_file_manager.h" |
| 100 | #include "object_lock.h" |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 101 | #include "os.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 102 | #include "runtime.h" |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 103 | #include "runtime_callbacks.h" |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 104 | #include "ScopedLocalRef.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 105 | #include "scoped_thread_state_change-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 106 | #include "thread-inl.h" |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 107 | #include "thread_list.h" |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 108 | #include "trace.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 109 | #include "utils.h" |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 110 | #include "utils/dex_cache_arrays_layout-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 111 | #include "verifier/method_verifier.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 112 | #include "well_known_classes.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 113 | |
| 114 | namespace art { |
| 115 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 116 | using android::base::StringPrintf; |
| 117 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 118 | static constexpr bool kSanityCheckObjects = kIsDebugBuild; |
Mathieu Chartier | 8790c7f | 2016-03-31 15:05:45 -0700 | [diff] [blame] | 119 | static constexpr bool kVerifyArtMethodDeclaringClasses = kIsDebugBuild; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 120 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 121 | static void ThrowNoClassDefFoundError(const char* fmt, ...) |
| 122 | __attribute__((__format__(__printf__, 1, 2))) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 123 | REQUIRES_SHARED(Locks::mutator_lock_); |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 124 | static void ThrowNoClassDefFoundError(const char* fmt, ...) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 125 | va_list args; |
| 126 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 127 | Thread* self = Thread::Current(); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 128 | self->ThrowNewExceptionV("Ljava/lang/NoClassDefFoundError;", fmt, args); |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 129 | va_end(args); |
| 130 | } |
| 131 | |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 132 | static bool HasInitWithString(Thread* self, ClassLinker* class_linker, const char* descriptor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 133 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 134 | ArtMethod* method = self->GetCurrentMethod(nullptr); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 135 | StackHandleScope<1> hs(self); |
| 136 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(method != nullptr ? |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 137 | method->GetDeclaringClass()->GetClassLoader() : nullptr)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 138 | ObjPtr<mirror::Class> exception_class = class_linker->FindClass(self, descriptor, class_loader); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 139 | |
| 140 | if (exception_class == nullptr) { |
| 141 | // No exc class ~ no <init>-with-string. |
| 142 | CHECK(self->IsExceptionPending()); |
| 143 | self->ClearException(); |
| 144 | return false; |
| 145 | } |
| 146 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 147 | ArtMethod* exception_init_method = exception_class->FindDeclaredDirectMethod( |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 148 | "<init>", "(Ljava/lang/String;)V", class_linker->GetImagePointerSize()); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 149 | return exception_init_method != nullptr; |
| 150 | } |
| 151 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 152 | static mirror::Object* GetVerifyError(ObjPtr<mirror::Class> c) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 153 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 154 | ObjPtr<mirror::ClassExt> ext(c->GetExtData()); |
| 155 | if (ext == nullptr) { |
| 156 | return nullptr; |
| 157 | } else { |
| 158 | return ext->GetVerifyError(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // Helper for ThrowEarlierClassFailure. Throws the stored error. |
| 163 | static void HandleEarlierVerifyError(Thread* self, |
| 164 | ClassLinker* class_linker, |
| 165 | ObjPtr<mirror::Class> c) |
| 166 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 167 | ObjPtr<mirror::Object> obj = GetVerifyError(c); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 168 | DCHECK(obj != nullptr); |
| 169 | self->AssertNoPendingException(); |
| 170 | if (obj->IsClass()) { |
| 171 | // Previous error has been stored as class. Create a new exception of that type. |
| 172 | |
| 173 | // It's possible the exception doesn't have a <init>(String). |
| 174 | std::string temp; |
| 175 | const char* descriptor = obj->AsClass()->GetDescriptor(&temp); |
| 176 | |
| 177 | if (HasInitWithString(self, class_linker, descriptor)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 178 | self->ThrowNewException(descriptor, c->PrettyDescriptor().c_str()); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 179 | } else { |
| 180 | self->ThrowNewException(descriptor, nullptr); |
| 181 | } |
| 182 | } else { |
| 183 | // Previous error has been stored as an instance. Just rethrow. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 184 | ObjPtr<mirror::Class> throwable_class = |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 185 | self->DecodeJObject(WellKnownClasses::java_lang_Throwable)->AsClass(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 186 | ObjPtr<mirror::Class> error_class = obj->GetClass(); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 187 | CHECK(throwable_class->IsAssignableFrom(error_class)); |
| 188 | self->SetException(obj->AsThrowable()); |
| 189 | } |
| 190 | self->AssertPendingException(); |
| 191 | } |
| 192 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 193 | void ClassLinker::ThrowEarlierClassFailure(ObjPtr<mirror::Class> c, bool wrap_in_no_class_def) { |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 194 | // The class failed to initialize on a previous attempt, so we want to throw |
| 195 | // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we |
| 196 | // failed in verification, in which case v2 5.4.1 says we need to re-throw |
| 197 | // the previous error. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 198 | Runtime* const runtime = Runtime::Current(); |
| 199 | if (!runtime->IsAotCompiler()) { // Give info if this occurs at runtime. |
Andreas Gampe | 3d6b470 | 2015-09-21 08:35:52 -0700 | [diff] [blame] | 200 | std::string extra; |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 201 | if (GetVerifyError(c) != nullptr) { |
| 202 | ObjPtr<mirror::Object> verify_error = GetVerifyError(c); |
Andreas Gampe | 369c851 | 2016-01-28 15:31:39 -0800 | [diff] [blame] | 203 | if (verify_error->IsClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 204 | extra = mirror::Class::PrettyDescriptor(verify_error->AsClass()); |
Andreas Gampe | 369c851 | 2016-01-28 15:31:39 -0800 | [diff] [blame] | 205 | } else { |
| 206 | extra = verify_error->AsThrowable()->Dump(); |
| 207 | } |
Andreas Gampe | 3d6b470 | 2015-09-21 08:35:52 -0700 | [diff] [blame] | 208 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 209 | LOG(INFO) << "Rejecting re-init on previously-failed class " << c->PrettyClass() |
| 210 | << ": " << extra; |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 211 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 212 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 213 | CHECK(c->IsErroneous()) << c->PrettyClass() << " " << c->GetStatus(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 214 | Thread* self = Thread::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 215 | if (runtime->IsAotCompiler()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 216 | // At compile time, accurate errors and NCDFE are disabled to speed compilation. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 217 | ObjPtr<mirror::Throwable> pre_allocated = runtime->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 218 | self->SetException(pre_allocated); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 219 | } else { |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 220 | if (GetVerifyError(c) != nullptr) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 221 | // Rethrow stored error. |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 222 | HandleEarlierVerifyError(self, this, c); |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 223 | } |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 224 | // TODO This might be wrong if we hit an OOME while allocating the ClassExt. In that case we |
| 225 | // might have meant to go down the earlier if statement with the original error but it got |
| 226 | // swallowed by the OOM so we end up here. |
| 227 | if (GetVerifyError(c) == nullptr || wrap_in_no_class_def) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 228 | // If there isn't a recorded earlier error, or this is a repeat throw from initialization, |
| 229 | // the top-level exception must be a NoClassDefFoundError. The potentially already pending |
| 230 | // exception will be a cause. |
| 231 | self->ThrowNewWrappedException("Ljava/lang/NoClassDefFoundError;", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 232 | c->PrettyDescriptor().c_str()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 233 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 237 | static void VlogClassInitializationFailure(Handle<mirror::Class> klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 238 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 239 | if (VLOG_IS_ON(class_linker)) { |
| 240 | std::string temp; |
| 241 | LOG(INFO) << "Failed to initialize class " << klass->GetDescriptor(&temp) << " from " |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 242 | << klass->GetLocation() << "\n" << Thread::Current()->GetException()->Dump(); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
| 246 | static void WrapExceptionInInitializer(Handle<mirror::Class> klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 247 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 248 | Thread* self = Thread::Current(); |
| 249 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 250 | |
| 251 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 252 | CHECK(cause.get() != nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 253 | |
Andreas Gampe | 1e8a395 | 2016-11-30 10:13:19 -0800 | [diff] [blame] | 254 | // Boot classpath classes should not fail initialization. This is a sanity debug check. This |
| 255 | // cannot in general be guaranteed, but in all likelihood leads to breakage down the line. |
| 256 | if (klass->GetClassLoader() == nullptr && !Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | 22f71d2 | 2016-11-21 10:10:08 -0800 | [diff] [blame] | 257 | std::string tmp; |
Andreas Gampe | 1e8a395 | 2016-11-30 10:13:19 -0800 | [diff] [blame] | 258 | LOG(kIsDebugBuild ? FATAL : WARNING) << klass->GetDescriptor(&tmp) << " failed initialization"; |
Andreas Gampe | 22f71d2 | 2016-11-21 10:10:08 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 261 | env->ExceptionClear(); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 262 | bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error); |
| 263 | env->Throw(cause.get()); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 264 | |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 265 | // We only wrap non-Error exceptions; an Error can just be used as-is. |
| 266 | if (!is_error) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 267 | self->ThrowNewWrappedException("Ljava/lang/ExceptionInInitializerError;", nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 268 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 269 | VlogClassInitializationFailure(klass); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 272 | // Gap between two fields in object layout. |
| 273 | struct FieldGap { |
| 274 | uint32_t start_offset; // The offset from the start of the object. |
| 275 | uint32_t size; // The gap size of 1, 2, or 4 bytes. |
| 276 | }; |
| 277 | struct FieldGapsComparator { |
| 278 | explicit FieldGapsComparator() { |
| 279 | } |
| 280 | bool operator() (const FieldGap& lhs, const FieldGap& rhs) |
| 281 | NO_THREAD_SAFETY_ANALYSIS { |
Andreas Gampe | f52857f | 2015-02-18 15:38:57 -0800 | [diff] [blame] | 282 | // Sort by gap size, largest first. Secondary sort by starting offset. |
Richard Uhler | fab6788 | 2015-07-13 17:00:35 -0700 | [diff] [blame] | 283 | // Note that the priority queue returns the largest element, so operator() |
| 284 | // should return true if lhs is less than rhs. |
| 285 | 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] | 286 | } |
| 287 | }; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 288 | typedef std::priority_queue<FieldGap, std::vector<FieldGap>, FieldGapsComparator> FieldGaps; |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 289 | |
| 290 | // Adds largest aligned gaps to queue of gaps. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 291 | 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] | 292 | DCHECK(gaps != nullptr); |
| 293 | |
| 294 | uint32_t current_offset = gap_start; |
| 295 | while (current_offset != gap_end) { |
| 296 | size_t remaining = gap_end - current_offset; |
| 297 | if (remaining >= sizeof(uint32_t) && IsAligned<4>(current_offset)) { |
| 298 | gaps->push(FieldGap {current_offset, sizeof(uint32_t)}); |
| 299 | current_offset += sizeof(uint32_t); |
| 300 | } else if (remaining >= sizeof(uint16_t) && IsAligned<2>(current_offset)) { |
| 301 | gaps->push(FieldGap {current_offset, sizeof(uint16_t)}); |
| 302 | current_offset += sizeof(uint16_t); |
| 303 | } else { |
| 304 | gaps->push(FieldGap {current_offset, sizeof(uint8_t)}); |
| 305 | current_offset += sizeof(uint8_t); |
| 306 | } |
| 307 | DCHECK_LE(current_offset, gap_end) << "Overran gap"; |
| 308 | } |
| 309 | } |
| 310 | // Shuffle fields forward, making use of gaps whenever possible. |
| 311 | template<int n> |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 312 | static void ShuffleForward(size_t* current_field_idx, |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 313 | MemberOffset* field_offset, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 314 | std::deque<ArtField*>* grouped_and_sorted_fields, |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 315 | FieldGaps* gaps) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 316 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 317 | DCHECK(current_field_idx != nullptr); |
| 318 | DCHECK(grouped_and_sorted_fields != nullptr); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 319 | DCHECK(gaps != nullptr); |
| 320 | DCHECK(field_offset != nullptr); |
| 321 | |
| 322 | DCHECK(IsPowerOfTwo(n)); |
| 323 | while (!grouped_and_sorted_fields->empty()) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 324 | ArtField* field = grouped_and_sorted_fields->front(); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 325 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 326 | if (Primitive::ComponentSize(type) < n) { |
| 327 | break; |
| 328 | } |
| 329 | if (!IsAligned<n>(field_offset->Uint32Value())) { |
| 330 | MemberOffset old_offset = *field_offset; |
| 331 | *field_offset = MemberOffset(RoundUp(field_offset->Uint32Value(), n)); |
| 332 | AddFieldGap(old_offset.Uint32Value(), field_offset->Uint32Value(), gaps); |
| 333 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 334 | CHECK(type != Primitive::kPrimNot) << field->PrettyField(); // should be primitive types |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 335 | grouped_and_sorted_fields->pop_front(); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 336 | if (!gaps->empty() && gaps->top().size >= n) { |
| 337 | FieldGap gap = gaps->top(); |
| 338 | gaps->pop(); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 339 | DCHECK_ALIGNED(gap.start_offset, n); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 340 | field->SetOffset(MemberOffset(gap.start_offset)); |
| 341 | if (gap.size > n) { |
| 342 | AddFieldGap(gap.start_offset + n, gap.start_offset + gap.size, gaps); |
| 343 | } |
| 344 | } else { |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 345 | DCHECK_ALIGNED(field_offset->Uint32Value(), n); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 346 | field->SetOffset(*field_offset); |
| 347 | *field_offset = MemberOffset(field_offset->Uint32Value() + n); |
| 348 | } |
| 349 | ++(*current_field_idx); |
| 350 | } |
| 351 | } |
| 352 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 353 | ClassLinker::ClassLinker(InternTable* intern_table) |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 354 | : failed_dex_cache_class_lookups_(0), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 355 | class_roots_(nullptr), |
| 356 | array_iftable_(nullptr), |
| 357 | find_array_class_cache_next_victim_(0), |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 358 | init_done_(false), |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 359 | log_new_roots_(false), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 360 | intern_table_(intern_table), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 361 | quick_resolution_trampoline_(nullptr), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 362 | quick_imt_conflict_trampoline_(nullptr), |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 363 | quick_generic_jni_trampoline_(nullptr), |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 364 | quick_to_interpreter_bridge_trampoline_(nullptr), |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 365 | image_pointer_size_(kRuntimePointerSize) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 366 | CHECK(intern_table_ != nullptr); |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 367 | static_assert(kFindArrayCacheSize == arraysize(find_array_class_cache_), |
| 368 | "Array cache size wrong."); |
| 369 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 370 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 371 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 372 | void ClassLinker::CheckSystemClass(Thread* self, Handle<mirror::Class> c1, const char* descriptor) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 373 | ObjPtr<mirror::Class> c2 = FindSystemClass(self, descriptor); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 374 | if (c2 == nullptr) { |
| 375 | LOG(FATAL) << "Could not find class " << descriptor; |
| 376 | UNREACHABLE(); |
| 377 | } |
| 378 | if (c1.Get() != c2) { |
| 379 | std::ostringstream os1, os2; |
| 380 | c1->DumpClass(os1, mirror::Class::kDumpClassFullDetail); |
| 381 | c2->DumpClass(os2, mirror::Class::kDumpClassFullDetail); |
| 382 | LOG(FATAL) << "InitWithoutImage: Class mismatch for " << descriptor |
| 383 | << ". This is most likely the result of a broken build. Make sure that " |
| 384 | << "libcore and art projects match.\n\n" |
| 385 | << os1.str() << "\n\n" << os2.str(); |
| 386 | UNREACHABLE(); |
| 387 | } |
| 388 | } |
| 389 | |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 390 | bool ClassLinker::InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path, |
| 391 | std::string* error_msg) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 392 | VLOG(startup) << "ClassLinker::Init"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 393 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 394 | Thread* const self = Thread::Current(); |
| 395 | Runtime* const runtime = Runtime::Current(); |
| 396 | gc::Heap* const heap = runtime->GetHeap(); |
| 397 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 398 | CHECK(!heap->HasBootImageSpace()) << "Runtime has image. We should use it."; |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 399 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 400 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 401 | // Use the pointer size from the runtime since we are probably creating the image. |
| 402 | image_pointer_size_ = InstructionSetPointerSize(runtime->GetInstructionSet()); |
| 403 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 404 | // java_lang_Class comes first, it's needed for AllocClass |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 405 | // 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] | 406 | heap->IncrementDisableMovingGC(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 407 | StackHandleScope<64> hs(self); // 64 is picked arbitrarily. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 408 | auto class_class_size = mirror::Class::ClassClassSize(image_pointer_size_); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 409 | Handle<mirror::Class> java_lang_Class(hs.NewHandle(down_cast<mirror::Class*>( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 410 | heap->AllocNonMovableObject<true>(self, nullptr, class_class_size, VoidFunctor())))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 411 | CHECK(java_lang_Class != nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 412 | mirror::Class::SetClassClass(java_lang_Class.Get()); |
| 413 | java_lang_Class->SetClass(java_lang_Class.Get()); |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 414 | if (kUseBakerReadBarrier) { |
| 415 | java_lang_Class->AssertReadBarrierState(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 416 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 417 | java_lang_Class->SetClassSize(class_class_size); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 418 | java_lang_Class->SetPrimitiveType(Primitive::kPrimNot); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 419 | heap->DecrementDisableMovingGC(self); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 420 | // AllocClass(ObjPtr<mirror::Class>) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 421 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 422 | // Class[] is used for reflection support. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 423 | 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] | 424 | Handle<mirror::Class> class_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 425 | AllocClass(self, java_lang_Class.Get(), class_array_class_size))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 426 | class_array_class->SetComponentType(java_lang_Class.Get()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 427 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 428 | // 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] | 429 | Handle<mirror::Class> java_lang_Object(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 430 | AllocClass(self, java_lang_Class.Get(), mirror::Object::ClassSize(image_pointer_size_)))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 431 | CHECK(java_lang_Object != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 432 | // backfill Object as the super class of Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 433 | java_lang_Class->SetSuperClass(java_lang_Object.Get()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 434 | mirror::Class::SetStatus(java_lang_Object, mirror::Class::kStatusLoaded, self); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 435 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 436 | java_lang_Object->SetObjectSize(sizeof(mirror::Object)); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 437 | // Allocate in non-movable so that it's possible to check if a JNI weak global ref has been |
| 438 | // cleared without triggering the read barrier and unintentionally mark the sentinel alive. |
| 439 | runtime->SetSentinel(heap->AllocNonMovableObject<true>(self, |
| 440 | java_lang_Object.Get(), |
| 441 | java_lang_Object->GetObjectSize(), |
| 442 | VoidFunctor())); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 443 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 444 | // Object[] next to hold class roots. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 445 | Handle<mirror::Class> object_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 446 | AllocClass(self, java_lang_Class.Get(), |
| 447 | mirror::ObjectArray<mirror::Object>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 448 | object_array_class->SetComponentType(java_lang_Object.Get()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 449 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 450 | // Setup the char (primitive) class to be used for char[]. |
| 451 | Handle<mirror::Class> char_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 452 | AllocClass(self, java_lang_Class.Get(), |
| 453 | mirror::Class::PrimitiveClassSize(image_pointer_size_)))); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 454 | // The primitive char class won't be initialized by |
| 455 | // InitializePrimitiveClass until line 459, but strings (and |
| 456 | // internal char arrays) will be allocated before that and the |
| 457 | // component size, which is computed from the primitive type, needs |
| 458 | // to be set here. |
| 459 | char_class->SetPrimitiveType(Primitive::kPrimChar); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 460 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 461 | // Setup the char[] class to be used for String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 462 | Handle<mirror::Class> char_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 463 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 464 | char_array_class->SetComponentType(char_class.Get()); |
| 465 | mirror::CharArray::SetArrayClass(char_array_class.Get()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 466 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 467 | // Setup String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 468 | Handle<mirror::Class> java_lang_String(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 469 | AllocClass(self, java_lang_Class.Get(), mirror::String::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 470 | java_lang_String->SetStringClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 471 | mirror::String::SetClass(java_lang_String.Get()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 472 | mirror::Class::SetStatus(java_lang_String, mirror::Class::kStatusResolved, self); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 473 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 474 | // Setup java.lang.ref.Reference. |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 475 | Handle<mirror::Class> java_lang_ref_Reference(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 476 | AllocClass(self, java_lang_Class.Get(), mirror::Reference::ClassSize(image_pointer_size_)))); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 477 | mirror::Reference::SetClass(java_lang_ref_Reference.Get()); |
| 478 | java_lang_ref_Reference->SetObjectSize(mirror::Reference::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 479 | mirror::Class::SetStatus(java_lang_ref_Reference, mirror::Class::kStatusResolved, self); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 480 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 481 | // 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] | 482 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 483 | mirror::ObjectArray<mirror::Class>::Alloc(self, object_array_class.Get(), |
| 484 | kClassRootsMax)); |
| 485 | CHECK(!class_roots_.IsNull()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 486 | SetClassRoot(kJavaLangClass, java_lang_Class.Get()); |
| 487 | SetClassRoot(kJavaLangObject, java_lang_Object.Get()); |
| 488 | SetClassRoot(kClassArrayClass, class_array_class.Get()); |
| 489 | SetClassRoot(kObjectArrayClass, object_array_class.Get()); |
| 490 | SetClassRoot(kCharArrayClass, char_array_class.Get()); |
| 491 | SetClassRoot(kJavaLangString, java_lang_String.Get()); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 492 | SetClassRoot(kJavaLangRefReference, java_lang_ref_Reference.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 493 | |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 494 | // Fill in the empty iftable. Needs to be done after the kObjectArrayClass root is set. |
| 495 | java_lang_Object->SetIfTable(AllocIfTable(self, 0)); |
| 496 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 497 | // Setup the primitive type classes. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 498 | SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass(self, Primitive::kPrimBoolean)); |
| 499 | SetClassRoot(kPrimitiveByte, CreatePrimitiveClass(self, Primitive::kPrimByte)); |
| 500 | SetClassRoot(kPrimitiveShort, CreatePrimitiveClass(self, Primitive::kPrimShort)); |
| 501 | SetClassRoot(kPrimitiveInt, CreatePrimitiveClass(self, Primitive::kPrimInt)); |
| 502 | SetClassRoot(kPrimitiveLong, CreatePrimitiveClass(self, Primitive::kPrimLong)); |
| 503 | SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass(self, Primitive::kPrimFloat)); |
| 504 | SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass(self, Primitive::kPrimDouble)); |
| 505 | SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass(self, Primitive::kPrimVoid)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 506 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 507 | // Create array interface entries to populate once we can load system classes. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 508 | array_iftable_ = GcRoot<mirror::IfTable>(AllocIfTable(self, 2)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 509 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 510 | // Create int array type for AllocDexCache (done in AppendToBootClassPath). |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 511 | Handle<mirror::Class> int_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 512 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize(image_pointer_size_)))); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 513 | int_array_class->SetComponentType(GetClassRoot(kPrimitiveInt)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 514 | mirror::IntArray::SetArrayClass(int_array_class.Get()); |
| 515 | SetClassRoot(kIntArrayClass, int_array_class.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 516 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 517 | // Create long array type for AllocDexCache (done in AppendToBootClassPath). |
| 518 | Handle<mirror::Class> long_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 519 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 520 | long_array_class->SetComponentType(GetClassRoot(kPrimitiveLong)); |
| 521 | mirror::LongArray::SetArrayClass(long_array_class.Get()); |
| 522 | SetClassRoot(kLongArrayClass, long_array_class.Get()); |
| 523 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 524 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 525 | |
Ian Rogers | 52813c9 | 2012-10-11 11:50:38 -0700 | [diff] [blame] | 526 | // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 527 | Handle<mirror::Class> java_lang_DexCache(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 528 | AllocClass(self, java_lang_Class.Get(), mirror::DexCache::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 529 | SetClassRoot(kJavaLangDexCache, java_lang_DexCache.Get()); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 530 | java_lang_DexCache->SetDexCacheClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 531 | java_lang_DexCache->SetObjectSize(mirror::DexCache::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 532 | mirror::Class::SetStatus(java_lang_DexCache, mirror::Class::kStatusResolved, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 533 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 534 | |
| 535 | // Setup dalvik.system.ClassExt |
| 536 | Handle<mirror::Class> dalvik_system_ClassExt(hs.NewHandle( |
| 537 | AllocClass(self, java_lang_Class.Get(), mirror::ClassExt::ClassSize(image_pointer_size_)))); |
| 538 | SetClassRoot(kDalvikSystemClassExt, dalvik_system_ClassExt.Get()); |
| 539 | mirror::ClassExt::SetClass(dalvik_system_ClassExt.Get()); |
| 540 | mirror::Class::SetStatus(dalvik_system_ClassExt, mirror::Class::kStatusResolved, self); |
| 541 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 542 | // Set up array classes for string, field, method |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 543 | Handle<mirror::Class> object_array_string(hs.NewHandle( |
| 544 | AllocClass(self, java_lang_Class.Get(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 545 | mirror::ObjectArray<mirror::String>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 546 | object_array_string->SetComponentType(java_lang_String.Get()); |
| 547 | SetClassRoot(kJavaLangStringArrayClass, object_array_string.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 548 | |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 549 | LinearAlloc* linear_alloc = runtime->GetLinearAlloc(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 550 | // Create runtime resolution and imt conflict methods. |
| 551 | runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 552 | runtime->SetImtConflictMethod(runtime->CreateImtConflictMethod(linear_alloc)); |
| 553 | runtime->SetImtUnimplementedMethod(runtime->CreateImtConflictMethod(linear_alloc)); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 554 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 555 | // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create |
| 556 | // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses |
| 557 | // these roots. |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 558 | if (boot_class_path.empty()) { |
| 559 | *error_msg = "Boot classpath is empty."; |
| 560 | return false; |
| 561 | } |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 562 | for (auto& dex_file : boot_class_path) { |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 563 | if (dex_file.get() == nullptr) { |
| 564 | *error_msg = "Null dex file."; |
| 565 | return false; |
| 566 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 567 | AppendToBootClassPath(self, *dex_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 568 | boot_dex_files_.push_back(std::move(dex_file)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 569 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 570 | |
| 571 | // now we can use FindSystemClass |
| 572 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 573 | // run char class through InitializePrimitiveClass to finish init |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 574 | InitializePrimitiveClass(char_class.Get(), Primitive::kPrimChar); |
| 575 | SetClassRoot(kPrimitiveChar, char_class.Get()); // needs descriptor |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 576 | |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 577 | // Set up GenericJNI entrypoint. That is mainly a hack for common_compiler_test.h so that |
| 578 | // we do not need friend classes or a publicly exposed setter. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 579 | quick_generic_jni_trampoline_ = GetQuickGenericJniStub(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 580 | if (!runtime->IsAotCompiler()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 581 | // 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] | 582 | quick_resolution_trampoline_ = GetQuickResolutionStub(); |
| 583 | quick_imt_conflict_trampoline_ = GetQuickImtConflictStub(); |
| 584 | quick_to_interpreter_bridge_trampoline_ = GetQuickToInterpreterBridge(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 585 | } |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 586 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 587 | // Object, String, ClassExt and DexCache need to be rerun through FindSystemClass to finish init |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 588 | mirror::Class::SetStatus(java_lang_Object, mirror::Class::kStatusNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 589 | CheckSystemClass(self, java_lang_Object, "Ljava/lang/Object;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 590 | CHECK_EQ(java_lang_Object->GetObjectSize(), mirror::Object::InstanceSize()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 591 | mirror::Class::SetStatus(java_lang_String, mirror::Class::kStatusNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 592 | CheckSystemClass(self, java_lang_String, "Ljava/lang/String;"); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 593 | mirror::Class::SetStatus(java_lang_DexCache, mirror::Class::kStatusNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 594 | CheckSystemClass(self, java_lang_DexCache, "Ljava/lang/DexCache;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 595 | CHECK_EQ(java_lang_DexCache->GetObjectSize(), mirror::DexCache::InstanceSize()); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 596 | mirror::Class::SetStatus(dalvik_system_ClassExt, mirror::Class::kStatusNotReady, self); |
| 597 | CheckSystemClass(self, dalvik_system_ClassExt, "Ldalvik/system/ClassExt;"); |
| 598 | CHECK_EQ(dalvik_system_ClassExt->GetObjectSize(), mirror::ClassExt::InstanceSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 599 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 600 | // 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] | 601 | SetClassRoot(kBooleanArrayClass, FindSystemClass(self, "[Z")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 602 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 603 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 604 | SetClassRoot(kByteArrayClass, FindSystemClass(self, "[B")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 605 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 606 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 607 | CheckSystemClass(self, char_array_class, "[C"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 608 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 609 | SetClassRoot(kShortArrayClass, FindSystemClass(self, "[S")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 610 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 611 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 612 | CheckSystemClass(self, int_array_class, "[I"); |
| 613 | CheckSystemClass(self, long_array_class, "[J"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 614 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 615 | SetClassRoot(kFloatArrayClass, FindSystemClass(self, "[F")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 616 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 617 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 618 | SetClassRoot(kDoubleArrayClass, FindSystemClass(self, "[D")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 619 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 620 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 621 | // Run Class through FindSystemClass. This initializes the dex_cache_ fields and register it |
| 622 | // in class_table_. |
| 623 | CheckSystemClass(self, java_lang_Class, "Ljava/lang/Class;"); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 624 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 625 | CheckSystemClass(self, class_array_class, "[Ljava/lang/Class;"); |
| 626 | CheckSystemClass(self, object_array_class, "[Ljava/lang/Object;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 627 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 628 | // Setup the single, global copy of "iftable". |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 629 | auto java_lang_Cloneable = hs.NewHandle(FindSystemClass(self, "Ljava/lang/Cloneable;")); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 630 | CHECK(java_lang_Cloneable != nullptr); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 631 | auto java_io_Serializable = hs.NewHandle(FindSystemClass(self, "Ljava/io/Serializable;")); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 632 | CHECK(java_io_Serializable != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 633 | // We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to |
| 634 | // crawl up and explicitly list all of the supers as well. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 635 | array_iftable_.Read()->SetInterface(0, java_lang_Cloneable.Get()); |
| 636 | array_iftable_.Read()->SetInterface(1, java_io_Serializable.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 637 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 638 | // Sanity check Class[] and Object[]'s interfaces. GetDirectInterface may cause thread |
| 639 | // suspension. |
| 640 | CHECK_EQ(java_lang_Cloneable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 641 | mirror::Class::GetDirectInterface(self, class_array_class.Get(), 0)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 642 | CHECK_EQ(java_io_Serializable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 643 | mirror::Class::GetDirectInterface(self, class_array_class.Get(), 1)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 644 | CHECK_EQ(java_lang_Cloneable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 645 | mirror::Class::GetDirectInterface(self, object_array_class.Get(), 0)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 646 | CHECK_EQ(java_io_Serializable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 647 | mirror::Class::GetDirectInterface(self, object_array_class.Get(), 1)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 648 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 649 | CHECK_EQ(object_array_string.Get(), |
| 650 | FindSystemClass(self, GetClassRootDescriptor(kJavaLangStringArrayClass))); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 651 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 652 | // 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] | 653 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 654 | // Create java.lang.reflect.Proxy root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 655 | SetClassRoot(kJavaLangReflectProxy, FindSystemClass(self, "Ljava/lang/reflect/Proxy;")); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 656 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 657 | // Create java.lang.reflect.Field.class root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 658 | auto* class_root = FindSystemClass(self, "Ljava/lang/reflect/Field;"); |
| 659 | CHECK(class_root != nullptr); |
| 660 | SetClassRoot(kJavaLangReflectField, class_root); |
| 661 | mirror::Field::SetClass(class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 662 | |
| 663 | // Create java.lang.reflect.Field array root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 664 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Field;"); |
| 665 | CHECK(class_root != nullptr); |
| 666 | SetClassRoot(kJavaLangReflectFieldArrayClass, class_root); |
| 667 | mirror::Field::SetArrayClass(class_root); |
| 668 | |
| 669 | // Create java.lang.reflect.Constructor.class root and array root. |
| 670 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Constructor;"); |
| 671 | CHECK(class_root != nullptr); |
| 672 | SetClassRoot(kJavaLangReflectConstructor, class_root); |
| 673 | mirror::Constructor::SetClass(class_root); |
| 674 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Constructor;"); |
| 675 | CHECK(class_root != nullptr); |
| 676 | SetClassRoot(kJavaLangReflectConstructorArrayClass, class_root); |
| 677 | mirror::Constructor::SetArrayClass(class_root); |
| 678 | |
| 679 | // Create java.lang.reflect.Method.class root and array root. |
| 680 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Method;"); |
| 681 | CHECK(class_root != nullptr); |
| 682 | SetClassRoot(kJavaLangReflectMethod, class_root); |
| 683 | mirror::Method::SetClass(class_root); |
| 684 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Method;"); |
| 685 | CHECK(class_root != nullptr); |
| 686 | SetClassRoot(kJavaLangReflectMethodArrayClass, class_root); |
| 687 | mirror::Method::SetArrayClass(class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 688 | |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 689 | // Create java.lang.invoke.MethodType.class root |
| 690 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodType;"); |
| 691 | CHECK(class_root != nullptr); |
| 692 | SetClassRoot(kJavaLangInvokeMethodType, class_root); |
| 693 | mirror::MethodType::SetClass(class_root); |
| 694 | |
| 695 | // Create java.lang.invoke.MethodHandleImpl.class root |
| 696 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandleImpl;"); |
| 697 | CHECK(class_root != nullptr); |
| 698 | SetClassRoot(kJavaLangInvokeMethodHandleImpl, class_root); |
| 699 | mirror::MethodHandleImpl::SetClass(class_root); |
| 700 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 701 | // Create java.lang.invoke.MethodHandles.Lookup.class root |
| 702 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandles$Lookup;"); |
| 703 | CHECK(class_root != nullptr); |
| 704 | SetClassRoot(kJavaLangInvokeMethodHandlesLookup, class_root); |
| 705 | mirror::MethodHandlesLookup::SetClass(class_root); |
| 706 | |
| 707 | // Create java.lang.invoke.CallSite.class root |
| 708 | class_root = FindSystemClass(self, "Ljava/lang/invoke/CallSite;"); |
| 709 | CHECK(class_root != nullptr); |
| 710 | SetClassRoot(kJavaLangInvokeCallSite, class_root); |
| 711 | mirror::CallSite::SetClass(class_root); |
| 712 | |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 713 | class_root = FindSystemClass(self, "Ldalvik/system/EmulatedStackFrame;"); |
| 714 | CHECK(class_root != nullptr); |
| 715 | SetClassRoot(kDalvikSystemEmulatedStackFrame, class_root); |
| 716 | mirror::EmulatedStackFrame::SetClass(class_root); |
| 717 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 718 | // 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] | 719 | // finish initializing Reference class |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 720 | mirror::Class::SetStatus(java_lang_ref_Reference, mirror::Class::kStatusNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 721 | CheckSystemClass(self, java_lang_ref_Reference, "Ljava/lang/ref/Reference;"); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 722 | CHECK_EQ(java_lang_ref_Reference->GetObjectSize(), mirror::Reference::InstanceSize()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 723 | CHECK_EQ(java_lang_ref_Reference->GetClassSize(), |
| 724 | mirror::Reference::ClassSize(image_pointer_size_)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 725 | class_root = FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 726 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 727 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagFinalizerReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 728 | class_root = FindSystemClass(self, "Ljava/lang/ref/PhantomReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 729 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 730 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagPhantomReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 731 | class_root = FindSystemClass(self, "Ljava/lang/ref/SoftReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 732 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 733 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagSoftReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 734 | class_root = FindSystemClass(self, "Ljava/lang/ref/WeakReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 735 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 736 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagWeakReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 737 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 738 | // Setup the ClassLoader, verifying the object_size_. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 739 | class_root = FindSystemClass(self, "Ljava/lang/ClassLoader;"); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 740 | class_root->SetClassLoaderClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 741 | CHECK_EQ(class_root->GetObjectSize(), mirror::ClassLoader::InstanceSize()); |
| 742 | SetClassRoot(kJavaLangClassLoader, class_root); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 743 | |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 744 | // Set up java.lang.Throwable, java.lang.ClassNotFoundException, and |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 745 | // java.lang.StackTraceElement as a convenience. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 746 | SetClassRoot(kJavaLangThrowable, FindSystemClass(self, "Ljava/lang/Throwable;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 747 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 748 | SetClassRoot(kJavaLangClassNotFoundException, |
| 749 | FindSystemClass(self, "Ljava/lang/ClassNotFoundException;")); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 750 | SetClassRoot(kJavaLangStackTraceElement, FindSystemClass(self, "Ljava/lang/StackTraceElement;")); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 751 | SetClassRoot(kJavaLangStackTraceElementArrayClass, |
| 752 | FindSystemClass(self, "[Ljava/lang/StackTraceElement;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 753 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 754 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 755 | // Create conflict tables that depend on the class linker. |
| 756 | runtime->FixupConflictTables(); |
| 757 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 758 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 759 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 760 | VLOG(startup) << "ClassLinker::InitFromCompiler exiting"; |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 761 | |
| 762 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 765 | void ClassLinker::FinishInit(Thread* self) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 766 | VLOG(startup) << "ClassLinker::FinishInit entering"; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 767 | |
| 768 | // Let the heap know some key offsets into java.lang.ref instances |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 769 | // Note: we hard code the field indexes here rather than using FindInstanceField |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 770 | // as the types of the field can't be resolved prior to the runtime being |
| 771 | // fully initialized |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 772 | StackHandleScope<2> hs(self); |
| 773 | Handle<mirror::Class> java_lang_ref_Reference = hs.NewHandle(GetClassRoot(kJavaLangRefReference)); |
| 774 | Handle<mirror::Class> java_lang_ref_FinalizerReference = |
| 775 | hs.NewHandle(FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;")); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 776 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 777 | ArtField* pendingNext = java_lang_ref_Reference->GetInstanceField(0); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 778 | CHECK_STREQ(pendingNext->GetName(), "pendingNext"); |
| 779 | CHECK_STREQ(pendingNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 780 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 781 | ArtField* queue = java_lang_ref_Reference->GetInstanceField(1); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 782 | CHECK_STREQ(queue->GetName(), "queue"); |
| 783 | CHECK_STREQ(queue->GetTypeDescriptor(), "Ljava/lang/ref/ReferenceQueue;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 784 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 785 | ArtField* queueNext = java_lang_ref_Reference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 786 | CHECK_STREQ(queueNext->GetName(), "queueNext"); |
| 787 | CHECK_STREQ(queueNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 788 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 789 | ArtField* referent = java_lang_ref_Reference->GetInstanceField(3); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 790 | CHECK_STREQ(referent->GetName(), "referent"); |
| 791 | CHECK_STREQ(referent->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 792 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 793 | ArtField* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 794 | CHECK_STREQ(zombie->GetName(), "zombie"); |
| 795 | CHECK_STREQ(zombie->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 796 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 797 | // ensure all class_roots_ are initialized |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 798 | for (size_t i = 0; i < kClassRootsMax; i++) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 799 | ClassRoot class_root = static_cast<ClassRoot>(i); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 800 | ObjPtr<mirror::Class> klass = GetClassRoot(class_root); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 801 | CHECK(klass != nullptr); |
| 802 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != nullptr); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 803 | // note SetClassRoot does additional validation. |
| 804 | // if possible add new checks there to catch errors early |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 807 | CHECK(!array_iftable_.IsNull()); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 808 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 809 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 810 | // that Object, Class, and Object[] are setup |
| 811 | init_done_ = true; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 812 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 813 | VLOG(startup) << "ClassLinker::FinishInit exiting"; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 816 | void ClassLinker::RunRootClinits() { |
| 817 | Thread* self = Thread::Current(); |
| 818 | for (size_t i = 0; i < ClassLinker::kClassRootsMax; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 819 | ObjPtr<mirror::Class> c = GetClassRoot(ClassRoot(i)); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 820 | if (!c->IsArrayClass() && !c->IsPrimitive()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 821 | StackHandleScope<1> hs(self); |
| 822 | Handle<mirror::Class> h_class(hs.NewHandle(GetClassRoot(ClassRoot(i)))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 823 | EnsureInitialized(self, h_class, true, true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 824 | self->AssertNoPendingException(); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | } |
| 828 | |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 829 | // Set image methods' entry point to interpreter. |
| 830 | class SetInterpreterEntrypointArtMethodVisitor : public ArtMethodVisitor { |
| 831 | public: |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 832 | explicit SetInterpreterEntrypointArtMethodVisitor(PointerSize image_pointer_size) |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 833 | : image_pointer_size_(image_pointer_size) {} |
| 834 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 835 | void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 836 | if (kIsDebugBuild && !method->IsRuntimeMethod()) { |
| 837 | CHECK(method->GetDeclaringClass() != nullptr); |
| 838 | } |
| 839 | if (!method->IsNative() && !method->IsRuntimeMethod() && !method->IsResolutionMethod()) { |
| 840 | method->SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), |
| 841 | image_pointer_size_); |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | private: |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 846 | const PointerSize image_pointer_size_; |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 847 | |
| 848 | DISALLOW_COPY_AND_ASSIGN(SetInterpreterEntrypointArtMethodVisitor); |
| 849 | }; |
| 850 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 851 | struct TrampolineCheckData { |
| 852 | const void* quick_resolution_trampoline; |
| 853 | const void* quick_imt_conflict_trampoline; |
| 854 | const void* quick_generic_jni_trampoline; |
| 855 | const void* quick_to_interpreter_bridge_trampoline; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 856 | PointerSize pointer_size; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 857 | ArtMethod* m; |
| 858 | bool error; |
| 859 | }; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 860 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 861 | static void CheckTrampolines(mirror::Object* obj, void* arg) NO_THREAD_SAFETY_ANALYSIS { |
| 862 | if (obj->IsClass()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 863 | ObjPtr<mirror::Class> klass = obj->AsClass(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 864 | TrampolineCheckData* d = reinterpret_cast<TrampolineCheckData*>(arg); |
| 865 | for (ArtMethod& m : klass->GetMethods(d->pointer_size)) { |
| 866 | const void* entrypoint = m.GetEntryPointFromQuickCompiledCodePtrSize(d->pointer_size); |
| 867 | if (entrypoint == d->quick_resolution_trampoline || |
| 868 | entrypoint == d->quick_imt_conflict_trampoline || |
| 869 | entrypoint == d->quick_generic_jni_trampoline || |
| 870 | entrypoint == d->quick_to_interpreter_bridge_trampoline) { |
| 871 | d->m = &m; |
| 872 | d->error = true; |
| 873 | return; |
| 874 | } |
| 875 | } |
| 876 | } |
| 877 | } |
| 878 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 879 | bool ClassLinker::InitFromBootImage(std::string* error_msg) { |
| 880 | VLOG(startup) << __FUNCTION__ << " entering"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 881 | CHECK(!init_done_); |
| 882 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 883 | Runtime* const runtime = Runtime::Current(); |
| 884 | Thread* const self = Thread::Current(); |
| 885 | gc::Heap* const heap = runtime->GetHeap(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 886 | std::vector<gc::space::ImageSpace*> spaces = heap->GetBootImageSpaces(); |
| 887 | CHECK(!spaces.empty()); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 888 | uint32_t pointer_size_unchecked = spaces[0]->GetImageHeader().GetPointerSizeUnchecked(); |
| 889 | if (!ValidPointerSize(pointer_size_unchecked)) { |
| 890 | *error_msg = StringPrintf("Invalid image pointer size: %u", pointer_size_unchecked); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 891 | return false; |
| 892 | } |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 893 | image_pointer_size_ = spaces[0]->GetImageHeader().GetPointerSize(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 894 | if (!runtime->IsAotCompiler()) { |
| 895 | // Only the Aot compiler supports having an image with a different pointer size than the |
| 896 | // runtime. This happens on the host for compiling 32 bit tests since we use a 64 bit libart |
| 897 | // 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] | 898 | if (image_pointer_size_ != kRuntimePointerSize) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 899 | *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] | 900 | static_cast<size_t>(image_pointer_size_), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 901 | sizeof(void*)); |
| 902 | return false; |
| 903 | } |
| 904 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 905 | std::vector<const OatFile*> oat_files = |
| 906 | runtime->GetOatFileManager().RegisterImageOatFiles(spaces); |
| 907 | DCHECK(!oat_files.empty()); |
| 908 | const OatHeader& default_oat_header = oat_files[0]->GetOatHeader(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 909 | CHECK_EQ(default_oat_header.GetImageFileLocationOatDataBegin(), 0U); |
| 910 | const char* image_file_location = oat_files[0]->GetOatHeader(). |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 911 | GetStoreValueByKey(OatHeader::kImageLocationKey); |
| 912 | CHECK(image_file_location == nullptr || *image_file_location == 0); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 913 | quick_resolution_trampoline_ = default_oat_header.GetQuickResolutionTrampoline(); |
| 914 | quick_imt_conflict_trampoline_ = default_oat_header.GetQuickImtConflictTrampoline(); |
| 915 | quick_generic_jni_trampoline_ = default_oat_header.GetQuickGenericJniTrampoline(); |
| 916 | quick_to_interpreter_bridge_trampoline_ = default_oat_header.GetQuickToInterpreterBridge(); |
| 917 | if (kIsDebugBuild) { |
| 918 | // Check that the other images use the same trampoline. |
| 919 | for (size_t i = 1; i < oat_files.size(); ++i) { |
| 920 | const OatHeader& ith_oat_header = oat_files[i]->GetOatHeader(); |
| 921 | const void* ith_quick_resolution_trampoline = |
| 922 | ith_oat_header.GetQuickResolutionTrampoline(); |
| 923 | const void* ith_quick_imt_conflict_trampoline = |
| 924 | ith_oat_header.GetQuickImtConflictTrampoline(); |
| 925 | const void* ith_quick_generic_jni_trampoline = |
| 926 | ith_oat_header.GetQuickGenericJniTrampoline(); |
| 927 | const void* ith_quick_to_interpreter_bridge_trampoline = |
| 928 | ith_oat_header.GetQuickToInterpreterBridge(); |
| 929 | if (ith_quick_resolution_trampoline != quick_resolution_trampoline_ || |
| 930 | ith_quick_imt_conflict_trampoline != quick_imt_conflict_trampoline_ || |
| 931 | ith_quick_generic_jni_trampoline != quick_generic_jni_trampoline_ || |
| 932 | ith_quick_to_interpreter_bridge_trampoline != quick_to_interpreter_bridge_trampoline_) { |
| 933 | // Make sure that all methods in this image do not contain those trampolines as |
| 934 | // entrypoints. Otherwise the class-linker won't be able to work with a single set. |
| 935 | TrampolineCheckData data; |
| 936 | data.error = false; |
| 937 | data.pointer_size = GetImagePointerSize(); |
| 938 | data.quick_resolution_trampoline = ith_quick_resolution_trampoline; |
| 939 | data.quick_imt_conflict_trampoline = ith_quick_imt_conflict_trampoline; |
| 940 | data.quick_generic_jni_trampoline = ith_quick_generic_jni_trampoline; |
| 941 | data.quick_to_interpreter_bridge_trampoline = ith_quick_to_interpreter_bridge_trampoline; |
| 942 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 943 | spaces[i]->GetLiveBitmap()->Walk(CheckTrampolines, &data); |
| 944 | if (data.error) { |
| 945 | ArtMethod* m = data.m; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 946 | LOG(ERROR) << "Found a broken ArtMethod: " << ArtMethod::PrettyMethod(m); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 947 | *error_msg = "Found an ArtMethod with a bad entrypoint"; |
| 948 | return false; |
| 949 | } |
| 950 | } |
| 951 | } |
| 952 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 953 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 954 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
| 955 | down_cast<mirror::ObjectArray<mirror::Class>*>( |
| 956 | spaces[0]->GetImageHeader().GetImageRoot(ImageHeader::kClassRoots))); |
| 957 | mirror::Class::SetClassClass(class_roots_.Read()->Get(kJavaLangClass)); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 958 | |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 959 | // Special case of setting up the String class early so that we can test arbitrary objects |
| 960 | // as being Strings or not |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 961 | mirror::String::SetClass(GetClassRoot(kJavaLangString)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 962 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 963 | ObjPtr<mirror::Class> java_lang_Object = GetClassRoot(kJavaLangObject); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 964 | java_lang_Object->SetObjectSize(sizeof(mirror::Object)); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 965 | // Allocate in non-movable so that it's possible to check if a JNI weak global ref has been |
| 966 | // cleared without triggering the read barrier and unintentionally mark the sentinel alive. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 967 | runtime->SetSentinel(heap->AllocNonMovableObject<true>( |
| 968 | self, java_lang_Object, java_lang_Object->GetObjectSize(), VoidFunctor())); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 969 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 970 | // reinit array_iftable_ from any array class instance, they should be == |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 971 | array_iftable_ = GcRoot<mirror::IfTable>(GetClassRoot(kObjectArrayClass)->GetIfTable()); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 972 | DCHECK_EQ(array_iftable_.Read(), GetClassRoot(kBooleanArrayClass)->GetIfTable()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 973 | // String class root was set above |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 974 | mirror::Field::SetClass(GetClassRoot(kJavaLangReflectField)); |
| 975 | mirror::Field::SetArrayClass(GetClassRoot(kJavaLangReflectFieldArrayClass)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 976 | mirror::Constructor::SetClass(GetClassRoot(kJavaLangReflectConstructor)); |
| 977 | mirror::Constructor::SetArrayClass(GetClassRoot(kJavaLangReflectConstructorArrayClass)); |
| 978 | mirror::Method::SetClass(GetClassRoot(kJavaLangReflectMethod)); |
| 979 | mirror::Method::SetArrayClass(GetClassRoot(kJavaLangReflectMethodArrayClass)); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 980 | mirror::MethodType::SetClass(GetClassRoot(kJavaLangInvokeMethodType)); |
| 981 | mirror::MethodHandleImpl::SetClass(GetClassRoot(kJavaLangInvokeMethodHandleImpl)); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 982 | mirror::MethodHandlesLookup::SetClass(GetClassRoot(kJavaLangInvokeMethodHandlesLookup)); |
| 983 | mirror::CallSite::SetClass(GetClassRoot(kJavaLangInvokeCallSite)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 984 | mirror::Reference::SetClass(GetClassRoot(kJavaLangRefReference)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 985 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
| 986 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
| 987 | mirror::CharArray::SetArrayClass(GetClassRoot(kCharArrayClass)); |
| 988 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
| 989 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
| 990 | mirror::IntArray::SetArrayClass(GetClassRoot(kIntArrayClass)); |
| 991 | mirror::LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
| 992 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
| 993 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
| 994 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 995 | mirror::EmulatedStackFrame::SetClass(GetClassRoot(kDalvikSystemEmulatedStackFrame)); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 996 | mirror::ClassExt::SetClass(GetClassRoot(kDalvikSystemClassExt)); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 997 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 998 | for (gc::space::ImageSpace* image_space : spaces) { |
| 999 | // Boot class loader, use a null handle. |
| 1000 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1001 | if (!AddImageSpace(image_space, |
| 1002 | ScopedNullHandle<mirror::ClassLoader>(), |
| 1003 | /*dex_elements*/nullptr, |
| 1004 | /*dex_location*/nullptr, |
| 1005 | /*out*/&dex_files, |
| 1006 | error_msg)) { |
| 1007 | return false; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1008 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1009 | // Append opened dex files at the end. |
| 1010 | boot_dex_files_.insert(boot_dex_files_.end(), |
| 1011 | std::make_move_iterator(dex_files.begin()), |
| 1012 | std::make_move_iterator(dex_files.end())); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1013 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1014 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1015 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1016 | VLOG(startup) << __FUNCTION__ << " exiting"; |
| 1017 | return true; |
| 1018 | } |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 1019 | |
Jeff Hao | 5872d7c | 2016-04-27 11:07:41 -0700 | [diff] [blame] | 1020 | bool ClassLinker::IsBootClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1021 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1022 | return class_loader == nullptr || |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1023 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader) == |
| 1024 | class_loader->GetClass(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1025 | } |
| 1026 | |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1027 | static bool GetDexPathListElementName(ObjPtr<mirror::Object> element, |
| 1028 | ObjPtr<mirror::String>* out_name) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1029 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1030 | ArtField* const dex_file_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 1031 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1032 | ArtField* const dex_file_name_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 1033 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_fileName); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1034 | DCHECK(dex_file_field != nullptr); |
| 1035 | DCHECK(dex_file_name_field != nullptr); |
| 1036 | DCHECK(element != nullptr); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1037 | CHECK_EQ(dex_file_field->GetDeclaringClass(), element->GetClass()) << element->PrettyTypeOf(); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1038 | ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1039 | if (dex_file == nullptr) { |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1040 | // Null dex file means it was probably a jar with no dex files, return a null string. |
| 1041 | *out_name = nullptr; |
| 1042 | return true; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1043 | } |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1044 | ObjPtr<mirror::Object> name_object = dex_file_name_field->GetObject(dex_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1045 | if (name_object != nullptr) { |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1046 | *out_name = name_object->AsString(); |
| 1047 | return true; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1048 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1049 | return false; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1050 | } |
| 1051 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1052 | static bool FlattenPathClassLoader(ObjPtr<mirror::ClassLoader> class_loader, |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1053 | std::list<ObjPtr<mirror::String>>* out_dex_file_names, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1054 | std::string* error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1055 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1056 | DCHECK(out_dex_file_names != nullptr); |
| 1057 | DCHECK(error_msg != nullptr); |
| 1058 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1059 | ArtField* const dex_path_list_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 1060 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1061 | ArtField* const dex_elements_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 1062 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1063 | CHECK(dex_path_list_field != nullptr); |
| 1064 | CHECK(dex_elements_field != nullptr); |
Jeff Hao | 5872d7c | 2016-04-27 11:07:41 -0700 | [diff] [blame] | 1065 | while (!ClassLinker::IsBootClassLoader(soa, class_loader)) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1066 | if (soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader) != |
| 1067 | class_loader->GetClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1068 | *error_msg = StringPrintf("Unknown class loader type %s", |
| 1069 | class_loader->PrettyTypeOf().c_str()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1070 | // Unsupported class loader. |
| 1071 | return false; |
| 1072 | } |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1073 | ObjPtr<mirror::Object> dex_path_list = dex_path_list_field->GetObject(class_loader); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1074 | if (dex_path_list != nullptr) { |
| 1075 | // DexPathList has an array dexElements of Elements[] which each contain a dex file. |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1076 | ObjPtr<mirror::Object> dex_elements_obj = dex_elements_field->GetObject(dex_path_list); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1077 | // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look |
| 1078 | // at the mCookie which is a DexFile vector. |
| 1079 | if (dex_elements_obj != nullptr) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1080 | ObjPtr<mirror::ObjectArray<mirror::Object>> dex_elements = |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1081 | dex_elements_obj->AsObjectArray<mirror::Object>(); |
| 1082 | // Reverse order since we insert the parent at the front. |
| 1083 | for (int32_t i = dex_elements->GetLength() - 1; i >= 0; --i) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1084 | ObjPtr<mirror::Object> element = dex_elements->GetWithoutChecks(i); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1085 | if (element == nullptr) { |
| 1086 | *error_msg = StringPrintf("Null dex element at index %d", i); |
| 1087 | return false; |
| 1088 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1089 | ObjPtr<mirror::String> name; |
| 1090 | if (!GetDexPathListElementName(element, &name)) { |
| 1091 | *error_msg = StringPrintf("Invalid dex path list element at index %d", i); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1092 | return false; |
| 1093 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1094 | if (name != nullptr) { |
| 1095 | out_dex_file_names->push_front(name.Ptr()); |
| 1096 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | } |
| 1100 | class_loader = class_loader->GetParent(); |
| 1101 | } |
| 1102 | return true; |
| 1103 | } |
| 1104 | |
| 1105 | class FixupArtMethodArrayVisitor : public ArtMethodVisitor { |
| 1106 | public: |
| 1107 | explicit FixupArtMethodArrayVisitor(const ImageHeader& header) : header_(header) {} |
| 1108 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1109 | virtual void Visit(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 1110 | const bool is_copied = method->IsCopied(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1111 | ArtMethod** resolved_methods = method->GetDexCacheResolvedMethods(kRuntimePointerSize); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1112 | if (resolved_methods != nullptr) { |
| 1113 | bool in_image_space = false; |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 1114 | if (kIsDebugBuild || is_copied) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1115 | in_image_space = header_.GetImageSection(ImageHeader::kSectionDexCacheArrays).Contains( |
| 1116 | reinterpret_cast<const uint8_t*>(resolved_methods) - header_.GetImageBegin()); |
| 1117 | } |
| 1118 | // Must be in image space for non-miranda method. |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 1119 | DCHECK(is_copied || in_image_space) |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1120 | << resolved_methods << " is not in image starting at " |
| 1121 | << reinterpret_cast<void*>(header_.GetImageBegin()); |
Alex Light | 3612149 | 2016-02-22 13:43:29 -0800 | [diff] [blame] | 1122 | if (!is_copied || in_image_space) { |
Vladimir Marko | 6dd1488 | 2016-10-25 11:51:35 +0100 | [diff] [blame] | 1123 | method->SetDexCacheResolvedMethods(method->GetDexCache()->GetResolvedMethods(), |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1124 | kRuntimePointerSize); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | private: |
| 1130 | const ImageHeader& header_; |
| 1131 | }; |
| 1132 | |
| 1133 | class VerifyClassInTableArtMethodVisitor : public ArtMethodVisitor { |
| 1134 | public: |
| 1135 | explicit VerifyClassInTableArtMethodVisitor(ClassTable* table) : table_(table) {} |
| 1136 | |
| 1137 | virtual void Visit(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1138 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::classlinker_classes_lock_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1139 | ObjPtr<mirror::Class> klass = method->GetDeclaringClass(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1140 | if (klass != nullptr && !Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1141 | CHECK_EQ(table_->LookupByDescriptor(klass), klass) << mirror::Class::PrettyClass(klass); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | private: |
| 1146 | ClassTable* const table_; |
| 1147 | }; |
| 1148 | |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1149 | class VerifyDirectInterfacesInTableClassVisitor { |
| 1150 | public: |
| 1151 | explicit VerifyDirectInterfacesInTableClassVisitor(ObjPtr<mirror::ClassLoader> class_loader) |
| 1152 | : class_loader_(class_loader), self_(Thread::Current()) { } |
| 1153 | |
| 1154 | bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1155 | if (!klass->IsPrimitive() && klass->GetClassLoader() == class_loader_) { |
| 1156 | classes_.push_back(klass); |
| 1157 | } |
| 1158 | return true; |
| 1159 | } |
| 1160 | |
| 1161 | void Check() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1162 | for (ObjPtr<mirror::Class> klass : classes_) { |
| 1163 | for (uint32_t i = 0, num = klass->NumDirectInterfaces(); i != num; ++i) { |
| 1164 | CHECK(klass->GetDirectInterface(self_, klass, i) != nullptr) |
| 1165 | << klass->PrettyDescriptor() << " iface #" << i; |
| 1166 | } |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | private: |
| 1171 | ObjPtr<mirror::ClassLoader> class_loader_; |
| 1172 | Thread* self_; |
| 1173 | std::vector<ObjPtr<mirror::Class>> classes_; |
| 1174 | }; |
| 1175 | |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1176 | class VerifyDeclaringClassVisitor : public ArtMethodVisitor { |
| 1177 | public: |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1178 | VerifyDeclaringClassVisitor() REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1179 | : live_bitmap_(Runtime::Current()->GetHeap()->GetLiveBitmap()) {} |
| 1180 | |
| 1181 | virtual void Visit(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1182 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1183 | ObjPtr<mirror::Class> klass = method->GetDeclaringClassUnchecked(); |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1184 | if (klass != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1185 | CHECK(live_bitmap_->Test(klass.Ptr())) << "Image method has unmarked declaring class"; |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | private: |
| 1190 | gc::accounting::HeapBitmap* const live_bitmap_; |
| 1191 | }; |
| 1192 | |
Artem Udovichenko | b3ac45b | 2016-09-09 14:02:25 +0300 | [diff] [blame] | 1193 | // Copies data from one array to another array at the same position |
| 1194 | // if pred returns false. If there is a page of continuous data in |
| 1195 | // the src array for which pred consistently returns true then |
| 1196 | // corresponding page in the dst array will not be touched. |
| 1197 | // This should reduce number of allocated physical pages. |
| 1198 | template <class T, class NullPred> |
| 1199 | static void CopyNonNull(const T* src, size_t count, T* dst, const NullPred& pred) { |
| 1200 | for (size_t i = 0; i < count; ++i) { |
| 1201 | if (!pred(src[i])) { |
| 1202 | dst[i] = src[i]; |
| 1203 | } |
| 1204 | } |
| 1205 | } |
| 1206 | |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1207 | template <typename T> |
| 1208 | static void CopyDexCachePairs(const std::atomic<mirror::DexCachePair<T>>* src, |
| 1209 | size_t count, |
| 1210 | std::atomic<mirror::DexCachePair<T>>* dst) { |
| 1211 | DCHECK_NE(count, 0u); |
| 1212 | DCHECK(!src[0].load(std::memory_order_relaxed).object.IsNull() || |
| 1213 | src[0].load(std::memory_order_relaxed).index != 0u); |
| 1214 | for (size_t i = 0; i < count; ++i) { |
| 1215 | DCHECK_EQ(dst[i].load(std::memory_order_relaxed).index, 0u); |
| 1216 | DCHECK(dst[i].load(std::memory_order_relaxed).object.IsNull()); |
| 1217 | mirror::DexCachePair<T> source = src[i].load(std::memory_order_relaxed); |
| 1218 | if (source.index != 0u || !source.object.IsNull()) { |
| 1219 | dst[i].store(source, std::memory_order_relaxed); |
| 1220 | } |
| 1221 | } |
| 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 | 064e9d4 | 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_); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1240 | // Dex cache array fixup is all or nothing, we must reject app images that have mixed since we |
| 1241 | // rely on clobering the dex cache arrays in the image to forward to bss. |
| 1242 | size_t num_dex_caches_with_bss_arrays = 0; |
| 1243 | const size_t num_dex_caches = dex_caches->GetLength(); |
| 1244 | for (size_t i = 0; i < num_dex_caches; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1245 | ObjPtr<mirror::DexCache> const dex_cache = dex_caches->Get(i); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1246 | const DexFile* const dex_file = dex_cache->GetDexFile(); |
| 1247 | const OatFile::OatDexFile* oat_dex_file = dex_file->GetOatDexFile(); |
| 1248 | if (oat_dex_file != nullptr && oat_dex_file->GetDexCacheArrays() != nullptr) { |
| 1249 | ++num_dex_caches_with_bss_arrays; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1250 | } |
| 1251 | } |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1252 | *out_forward_dex_cache_array = num_dex_caches_with_bss_arrays != 0; |
| 1253 | if (*out_forward_dex_cache_array) { |
| 1254 | if (num_dex_caches_with_bss_arrays != num_dex_caches) { |
| 1255 | // Reject application image since we cannot forward only some of the dex cache arrays. |
| 1256 | // TODO: We could get around this by having a dedicated forwarding slot. It should be an |
| 1257 | // uncommon case. |
| 1258 | *out_error_msg = StringPrintf("Dex caches in bss does not match total: %zu vs %zu", |
| 1259 | num_dex_caches_with_bss_arrays, |
| 1260 | num_dex_caches); |
| 1261 | return false; |
| 1262 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1263 | } |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1264 | // Only add the classes to the class loader after the points where we can return false. |
| 1265 | for (size_t i = 0; i < num_dex_caches; i++) { |
Vladimir Marko | 1bc4b17 | 2016-10-24 16:53:39 +0000 | [diff] [blame] | 1266 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1267 | const DexFile* const dex_file = dex_cache->GetDexFile(); |
| 1268 | const OatFile::OatDexFile* oat_dex_file = dex_file->GetOatDexFile(); |
| 1269 | if (oat_dex_file != nullptr && oat_dex_file->GetDexCacheArrays() != nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1270 | // If the oat file expects the dex cache arrays to be in the BSS, then allocate there and |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1271 | // copy over the arrays. |
| 1272 | DCHECK(dex_file != nullptr); |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 1273 | size_t num_strings = mirror::DexCache::kDexCacheStringCacheSize; |
| 1274 | if (dex_file->NumStringIds() < num_strings) { |
| 1275 | num_strings = dex_file->NumStringIds(); |
| 1276 | } |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1277 | size_t num_types = mirror::DexCache::kDexCacheTypeCacheSize; |
| 1278 | if (dex_file->NumTypeIds() < num_types) { |
| 1279 | num_types = dex_file->NumTypeIds(); |
| 1280 | } |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1281 | const size_t num_methods = dex_file->NumMethodIds(); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1282 | size_t num_fields = mirror::DexCache::kDexCacheFieldCacheSize; |
| 1283 | if (dex_file->NumFieldIds() < num_fields) { |
| 1284 | num_fields = dex_file->NumFieldIds(); |
| 1285 | } |
Narayan Kamath | 269cb43 | 2016-10-28 10:19:54 +0100 | [diff] [blame] | 1286 | size_t num_method_types = mirror::DexCache::kDexCacheMethodTypeCacheSize; |
| 1287 | if (dex_file->NumProtoIds() < num_method_types) { |
| 1288 | num_method_types = dex_file->NumProtoIds(); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1289 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1290 | const size_t num_call_sites = dex_file->NumCallSiteIds(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1291 | CHECK_EQ(num_strings, dex_cache->NumStrings()); |
| 1292 | CHECK_EQ(num_types, dex_cache->NumResolvedTypes()); |
| 1293 | CHECK_EQ(num_methods, dex_cache->NumResolvedMethods()); |
| 1294 | CHECK_EQ(num_fields, dex_cache->NumResolvedFields()); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1295 | CHECK_EQ(num_method_types, dex_cache->NumResolvedMethodTypes()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1296 | CHECK_EQ(num_call_sites, dex_cache->NumResolvedCallSites()); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1297 | DexCacheArraysLayout layout(image_pointer_size_, dex_file); |
| 1298 | uint8_t* const raw_arrays = oat_dex_file->GetDexCacheArrays(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1299 | if (num_strings != 0u) { |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 1300 | mirror::StringDexCacheType* const image_resolved_strings = dex_cache->GetStrings(); |
| 1301 | mirror::StringDexCacheType* const strings = |
| 1302 | reinterpret_cast<mirror::StringDexCacheType*>(raw_arrays + layout.StringsOffset()); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1303 | CopyDexCachePairs(image_resolved_strings, num_strings, strings); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1304 | dex_cache->SetStrings(strings); |
| 1305 | } |
| 1306 | if (num_types != 0u) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1307 | mirror::TypeDexCacheType* const image_resolved_types = dex_cache->GetResolvedTypes(); |
| 1308 | mirror::TypeDexCacheType* const types = |
| 1309 | reinterpret_cast<mirror::TypeDexCacheType*>(raw_arrays + layout.TypesOffset()); |
| 1310 | CopyDexCachePairs(image_resolved_types, num_types, types); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1311 | dex_cache->SetResolvedTypes(types); |
| 1312 | } |
| 1313 | if (num_methods != 0u) { |
| 1314 | ArtMethod** const methods = reinterpret_cast<ArtMethod**>( |
| 1315 | raw_arrays + layout.MethodsOffset()); |
| 1316 | ArtMethod** const image_resolved_methods = dex_cache->GetResolvedMethods(); |
| 1317 | for (size_t j = 0; kIsDebugBuild && j < num_methods; ++j) { |
| 1318 | DCHECK(methods[j] == nullptr); |
| 1319 | } |
Artem Udovichenko | b3ac45b | 2016-09-09 14:02:25 +0300 | [diff] [blame] | 1320 | CopyNonNull(image_resolved_methods, |
| 1321 | num_methods, |
| 1322 | methods, |
| 1323 | [] (const ArtMethod* method) { |
| 1324 | return method == nullptr; |
| 1325 | }); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1326 | dex_cache->SetResolvedMethods(methods); |
| 1327 | } |
| 1328 | if (num_fields != 0u) { |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1329 | mirror::FieldDexCacheType* const image_resolved_fields = dex_cache->GetResolvedFields(); |
| 1330 | mirror::FieldDexCacheType* const fields = |
| 1331 | reinterpret_cast<mirror::FieldDexCacheType*>(raw_arrays + layout.FieldsOffset()); |
| 1332 | for (size_t j = 0; j < num_fields; ++j) { |
| 1333 | DCHECK_EQ(mirror::DexCache::GetNativePairPtrSize(fields, j, image_pointer_size_).index, |
| 1334 | 0u); |
| 1335 | DCHECK(mirror::DexCache::GetNativePairPtrSize(fields, j, image_pointer_size_).object == |
| 1336 | nullptr); |
| 1337 | mirror::DexCache::SetNativePairPtrSize( |
| 1338 | fields, |
| 1339 | j, |
| 1340 | mirror::DexCache::GetNativePairPtrSize(image_resolved_fields, |
| 1341 | j, |
| 1342 | image_pointer_size_), |
| 1343 | image_pointer_size_); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1344 | } |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1345 | dex_cache->SetResolvedFields(fields); |
| 1346 | } |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1347 | if (num_method_types != 0u) { |
| 1348 | // NOTE: We currently (Sep 2016) do not resolve any method types at |
| 1349 | // compile time, but plan to in the future. This code exists for the |
| 1350 | // sake of completeness. |
| 1351 | mirror::MethodTypeDexCacheType* const image_resolved_method_types = |
| 1352 | dex_cache->GetResolvedMethodTypes(); |
| 1353 | mirror::MethodTypeDexCacheType* const method_types = |
| 1354 | reinterpret_cast<mirror::MethodTypeDexCacheType*>( |
| 1355 | raw_arrays + layout.MethodTypesOffset()); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1356 | CopyDexCachePairs(image_resolved_method_types, num_method_types, method_types); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 1357 | dex_cache->SetResolvedMethodTypes(method_types); |
| 1358 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 1359 | if (num_call_sites != 0u) { |
| 1360 | GcRoot<mirror::CallSite>* const image_resolved_call_sites = |
| 1361 | dex_cache->GetResolvedCallSites(); |
| 1362 | GcRoot<mirror::CallSite>* const call_sites = |
| 1363 | reinterpret_cast<GcRoot<mirror::CallSite>*>(raw_arrays + layout.CallSitesOffset()); |
| 1364 | for (size_t j = 0; kIsDebugBuild && j < num_call_sites; ++j) { |
| 1365 | DCHECK(call_sites[j].IsNull()); |
| 1366 | } |
| 1367 | CopyNonNull(image_resolved_call_sites, |
| 1368 | num_call_sites, |
| 1369 | call_sites, |
| 1370 | [](const GcRoot<mirror::CallSite>& elem) { |
| 1371 | return elem.IsNull(); |
| 1372 | }); |
| 1373 | dex_cache->SetResolvedCallSites(call_sites); |
| 1374 | } |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1375 | } |
| 1376 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 1377 | WriterMutexLock mu2(self, *Locks::dex_lock_); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1378 | // Make sure to do this after we update the arrays since we store the resolved types array |
| 1379 | // in DexCacheData in RegisterDexFileLocked. We need the array pointer to be the one in the |
| 1380 | // BSS. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1381 | CHECK(!FindDexCacheDataLocked(*dex_file).IsValid()); |
| 1382 | RegisterDexFileLocked(*dex_file, dex_cache, class_loader.Get()); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1383 | } |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1384 | if (kIsDebugBuild) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1385 | CHECK(new_class_set != nullptr); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1386 | mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes(); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1387 | const size_t num_types = dex_cache->NumResolvedTypes(); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1388 | for (size_t j = 0; j != num_types; ++j) { |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1389 | // The image space is not yet added to the heap, avoid read barriers. |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1390 | ObjPtr<mirror::Class> klass = types[j].load(std::memory_order_relaxed).object.Read(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1391 | if (space->HasAddress(klass.Ptr())) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 1392 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 1393 | auto it = new_class_set->Find(ClassTable::TableSlot(klass)); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1394 | DCHECK(it != new_class_set->end()); |
| 1395 | DCHECK_EQ(it->Read(), klass); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1396 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1397 | if (super_class != nullptr && !heap->ObjectIsInBootImageSpace(super_class)) { |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 1398 | auto it2 = new_class_set->Find(ClassTable::TableSlot(super_class)); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1399 | DCHECK(it2 != new_class_set->end()); |
| 1400 | DCHECK_EQ(it2->Read(), super_class); |
| 1401 | } |
| 1402 | for (ArtMethod& m : klass->GetDirectMethods(kRuntimePointerSize)) { |
| 1403 | const void* code = m.GetEntryPointFromQuickCompiledCode(); |
| 1404 | const void* oat_code = m.IsInvokable() ? GetQuickOatCodeFor(&m) : code; |
| 1405 | if (!IsQuickResolutionStub(code) && |
| 1406 | !IsQuickGenericJniStub(code) && |
| 1407 | !IsQuickToInterpreterBridge(code) && |
| 1408 | !m.IsNative()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1409 | DCHECK_EQ(code, oat_code) << m.PrettyMethod(); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1410 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1411 | } |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1412 | for (ArtMethod& m : klass->GetVirtualMethods(kRuntimePointerSize)) { |
| 1413 | const void* code = m.GetEntryPointFromQuickCompiledCode(); |
| 1414 | const void* oat_code = m.IsInvokable() ? GetQuickOatCodeFor(&m) : code; |
| 1415 | if (!IsQuickResolutionStub(code) && |
| 1416 | !IsQuickGenericJniStub(code) && |
| 1417 | !IsQuickToInterpreterBridge(code) && |
| 1418 | !m.IsNative()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1419 | DCHECK_EQ(code, oat_code) << m.PrettyMethod(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1420 | } |
| 1421 | } |
| 1422 | } |
| 1423 | } |
| 1424 | } |
| 1425 | } |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1426 | } |
| 1427 | if (*out_forward_dex_cache_array) { |
| 1428 | ScopedTrace timing("Fixup ArtMethod dex cache arrays"); |
| 1429 | FixupArtMethodArrayVisitor visitor(header); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1430 | header.VisitPackedArtMethods(&visitor, space->Begin(), kRuntimePointerSize); |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1431 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader.Get()); |
| 1432 | } |
| 1433 | if (kVerifyArtMethodDeclaringClasses) { |
| 1434 | ScopedTrace timing("Verify declaring classes"); |
| 1435 | ReaderMutexLock rmu(self, *Locks::heap_bitmap_lock_); |
| 1436 | VerifyDeclaringClassVisitor visitor; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1437 | header.VisitPackedArtMethods(&visitor, space->Begin(), kRuntimePointerSize); |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1438 | } |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1439 | return true; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1440 | } |
| 1441 | |
Nicolas Geoffray | f9bf250 | 2016-12-14 14:59:04 +0000 | [diff] [blame] | 1442 | // Update the class loader. Should only be used on classes in the image space. |
| 1443 | class UpdateClassLoaderVisitor { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1444 | public: |
Nicolas Geoffray | f9bf250 | 2016-12-14 14:59:04 +0000 | [diff] [blame] | 1445 | UpdateClassLoaderVisitor(gc::space::ImageSpace* space, ObjPtr<mirror::ClassLoader> class_loader) |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1446 | : space_(space), |
Nicolas Geoffray | f9bf250 | 2016-12-14 14:59:04 +0000 | [diff] [blame] | 1447 | class_loader_(class_loader) {} |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1448 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1449 | bool operator()(ObjPtr<mirror::Class> klass) const REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 1450 | // Do not update class loader for boot image classes where the app image |
| 1451 | // class loader is only the initiating loader but not the defining loader. |
| 1452 | if (klass->GetClassLoader() != nullptr) { |
| 1453 | klass->SetClassLoader(class_loader_); |
| 1454 | } |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1455 | return true; |
| 1456 | } |
| 1457 | |
| 1458 | gc::space::ImageSpace* const space_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1459 | ObjPtr<mirror::ClassLoader> const class_loader_; |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1460 | }; |
| 1461 | |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1462 | static std::unique_ptr<const DexFile> OpenOatDexFile(const OatFile* oat_file, |
| 1463 | const char* location, |
| 1464 | std::string* error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1465 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1466 | DCHECK(error_msg != nullptr); |
| 1467 | std::unique_ptr<const DexFile> dex_file; |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 1468 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(location, nullptr, error_msg); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1469 | if (oat_dex_file == nullptr) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1470 | return std::unique_ptr<const DexFile>(); |
| 1471 | } |
| 1472 | std::string inner_error_msg; |
| 1473 | dex_file = oat_dex_file->OpenDexFile(&inner_error_msg); |
| 1474 | if (dex_file == nullptr) { |
| 1475 | *error_msg = StringPrintf("Failed to open dex file %s from within oat file %s error '%s'", |
| 1476 | location, |
| 1477 | oat_file->GetLocation().c_str(), |
| 1478 | inner_error_msg.c_str()); |
| 1479 | return std::unique_ptr<const DexFile>(); |
| 1480 | } |
| 1481 | |
| 1482 | if (dex_file->GetLocationChecksum() != oat_dex_file->GetDexFileLocationChecksum()) { |
| 1483 | *error_msg = StringPrintf("Checksums do not match for %s: %x vs %x", |
| 1484 | location, |
| 1485 | dex_file->GetLocationChecksum(), |
| 1486 | oat_dex_file->GetDexFileLocationChecksum()); |
| 1487 | return std::unique_ptr<const DexFile>(); |
| 1488 | } |
| 1489 | return dex_file; |
| 1490 | } |
| 1491 | |
| 1492 | bool ClassLinker::OpenImageDexFiles(gc::space::ImageSpace* space, |
| 1493 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1494 | std::string* error_msg) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 1495 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1496 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1497 | ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1498 | DCHECK(dex_caches_object != nullptr); |
| 1499 | mirror::ObjectArray<mirror::DexCache>* dex_caches = |
| 1500 | dex_caches_object->AsObjectArray<mirror::DexCache>(); |
| 1501 | const OatFile* oat_file = space->GetOatFile(); |
| 1502 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1503 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1504 | std::string dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
| 1505 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 1506 | dex_file_location.c_str(), |
| 1507 | error_msg); |
| 1508 | if (dex_file == nullptr) { |
| 1509 | return false; |
| 1510 | } |
| 1511 | dex_cache->SetDexFile(dex_file.get()); |
| 1512 | out_dex_files->push_back(std::move(dex_file)); |
| 1513 | } |
| 1514 | return true; |
| 1515 | } |
| 1516 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1517 | // Helper class for ArtMethod checks when adding an image. Keeps all required functionality |
| 1518 | // together and caches some intermediate results. |
| 1519 | class ImageSanityChecks FINAL { |
| 1520 | public: |
| 1521 | static void CheckObjects(gc::Heap* heap, ClassLinker* class_linker) |
| 1522 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1523 | ImageSanityChecks isc(heap, class_linker); |
| 1524 | heap->VisitObjects(ImageSanityChecks::SanityCheckObjectsCallback, &isc); |
| 1525 | } |
| 1526 | |
| 1527 | static void CheckPointerArray(gc::Heap* heap, |
| 1528 | ClassLinker* class_linker, |
| 1529 | ArtMethod** arr, |
| 1530 | size_t size) |
| 1531 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1532 | ImageSanityChecks isc(heap, class_linker); |
| 1533 | isc.SanityCheckArtMethodPointerArray(arr, size); |
| 1534 | } |
| 1535 | |
| 1536 | static void SanityCheckObjectsCallback(mirror::Object* obj, void* arg) |
| 1537 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1538 | DCHECK(obj != nullptr); |
| 1539 | CHECK(obj->GetClass() != nullptr) << "Null class in object " << obj; |
| 1540 | CHECK(obj->GetClass()->GetClass() != nullptr) << "Null class class " << obj; |
| 1541 | if (obj->IsClass()) { |
| 1542 | ImageSanityChecks* isc = reinterpret_cast<ImageSanityChecks*>(arg); |
| 1543 | |
| 1544 | auto klass = obj->AsClass(); |
| 1545 | for (ArtField& field : klass->GetIFields()) { |
| 1546 | CHECK_EQ(field.GetDeclaringClass(), klass); |
| 1547 | } |
| 1548 | for (ArtField& field : klass->GetSFields()) { |
| 1549 | CHECK_EQ(field.GetDeclaringClass(), klass); |
| 1550 | } |
| 1551 | const auto pointer_size = isc->pointer_size_; |
| 1552 | for (auto& m : klass->GetMethods(pointer_size)) { |
| 1553 | isc->SanityCheckArtMethod(&m, klass); |
| 1554 | } |
| 1555 | auto* vtable = klass->GetVTable(); |
| 1556 | if (vtable != nullptr) { |
| 1557 | isc->SanityCheckArtMethodPointerArray(vtable, nullptr); |
| 1558 | } |
| 1559 | if (klass->ShouldHaveImt()) { |
| 1560 | ImTable* imt = klass->GetImt(pointer_size); |
| 1561 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 1562 | isc->SanityCheckArtMethod(imt->Get(i, pointer_size), nullptr); |
| 1563 | } |
| 1564 | } |
| 1565 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 1566 | for (int32_t i = 0; i < klass->GetEmbeddedVTableLength(); ++i) { |
| 1567 | isc->SanityCheckArtMethod(klass->GetEmbeddedVTableEntry(i, pointer_size), nullptr); |
| 1568 | } |
| 1569 | } |
| 1570 | mirror::IfTable* iftable = klass->GetIfTable(); |
| 1571 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 1572 | if (iftable->GetMethodArrayCount(i) > 0) { |
| 1573 | isc->SanityCheckArtMethodPointerArray(iftable->GetMethodArray(i), nullptr); |
| 1574 | } |
| 1575 | } |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | private: |
| 1580 | ImageSanityChecks(gc::Heap* heap, ClassLinker* class_linker) |
| 1581 | : spaces_(heap->GetBootImageSpaces()), |
| 1582 | pointer_size_(class_linker->GetImagePointerSize()) { |
| 1583 | space_begin_.reserve(spaces_.size()); |
| 1584 | method_sections_.reserve(spaces_.size()); |
| 1585 | runtime_method_sections_.reserve(spaces_.size()); |
| 1586 | for (gc::space::ImageSpace* space : spaces_) { |
| 1587 | space_begin_.push_back(space->Begin()); |
| 1588 | auto& header = space->GetImageHeader(); |
| 1589 | method_sections_.push_back(&header.GetMethodsSection()); |
| 1590 | runtime_method_sections_.push_back(&header.GetRuntimeMethodsSection()); |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | void SanityCheckArtMethod(ArtMethod* m, ObjPtr<mirror::Class> expected_class) |
| 1595 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1596 | if (m->IsRuntimeMethod()) { |
| 1597 | ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClassUnchecked(); |
| 1598 | CHECK(declaring_class == nullptr) << declaring_class << " " << m->PrettyMethod(); |
| 1599 | } else if (m->IsCopied()) { |
| 1600 | CHECK(m->GetDeclaringClass() != nullptr) << m->PrettyMethod(); |
| 1601 | } else if (expected_class != nullptr) { |
| 1602 | CHECK_EQ(m->GetDeclaringClassUnchecked(), expected_class) << m->PrettyMethod(); |
| 1603 | } |
| 1604 | if (!spaces_.empty()) { |
| 1605 | bool contains = false; |
| 1606 | for (size_t i = 0; !contains && i != space_begin_.size(); ++i) { |
| 1607 | const size_t offset = reinterpret_cast<uint8_t*>(m) - space_begin_[i]; |
| 1608 | contains = method_sections_[i]->Contains(offset) || |
| 1609 | runtime_method_sections_[i]->Contains(offset); |
| 1610 | } |
| 1611 | CHECK(contains) << m << " not found"; |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | void SanityCheckArtMethodPointerArray(ObjPtr<mirror::PointerArray> arr, |
| 1616 | ObjPtr<mirror::Class> expected_class) |
| 1617 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1618 | CHECK(arr != nullptr); |
| 1619 | for (int32_t j = 0; j < arr->GetLength(); ++j) { |
| 1620 | auto* method = arr->GetElementPtrSize<ArtMethod*>(j, pointer_size_); |
| 1621 | // expected_class == null means we are a dex cache. |
| 1622 | if (expected_class != nullptr) { |
| 1623 | CHECK(method != nullptr); |
| 1624 | } |
| 1625 | if (method != nullptr) { |
| 1626 | SanityCheckArtMethod(method, expected_class); |
| 1627 | } |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | void SanityCheckArtMethodPointerArray(ArtMethod** arr, size_t size) |
| 1632 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1633 | CHECK_EQ(arr != nullptr, size != 0u); |
| 1634 | if (arr != nullptr) { |
| 1635 | bool contains = false; |
| 1636 | for (auto space : spaces_) { |
| 1637 | auto offset = reinterpret_cast<uint8_t*>(arr) - space->Begin(); |
| 1638 | if (space->GetImageHeader().GetImageSection( |
| 1639 | ImageHeader::kSectionDexCacheArrays).Contains(offset)) { |
| 1640 | contains = true; |
| 1641 | break; |
| 1642 | } |
| 1643 | } |
| 1644 | CHECK(contains); |
| 1645 | } |
| 1646 | for (size_t j = 0; j < size; ++j) { |
| 1647 | ArtMethod* method = mirror::DexCache::GetElementPtrSize(arr, j, pointer_size_); |
| 1648 | // expected_class == null means we are a dex cache. |
| 1649 | if (method != nullptr) { |
| 1650 | SanityCheckArtMethod(method, nullptr); |
| 1651 | } |
| 1652 | } |
| 1653 | } |
| 1654 | |
| 1655 | const std::vector<gc::space::ImageSpace*>& spaces_; |
| 1656 | const PointerSize pointer_size_; |
| 1657 | |
| 1658 | // Cached sections from the spaces. |
| 1659 | std::vector<const uint8_t*> space_begin_; |
| 1660 | std::vector<const ImageSection*> method_sections_; |
| 1661 | std::vector<const ImageSection*> runtime_method_sections_; |
| 1662 | }; |
| 1663 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1664 | bool ClassLinker::AddImageSpace( |
| 1665 | gc::space::ImageSpace* space, |
| 1666 | Handle<mirror::ClassLoader> class_loader, |
| 1667 | jobjectArray dex_elements, |
| 1668 | const char* dex_location, |
| 1669 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1670 | std::string* error_msg) { |
| 1671 | DCHECK(out_dex_files != nullptr); |
| 1672 | DCHECK(error_msg != nullptr); |
| 1673 | const uint64_t start_time = NanoTime(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1674 | const bool app_image = class_loader != nullptr; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1675 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1676 | ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1677 | DCHECK(dex_caches_object != nullptr); |
| 1678 | Runtime* const runtime = Runtime::Current(); |
| 1679 | gc::Heap* const heap = runtime->GetHeap(); |
| 1680 | Thread* const self = Thread::Current(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1681 | // Check that the image is what we are expecting. |
| 1682 | if (image_pointer_size_ != space->GetImageHeader().GetPointerSize()) { |
| 1683 | *error_msg = StringPrintf("Application image pointer size does not match runtime: %zu vs %zu", |
| 1684 | static_cast<size_t>(space->GetImageHeader().GetPointerSize()), |
| 1685 | image_pointer_size_); |
| 1686 | return false; |
| 1687 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1688 | size_t expected_image_roots = ImageHeader::NumberOfImageRoots(app_image); |
| 1689 | if (static_cast<size_t>(header.GetImageRoots()->GetLength()) != expected_image_roots) { |
| 1690 | *error_msg = StringPrintf("Expected %zu image roots but got %d", |
| 1691 | expected_image_roots, |
| 1692 | header.GetImageRoots()->GetLength()); |
| 1693 | return false; |
| 1694 | } |
| 1695 | StackHandleScope<3> hs(self); |
| 1696 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches( |
| 1697 | hs.NewHandle(dex_caches_object->AsObjectArray<mirror::DexCache>())); |
| 1698 | Handle<mirror::ObjectArray<mirror::Class>> class_roots(hs.NewHandle( |
| 1699 | header.GetImageRoot(ImageHeader::kClassRoots)->AsObjectArray<mirror::Class>())); |
| 1700 | static_assert(ImageHeader::kClassLoader + 1u == ImageHeader::kImageRootsMax, |
| 1701 | "Class loader should be the last image root."); |
| 1702 | MutableHandle<mirror::ClassLoader> image_class_loader(hs.NewHandle( |
| 1703 | app_image ? header.GetImageRoot(ImageHeader::kClassLoader)->AsClassLoader() : nullptr)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1704 | DCHECK(class_roots != nullptr); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1705 | if (class_roots->GetLength() != static_cast<int32_t>(kClassRootsMax)) { |
| 1706 | *error_msg = StringPrintf("Expected %d class roots but got %d", |
| 1707 | class_roots->GetLength(), |
| 1708 | static_cast<int32_t>(kClassRootsMax)); |
| 1709 | return false; |
| 1710 | } |
| 1711 | // Check against existing class roots to make sure they match the ones in the boot image. |
| 1712 | for (size_t i = 0; i < kClassRootsMax; i++) { |
| 1713 | if (class_roots->Get(i) != GetClassRoot(static_cast<ClassRoot>(i))) { |
| 1714 | *error_msg = "App image class roots must have pointer equality with runtime ones."; |
| 1715 | return false; |
| 1716 | } |
| 1717 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1718 | const OatFile* oat_file = space->GetOatFile(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1719 | if (oat_file->GetOatHeader().GetDexFileCount() != |
| 1720 | static_cast<uint32_t>(dex_caches->GetLength())) { |
| 1721 | *error_msg = "Dex cache count and dex file count mismatch while trying to initialize from " |
| 1722 | "image"; |
| 1723 | return false; |
| 1724 | } |
| 1725 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1726 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1727 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
| 1728 | std::string dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1729 | // TODO: Only store qualified paths. |
| 1730 | // If non qualified, qualify it. |
| 1731 | if (dex_file_location.find('/') == std::string::npos) { |
| 1732 | std::string dex_location_path = dex_location; |
| 1733 | const size_t pos = dex_location_path.find_last_of('/'); |
| 1734 | CHECK_NE(pos, std::string::npos); |
| 1735 | dex_location_path = dex_location_path.substr(0, pos + 1); // Keep trailing '/' |
| 1736 | dex_file_location = dex_location_path + dex_file_location; |
| 1737 | } |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1738 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 1739 | dex_file_location.c_str(), |
| 1740 | error_msg); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1741 | if (dex_file == nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1742 | return false; |
| 1743 | } |
| 1744 | |
| 1745 | if (app_image) { |
| 1746 | // The current dex file field is bogus, overwrite it so that we can get the dex file in the |
| 1747 | // loop below. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1748 | dex_cache->SetDexFile(dex_file.get()); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1749 | mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes(); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1750 | for (int32_t j = 0, num_types = dex_cache->NumResolvedTypes(); j < num_types; j++) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1751 | ObjPtr<mirror::Class> klass = types[j].load(std::memory_order_relaxed).object.Read(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1752 | if (klass != nullptr) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 1753 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1754 | } |
| 1755 | } |
| 1756 | } else { |
| 1757 | if (kSanityCheckObjects) { |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1758 | ImageSanityChecks::CheckPointerArray(heap, |
| 1759 | this, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1760 | dex_cache->GetResolvedMethods(), |
| 1761 | dex_cache->NumResolvedMethods()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1762 | } |
| 1763 | // Register dex files, keep track of existing ones that are conflicts. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1764 | AppendToBootClassPath(*dex_file.get(), dex_cache); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1765 | } |
| 1766 | out_dex_files->push_back(std::move(dex_file)); |
| 1767 | } |
| 1768 | |
| 1769 | if (app_image) { |
| 1770 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1771 | // Check that the class loader resolves the same way as the ones in the image. |
| 1772 | // Image class loader [A][B][C][image dex files] |
| 1773 | // Class loader = [???][dex_elements][image dex files] |
| 1774 | // Need to ensure that [???][dex_elements] == [A][B][C]. |
| 1775 | // For each class loader, PathClassLoader, the laoder checks the parent first. Also the logic |
| 1776 | // for PathClassLoader does this by looping through the array of dex files. To ensure they |
| 1777 | // resolve the same way, simply flatten the hierarchy in the way the resolution order would be, |
| 1778 | // and check that the dex file names are the same. |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1779 | if (IsBootClassLoader(soa, image_class_loader.Get())) { |
| 1780 | *error_msg = "Unexpected BootClassLoader in app image"; |
| 1781 | return false; |
| 1782 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1783 | std::list<ObjPtr<mirror::String>> image_dex_file_names; |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1784 | std::string temp_error_msg; |
| 1785 | if (!FlattenPathClassLoader(image_class_loader.Get(), &image_dex_file_names, &temp_error_msg)) { |
| 1786 | *error_msg = StringPrintf("Failed to flatten image class loader hierarchy '%s'", |
| 1787 | temp_error_msg.c_str()); |
| 1788 | return false; |
| 1789 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1790 | std::list<ObjPtr<mirror::String>> loader_dex_file_names; |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1791 | if (!FlattenPathClassLoader(class_loader.Get(), &loader_dex_file_names, &temp_error_msg)) { |
| 1792 | *error_msg = StringPrintf("Failed to flatten class loader hierarchy '%s'", |
| 1793 | temp_error_msg.c_str()); |
| 1794 | return false; |
| 1795 | } |
| 1796 | // Add the temporary dex path list elements at the end. |
| 1797 | auto elements = soa.Decode<mirror::ObjectArray<mirror::Object>>(dex_elements); |
| 1798 | for (size_t i = 0, num_elems = elements->GetLength(); i < num_elems; ++i) { |
| 1799 | ObjPtr<mirror::Object> element = elements->GetWithoutChecks(i); |
| 1800 | if (element != nullptr) { |
| 1801 | // If we are somewhere in the middle of the array, there may be nulls at the end. |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1802 | ObjPtr<mirror::String> name; |
| 1803 | if (GetDexPathListElementName(element, &name) && name != nullptr) { |
| 1804 | loader_dex_file_names.push_back(name); |
| 1805 | } |
Nicolas Geoffray | 1df3b55 | 2016-04-26 18:30:31 +0100 | [diff] [blame] | 1806 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1807 | } |
| 1808 | // Ignore the number of image dex files since we are adding those to the class loader anyways. |
| 1809 | CHECK_GE(static_cast<size_t>(image_dex_file_names.size()), |
| 1810 | static_cast<size_t>(dex_caches->GetLength())); |
| 1811 | size_t image_count = image_dex_file_names.size() - dex_caches->GetLength(); |
| 1812 | // Check that the dex file names match. |
| 1813 | bool equal = image_count == loader_dex_file_names.size(); |
| 1814 | if (equal) { |
| 1815 | auto it1 = image_dex_file_names.begin(); |
| 1816 | auto it2 = loader_dex_file_names.begin(); |
| 1817 | for (size_t i = 0; equal && i < image_count; ++i, ++it1, ++it2) { |
| 1818 | equal = equal && (*it1)->Equals(*it2); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1819 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1820 | } |
| 1821 | if (!equal) { |
| 1822 | VLOG(image) << "Image dex files " << image_dex_file_names.size(); |
| 1823 | for (ObjPtr<mirror::String> name : image_dex_file_names) { |
| 1824 | VLOG(image) << name->ToModifiedUtf8(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1825 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1826 | VLOG(image) << "Loader dex files " << loader_dex_file_names.size(); |
| 1827 | for (ObjPtr<mirror::String> name : loader_dex_file_names) { |
| 1828 | VLOG(image) << name->ToModifiedUtf8(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1829 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1830 | *error_msg = "Rejecting application image due to class loader mismatch"; |
| 1831 | // Ignore class loader mismatch for now since these would just use possibly incorrect |
| 1832 | // 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] | 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | if (kSanityCheckObjects) { |
| 1837 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 1838 | auto* dex_cache = dex_caches->Get(i); |
| 1839 | for (size_t j = 0; j < dex_cache->NumResolvedFields(); ++j) { |
| 1840 | auto* field = dex_cache->GetResolvedField(j, image_pointer_size_); |
| 1841 | if (field != nullptr) { |
| 1842 | CHECK(field->GetDeclaringClass()->GetClass() != nullptr); |
| 1843 | } |
| 1844 | } |
| 1845 | } |
| 1846 | if (!app_image) { |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1847 | ImageSanityChecks::CheckObjects(heap, this); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1848 | } |
| 1849 | } |
| 1850 | |
| 1851 | // Set entry point to interpreter if in InterpretOnly mode. |
| 1852 | if (!runtime->IsAotCompiler() && runtime->GetInstrumentation()->InterpretOnly()) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1853 | SetInterpreterEntrypointArtMethodVisitor visitor(image_pointer_size_); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 1854 | header.VisitPackedArtMethods(&visitor, space->Begin(), image_pointer_size_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1855 | } |
| 1856 | |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1857 | ClassTable* class_table = nullptr; |
| 1858 | { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1859 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1860 | class_table = InsertClassTableForClassLoader(class_loader.Get()); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1861 | } |
| 1862 | // If we have a class table section, read it and use it for verification in |
| 1863 | // UpdateAppImageClassLoadersAndDexCaches. |
| 1864 | ClassTable::ClassSet temp_set; |
| 1865 | const ImageSection& class_table_section = header.GetImageSection(ImageHeader::kSectionClassTable); |
| 1866 | const bool added_class_table = class_table_section.Size() > 0u; |
| 1867 | if (added_class_table) { |
| 1868 | const uint64_t start_time2 = NanoTime(); |
| 1869 | size_t read_count = 0; |
| 1870 | temp_set = ClassTable::ClassSet(space->Begin() + class_table_section.Offset(), |
| 1871 | /*make copy*/false, |
| 1872 | &read_count); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1873 | VLOG(image) << "Adding class table classes took " << PrettyDuration(NanoTime() - start_time2); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1874 | } |
| 1875 | if (app_image) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1876 | bool forward_dex_cache_arrays = false; |
| 1877 | if (!UpdateAppImageClassLoadersAndDexCaches(space, |
| 1878 | class_loader, |
| 1879 | dex_caches, |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1880 | &temp_set, |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1881 | /*out*/&forward_dex_cache_arrays, |
| 1882 | /*out*/error_msg)) { |
| 1883 | return false; |
| 1884 | } |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1885 | // Update class loader and resolved strings. If added_class_table is false, the resolved |
| 1886 | // strings were forwarded UpdateAppImageClassLoadersAndDexCaches. |
Nicolas Geoffray | f9bf250 | 2016-12-14 14:59:04 +0000 | [diff] [blame] | 1887 | UpdateClassLoaderVisitor visitor(space, class_loader.Get()); |
| 1888 | for (const ClassTable::TableSlot& root : temp_set) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1889 | visitor(root.Read()); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1890 | } |
Mathieu Chartier | 8c4f041 | 2016-02-03 16:40:20 -0800 | [diff] [blame] | 1891 | // forward_dex_cache_arrays is true iff we copied all of the dex cache arrays into the .bss. |
| 1892 | // In this case, madvise away the dex cache arrays section of the image to reduce RAM usage and |
| 1893 | // mark as PROT_NONE to catch any invalid accesses. |
| 1894 | if (forward_dex_cache_arrays) { |
| 1895 | const ImageSection& dex_cache_section = header.GetImageSection( |
| 1896 | ImageHeader::kSectionDexCacheArrays); |
| 1897 | uint8_t* section_begin = AlignUp(space->Begin() + dex_cache_section.Offset(), kPageSize); |
| 1898 | uint8_t* section_end = AlignDown(space->Begin() + dex_cache_section.End(), kPageSize); |
| 1899 | if (section_begin < section_end) { |
| 1900 | madvise(section_begin, section_end - section_begin, MADV_DONTNEED); |
| 1901 | mprotect(section_begin, section_end - section_begin, PROT_NONE); |
| 1902 | VLOG(image) << "Released and protected dex cache array image section from " |
| 1903 | << reinterpret_cast<const void*>(section_begin) << "-" |
| 1904 | << reinterpret_cast<const void*>(section_end); |
| 1905 | } |
| 1906 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 1907 | } |
| 1908 | if (!oat_file->GetBssGcRoots().empty()) { |
| 1909 | // Insert oat file to class table for visiting .bss GC roots. |
| 1910 | class_table->InsertOatFile(oat_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1911 | } |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1912 | if (added_class_table) { |
| 1913 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 1914 | class_table->AddClassSet(std::move(temp_set)); |
| 1915 | } |
| 1916 | if (kIsDebugBuild && app_image) { |
| 1917 | // This verification needs to happen after the classes have been added to the class loader. |
| 1918 | // Since it ensures classes are in the class table. |
| 1919 | VerifyClassInTableArtMethodVisitor visitor2(class_table); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1920 | header.VisitPackedArtMethods(&visitor2, space->Begin(), kRuntimePointerSize); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1921 | // Verify that all direct interfaces of classes in the class table are also resolved. |
| 1922 | VerifyDirectInterfacesInTableClassVisitor visitor(class_loader.Get()); |
| 1923 | class_table->Visit(visitor); |
| 1924 | visitor.Check(); |
| 1925 | // Check that all non-primitive classes in dex caches are also in the class table. |
| 1926 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 1927 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
| 1928 | mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes(); |
| 1929 | for (int32_t j = 0, num_types = dex_cache->NumResolvedTypes(); j < num_types; j++) { |
| 1930 | ObjPtr<mirror::Class> klass = types[j].load(std::memory_order_relaxed).object.Read(); |
| 1931 | if (klass != nullptr && !klass->IsPrimitive()) { |
| 1932 | CHECK(class_table->Contains(klass)) << klass->PrettyDescriptor() |
| 1933 | << " " << dex_cache->GetDexFile()->GetLocation(); |
| 1934 | } |
| 1935 | } |
| 1936 | } |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1937 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1938 | VLOG(class_linker) << "Adding image space took " << PrettyDuration(NanoTime() - start_time); |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 1939 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1940 | } |
| 1941 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1942 | bool ClassLinker::ClassInClassTable(ObjPtr<mirror::Class> klass) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1943 | ClassTable* const class_table = ClassTableForClassLoader(klass->GetClassLoader()); |
| 1944 | return class_table != nullptr && class_table->Contains(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1945 | } |
| 1946 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1947 | void ClassLinker::VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 1948 | // Acquire tracing_enabled before locking class linker lock to prevent lock order violation. Since |
| 1949 | // enabling tracing requires the mutator lock, there are no race conditions here. |
| 1950 | const bool tracing_enabled = Trace::IsTracingEnabled(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 1951 | Thread* const self = Thread::Current(); |
| 1952 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 1953 | if (kUseReadBarrier) { |
| 1954 | // We do not track new roots for CC. |
| 1955 | DCHECK_EQ(0, flags & (kVisitRootFlagNewRoots | |
| 1956 | kVisitRootFlagClearRootLog | |
| 1957 | kVisitRootFlagStartLoggingNewRoots | |
| 1958 | kVisitRootFlagStopLoggingNewRoots)); |
| 1959 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1960 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1961 | // Argument for how root visiting deals with ArtField and ArtMethod roots. |
| 1962 | // There is 3 GC cases to handle: |
| 1963 | // Non moving concurrent: |
| 1964 | // 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] | 1965 | // live by the class and class roots. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1966 | // |
| 1967 | // Moving non-concurrent: |
| 1968 | // This case needs to call visit VisitNativeRoots in case the classes or dex cache arrays move. |
| 1969 | // To prevent missing roots, this case needs to ensure that there is no |
| 1970 | // suspend points between the point which we allocate ArtMethod arrays and place them in a |
| 1971 | // class which is in the class table. |
| 1972 | // |
| 1973 | // Moving concurrent: |
| 1974 | // Need to make sure to not copy ArtMethods without doing read barriers since the roots are |
| 1975 | // marked concurrently and we don't hold the classlinker_classes_lock_ when we do the copy. |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 1976 | // |
| 1977 | // Use an unbuffered visitor since the class table uses a temporary GcRoot for holding decoded |
| 1978 | // ClassTable::TableSlot. The buffered root visiting would access a stale stack location for |
| 1979 | // these objects. |
| 1980 | UnbufferedRootVisitor root_visitor(visitor, RootInfo(kRootStickyClass)); |
| 1981 | boot_class_table_.VisitRoots(root_visitor); |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 1982 | // If tracing is enabled, then mark all the class loaders to prevent unloading. |
neo.chae | a2d1b28 | 2016-11-08 08:40:46 +0900 | [diff] [blame] | 1983 | if ((flags & kVisitRootFlagClassLoader) != 0 || tracing_enabled) { |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 1984 | for (const ClassLoaderData& data : class_loaders_) { |
| 1985 | GcRoot<mirror::Object> root(GcRoot<mirror::Object>(self->DecodeJObject(data.weak_root))); |
| 1986 | root.VisitRoot(visitor, RootInfo(kRootVMInternal)); |
| 1987 | } |
| 1988 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 1989 | } else if (!kUseReadBarrier && (flags & kVisitRootFlagNewRoots) != 0) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1990 | for (auto& root : new_class_roots_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1991 | ObjPtr<mirror::Class> old_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1992 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1993 | ObjPtr<mirror::Class> new_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1994 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 1995 | CHECK_EQ(new_ref, old_ref); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1996 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 1997 | for (const OatFile* oat_file : new_bss_roots_boot_oat_files_) { |
| 1998 | for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) { |
| 1999 | ObjPtr<mirror::Object> old_ref = root.Read<kWithoutReadBarrier>(); |
| 2000 | if (old_ref != nullptr) { |
| 2001 | DCHECK(old_ref->IsClass()); |
| 2002 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
| 2003 | ObjPtr<mirror::Object> new_ref = root.Read<kWithoutReadBarrier>(); |
| 2004 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 2005 | CHECK_EQ(new_ref, old_ref); |
| 2006 | } |
| 2007 | } |
| 2008 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2009 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2010 | if (!kUseReadBarrier && (flags & kVisitRootFlagClearRootLog) != 0) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2011 | new_class_roots_.clear(); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2012 | new_bss_roots_boot_oat_files_.clear(); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2013 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2014 | if (!kUseReadBarrier && (flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2015 | log_new_roots_ = true; |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2016 | } else if (!kUseReadBarrier && (flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2017 | log_new_roots_ = false; |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2018 | } |
| 2019 | // We deliberately ignore the class roots in the image since we |
| 2020 | // handle image roots by using the MS/CMS rescanning of dirty cards. |
| 2021 | } |
| 2022 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2023 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 2024 | // reinit references to when reinitializing a ClassLinker from a |
| 2025 | // mapped image. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2026 | void ClassLinker::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 2027 | class_roots_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2028 | VisitClassRoots(visitor, flags); |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 2029 | array_iftable_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); |
Mathieu Chartier | 6cfc2c0 | 2015-10-12 15:06:16 -0700 | [diff] [blame] | 2030 | // Instead of visiting the find_array_class_cache_ drop it so that it doesn't prevent class |
| 2031 | // unloading if we are marking roots. |
| 2032 | DropFindArrayClassCache(); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2033 | } |
| 2034 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2035 | class VisitClassLoaderClassesVisitor : public ClassLoaderVisitor { |
| 2036 | public: |
| 2037 | explicit VisitClassLoaderClassesVisitor(ClassVisitor* visitor) |
| 2038 | : visitor_(visitor), |
| 2039 | done_(false) {} |
| 2040 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2041 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2042 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) OVERRIDE { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2043 | ClassTable* const class_table = class_loader->GetClassTable(); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2044 | if (!done_ && class_table != nullptr) { |
| 2045 | DefiningClassLoaderFilterVisitor visitor(class_loader, visitor_); |
| 2046 | if (!class_table->Visit(visitor)) { |
| 2047 | // If the visitor ClassTable returns false it means that we don't need to continue. |
| 2048 | done_ = true; |
| 2049 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | private: |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2054 | // Class visitor that limits the class visits from a ClassTable to the classes with |
| 2055 | // the provided defining class loader. This filter is used to avoid multiple visits |
| 2056 | // of the same class which can be recorded for multiple initiating class loaders. |
| 2057 | class DefiningClassLoaderFilterVisitor : public ClassVisitor { |
| 2058 | public: |
| 2059 | DefiningClassLoaderFilterVisitor(ObjPtr<mirror::ClassLoader> defining_class_loader, |
| 2060 | ClassVisitor* visitor) |
| 2061 | : defining_class_loader_(defining_class_loader), visitor_(visitor) { } |
| 2062 | |
| 2063 | bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2064 | if (klass->GetClassLoader() != defining_class_loader_) { |
| 2065 | return true; |
| 2066 | } |
| 2067 | return (*visitor_)(klass); |
| 2068 | } |
| 2069 | |
| 2070 | ObjPtr<mirror::ClassLoader> const defining_class_loader_; |
| 2071 | ClassVisitor* const visitor_; |
| 2072 | }; |
| 2073 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2074 | ClassVisitor* const visitor_; |
| 2075 | // If done is true then we don't need to do any more visiting. |
| 2076 | bool done_; |
| 2077 | }; |
| 2078 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2079 | void ClassLinker::VisitClassesInternal(ClassVisitor* visitor) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2080 | if (boot_class_table_.Visit(*visitor)) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2081 | VisitClassLoaderClassesVisitor loader_visitor(visitor); |
| 2082 | VisitClassLoaders(&loader_visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2083 | } |
| 2084 | } |
| 2085 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2086 | void ClassLinker::VisitClasses(ClassVisitor* visitor) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2087 | Thread* const self = Thread::Current(); |
| 2088 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2089 | // Not safe to have thread suspension when we are holding a lock. |
| 2090 | if (self != nullptr) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2091 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2092 | VisitClassesInternal(visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2093 | } else { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2094 | VisitClassesInternal(visitor); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2095 | } |
| 2096 | } |
| 2097 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2098 | class GetClassesInToVector : public ClassVisitor { |
| 2099 | public: |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2100 | bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2101 | classes_.push_back(klass); |
| 2102 | return true; |
| 2103 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2104 | std::vector<ObjPtr<mirror::Class>> classes_; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2105 | }; |
| 2106 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2107 | class GetClassInToObjectArray : public ClassVisitor { |
| 2108 | public: |
| 2109 | explicit GetClassInToObjectArray(mirror::ObjectArray<mirror::Class>* arr) |
| 2110 | : arr_(arr), index_(0) {} |
| 2111 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2112 | bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2113 | ++index_; |
| 2114 | if (index_ <= arr_->GetLength()) { |
| 2115 | arr_->Set(index_ - 1, klass); |
| 2116 | return true; |
| 2117 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2118 | return false; |
| 2119 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2120 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2121 | bool Succeeded() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2122 | return index_ <= arr_->GetLength(); |
| 2123 | } |
| 2124 | |
| 2125 | private: |
| 2126 | mirror::ObjectArray<mirror::Class>* const arr_; |
| 2127 | int32_t index_; |
| 2128 | }; |
| 2129 | |
| 2130 | void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2131 | // TODO: it may be possible to avoid secondary storage if we iterate over dex caches. The problem |
| 2132 | // is avoiding duplicates. |
| 2133 | if (!kMovingClasses) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2134 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2135 | GetClassesInToVector accumulator; |
| 2136 | VisitClasses(&accumulator); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2137 | for (ObjPtr<mirror::Class> klass : accumulator.classes_) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2138 | if (!visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2139 | return; |
| 2140 | } |
| 2141 | } |
| 2142 | } else { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2143 | Thread* const self = Thread::Current(); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2144 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2145 | auto classes = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2146 | // We size the array assuming classes won't be added to the class table during the visit. |
| 2147 | // If this assumption fails we iterate again. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2148 | while (true) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2149 | size_t class_table_size; |
| 2150 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2151 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2152 | // Add 100 in case new classes get loaded when we are filling in the object array. |
| 2153 | class_table_size = NumZygoteClasses() + NumNonZygoteClasses() + 100; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2154 | } |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 2155 | ObjPtr<mirror::Class> class_type = mirror::Class::GetJavaLangClass(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2156 | ObjPtr<mirror::Class> array_of_class = FindArrayClass(self, &class_type); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2157 | classes.Assign( |
| 2158 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, class_table_size)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2159 | CHECK(classes != nullptr); // OOME. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2160 | GetClassInToObjectArray accumulator(classes.Get()); |
| 2161 | VisitClasses(&accumulator); |
| 2162 | if (accumulator.Succeeded()) { |
| 2163 | break; |
| 2164 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2165 | } |
| 2166 | for (int32_t i = 0; i < classes->GetLength(); ++i) { |
| 2167 | // If the class table shrank during creation of the clases array we expect null elements. If |
| 2168 | // the class table grew then the loop repeats. If classes are created after the loop has |
| 2169 | // finished then we don't visit. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2170 | ObjPtr<mirror::Class> klass = classes->Get(i); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2171 | if (klass != nullptr && !visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2172 | return; |
| 2173 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2174 | } |
| 2175 | } |
| 2176 | } |
| 2177 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2178 | ClassLinker::~ClassLinker() { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2179 | mirror::Class::ResetClass(); |
| 2180 | mirror::Constructor::ResetClass(); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 2181 | mirror::Field::ResetClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2182 | mirror::Method::ResetClass(); |
| 2183 | mirror::Reference::ResetClass(); |
| 2184 | mirror::StackTraceElement::ResetClass(); |
| 2185 | mirror::String::ResetClass(); |
| 2186 | mirror::Throwable::ResetClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2187 | mirror::BooleanArray::ResetArrayClass(); |
| 2188 | mirror::ByteArray::ResetArrayClass(); |
| 2189 | mirror::CharArray::ResetArrayClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2190 | mirror::Constructor::ResetArrayClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2191 | mirror::DoubleArray::ResetArrayClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2192 | mirror::Field::ResetArrayClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2193 | mirror::FloatArray::ResetArrayClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 2194 | mirror::Method::ResetArrayClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2195 | mirror::IntArray::ResetArrayClass(); |
| 2196 | mirror::LongArray::ResetArrayClass(); |
| 2197 | mirror::ShortArray::ResetArrayClass(); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 2198 | mirror::MethodType::ResetClass(); |
| 2199 | mirror::MethodHandleImpl::ResetClass(); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 2200 | mirror::MethodHandlesLookup::ResetClass(); |
| 2201 | mirror::CallSite::ResetClass(); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 2202 | mirror::EmulatedStackFrame::ResetClass(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2203 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 2204 | for (const ClassLoaderData& data : class_loaders_) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2205 | DeleteClassLoader(self, data); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 2206 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2207 | class_loaders_.clear(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2208 | } |
| 2209 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2210 | void ClassLinker::DeleteClassLoader(Thread* self, const ClassLoaderData& data) { |
| 2211 | Runtime* const runtime = Runtime::Current(); |
| 2212 | JavaVMExt* const vm = runtime->GetJavaVM(); |
| 2213 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 2214 | // 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] | 2215 | if (runtime->GetJit() != nullptr) { |
| 2216 | jit::JitCodeCache* code_cache = runtime->GetJit()->GetCodeCache(); |
| 2217 | if (code_cache != nullptr) { |
| 2218 | code_cache->RemoveMethodsIn(self, *data.allocator); |
| 2219 | } |
| 2220 | } |
| 2221 | delete data.allocator; |
| 2222 | delete data.class_table; |
| 2223 | } |
| 2224 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2225 | mirror::PointerArray* ClassLinker::AllocPointerArray(Thread* self, size_t length) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2226 | return down_cast<mirror::PointerArray*>( |
| 2227 | image_pointer_size_ == PointerSize::k64 |
| 2228 | ? static_cast<mirror::Array*>(mirror::LongArray::Alloc(self, length)) |
| 2229 | : static_cast<mirror::Array*>(mirror::IntArray::Alloc(self, length))); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2230 | } |
| 2231 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2232 | mirror::DexCache* ClassLinker::AllocDexCache(ObjPtr<mirror::String>* out_location, |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2233 | Thread* self, |
| 2234 | const DexFile& dex_file) { |
| 2235 | StackHandleScope<1> hs(self); |
| 2236 | DCHECK(out_location != nullptr); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 2237 | auto dex_cache(hs.NewHandle(ObjPtr<mirror::DexCache>::DownCast( |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2238 | GetClassRoot(kJavaLangDexCache)->AllocObject(self)))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2239 | if (dex_cache == nullptr) { |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2240 | self->AssertPendingOOMException(); |
| 2241 | return nullptr; |
| 2242 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2243 | ObjPtr<mirror::String> location = intern_table_->InternStrong(dex_file.GetLocation().c_str()); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2244 | if (location == nullptr) { |
| 2245 | self->AssertPendingOOMException(); |
| 2246 | return nullptr; |
| 2247 | } |
| 2248 | *out_location = location; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2249 | return dex_cache.Get(); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2250 | } |
| 2251 | |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2252 | mirror::DexCache* ClassLinker::AllocAndInitializeDexCache(Thread* self, |
| 2253 | const DexFile& dex_file, |
| 2254 | LinearAlloc* linear_alloc) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2255 | ObjPtr<mirror::String> location = nullptr; |
| 2256 | ObjPtr<mirror::DexCache> dex_cache = AllocDexCache(&location, self, dex_file); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2257 | if (dex_cache != nullptr) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 2258 | WriterMutexLock mu(self, *Locks::dex_lock_); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2259 | DCHECK(location != nullptr); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 2260 | mirror::DexCache::InitializeDexCache(self, |
| 2261 | dex_cache, |
| 2262 | location, |
| 2263 | &dex_file, |
| 2264 | linear_alloc, |
| 2265 | image_pointer_size_); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2266 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2267 | return dex_cache.Ptr(); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2268 | } |
| 2269 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2270 | mirror::Class* ClassLinker::AllocClass(Thread* self, |
| 2271 | ObjPtr<mirror::Class> java_lang_Class, |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2272 | uint32_t class_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2273 | DCHECK_GE(class_size, sizeof(mirror::Class)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2274 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2275 | mirror::Class::InitializeClassVisitor visitor(class_size); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2276 | ObjPtr<mirror::Object> k = kMovingClasses ? |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 2277 | heap->AllocObject<true>(self, java_lang_Class, class_size, visitor) : |
| 2278 | heap->AllocNonMovableObject<true>(self, java_lang_Class, class_size, visitor); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2279 | if (UNLIKELY(k == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2280 | self->AssertPendingOOMException(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2281 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2282 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2283 | return k->AsClass(); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 2284 | } |
| 2285 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2286 | mirror::Class* ClassLinker::AllocClass(Thread* self, uint32_t class_size) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 2287 | return AllocClass(self, GetClassRoot(kJavaLangClass), class_size); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2288 | } |
| 2289 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2290 | mirror::ObjectArray<mirror::StackTraceElement>* ClassLinker::AllocStackTraceElementArray( |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2291 | Thread* self, |
| 2292 | size_t length) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2293 | return mirror::ObjectArray<mirror::StackTraceElement>::Alloc( |
| 2294 | self, GetClassRoot(kJavaLangStackTraceElementArrayClass), length); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2295 | } |
| 2296 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2297 | mirror::Class* ClassLinker::EnsureResolved(Thread* self, |
| 2298 | const char* descriptor, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2299 | ObjPtr<mirror::Class> klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2300 | DCHECK(klass != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2301 | if (kIsDebugBuild) { |
| 2302 | StackHandleScope<1> hs(self); |
| 2303 | HandleWrapperObjPtr<mirror::Class> h = hs.NewHandleWrapper(&klass); |
| 2304 | Thread::PoisonObjectPointersIfDebug(); |
| 2305 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2306 | |
| 2307 | // For temporary classes we must wait for them to be retired. |
| 2308 | if (init_done_ && klass->IsTemp()) { |
| 2309 | CHECK(!klass->IsResolved()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2310 | if (klass->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2311 | ThrowEarlierClassFailure(klass); |
| 2312 | return nullptr; |
| 2313 | } |
| 2314 | StackHandleScope<1> hs(self); |
| 2315 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 2316 | ObjectLock<mirror::Class> lock(self, h_class); |
| 2317 | // Loop and wait for the resolving thread to retire this class. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2318 | while (!h_class->IsRetired() && !h_class->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2319 | lock.WaitIgnoringInterrupts(); |
| 2320 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2321 | if (h_class->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2322 | ThrowEarlierClassFailure(h_class.Get()); |
| 2323 | return nullptr; |
| 2324 | } |
| 2325 | CHECK(h_class->IsRetired()); |
| 2326 | // Get the updated class from class table. |
Andreas Gampe | 34ee684 | 2014-12-02 15:43:52 -0800 | [diff] [blame] | 2327 | klass = LookupClass(self, descriptor, h_class.Get()->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2328 | } |
| 2329 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2330 | // Wait for the class if it has not already been linked. |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2331 | size_t index = 0; |
| 2332 | // Maximum number of yield iterations until we start sleeping. |
| 2333 | static const size_t kNumYieldIterations = 1000; |
| 2334 | // How long each sleep is in us. |
| 2335 | static const size_t kSleepDurationUS = 1000; // 1 ms. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2336 | while (!klass->IsResolved() && !klass->IsErroneousUnresolved()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2337 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2338 | HandleWrapperObjPtr<mirror::Class> h_class(hs.NewHandleWrapper(&klass)); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2339 | { |
| 2340 | ObjectTryLock<mirror::Class> lock(self, h_class); |
| 2341 | // Can not use a monitor wait here since it may block when returning and deadlock if another |
| 2342 | // thread has locked klass. |
| 2343 | if (lock.Acquired()) { |
| 2344 | // Check for circular dependencies between classes, the lock is required for SetStatus. |
| 2345 | if (!h_class->IsResolved() && h_class->GetClinitThreadId() == self->GetTid()) { |
| 2346 | ThrowClassCircularityError(h_class.Get()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2347 | mirror::Class::SetStatus(h_class, mirror::Class::kStatusErrorUnresolved, self); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2348 | return nullptr; |
| 2349 | } |
| 2350 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2351 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2352 | { |
| 2353 | // Handle wrapper deals with klass moving. |
| 2354 | ScopedThreadSuspension sts(self, kSuspended); |
| 2355 | if (index < kNumYieldIterations) { |
| 2356 | sched_yield(); |
| 2357 | } else { |
| 2358 | usleep(kSleepDurationUS); |
| 2359 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2360 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2361 | ++index; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2362 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2363 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2364 | if (klass->IsErroneousUnresolved()) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2365 | ThrowEarlierClassFailure(klass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2366 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2367 | } |
| 2368 | // Return the loaded class. No exceptions should be pending. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2369 | CHECK(klass->IsResolved()) << klass->PrettyClass(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2370 | self->AssertNoPendingException(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2371 | return klass.Ptr(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2372 | } |
| 2373 | |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2374 | typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry; |
| 2375 | |
| 2376 | // Search a collection of DexFiles for a descriptor |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2377 | ClassPathEntry FindInClassPath(const char* descriptor, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2378 | size_t hash, const std::vector<const DexFile*>& class_path) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2379 | for (const DexFile* dex_file : class_path) { |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 2380 | const DexFile::ClassDef* dex_class_def = OatDexFile::FindClassDef(*dex_file, descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2381 | if (dex_class_def != nullptr) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2382 | return ClassPathEntry(dex_file, dex_class_def); |
| 2383 | } |
| 2384 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2385 | return ClassPathEntry(nullptr, nullptr); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2386 | } |
| 2387 | |
Nicolas Geoffray | 7d8d8ff | 2016-11-02 12:38:05 +0000 | [diff] [blame] | 2388 | bool ClassLinker::FindClassInBaseDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
| 2389 | Thread* self, |
| 2390 | const char* descriptor, |
| 2391 | size_t hash, |
| 2392 | Handle<mirror::ClassLoader> class_loader, |
| 2393 | ObjPtr<mirror::Class>* result) { |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2394 | // Termination case: boot class-loader. |
| 2395 | if (IsBootClassLoader(soa, class_loader.Get())) { |
| 2396 | // The boot class loader, search the boot class path. |
| 2397 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
| 2398 | if (pair.second != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2399 | ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, nullptr); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2400 | if (klass != nullptr) { |
| 2401 | *result = EnsureResolved(self, descriptor, klass); |
| 2402 | } else { |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 2403 | *result = DefineClass(self, |
| 2404 | descriptor, |
| 2405 | hash, |
| 2406 | ScopedNullHandle<mirror::ClassLoader>(), |
| 2407 | *pair.first, |
| 2408 | *pair.second); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2409 | } |
| 2410 | if (*result == nullptr) { |
| 2411 | CHECK(self->IsExceptionPending()) << descriptor; |
| 2412 | self->ClearException(); |
| 2413 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2414 | } else { |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2415 | *result = nullptr; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2416 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2417 | return true; |
| 2418 | } |
| 2419 | |
| 2420 | // Unsupported class-loader? |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 2421 | if (soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader) != |
| 2422 | class_loader->GetClass()) { |
Nicolas Geoffray | 7d8d8ff | 2016-11-02 12:38:05 +0000 | [diff] [blame] | 2423 | // PathClassLoader is the most common case, so it's the one we check first. For secondary dex |
| 2424 | // files, we also check DexClassLoader here. |
| 2425 | if (soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_DexClassLoader) != |
| 2426 | class_loader->GetClass()) { |
| 2427 | *result = nullptr; |
| 2428 | return false; |
| 2429 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension). |
| 2433 | StackHandleScope<4> hs(self); |
| 2434 | Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent())); |
Nicolas Geoffray | 7d8d8ff | 2016-11-02 12:38:05 +0000 | [diff] [blame] | 2435 | bool recursive_result = FindClassInBaseDexClassLoader(soa, |
| 2436 | self, |
| 2437 | descriptor, |
| 2438 | hash, |
| 2439 | h_parent, |
| 2440 | result); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2441 | |
| 2442 | if (!recursive_result) { |
| 2443 | // Something wrong up the chain. |
| 2444 | return false; |
| 2445 | } |
| 2446 | |
| 2447 | if (*result != nullptr) { |
| 2448 | // Found the class up the chain. |
| 2449 | return true; |
| 2450 | } |
| 2451 | |
| 2452 | // Handle this step. |
| 2453 | // Handle as if this is the child PathClassLoader. |
| 2454 | // The class loader is a PathClassLoader which inherits from BaseDexClassLoader. |
| 2455 | // We need to get the DexPathList and loop through it. |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2456 | ArtField* const cookie_field = |
| 2457 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2458 | ArtField* const dex_file_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2459 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2460 | ObjPtr<mirror::Object> dex_path_list = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2461 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList)-> |
| 2462 | GetObject(class_loader.Get()); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2463 | if (dex_path_list != nullptr && dex_file_field != nullptr && cookie_field != nullptr) { |
| 2464 | // DexPathList has an array dexElements of Elements[] which each contain a dex file. |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2465 | ObjPtr<mirror::Object> dex_elements_obj = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 2466 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements)-> |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2467 | GetObject(dex_path_list); |
| 2468 | // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look |
| 2469 | // at the mCookie which is a DexFile vector. |
| 2470 | if (dex_elements_obj != nullptr) { |
| 2471 | Handle<mirror::ObjectArray<mirror::Object>> dex_elements = |
| 2472 | hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>()); |
| 2473 | for (int32_t i = 0; i < dex_elements->GetLength(); ++i) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2474 | ObjPtr<mirror::Object> element = dex_elements->GetWithoutChecks(i); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2475 | if (element == nullptr) { |
| 2476 | // Should never happen, fall back to java code to throw a NPE. |
| 2477 | break; |
| 2478 | } |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2479 | ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2480 | if (dex_file != nullptr) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 2481 | ObjPtr<mirror::LongArray> long_array = cookie_field->GetObject(dex_file)->AsLongArray(); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2482 | if (long_array == nullptr) { |
| 2483 | // This should never happen so log a warning. |
| 2484 | LOG(WARNING) << "Null DexFile::mCookie for " << descriptor; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2485 | break; |
| 2486 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2487 | int32_t long_array_size = long_array->GetLength(); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 2488 | // First element is the oat file. |
| 2489 | for (int32_t j = kDexFileIndexStart; j < long_array_size; ++j) { |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2490 | const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>( |
| 2491 | long_array->GetWithoutChecks(j))); |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 2492 | const DexFile::ClassDef* dex_class_def = |
| 2493 | OatDexFile::FindClassDef(*cp_dex_file, descriptor, hash); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2494 | if (dex_class_def != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2495 | ObjPtr<mirror::Class> klass = DefineClass(self, |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 2496 | descriptor, |
| 2497 | hash, |
| 2498 | class_loader, |
| 2499 | *cp_dex_file, |
| 2500 | *dex_class_def); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2501 | if (klass == nullptr) { |
| 2502 | CHECK(self->IsExceptionPending()) << descriptor; |
| 2503 | self->ClearException(); |
| 2504 | // TODO: Is it really right to break here, and not check the other dex files? |
| 2505 | return true; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2506 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2507 | *result = klass; |
| 2508 | return true; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2509 | } |
| 2510 | } |
| 2511 | } |
| 2512 | } |
| 2513 | } |
Ian Rogers | 3242729 | 2014-11-19 14:05:21 -0800 | [diff] [blame] | 2514 | self->AssertNoPendingException(); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2515 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2516 | |
| 2517 | // Result is still null from the parent call, no need to set it again... |
| 2518 | return true; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2519 | } |
| 2520 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2521 | mirror::Class* ClassLinker::FindClass(Thread* self, |
| 2522 | const char* descriptor, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2523 | Handle<mirror::ClassLoader> class_loader) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 2524 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2525 | DCHECK(self != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2526 | self->AssertNoPendingException(); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 2527 | self->PoisonObjectPointers(); // For DefineClass, CreateArrayClass, etc... |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 2528 | if (descriptor[1] == '\0') { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2529 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 2530 | // for primitive classes that aren't backed by dex files. |
| 2531 | return FindPrimitiveClass(descriptor[0]); |
| 2532 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2533 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2534 | // Find the class in the loaded classes table. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2535 | ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, class_loader.Get()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2536 | if (klass != nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2537 | return EnsureResolved(self, descriptor, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2538 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2539 | // Class is not yet loaded. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2540 | if (descriptor[0] != '[' && class_loader == nullptr) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2541 | // Non-array class and the boot class loader, search the boot class path. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2542 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2543 | if (pair.second != nullptr) { |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 2544 | return DefineClass(self, |
| 2545 | descriptor, |
| 2546 | hash, |
| 2547 | ScopedNullHandle<mirror::ClassLoader>(), |
| 2548 | *pair.first, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2549 | *pair.second); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2550 | } else { |
| 2551 | // The boot class loader is searched ahead of the application class loader, failures are |
| 2552 | // expected and will be wrapped in a ClassNotFoundException. Use the pre-allocated error to |
| 2553 | // trigger the chaining with a proper stack trace. |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2554 | ObjPtr<mirror::Throwable> pre_allocated = |
| 2555 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2556 | self->SetException(pre_allocated); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2557 | return nullptr; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2558 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2559 | } |
| 2560 | ObjPtr<mirror::Class> result_ptr; |
| 2561 | bool descriptor_equals; |
| 2562 | if (descriptor[0] == '[') { |
| 2563 | result_ptr = CreateArrayClass(self, descriptor, hash, class_loader); |
| 2564 | DCHECK_EQ(result_ptr == nullptr, self->IsExceptionPending()); |
| 2565 | DCHECK(result_ptr == nullptr || result_ptr->DescriptorEquals(descriptor)); |
| 2566 | descriptor_equals = true; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2567 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2568 | ScopedObjectAccessUnchecked soa(self); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2569 | bool known_hierarchy = |
| 2570 | FindClassInBaseDexClassLoader(soa, self, descriptor, hash, class_loader, &result_ptr); |
| 2571 | if (result_ptr != nullptr) { |
| 2572 | // The chain was understood and we found the class. We still need to add the class to |
| 2573 | // the class table to protect from racy programs that can try and redefine the path list |
| 2574 | // which would change the Class<?> returned for subsequent evaluation of const-class. |
| 2575 | DCHECK(known_hierarchy); |
| 2576 | DCHECK(result_ptr->DescriptorEquals(descriptor)); |
| 2577 | descriptor_equals = true; |
| 2578 | } else { |
| 2579 | // Either the chain wasn't understood or the class wasn't found. |
| 2580 | // |
| 2581 | // If the chain was understood but we did not find the class, let the Java-side |
| 2582 | // rediscover all this and throw the exception with the right stack trace. Note that |
| 2583 | // the Java-side could still succeed for racy programs if another thread is actively |
| 2584 | // modifying the class loader's path list. |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2585 | |
Calin Juravle | ccd5695 | 2016-12-15 17:57:38 +0000 | [diff] [blame] | 2586 | if (!self->CanCallIntoJava()) { |
| 2587 | // Oops, we can't call into java so we can't run actual class-loader code. |
| 2588 | // This is true for e.g. for the compiler (jit or aot). |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2589 | ObjPtr<mirror::Throwable> pre_allocated = |
| 2590 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 2591 | self->SetException(pre_allocated); |
Vladimir Marko | 2c8c6b6 | 2016-12-01 17:42:00 +0000 | [diff] [blame] | 2592 | return nullptr; |
| 2593 | } |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2594 | |
| 2595 | ScopedLocalRef<jobject> class_loader_object( |
| 2596 | soa.Env(), soa.AddLocalReference<jobject>(class_loader.Get())); |
| 2597 | std::string class_name_string(DescriptorToDot(descriptor)); |
| 2598 | ScopedLocalRef<jobject> result(soa.Env(), nullptr); |
| 2599 | { |
| 2600 | ScopedThreadStateChange tsc(self, kNative); |
| 2601 | ScopedLocalRef<jobject> class_name_object( |
| 2602 | soa.Env(), soa.Env()->NewStringUTF(class_name_string.c_str())); |
| 2603 | if (class_name_object.get() == nullptr) { |
| 2604 | DCHECK(self->IsExceptionPending()); // OOME. |
| 2605 | return nullptr; |
| 2606 | } |
| 2607 | CHECK(class_loader_object.get() != nullptr); |
| 2608 | result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(), |
| 2609 | WellKnownClasses::java_lang_ClassLoader_loadClass, |
| 2610 | class_name_object.get())); |
| 2611 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2612 | if (result.get() == nullptr && !self->IsExceptionPending()) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2613 | // broken loader - throw NPE to be compatible with Dalvik |
| 2614 | ThrowNullPointerException(StringPrintf("ClassLoader.loadClass returned null for %s", |
| 2615 | class_name_string.c_str()).c_str()); |
| 2616 | return nullptr; |
| 2617 | } |
| 2618 | result_ptr = soa.Decode<mirror::Class>(result.get()); |
| 2619 | // Check the name of the returned class. |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2620 | descriptor_equals = (result_ptr != nullptr) && result_ptr->DescriptorEquals(descriptor); |
Vladimir Marko | 2c8c6b6 | 2016-12-01 17:42:00 +0000 | [diff] [blame] | 2621 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2622 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2623 | |
| 2624 | if (self->IsExceptionPending()) { |
| 2625 | // If the ClassLoader threw or array class allocation failed, pass that exception up. |
| 2626 | // However, to comply with the RI behavior, first check if another thread succeeded. |
| 2627 | result_ptr = LookupClass(self, descriptor, hash, class_loader.Get()); |
| 2628 | if (result_ptr != nullptr && !result_ptr->IsErroneous()) { |
| 2629 | self->ClearException(); |
| 2630 | return EnsureResolved(self, descriptor, result_ptr); |
| 2631 | } |
| 2632 | return nullptr; |
| 2633 | } |
| 2634 | |
| 2635 | // Try to insert the class to the class table, checking for mismatch. |
| 2636 | ObjPtr<mirror::Class> old; |
| 2637 | { |
| 2638 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2639 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader.Get()); |
| 2640 | old = class_table->Lookup(descriptor, hash); |
| 2641 | if (old == nullptr) { |
| 2642 | old = result_ptr; // For the comparison below, after releasing the lock. |
| 2643 | if (descriptor_equals) { |
| 2644 | class_table->InsertWithHash(result_ptr.Ptr(), hash); |
| 2645 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader.Get()); |
| 2646 | } // else throw below, after releasing the lock. |
| 2647 | } |
| 2648 | } |
| 2649 | if (UNLIKELY(old != result_ptr)) { |
| 2650 | // Return `old` (even if `!descriptor_equals`) to mimic the RI behavior for parallel |
| 2651 | // capable class loaders. (All class loaders are considered parallel capable on Android.) |
| 2652 | mirror::Class* loader_class = class_loader->GetClass(); |
| 2653 | const char* loader_class_name = |
| 2654 | loader_class->GetDexFile().StringByTypeIdx(loader_class->GetDexTypeIndex()); |
| 2655 | LOG(WARNING) << "Initiating class loader of type " << DescriptorToDot(loader_class_name) |
| 2656 | << " is not well-behaved; it returned a different Class for racing loadClass(\"" |
| 2657 | << DescriptorToDot(descriptor) << "\")."; |
| 2658 | return EnsureResolved(self, descriptor, old); |
| 2659 | } |
| 2660 | if (UNLIKELY(!descriptor_equals)) { |
| 2661 | std::string result_storage; |
| 2662 | const char* result_name = result_ptr->GetDescriptor(&result_storage); |
| 2663 | std::string loader_storage; |
| 2664 | const char* loader_class_name = class_loader->GetClass()->GetDescriptor(&loader_storage); |
| 2665 | ThrowNoClassDefFoundError( |
| 2666 | "Initiating class loader of type %s returned class %s instead of %s.", |
| 2667 | DescriptorToDot(loader_class_name).c_str(), |
| 2668 | DescriptorToDot(result_name).c_str(), |
| 2669 | DescriptorToDot(descriptor).c_str()); |
| 2670 | return nullptr; |
| 2671 | } |
| 2672 | // success, return mirror::Class* |
| 2673 | return result_ptr.Ptr(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2674 | } |
| 2675 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2676 | mirror::Class* ClassLinker::DefineClass(Thread* self, |
| 2677 | const char* descriptor, |
| 2678 | size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2679 | Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2680 | const DexFile& dex_file, |
| 2681 | const DexFile::ClassDef& dex_class_def) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2682 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2683 | auto klass = hs.NewHandle<mirror::Class>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2684 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2685 | // Load the class from the dex file. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2686 | if (UNLIKELY(!init_done_)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2687 | // finish up init of hand crafted class_roots_ |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2688 | if (strcmp(descriptor, "Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2689 | klass.Assign(GetClassRoot(kJavaLangObject)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2690 | } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2691 | klass.Assign(GetClassRoot(kJavaLangClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2692 | } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2693 | klass.Assign(GetClassRoot(kJavaLangString)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 2694 | } else if (strcmp(descriptor, "Ljava/lang/ref/Reference;") == 0) { |
| 2695 | klass.Assign(GetClassRoot(kJavaLangRefReference)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2696 | } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2697 | klass.Assign(GetClassRoot(kJavaLangDexCache)); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 2698 | } else if (strcmp(descriptor, "Ldalvik/system/ClassExt;") == 0) { |
| 2699 | klass.Assign(GetClassRoot(kDalvikSystemClassExt)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2700 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2701 | } |
| 2702 | |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2703 | if (klass == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2704 | // Allocate a class with the status of not ready. |
| 2705 | // Interface object should get the right size here. Regular class will |
| 2706 | // figure out the right size later and be replaced with one of the right |
| 2707 | // size when the class becomes resolved. |
| 2708 | klass.Assign(AllocClass(self, SizeOfClassWithoutEmbeddedTables(dex_file, dex_class_def))); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2709 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2710 | if (UNLIKELY(klass == nullptr)) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2711 | self->AssertPendingOOMException(); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2712 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2713 | } |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2714 | // Get the real dex file. This will return the input if there aren't any callbacks or they do |
| 2715 | // nothing. |
| 2716 | DexFile const* new_dex_file = nullptr; |
| 2717 | DexFile::ClassDef const* new_class_def = nullptr; |
| 2718 | // TODO We should ideally figure out some way to move this after we get a lock on the klass so it |
| 2719 | // will only be called once. |
| 2720 | Runtime::Current()->GetRuntimeCallbacks()->ClassPreDefine(descriptor, |
| 2721 | klass, |
| 2722 | class_loader, |
| 2723 | dex_file, |
| 2724 | dex_class_def, |
| 2725 | &new_dex_file, |
| 2726 | &new_class_def); |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 2727 | // Check to see if an exception happened during runtime callbacks. Return if so. |
| 2728 | if (self->IsExceptionPending()) { |
| 2729 | return nullptr; |
| 2730 | } |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2731 | ObjPtr<mirror::DexCache> dex_cache = RegisterDexFile(*new_dex_file, class_loader.Get()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2732 | if (dex_cache == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 2733 | self->AssertPendingException(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2734 | return nullptr; |
| 2735 | } |
| 2736 | klass->SetDexCache(dex_cache); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2737 | SetupClass(*new_dex_file, *new_class_def, klass, class_loader.Get()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2738 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 2739 | // Mark the string class by setting its access flag. |
| 2740 | if (UNLIKELY(!init_done_)) { |
| 2741 | if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
| 2742 | klass->SetStringClass(); |
| 2743 | } |
| 2744 | } |
| 2745 | |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 2746 | ObjectLock<mirror::Class> lock(self, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2747 | klass->SetClinitThreadId(self->GetTid()); |
Mathieu Chartier | 1e4841e | 2016-12-15 14:21:04 -0800 | [diff] [blame] | 2748 | // Make sure we have a valid empty iftable even if there are errors. |
| 2749 | klass->SetIfTable(GetClassRoot(kJavaLangObject)->GetIfTable()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2750 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2751 | // Add the newly loaded class to the loaded classes table. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2752 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, klass.Get(), hash); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2753 | if (existing != nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2754 | // We failed to insert because we raced with another thread. Calling EnsureResolved may cause |
| 2755 | // this thread to block. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2756 | return EnsureResolved(self, descriptor, existing); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2757 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2758 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2759 | // Load the fields and other things after we are inserted in the table. This is so that we don't |
| 2760 | // end up allocating unfree-able linear alloc resources and then lose the race condition. The |
| 2761 | // other reason is that the field roots are only visited from the class table. So we need to be |
| 2762 | // inserted before we allocate / fill in these fields. |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2763 | LoadClass(self, *new_dex_file, *new_class_def, klass); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2764 | if (self->IsExceptionPending()) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2765 | VLOG(class_linker) << self->GetException()->Dump(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2766 | // An exception occured during load, set status to erroneous while holding klass' lock in case |
| 2767 | // notification is necessary. |
| 2768 | if (!klass->IsErroneous()) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2769 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorUnresolved, self); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2770 | } |
| 2771 | return nullptr; |
| 2772 | } |
| 2773 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2774 | // Finish loading (if necessary) by finding parents |
| 2775 | CHECK(!klass->IsLoaded()); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2776 | if (!LoadSuperAndInterfaces(klass, *new_dex_file)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2777 | // Loading failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2778 | if (!klass->IsErroneous()) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2779 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorUnresolved, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2780 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2781 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2782 | } |
| 2783 | CHECK(klass->IsLoaded()); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 2784 | |
| 2785 | // At this point the class is loaded. Publish a ClassLoad even. |
| 2786 | // Note: this may be a temporary class. It is a listener's responsibility to handle this. |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 2787 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(klass); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 2788 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2789 | // Link the class (if necessary) |
| 2790 | CHECK(!klass->IsResolved()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2791 | // TODO: Use fast jobjects? |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2792 | auto interfaces = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2793 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 2794 | MutableHandle<mirror::Class> h_new_class = hs.NewHandle<mirror::Class>(nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2795 | if (!LinkClass(self, descriptor, klass, interfaces, &h_new_class)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2796 | // Linking failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2797 | if (!klass->IsErroneous()) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2798 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorUnresolved, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2799 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2800 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2801 | } |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 2802 | self->AssertNoPendingException(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2803 | CHECK(h_new_class != nullptr) << descriptor; |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2804 | CHECK(h_new_class->IsResolved() && !h_new_class->IsErroneousResolved()) << descriptor; |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2805 | |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 2806 | // Instrumentation may have updated entrypoints for all methods of all |
| 2807 | // classes. However it could not update methods of this class while we |
| 2808 | // were loading it. Now the class is resolved, we can update entrypoints |
| 2809 | // as required by instrumentation. |
| 2810 | if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) { |
| 2811 | // We must be in the kRunnable state to prevent instrumentation from |
| 2812 | // suspending all threads to update entrypoints while we are doing it |
| 2813 | // for this class. |
| 2814 | DCHECK_EQ(self->GetState(), kRunnable); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 2815 | Runtime::Current()->GetInstrumentation()->InstallStubsForClass(h_new_class.Get()); |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 2816 | } |
| 2817 | |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2818 | /* |
| 2819 | * We send CLASS_PREPARE events to the debugger from here. The |
| 2820 | * definition of "preparation" is creating the static fields for a |
| 2821 | * class and initializing them to the standard default values, but not |
| 2822 | * executing any code (that comes later, during "initialization"). |
| 2823 | * |
| 2824 | * We did the static preparation in LinkClass. |
| 2825 | * |
| 2826 | * The class has been prepared and resolved but possibly not yet verified |
| 2827 | * at this point. |
| 2828 | */ |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 2829 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(klass, h_new_class); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2830 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 2831 | // Notify native debugger of the new class and its layout. |
| 2832 | jit::Jit::NewTypeLoadedIfUsingJit(h_new_class.Get()); |
| 2833 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 2834 | return h_new_class.Get(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2835 | } |
| 2836 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2837 | uint32_t ClassLinker::SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, |
| 2838 | const DexFile::ClassDef& dex_class_def) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2839 | const uint8_t* class_data = dex_file.GetClassData(dex_class_def); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2840 | size_t num_ref = 0; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2841 | size_t num_8 = 0; |
| 2842 | size_t num_16 = 0; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2843 | size_t num_32 = 0; |
| 2844 | size_t num_64 = 0; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2845 | if (class_data != nullptr) { |
Vladimir Marko | 879d27b | 2016-03-15 20:31:50 +0000 | [diff] [blame] | 2846 | // We allow duplicate definitions of the same field in a class_data_item |
| 2847 | // but ignore the repeated indexes here, b/21868015. |
| 2848 | uint32_t last_field_idx = DexFile::kDexNoIndex; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2849 | for (ClassDataItemIterator it(dex_file, class_data); it.HasNextStaticField(); it.Next()) { |
Vladimir Marko | 879d27b | 2016-03-15 20:31:50 +0000 | [diff] [blame] | 2850 | uint32_t field_idx = it.GetMemberIndex(); |
| 2851 | // Ordering enforced by DexFileVerifier. |
| 2852 | DCHECK(last_field_idx == DexFile::kDexNoIndex || last_field_idx <= field_idx); |
| 2853 | if (UNLIKELY(field_idx == last_field_idx)) { |
| 2854 | continue; |
| 2855 | } |
| 2856 | last_field_idx = field_idx; |
| 2857 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2858 | const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2859 | char c = descriptor[0]; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2860 | switch (c) { |
| 2861 | case 'L': |
| 2862 | case '[': |
| 2863 | num_ref++; |
| 2864 | break; |
| 2865 | case 'J': |
| 2866 | case 'D': |
| 2867 | num_64++; |
| 2868 | break; |
| 2869 | case 'I': |
| 2870 | case 'F': |
| 2871 | num_32++; |
| 2872 | break; |
| 2873 | case 'S': |
| 2874 | case 'C': |
| 2875 | num_16++; |
| 2876 | break; |
| 2877 | case 'B': |
| 2878 | case 'Z': |
| 2879 | num_8++; |
| 2880 | break; |
| 2881 | default: |
| 2882 | LOG(FATAL) << "Unknown descriptor: " << c; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 2883 | UNREACHABLE(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2884 | } |
| 2885 | } |
| 2886 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2887 | return mirror::Class::ComputeClassSize(false, |
| 2888 | 0, |
| 2889 | num_8, |
| 2890 | num_16, |
| 2891 | num_32, |
| 2892 | num_64, |
| 2893 | num_ref, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2894 | image_pointer_size_); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2895 | } |
| 2896 | |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2897 | // Special case to get oat code without overwriting a trampoline. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2898 | const void* ClassLinker::GetQuickOatCodeFor(ArtMethod* method) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2899 | CHECK(method->IsInvokable()) << method->PrettyMethod(); |
Nicolas Geoffray | a7a4759 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 2900 | if (method->IsProxyMethod()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2901 | return GetQuickProxyInvokeHandler(); |
Jeff Hao | 8df6cea | 2013-07-29 13:54:48 -0700 | [diff] [blame] | 2902 | } |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 2903 | auto* code = method->GetOatMethodQuickCode(GetImagePointerSize()); |
| 2904 | if (code != nullptr) { |
| 2905 | return code; |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame] | 2906 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2907 | if (method->IsNative()) { |
| 2908 | // No code and native? Use generic trampoline. |
| 2909 | return GetQuickGenericJniStub(); |
| 2910 | } |
| 2911 | return GetQuickToInterpreterBridge(); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2912 | } |
| 2913 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2914 | bool ClassLinker::ShouldUseInterpreterEntrypoint(ArtMethod* method, const void* quick_code) { |
| 2915 | if (UNLIKELY(method->IsNative() || method->IsProxyMethod())) { |
| 2916 | return false; |
| 2917 | } |
| 2918 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2919 | if (quick_code == nullptr) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2920 | return true; |
| 2921 | } |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2922 | |
| 2923 | Runtime* runtime = Runtime::Current(); |
| 2924 | instrumentation::Instrumentation* instr = runtime->GetInstrumentation(); |
| 2925 | if (instr->InterpretOnly()) { |
| 2926 | return true; |
| 2927 | } |
| 2928 | |
| 2929 | if (runtime->GetClassLinker()->IsQuickToInterpreterBridge(quick_code)) { |
| 2930 | // Doing this check avoids doing compiled/interpreter transitions. |
| 2931 | return true; |
| 2932 | } |
| 2933 | |
| 2934 | if (Dbg::IsForcedInterpreterNeededForCalling(Thread::Current(), method)) { |
| 2935 | // Force the use of interpreter when it is required by the debugger. |
| 2936 | return true; |
| 2937 | } |
| 2938 | |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 2939 | if (runtime->IsJavaDebuggable()) { |
| 2940 | // For simplicity, we ignore precompiled code and go to the interpreter |
| 2941 | // assuming we don't already have jitted code. |
| 2942 | // We could look at the oat file where `quick_code` is being defined, |
| 2943 | // and check whether it's been compiled debuggable, but we decided to |
| 2944 | // only rely on the JIT for debuggable apps. |
Alex Light | 6b16d89 | 2016-11-11 11:21:04 -0800 | [diff] [blame] | 2945 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 2946 | return (jit == nullptr) || !jit->GetCodeCache()->ContainsPc(quick_code); |
| 2947 | } |
| 2948 | |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 2949 | if (runtime->IsNativeDebuggable()) { |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 2950 | DCHECK(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse()); |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 2951 | // If we are doing native debugging, ignore application's AOT code, |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 2952 | // since we want to JIT it (at first use) with extra stackmaps for native |
| 2953 | // debugging. We keep however all AOT code from the boot image, |
| 2954 | // since the JIT-at-first-use is blocking and would result in non-negligible |
| 2955 | // startup performance impact. |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 2956 | return !runtime->GetHeap()->IsInBootImageOatFile(quick_code); |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
| 2959 | return false; |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2960 | } |
| 2961 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2962 | void ClassLinker::FixupStaticTrampolines(ObjPtr<mirror::Class> klass) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2963 | DCHECK(klass->IsInitialized()) << klass->PrettyDescriptor(); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2964 | if (klass->NumDirectMethods() == 0) { |
| 2965 | return; // No direct methods => no static methods. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2966 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2967 | Runtime* runtime = Runtime::Current(); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 2968 | if (!runtime->IsStarted()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2969 | if (runtime->IsAotCompiler() || runtime->GetHeap()->HasBootImageSpace()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 2970 | return; // OAT file unavailable. |
| 2971 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2972 | } |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 2973 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 2974 | const DexFile& dex_file = klass->GetDexFile(); |
| 2975 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2976 | CHECK(dex_class_def != nullptr); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2977 | const uint8_t* class_data = dex_file.GetClassData(*dex_class_def); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2978 | // There should always be class data if there were direct methods. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2979 | CHECK(class_data != nullptr) << klass->PrettyDescriptor(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2980 | ClassDataItemIterator it(dex_file, class_data); |
| 2981 | // Skip fields |
| 2982 | while (it.HasNextStaticField()) { |
| 2983 | it.Next(); |
| 2984 | } |
| 2985 | while (it.HasNextInstanceField()) { |
| 2986 | it.Next(); |
| 2987 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2988 | bool has_oat_class; |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 2989 | OatFile::OatClass oat_class = OatFile::FindOatClass(dex_file, |
| 2990 | klass->GetDexClassDefIndex(), |
| 2991 | &has_oat_class); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2992 | // Link the code of methods skipped by LinkCode. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2993 | for (size_t method_index = 0; it.HasNextDirectMethod(); ++method_index, it.Next()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2994 | ArtMethod* method = klass->GetDirectMethod(method_index, image_pointer_size_); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2995 | if (!method->IsStatic()) { |
| 2996 | // Only update static methods. |
| 2997 | continue; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2998 | } |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2999 | const void* quick_code = nullptr; |
| 3000 | if (has_oat_class) { |
| 3001 | OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3002 | quick_code = oat_method.GetQuickCode(); |
| 3003 | } |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3004 | // Check whether the method is native, in which case it's generic JNI. |
| 3005 | if (quick_code == nullptr && method->IsNative()) { |
| 3006 | quick_code = GetQuickGenericJniStub(); |
| 3007 | } else if (ShouldUseInterpreterEntrypoint(method, quick_code)) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3008 | // Use interpreter entry point. |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3009 | quick_code = GetQuickToInterpreterBridge(); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3010 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 3011 | runtime->GetInstrumentation()->UpdateMethodsCode(method, quick_code); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3012 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3013 | // Ignore virtual methods on the iterator. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3014 | } |
| 3015 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3016 | // Does anything needed to make sure that the compiler will not generate a direct invoke to this |
| 3017 | // method. Should only be called on non-invokable methods. |
| 3018 | inline void EnsureThrowsInvocationError(ClassLinker* class_linker, ArtMethod* method) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3019 | DCHECK(method != nullptr); |
| 3020 | DCHECK(!method->IsInvokable()); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3021 | method->SetEntryPointFromQuickCompiledCodePtrSize( |
| 3022 | class_linker->GetQuickToInterpreterBridgeTrampoline(), |
| 3023 | class_linker->GetImagePointerSize()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3024 | } |
| 3025 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3026 | static void LinkCode(ClassLinker* class_linker, |
| 3027 | ArtMethod* method, |
| 3028 | const OatFile::OatClass* oat_class, |
| 3029 | uint32_t class_def_method_index) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3030 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3031 | if (runtime->IsAotCompiler()) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3032 | // The following code only applies to a non-compiler runtime. |
| 3033 | return; |
| 3034 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3035 | // Method shouldn't have already been linked. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3036 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3037 | if (oat_class != nullptr) { |
| 3038 | // Every kind of method should at least get an invoke stub from the oat_method. |
| 3039 | // non-abstract methods also get their code pointers. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 3040 | const OatFile::OatMethod oat_method = oat_class->GetOatMethod(class_def_method_index); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3041 | oat_method.LinkMethod(method); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3042 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3043 | |
Tamas Berghammer | 3a98aae | 2016-02-08 20:21:54 +0000 | [diff] [blame] | 3044 | // Install entry point from interpreter. |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3045 | const void* quick_code = method->GetEntryPointFromQuickCompiledCode(); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3046 | bool enter_interpreter = class_linker->ShouldUseInterpreterEntrypoint(method, quick_code); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 3047 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3048 | if (!method->IsInvokable()) { |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3049 | EnsureThrowsInvocationError(class_linker, method); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3050 | return; |
| 3051 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3052 | |
| 3053 | if (method->IsStatic() && !method->IsConstructor()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3054 | // For static methods excluding the class initializer, install the trampoline. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3055 | // It will be replaced by the proper entry point by ClassLinker::FixupStaticTrampolines |
| 3056 | // after initializing class (see ClassLinker::InitializeClass method). |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3057 | method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionStub()); |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3058 | } else if (quick_code == nullptr && method->IsNative()) { |
| 3059 | method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub()); |
Tamas Berghammer | 3a98aae | 2016-02-08 20:21:54 +0000 | [diff] [blame] | 3060 | } else if (enter_interpreter) { |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3061 | // Set entry point from compiled code if there's no code or in interpreter only mode. |
| 3062 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
Ian Rogers | 0d6de04 | 2012-02-29 08:50:26 -0800 | [diff] [blame] | 3063 | } |
jeffhao | 26c0a1a | 2012-01-17 16:28:33 -0800 | [diff] [blame] | 3064 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3065 | if (method->IsNative()) { |
| 3066 | // Unregistering restores the dlsym lookup stub. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3067 | method->UnregisterNative(); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 3068 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3069 | if (enter_interpreter || quick_code == nullptr) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3070 | // We have a native method here without code. Then it should have either the generic JNI |
| 3071 | // trampoline as entrypoint (non-static), or the resolution trampoline (static). |
| 3072 | // TODO: this doesn't handle all the cases where trampolines may be installed. |
| 3073 | const void* entry_point = method->GetEntryPointFromQuickCompiledCode(); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3074 | DCHECK(class_linker->IsQuickGenericJniStub(entry_point) || |
| 3075 | class_linker->IsQuickResolutionStub(entry_point)); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 3076 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3077 | } |
| 3078 | } |
| 3079 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3080 | void ClassLinker::SetupClass(const DexFile& dex_file, |
| 3081 | const DexFile::ClassDef& dex_class_def, |
| 3082 | Handle<mirror::Class> klass, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3083 | ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3084 | CHECK(klass != nullptr); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3085 | CHECK(klass->GetDexCache() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3086 | CHECK_EQ(mirror::Class::kStatusNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 3087 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3088 | CHECK(descriptor != nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3089 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3090 | klass->SetClass(GetClassRoot(kJavaLangClass)); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 3091 | uint32_t access_flags = dex_class_def.GetJavaAccessFlags(); |
Brian Carlstrom | 8e3fb14 | 2013-10-09 21:00:27 -0700 | [diff] [blame] | 3092 | CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3093 | klass->SetAccessFlags(access_flags); |
| 3094 | klass->SetClassLoader(class_loader); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3095 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3096 | mirror::Class::SetStatus(klass, mirror::Class::kStatusIdx, nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3097 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3098 | klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3099 | klass->SetDexTypeIndex(dex_class_def.class_idx_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3100 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3101 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3102 | void ClassLinker::LoadClass(Thread* self, |
| 3103 | const DexFile& dex_file, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3104 | const DexFile::ClassDef& dex_class_def, |
| 3105 | Handle<mirror::Class> klass) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 3106 | const uint8_t* class_data = dex_file.GetClassData(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3107 | if (class_data == nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3108 | return; // no fields or methods - for example a marker interface |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3109 | } |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3110 | LoadClassMembers(self, dex_file, class_data, klass); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 3111 | } |
| 3112 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3113 | LengthPrefixedArray<ArtField>* ClassLinker::AllocArtFieldArray(Thread* self, |
| 3114 | LinearAlloc* allocator, |
| 3115 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3116 | if (length == 0) { |
| 3117 | return nullptr; |
| 3118 | } |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3119 | // If the ArtField alignment changes, review all uses of LengthPrefixedArray<ArtField>. |
| 3120 | static_assert(alignof(ArtField) == 4, "ArtField alignment is expected to be 4."); |
| 3121 | size_t storage_size = LengthPrefixedArray<ArtField>::ComputeSize(length); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3122 | void* array_storage = allocator->Alloc(self, storage_size); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3123 | auto* ret = new(array_storage) LengthPrefixedArray<ArtField>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3124 | CHECK(ret != nullptr); |
| 3125 | std::uninitialized_fill_n(&ret->At(0), length, ArtField()); |
| 3126 | return ret; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3127 | } |
| 3128 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3129 | LengthPrefixedArray<ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self, |
| 3130 | LinearAlloc* allocator, |
| 3131 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3132 | if (length == 0) { |
| 3133 | return nullptr; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3134 | } |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 3135 | const size_t method_alignment = ArtMethod::Alignment(image_pointer_size_); |
| 3136 | const size_t method_size = ArtMethod::Size(image_pointer_size_); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3137 | const size_t storage_size = |
| 3138 | LengthPrefixedArray<ArtMethod>::ComputeSize(length, method_size, method_alignment); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3139 | void* array_storage = allocator->Alloc(self, storage_size); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3140 | auto* ret = new (array_storage) LengthPrefixedArray<ArtMethod>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3141 | CHECK(ret != nullptr); |
| 3142 | for (size_t i = 0; i < length; ++i) { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3143 | new(reinterpret_cast<void*>(&ret->At(i, method_size, method_alignment))) ArtMethod; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3144 | } |
| 3145 | return ret; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3146 | } |
| 3147 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3148 | LinearAlloc* ClassLinker::GetAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3149 | if (class_loader == nullptr) { |
| 3150 | return Runtime::Current()->GetLinearAlloc(); |
| 3151 | } |
| 3152 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3153 | DCHECK(allocator != nullptr); |
| 3154 | return allocator; |
| 3155 | } |
| 3156 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3157 | LinearAlloc* ClassLinker::GetOrCreateAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3158 | if (class_loader == nullptr) { |
| 3159 | return Runtime::Current()->GetLinearAlloc(); |
| 3160 | } |
| 3161 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3162 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3163 | if (allocator == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 3164 | RegisterClassLoader(class_loader); |
| 3165 | allocator = class_loader->GetAllocator(); |
| 3166 | CHECK(allocator != nullptr); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3167 | } |
| 3168 | return allocator; |
| 3169 | } |
| 3170 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3171 | void ClassLinker::LoadClassMembers(Thread* self, |
| 3172 | const DexFile& dex_file, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 3173 | const uint8_t* class_data, |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3174 | Handle<mirror::Class> klass) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3175 | { |
| 3176 | // Note: We cannot have thread suspension until the field and method arrays are setup or else |
| 3177 | // Class::VisitFieldRoots may miss some fields or methods. |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3178 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3179 | // Load static fields. |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3180 | // We allow duplicate definitions of the same field in a class_data_item |
| 3181 | // but ignore the repeated indexes here, b/21868015. |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3182 | LinearAlloc* const allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3183 | ClassDataItemIterator it(dex_file, class_data); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3184 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, |
| 3185 | allocator, |
| 3186 | it.NumStaticFields()); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3187 | size_t num_sfields = 0; |
| 3188 | uint32_t last_field_idx = 0u; |
| 3189 | for (; it.HasNextStaticField(); it.Next()) { |
| 3190 | uint32_t field_idx = it.GetMemberIndex(); |
| 3191 | DCHECK_GE(field_idx, last_field_idx); // Ordering enforced by DexFileVerifier. |
| 3192 | if (num_sfields == 0 || LIKELY(field_idx > last_field_idx)) { |
| 3193 | DCHECK_LT(num_sfields, it.NumStaticFields()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3194 | LoadField(it, klass, &sfields->At(num_sfields)); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3195 | ++num_sfields; |
| 3196 | last_field_idx = field_idx; |
| 3197 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3198 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3199 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3200 | // Load instance fields. |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3201 | LengthPrefixedArray<ArtField>* ifields = AllocArtFieldArray(self, |
| 3202 | allocator, |
| 3203 | it.NumInstanceFields()); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3204 | size_t num_ifields = 0u; |
| 3205 | last_field_idx = 0u; |
| 3206 | for (; it.HasNextInstanceField(); it.Next()) { |
| 3207 | uint32_t field_idx = it.GetMemberIndex(); |
| 3208 | DCHECK_GE(field_idx, last_field_idx); // Ordering enforced by DexFileVerifier. |
| 3209 | if (num_ifields == 0 || LIKELY(field_idx > last_field_idx)) { |
| 3210 | DCHECK_LT(num_ifields, it.NumInstanceFields()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3211 | LoadField(it, klass, &ifields->At(num_ifields)); |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3212 | ++num_ifields; |
| 3213 | last_field_idx = field_idx; |
| 3214 | } |
| 3215 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3216 | |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3217 | if (UNLIKELY(num_sfields != it.NumStaticFields()) || |
| 3218 | UNLIKELY(num_ifields != it.NumInstanceFields())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3219 | LOG(WARNING) << "Duplicate fields in class " << klass->PrettyDescriptor() |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3220 | << " (unique static fields: " << num_sfields << "/" << it.NumStaticFields() |
| 3221 | << ", unique instance fields: " << num_ifields << "/" << it.NumInstanceFields() << ")"; |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 3222 | // NOTE: Not shrinking the over-allocated sfields/ifields, just setting size. |
| 3223 | if (sfields != nullptr) { |
| 3224 | sfields->SetSize(num_sfields); |
| 3225 | } |
| 3226 | if (ifields != nullptr) { |
| 3227 | ifields->SetSize(num_ifields); |
| 3228 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3229 | } |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 3230 | // Set the field arrays. |
| 3231 | klass->SetSFieldsPtr(sfields); |
| 3232 | DCHECK_EQ(klass->NumStaticFields(), num_sfields); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3233 | klass->SetIFieldsPtr(ifields); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3234 | DCHECK_EQ(klass->NumInstanceFields(), num_ifields); |
| 3235 | // Load methods. |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3236 | bool has_oat_class = false; |
| 3237 | const OatFile::OatClass oat_class = |
| 3238 | (Runtime::Current()->IsStarted() && !Runtime::Current()->IsAotCompiler()) |
| 3239 | ? OatFile::FindOatClass(dex_file, klass->GetDexClassDefIndex(), &has_oat_class) |
| 3240 | : OatFile::OatClass::Invalid(); |
| 3241 | const OatFile::OatClass* oat_class_ptr = has_oat_class ? &oat_class : nullptr; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 3242 | klass->SetMethodsPtr( |
| 3243 | AllocArtMethodArray(self, allocator, it.NumDirectMethods() + it.NumVirtualMethods()), |
| 3244 | it.NumDirectMethods(), |
| 3245 | it.NumVirtualMethods()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3246 | size_t class_def_method_index = 0; |
| 3247 | uint32_t last_dex_method_index = DexFile::kDexNoIndex; |
| 3248 | size_t last_class_def_method_index = 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 3249 | // TODO These should really use the iterators. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3250 | for (size_t i = 0; it.HasNextDirectMethod(); i++, it.Next()) { |
| 3251 | ArtMethod* method = klass->GetDirectMethodUnchecked(i, image_pointer_size_); |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3252 | LoadMethod(dex_file, it, klass, method); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3253 | LinkCode(this, method, oat_class_ptr, class_def_method_index); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3254 | uint32_t it_method_index = it.GetMemberIndex(); |
| 3255 | if (last_dex_method_index == it_method_index) { |
| 3256 | // duplicate case |
| 3257 | method->SetMethodIndex(last_class_def_method_index); |
| 3258 | } else { |
| 3259 | method->SetMethodIndex(class_def_method_index); |
| 3260 | last_dex_method_index = it_method_index; |
| 3261 | last_class_def_method_index = class_def_method_index; |
| 3262 | } |
| 3263 | class_def_method_index++; |
| 3264 | } |
| 3265 | for (size_t i = 0; it.HasNextVirtualMethod(); i++, it.Next()) { |
| 3266 | ArtMethod* method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3267 | LoadMethod(dex_file, it, klass, method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3268 | DCHECK_EQ(class_def_method_index, it.NumDirectMethods() + i); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3269 | LinkCode(this, method, oat_class_ptr, class_def_method_index); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3270 | class_def_method_index++; |
| 3271 | } |
| 3272 | DCHECK(!it.HasNext()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3273 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 3274 | // Ensure that the card is marked so that remembered sets pick up native roots. |
| 3275 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(klass.Get()); |
Mathieu Chartier | f3f2a7a | 2015-04-14 15:43:10 -0700 | [diff] [blame] | 3276 | self->AllowThreadSuspension(); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3277 | } |
| 3278 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3279 | void ClassLinker::LoadField(const ClassDataItemIterator& it, |
| 3280 | Handle<mirror::Class> klass, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3281 | ArtField* dst) { |
| 3282 | const uint32_t field_idx = it.GetMemberIndex(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3283 | dst->SetDexFieldIndex(field_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3284 | dst->SetDeclaringClass(klass.Get()); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 3285 | dst->SetAccessFlags(it.GetFieldAccessFlags()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3286 | } |
| 3287 | |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3288 | void ClassLinker::LoadMethod(const DexFile& dex_file, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3289 | const ClassDataItemIterator& it, |
| 3290 | Handle<mirror::Class> klass, |
| 3291 | ArtMethod* dst) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3292 | uint32_t dex_method_idx = it.GetMemberIndex(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3293 | const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3294 | const char* method_name = dex_file.StringDataByIdx(method_id.name_idx_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3295 | |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3296 | ScopedAssertNoThreadSuspension ants("LoadMethod"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3297 | dst->SetDexMethodIndex(dex_method_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3298 | dst->SetDeclaringClass(klass.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3299 | dst->SetCodeItemOffset(it.GetMethodCodeItemOffset()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3300 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 3301 | dst->SetDexCacheResolvedMethods(klass->GetDexCache()->GetResolvedMethods(), image_pointer_size_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3302 | |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 3303 | uint32_t access_flags = it.GetMethodAccessFlags(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3304 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3305 | if (UNLIKELY(strcmp("finalize", method_name) == 0)) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3306 | // Set finalizable flag on declaring class. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3307 | if (strcmp("V", dex_file.GetShorty(method_id.proto_idx_)) == 0) { |
| 3308 | // Void return type. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3309 | if (klass->GetClassLoader() != nullptr) { // All non-boot finalizer methods are flagged. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3310 | klass->SetFinalizable(); |
| 3311 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3312 | std::string temp; |
| 3313 | const char* klass_descriptor = klass->GetDescriptor(&temp); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3314 | // The Enum class declares a "final" finalize() method to prevent subclasses from |
| 3315 | // introducing a finalizer. We don't want to set the finalizable flag for Enum or its |
| 3316 | // subclasses, so we exclude it here. |
| 3317 | // We also want to avoid setting the flag on Object, where we know that finalize() is |
| 3318 | // empty. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3319 | if (strcmp(klass_descriptor, "Ljava/lang/Object;") != 0 && |
| 3320 | strcmp(klass_descriptor, "Ljava/lang/Enum;") != 0) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3321 | klass->SetFinalizable(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3322 | } |
| 3323 | } |
| 3324 | } |
| 3325 | } else if (method_name[0] == '<') { |
| 3326 | // Fix broken access flags for initializers. Bug 11157540. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3327 | bool is_init = (strcmp("<init>", method_name) == 0); |
| 3328 | bool is_clinit = !is_init && (strcmp("<clinit>", method_name) == 0); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3329 | if (UNLIKELY(!is_init && !is_clinit)) { |
| 3330 | LOG(WARNING) << "Unexpected '<' at start of method name " << method_name; |
| 3331 | } else { |
| 3332 | if (UNLIKELY((access_flags & kAccConstructor) == 0)) { |
| 3333 | LOG(WARNING) << method_name << " didn't have expected constructor access flag in class " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3334 | << klass->PrettyDescriptor() << " in dex file " << dex_file.GetLocation(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3335 | access_flags |= kAccConstructor; |
| 3336 | } |
| 3337 | } |
| 3338 | } |
| 3339 | dst->SetAccessFlags(access_flags); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3340 | } |
| 3341 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3342 | void ClassLinker::AppendToBootClassPath(Thread* self, const DexFile& dex_file) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3343 | ObjPtr<mirror::DexCache> dex_cache = AllocAndInitializeDexCache( |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3344 | self, |
| 3345 | dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3346 | Runtime::Current()->GetLinearAlloc()); |
| 3347 | CHECK(dex_cache != nullptr) << "Failed to allocate dex cache for " << dex_file.GetLocation(); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 3348 | AppendToBootClassPath(dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 3349 | } |
| 3350 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3351 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3352 | ObjPtr<mirror::DexCache> dex_cache) { |
| 3353 | CHECK(dex_cache != nullptr) << dex_file.GetLocation(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 3354 | boot_class_path_.push_back(&dex_file); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3355 | RegisterBootClassPathDexFile(dex_file, dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3356 | } |
| 3357 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3358 | void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3359 | ObjPtr<mirror::DexCache> dex_cache, |
| 3360 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3361 | Thread* const self = Thread::Current(); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3362 | Locks::dex_lock_->AssertExclusiveHeld(self); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3363 | CHECK(dex_cache != nullptr) << dex_file.GetLocation(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3364 | // For app images, the dex cache location may be a suffix of the dex file location since the |
| 3365 | // dex file location is an absolute path. |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3366 | const std::string dex_cache_location = dex_cache->GetLocation()->ToModifiedUtf8(); |
| 3367 | const size_t dex_cache_length = dex_cache_location.length(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3368 | CHECK_GT(dex_cache_length, 0u) << dex_file.GetLocation(); |
| 3369 | std::string dex_file_location = dex_file.GetLocation(); |
| 3370 | CHECK_GE(dex_file_location.length(), dex_cache_length) |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3371 | << dex_cache_location << " " << dex_file.GetLocation(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3372 | // Take suffix. |
| 3373 | const std::string dex_file_suffix = dex_file_location.substr( |
| 3374 | dex_file_location.length() - dex_cache_length, |
| 3375 | dex_cache_length); |
| 3376 | // Example dex_cache location is SettingsProvider.apk and |
| 3377 | // dex file location is /system/priv-app/SettingsProvider/SettingsProvider.apk |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3378 | CHECK_EQ(dex_cache_location, dex_file_suffix); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3379 | // Clean up pass to remove null dex caches. |
| 3380 | // Null dex caches can occur due to class unloading and we are lazily removing null entries. |
| 3381 | JavaVMExt* const vm = self->GetJniEnv()->vm; |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3382 | for (auto it = dex_caches_.begin(); it != dex_caches_.end(); ) { |
| 3383 | DexCacheData data = *it; |
| 3384 | if (self->IsJWeakCleared(data.weak_root)) { |
| 3385 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3386 | it = dex_caches_.erase(it); |
| 3387 | } else { |
| 3388 | ++it; |
| 3389 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3390 | } |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3391 | jweak dex_cache_jweak = vm->AddWeakGlobalRef(self, dex_cache); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3392 | dex_cache->SetDexFile(&dex_file); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3393 | DexCacheData data; |
| 3394 | data.weak_root = dex_cache_jweak; |
| 3395 | data.dex_file = dex_cache->GetDexFile(); |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 3396 | data.resolved_methods = dex_cache->GetResolvedMethods(); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3397 | data.class_table = ClassTableForClassLoader(class_loader); |
| 3398 | DCHECK(data.class_table != nullptr); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3399 | dex_caches_.push_back(data); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3400 | } |
| 3401 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3402 | ObjPtr<mirror::DexCache> ClassLinker::DecodeDexCache(Thread* self, const DexCacheData& data) { |
| 3403 | return data.IsValid() |
| 3404 | ? ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root)) |
| 3405 | : nullptr; |
| 3406 | } |
| 3407 | |
| 3408 | ObjPtr<mirror::DexCache> ClassLinker::EnsureSameClassLoader( |
| 3409 | Thread* self, |
| 3410 | ObjPtr<mirror::DexCache> dex_cache, |
| 3411 | const DexCacheData& data, |
| 3412 | ObjPtr<mirror::ClassLoader> class_loader) { |
| 3413 | DCHECK_EQ(dex_cache->GetDexFile(), data.dex_file); |
| 3414 | if (data.class_table != ClassTableForClassLoader(class_loader)) { |
| 3415 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
| 3416 | "Attempt to register dex file %s with multiple class loaders", |
| 3417 | data.dex_file->GetLocation().c_str()); |
| 3418 | return nullptr; |
| 3419 | } |
| 3420 | return dex_cache; |
| 3421 | } |
| 3422 | |
| 3423 | ObjPtr<mirror::DexCache> ClassLinker::RegisterDexFile(const DexFile& dex_file, |
| 3424 | ObjPtr<mirror::ClassLoader> class_loader) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3425 | Thread* self = Thread::Current(); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3426 | DexCacheData old_data; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3427 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3428 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3429 | old_data = FindDexCacheDataLocked(dex_file); |
| 3430 | } |
| 3431 | ObjPtr<mirror::DexCache> old_dex_cache = DecodeDexCache(self, old_data); |
| 3432 | if (old_dex_cache != nullptr) { |
| 3433 | return EnsureSameClassLoader(self, old_dex_cache, old_data, class_loader); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3434 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3435 | LinearAlloc* const linear_alloc = GetOrCreateAllocatorForClassLoader(class_loader); |
| 3436 | DCHECK(linear_alloc != nullptr); |
| 3437 | ClassTable* table; |
| 3438 | { |
| 3439 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3440 | table = InsertClassTableForClassLoader(class_loader); |
| 3441 | } |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3442 | // Don't alloc while holding the lock, since allocation may need to |
| 3443 | // suspend all threads and another thread may need the dex_lock_ to |
| 3444 | // get to a suspend point. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3445 | StackHandleScope<3> hs(self); |
| 3446 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3447 | ObjPtr<mirror::String> location; |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 3448 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(AllocDexCache(/*out*/&location, |
| 3449 | self, |
| 3450 | dex_file))); |
| 3451 | Handle<mirror::String> h_location(hs.NewHandle(location)); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3452 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3453 | WriterMutexLock mu(self, *Locks::dex_lock_); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3454 | old_data = FindDexCacheDataLocked(dex_file); |
| 3455 | old_dex_cache = DecodeDexCache(self, old_data); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3456 | if (old_dex_cache == nullptr && h_dex_cache != nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3457 | // Do InitializeDexCache while holding dex lock to make sure two threads don't call it at the |
| 3458 | // same time with the same dex cache. Since the .bss is shared this can cause failing DCHECK |
| 3459 | // that the arrays are null. |
| 3460 | mirror::DexCache::InitializeDexCache(self, |
| 3461 | h_dex_cache.Get(), |
| 3462 | h_location.Get(), |
| 3463 | &dex_file, |
| 3464 | linear_alloc, |
| 3465 | image_pointer_size_); |
| 3466 | RegisterDexFileLocked(dex_file, h_dex_cache.Get(), h_class_loader.Get()); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3467 | } |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3468 | } |
| 3469 | if (old_dex_cache != nullptr) { |
| 3470 | // Another thread managed to initialize the dex cache faster, so use that DexCache. |
| 3471 | // If this thread encountered OOME, ignore it. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3472 | DCHECK_EQ(h_dex_cache == nullptr, self->IsExceptionPending()); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3473 | self->ClearException(); |
| 3474 | // We cannot call EnsureSameClassLoader() while holding the dex_lock_. |
| 3475 | return EnsureSameClassLoader(self, old_dex_cache, old_data, h_class_loader.Get()); |
| 3476 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3477 | if (h_dex_cache == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3478 | self->AssertPendingOOMException(); |
| 3479 | return nullptr; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3480 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3481 | table->InsertStrongRoot(h_dex_cache.Get()); |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 3482 | if (h_class_loader.Get() != nullptr) { |
| 3483 | // Since we added a strong root to the class table, do the write barrier as required for |
| 3484 | // remembered sets and generational GCs. |
| 3485 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(h_class_loader.Get()); |
| 3486 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3487 | return h_dex_cache.Get(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3488 | } |
| 3489 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3490 | void ClassLinker::RegisterBootClassPathDexFile(const DexFile& dex_file, |
| 3491 | ObjPtr<mirror::DexCache> dex_cache) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3492 | WriterMutexLock mu(Thread::Current(), *Locks::dex_lock_); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3493 | RegisterDexFileLocked(dex_file, dex_cache, /* class_loader */ nullptr); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3494 | } |
| 3495 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3496 | bool ClassLinker::IsDexFileRegistered(Thread* self, const DexFile& dex_file) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3497 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3498 | return DecodeDexCache(self, FindDexCacheDataLocked(dex_file)) != nullptr; |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3499 | } |
| 3500 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3501 | ObjPtr<mirror::DexCache> ClassLinker::FindDexCache(Thread* self, const DexFile& dex_file) { |
| 3502 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 3503 | ObjPtr<mirror::DexCache> dex_cache = DecodeDexCache(self, FindDexCacheDataLocked(dex_file)); |
| 3504 | if (dex_cache != nullptr) { |
| 3505 | return dex_cache; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3506 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3507 | // Failure, dump diagnostic and abort. |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3508 | for (const DexCacheData& data : dex_caches_) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3509 | if (DecodeDexCache(self, data) != nullptr) { |
Andreas Gampe | 37c5846 | 2017-03-27 15:14:27 -0700 | [diff] [blame] | 3510 | LOG(FATAL_WITHOUT_ABORT) << "Registered dex file " << data.dex_file->GetLocation(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3511 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3512 | } |
Andreas Gampe | 37c5846 | 2017-03-27 15:14:27 -0700 | [diff] [blame] | 3513 | LOG(FATAL) << "Failed to find DexCache for DexFile " << dex_file.GetLocation(); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 3514 | UNREACHABLE(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3515 | } |
| 3516 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3517 | ClassTable* ClassLinker::FindClassTable(Thread* self, ObjPtr<mirror::DexCache> dex_cache) { |
| 3518 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 3519 | DCHECK(dex_file != nullptr); |
| 3520 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 3521 | // Search assuming unique-ness of dex file. |
| 3522 | for (const DexCacheData& data : dex_caches_) { |
| 3523 | // Avoid decoding (and read barriers) other unrelated dex caches. |
| 3524 | if (data.dex_file == dex_file) { |
| 3525 | ObjPtr<mirror::DexCache> registered_dex_cache = DecodeDexCache(self, data); |
| 3526 | if (registered_dex_cache != nullptr) { |
| 3527 | CHECK_EQ(registered_dex_cache, dex_cache) << dex_file->GetLocation(); |
| 3528 | return data.class_table; |
| 3529 | } |
| 3530 | } |
| 3531 | } |
| 3532 | return nullptr; |
| 3533 | } |
| 3534 | |
| 3535 | ClassLinker::DexCacheData ClassLinker::FindDexCacheDataLocked(const DexFile& dex_file) { |
| 3536 | // Search assuming unique-ness of dex file. |
| 3537 | for (const DexCacheData& data : dex_caches_) { |
| 3538 | // Avoid decoding (and read barriers) other unrelated dex caches. |
| 3539 | if (data.dex_file == &dex_file) { |
| 3540 | return data; |
| 3541 | } |
| 3542 | } |
| 3543 | return DexCacheData(); |
| 3544 | } |
| 3545 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3546 | void ClassLinker::FixupDexCaches(ArtMethod* resolution_method) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3547 | Thread* const self = Thread::Current(); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3548 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3549 | for (const DexCacheData& data : dex_caches_) { |
| 3550 | if (!self->IsJWeakCleared(data.weak_root)) { |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 3551 | ObjPtr<mirror::DexCache> dex_cache = ObjPtr<mirror::DexCache>::DownCast( |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3552 | self->DecodeJObject(data.weak_root)); |
| 3553 | if (dex_cache != nullptr) { |
| 3554 | dex_cache->Fixup(resolution_method, image_pointer_size_); |
| 3555 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3556 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3557 | } |
| 3558 | } |
| 3559 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3560 | mirror::Class* ClassLinker::CreatePrimitiveClass(Thread* self, Primitive::Type type) { |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 3561 | ObjPtr<mirror::Class> klass = |
| 3562 | AllocClass(self, mirror::Class::PrimitiveClassSize(image_pointer_size_)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3563 | if (UNLIKELY(klass == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3564 | self->AssertPendingOOMException(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3565 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3566 | } |
| 3567 | return InitializePrimitiveClass(klass, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3568 | } |
| 3569 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3570 | mirror::Class* ClassLinker::InitializePrimitiveClass(ObjPtr<mirror::Class> primitive_class, |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3571 | Primitive::Type type) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3572 | CHECK(primitive_class != nullptr); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3573 | // Must hold lock on object when initializing. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3574 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3575 | StackHandleScope<1> hs(self); |
| 3576 | Handle<mirror::Class> h_class(hs.NewHandle(primitive_class)); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3577 | ObjectLock<mirror::Class> lock(self, h_class); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3578 | h_class->SetAccessFlags(kAccPublic | kAccFinal | kAccAbstract); |
| 3579 | h_class->SetPrimitiveType(type); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 3580 | h_class->SetIfTable(GetClassRoot(kJavaLangObject)->GetIfTable()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3581 | mirror::Class::SetStatus(h_class, mirror::Class::kStatusInitialized, self); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3582 | const char* descriptor = Primitive::Descriptor(type); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 3583 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, |
| 3584 | h_class.Get(), |
| 3585 | ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3586 | CHECK(existing == nullptr) << "InitPrimitiveClass(" << type << ") failed"; |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3587 | return h_class.Get(); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 3588 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3589 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3590 | // Create an array class (i.e. the class object for the array, not the |
| 3591 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 3592 | // "[Ljava/lang/String;". |
| 3593 | // |
| 3594 | // If "descriptor" refers to an array of primitives, look up the |
| 3595 | // primitive type's internally-generated class object. |
| 3596 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3597 | // "class_loader" is the class loader of the class that's referring to |
| 3598 | // us. It's used to ensure that we're looking for the element type in |
| 3599 | // the right context. It does NOT become the class loader for the |
| 3600 | // array class; that always comes from the base element class. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3601 | // |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 3602 | // Returns null with an exception raised on failure. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 3603 | mirror::Class* ClassLinker::CreateArrayClass(Thread* self, const char* descriptor, size_t hash, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3604 | Handle<mirror::ClassLoader> class_loader) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3605 | // Identify the underlying component type |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3606 | CHECK_EQ('[', descriptor[0]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3607 | StackHandleScope<2> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3608 | MutableHandle<mirror::Class> component_type(hs.NewHandle(FindClass(self, descriptor + 1, |
| 3609 | class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3610 | if (component_type == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3611 | DCHECK(self->IsExceptionPending()); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 3612 | // We need to accept erroneous classes as component types. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3613 | const size_t component_hash = ComputeModifiedUtf8Hash(descriptor + 1); |
| 3614 | component_type.Assign(LookupClass(self, descriptor + 1, component_hash, class_loader.Get())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3615 | if (component_type == nullptr) { |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 3616 | DCHECK(self->IsExceptionPending()); |
| 3617 | return nullptr; |
| 3618 | } else { |
| 3619 | self->ClearException(); |
| 3620 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3621 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 3622 | if (UNLIKELY(component_type->IsPrimitiveVoid())) { |
| 3623 | ThrowNoClassDefFoundError("Attempt to create array of void primitive type"); |
| 3624 | return nullptr; |
| 3625 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3626 | // See if the component type is already loaded. Array classes are |
| 3627 | // always associated with the class loader of their underlying |
| 3628 | // element type -- an array of Strings goes with the loader for |
| 3629 | // java/lang/String -- so we need to look for it there. (The |
| 3630 | // caller should have checked for the existence of the class |
| 3631 | // before calling here, but they did so with *their* class loader, |
| 3632 | // not the component type's loader.) |
| 3633 | // |
| 3634 | // If we find it, the caller adds "loader" to the class' initiating |
| 3635 | // loader list, which should prevent us from going through this again. |
| 3636 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3637 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3638 | // are the same, because our caller (FindClass) just did the |
| 3639 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 3640 | // because we effectively do this lookup again when we add the new |
| 3641 | // class to the hash table --- necessary because of possible races with |
| 3642 | // other threads.) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3643 | if (class_loader.Get() != component_type->GetClassLoader()) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3644 | ObjPtr<mirror::Class> new_class = |
| 3645 | LookupClass(self, descriptor, hash, component_type->GetClassLoader()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3646 | if (new_class != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3647 | return new_class.Ptr(); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3648 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3649 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3650 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3651 | // Fill out the fields in the Class. |
| 3652 | // |
| 3653 | // It is possible to execute some methods against arrays, because |
| 3654 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 3655 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 3656 | // |
| 3657 | // Array classes are simple enough that we don't need to do a full |
| 3658 | // link step. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3659 | auto new_class = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3660 | if (UNLIKELY(!init_done_)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3661 | // Classes that were hand created, ie not by FindSystemClass |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3662 | if (strcmp(descriptor, "[Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3663 | new_class.Assign(GetClassRoot(kClassArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3664 | } else if (strcmp(descriptor, "[Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3665 | new_class.Assign(GetClassRoot(kObjectArrayClass)); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3666 | } else if (strcmp(descriptor, GetClassRootDescriptor(kJavaLangStringArrayClass)) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3667 | new_class.Assign(GetClassRoot(kJavaLangStringArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3668 | } else if (strcmp(descriptor, "[C") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3669 | new_class.Assign(GetClassRoot(kCharArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3670 | } else if (strcmp(descriptor, "[I") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3671 | new_class.Assign(GetClassRoot(kIntArrayClass)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3672 | } else if (strcmp(descriptor, "[J") == 0) { |
| 3673 | new_class.Assign(GetClassRoot(kLongArrayClass)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3674 | } |
| 3675 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3676 | if (new_class == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3677 | new_class.Assign(AllocClass(self, mirror::Array::ClassSize(image_pointer_size_))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3678 | if (new_class == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3679 | self->AssertPendingOOMException(); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3680 | return nullptr; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3681 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3682 | new_class->SetComponentType(component_type.Get()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3683 | } |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3684 | 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] | 3685 | DCHECK(new_class->GetComponentType() != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3686 | ObjPtr<mirror::Class> java_lang_Object = GetClassRoot(kJavaLangObject); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3687 | new_class->SetSuperClass(java_lang_Object); |
| 3688 | new_class->SetVTable(java_lang_Object->GetVTable()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3689 | new_class->SetPrimitiveType(Primitive::kPrimNot); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3690 | new_class->SetClassLoader(component_type->GetClassLoader()); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 3691 | if (component_type->IsPrimitive()) { |
| 3692 | new_class->SetClassFlags(mirror::kClassFlagNoReferenceFields); |
| 3693 | } else { |
| 3694 | new_class->SetClassFlags(mirror::kClassFlagObjectArray); |
| 3695 | } |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3696 | mirror::Class::SetStatus(new_class, mirror::Class::kStatusLoaded, self); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 3697 | new_class->PopulateEmbeddedVTable(image_pointer_size_); |
| 3698 | ImTable* object_imt = java_lang_Object->GetImt(image_pointer_size_); |
| 3699 | new_class->SetImt(object_imt, image_pointer_size_); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3700 | mirror::Class::SetStatus(new_class, mirror::Class::kStatusInitialized, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3701 | // don't need to set new_class->SetObjectSize(..) |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 3702 | // because Object::SizeOf delegates to Array::SizeOf |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3703 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3704 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 3705 | // interfaces. We need to set that up here, so that stuff like |
| 3706 | // "instanceof" works right. |
| 3707 | // |
| 3708 | // Note: The GC could run during the call to FindSystemClass, |
| 3709 | // so we need to make sure the class object is GC-valid while we're in |
| 3710 | // there. Do this by clearing the interface list so the GC will just |
| 3711 | // think that the entries are null. |
| 3712 | |
| 3713 | |
| 3714 | // Use the single, global copies of "interfaces" and "iftable" |
| 3715 | // (remember not to free them for arrays). |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3716 | { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3717 | ObjPtr<mirror::IfTable> array_iftable = array_iftable_.Read(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3718 | CHECK(array_iftable != nullptr); |
| 3719 | new_class->SetIfTable(array_iftable); |
| 3720 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3721 | |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 3722 | // Inherit access flags from the component type. |
| 3723 | int access_flags = new_class->GetComponentType()->GetAccessFlags(); |
| 3724 | // Lose any implementation detail flags; in particular, arrays aren't finalizable. |
| 3725 | access_flags &= kAccJavaFlagsMask; |
| 3726 | // 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] | 3727 | // and remove "interface". |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 3728 | access_flags |= kAccAbstract | kAccFinal; |
| 3729 | access_flags &= ~kAccInterface; |
| 3730 | |
| 3731 | new_class->SetAccessFlags(access_flags); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3732 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3733 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, new_class.Get(), hash); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3734 | if (existing == nullptr) { |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 3735 | jit::Jit::NewTypeLoadedIfUsingJit(new_class.Get()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3736 | return new_class.Get(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3737 | } |
| 3738 | // Another thread must have loaded the class after we |
| 3739 | // started but before we finished. Abandon what we've |
| 3740 | // done. |
| 3741 | // |
| 3742 | // (Yes, this happens.) |
| 3743 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3744 | return existing.Ptr(); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3745 | } |
| 3746 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3747 | mirror::Class* ClassLinker::FindPrimitiveClass(char type) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3748 | switch (type) { |
| 3749 | case 'B': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3750 | return GetClassRoot(kPrimitiveByte); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3751 | case 'C': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3752 | return GetClassRoot(kPrimitiveChar); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3753 | case 'D': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3754 | return GetClassRoot(kPrimitiveDouble); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3755 | case 'F': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3756 | return GetClassRoot(kPrimitiveFloat); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3757 | case 'I': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3758 | return GetClassRoot(kPrimitiveInt); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3759 | case 'J': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3760 | return GetClassRoot(kPrimitiveLong); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3761 | case 'S': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3762 | return GetClassRoot(kPrimitiveShort); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3763 | case 'Z': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3764 | return GetClassRoot(kPrimitiveBoolean); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3765 | case 'V': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3766 | return GetClassRoot(kPrimitiveVoid); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3767 | default: |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3768 | break; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 3769 | } |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 3770 | std::string printable_type(PrintableChar(type)); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 3771 | ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3772 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3773 | } |
| 3774 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3775 | mirror::Class* ClassLinker::InsertClass(const char* descriptor, ObjPtr<mirror::Class> klass, size_t hash) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3776 | if (VLOG_IS_ON(class_linker)) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3777 | ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3778 | std::string source; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3779 | if (dex_cache != nullptr) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3780 | source += " from "; |
| 3781 | source += dex_cache->GetLocation()->ToModifiedUtf8(); |
| 3782 | } |
| 3783 | LOG(INFO) << "Loaded class " << descriptor << source; |
| 3784 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3785 | { |
| 3786 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3787 | ObjPtr<mirror::ClassLoader> const class_loader = klass->GetClassLoader(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3788 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3789 | ObjPtr<mirror::Class> existing = class_table->Lookup(descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3790 | if (existing != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3791 | return existing.Ptr(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3792 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3793 | VerifyObject(klass); |
| 3794 | class_table->InsertWithHash(klass, hash); |
| 3795 | if (class_loader != nullptr) { |
| 3796 | // This is necessary because we need to have the card dirtied for remembered sets. |
| 3797 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader); |
| 3798 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 3799 | if (log_new_roots_) { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3800 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3801 | } |
| 3802 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3803 | if (kIsDebugBuild) { |
| 3804 | // Test that copied methods correctly can find their holder. |
| 3805 | for (ArtMethod& method : klass->GetCopiedMethods(image_pointer_size_)) { |
| 3806 | CHECK_EQ(GetHoldingClassOfCopiedMethod(&method), klass); |
| 3807 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3808 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3809 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3810 | } |
| 3811 | |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 3812 | void ClassLinker::WriteBarrierForBootOatFileBssRoots(const OatFile* oat_file) { |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 3813 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3814 | DCHECK(!oat_file->GetBssGcRoots().empty()) << oat_file->GetLocation(); |
| 3815 | if (log_new_roots_ && !ContainsElement(new_bss_roots_boot_oat_files_, oat_file)) { |
| 3816 | new_bss_roots_boot_oat_files_.push_back(oat_file); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 3817 | } |
| 3818 | } |
| 3819 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 3820 | // TODO This should really be in mirror::Class. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3821 | void ClassLinker::UpdateClassMethods(ObjPtr<mirror::Class> klass, |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 3822 | LengthPrefixedArray<ArtMethod>* new_methods) { |
| 3823 | klass->SetMethodsPtrUnchecked(new_methods, |
| 3824 | klass->NumDirectMethods(), |
| 3825 | klass->NumDeclaredVirtualMethods()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3826 | // 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] | 3827 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3828 | } |
| 3829 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3830 | mirror::Class* ClassLinker::LookupClass(Thread* self, |
| 3831 | const char* descriptor, |
| 3832 | size_t hash, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3833 | ObjPtr<mirror::ClassLoader> class_loader) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 3834 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3835 | ClassTable* const class_table = ClassTableForClassLoader(class_loader); |
| 3836 | if (class_table != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3837 | ObjPtr<mirror::Class> result = class_table->Lookup(descriptor, hash); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 3838 | if (result != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3839 | return result.Ptr(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3840 | } |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 3841 | } |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 3842 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3843 | } |
| 3844 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3845 | class MoveClassTableToPreZygoteVisitor : public ClassLoaderVisitor { |
| 3846 | public: |
| 3847 | explicit MoveClassTableToPreZygoteVisitor() {} |
| 3848 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3849 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3850 | REQUIRES(Locks::classlinker_classes_lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3851 | REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3852 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3853 | if (class_table != nullptr) { |
| 3854 | class_table->FreezeSnapshot(); |
| 3855 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 3856 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3857 | }; |
| 3858 | |
| 3859 | void ClassLinker::MoveClassTableToPreZygote() { |
| 3860 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3861 | boot_class_table_.FreezeSnapshot(); |
| 3862 | MoveClassTableToPreZygoteVisitor visitor; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3863 | VisitClassLoaders(&visitor); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3864 | } |
| 3865 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3866 | // Look up classes by hash and descriptor and put all matching ones in the result array. |
| 3867 | class LookupClassesVisitor : public ClassLoaderVisitor { |
| 3868 | public: |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3869 | LookupClassesVisitor(const char* descriptor, |
| 3870 | size_t hash, |
| 3871 | std::vector<ObjPtr<mirror::Class>>* result) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3872 | : descriptor_(descriptor), |
| 3873 | hash_(hash), |
| 3874 | result_(result) {} |
| 3875 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3876 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 3877 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) OVERRIDE { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3878 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3879 | ObjPtr<mirror::Class> klass = class_table->Lookup(descriptor_, hash_); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 3880 | // Add `klass` only if `class_loader` is its defining (not just initiating) class loader. |
| 3881 | if (klass != nullptr && klass->GetClassLoader() == class_loader) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3882 | result_->push_back(klass); |
| 3883 | } |
| 3884 | } |
| 3885 | |
| 3886 | private: |
| 3887 | const char* const descriptor_; |
| 3888 | const size_t hash_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3889 | std::vector<ObjPtr<mirror::Class>>* const result_; |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3890 | }; |
| 3891 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3892 | void ClassLinker::LookupClasses(const char* descriptor, |
| 3893 | std::vector<ObjPtr<mirror::Class>>& result) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3894 | result.clear(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3895 | Thread* const self = Thread::Current(); |
| 3896 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3897 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3898 | ObjPtr<mirror::Class> klass = boot_class_table_.Lookup(descriptor, hash); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3899 | if (klass != nullptr) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 3900 | DCHECK(klass->GetClassLoader() == nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3901 | result.push_back(klass); |
| 3902 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3903 | LookupClassesVisitor visitor(descriptor, hash, &result); |
| 3904 | VisitClassLoaders(&visitor); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 3905 | } |
| 3906 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3907 | bool ClassLinker::AttemptSupertypeVerification(Thread* self, |
| 3908 | Handle<mirror::Class> klass, |
| 3909 | Handle<mirror::Class> supertype) { |
| 3910 | DCHECK(self != nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3911 | DCHECK(klass != nullptr); |
| 3912 | DCHECK(supertype != nullptr); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3913 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3914 | if (!supertype->IsVerified() && !supertype->IsErroneous()) { |
| 3915 | VerifyClass(self, supertype); |
| 3916 | } |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 3917 | |
| 3918 | if (supertype->IsVerified() || supertype->ShouldVerifyAtRuntime()) { |
| 3919 | // The supertype is either verified, or we soft failed at AOT time. |
| 3920 | DCHECK(supertype->IsVerified() || Runtime::Current()->IsAotCompiler()); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3921 | return true; |
| 3922 | } |
| 3923 | // If we got this far then we have a hard failure. |
| 3924 | std::string error_msg = |
| 3925 | StringPrintf("Rejecting class %s that attempts to sub-type erroneous class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3926 | klass->PrettyDescriptor().c_str(), |
| 3927 | supertype->PrettyDescriptor().c_str()); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3928 | LOG(WARNING) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3929 | StackHandleScope<1> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3930 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3931 | if (cause != nullptr) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3932 | // Set during VerifyClass call (if at all). |
| 3933 | self->ClearException(); |
| 3934 | } |
| 3935 | // Change into a verify error. |
| 3936 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3937 | if (cause != nullptr) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3938 | self->GetException()->SetCause(cause.Get()); |
| 3939 | } |
| 3940 | ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex()); |
| 3941 | if (Runtime::Current()->IsAotCompiler()) { |
| 3942 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
| 3943 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3944 | // Need to grab the lock to change status. |
| 3945 | ObjectLock<mirror::Class> super_lock(self, klass); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 3946 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3947 | return false; |
| 3948 | } |
| 3949 | |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3950 | // Ensures that methods have the kAccSkipAccessChecks bit set. We use the |
| 3951 | // kAccVerificationAttempted bit on the class access flags to determine whether this has been done |
| 3952 | // before. |
| 3953 | static void EnsureSkipAccessChecksMethods(Handle<mirror::Class> klass, PointerSize pointer_size) |
| 3954 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3955 | if (!klass->WasVerificationAttempted()) { |
| 3956 | klass->SetSkipAccessChecksFlagOnAllMethods(pointer_size); |
| 3957 | klass->SetVerificationAttempted(); |
| 3958 | } |
| 3959 | } |
| 3960 | |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 3961 | verifier::MethodVerifier::FailureKind ClassLinker::VerifyClass( |
| 3962 | Thread* self, Handle<mirror::Class> klass, verifier::HardFailLogMode log_level) { |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3963 | { |
| 3964 | // TODO: assert that the monitor on the Class is held |
| 3965 | ObjectLock<mirror::Class> lock(self, klass); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 3966 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3967 | // Is somebody verifying this now? |
| 3968 | mirror::Class::Status old_status = klass->GetStatus(); |
| 3969 | while (old_status == mirror::Class::kStatusVerifying || |
| 3970 | old_status == mirror::Class::kStatusVerifyingAtRuntime) { |
| 3971 | lock.WaitIgnoringInterrupts(); |
Alex Light | 06039ba | 2016-04-04 13:23:14 -0700 | [diff] [blame] | 3972 | CHECK(klass->IsErroneous() || (klass->GetStatus() > old_status)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3973 | << "Class '" << klass->PrettyClass() |
| 3974 | << "' performed an illegal verification state transition from " << old_status |
| 3975 | << " to " << klass->GetStatus(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3976 | old_status = klass->GetStatus(); |
| 3977 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3978 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3979 | // The class might already be erroneous, for example at compile time if we attempted to verify |
| 3980 | // this class as a parent to another. |
| 3981 | if (klass->IsErroneous()) { |
| 3982 | ThrowEarlierClassFailure(klass.Get()); |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 3983 | return verifier::MethodVerifier::kHardFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3984 | } |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 3985 | |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 3986 | // Don't attempt to re-verify if already verified. |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3987 | if (klass->IsVerified()) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3988 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 3989 | return verifier::MethodVerifier::kNoFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3990 | } |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 3991 | |
| 3992 | // For AOT, don't attempt to re-verify if we have already found we should |
| 3993 | // verify at runtime. |
| 3994 | if (Runtime::Current()->IsAotCompiler() && klass->ShouldVerifyAtRuntime()) { |
| 3995 | return verifier::MethodVerifier::kSoftFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3996 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3997 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3998 | if (klass->GetStatus() == mirror::Class::kStatusResolved) { |
| 3999 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerifying, self); |
| 4000 | } else { |
| 4001 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4002 | << klass->PrettyClass(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4003 | CHECK(!Runtime::Current()->IsAotCompiler()); |
| 4004 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerifyingAtRuntime, self); |
| 4005 | } |
| 4006 | |
| 4007 | // Skip verification if disabled. |
| 4008 | if (!Runtime::Current()->IsVerificationEnabled()) { |
| 4009 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, self); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4010 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 4011 | return verifier::MethodVerifier::kNoFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4012 | } |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 4013 | } |
| 4014 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 4015 | // Verify super class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4016 | StackHandleScope<2> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4017 | MutableHandle<mirror::Class> supertype(hs.NewHandle(klass->GetSuperClass())); |
| 4018 | // If we have a superclass and we get a hard verification failure we can return immediately. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4019 | if (supertype != nullptr && !AttemptSupertypeVerification(self, klass, supertype)) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4020 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 4021 | return verifier::MethodVerifier::kHardFailure; |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4022 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4023 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4024 | // Verify all default super-interfaces. |
| 4025 | // |
| 4026 | // (1) Don't bother if the superclass has already had a soft verification failure. |
| 4027 | // |
| 4028 | // (2) Interfaces shouldn't bother to do this recursive verification because they cannot cause |
| 4029 | // recursive initialization by themselves. This is because when an interface is initialized |
| 4030 | // directly it must not initialize its superinterfaces. We are allowed to verify regardless |
| 4031 | // but choose not to for an optimization. If the interfaces is being verified due to a class |
| 4032 | // initialization (which would need all the default interfaces to be verified) the class code |
| 4033 | // will trigger the recursive verification anyway. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4034 | if ((supertype == nullptr || supertype->IsVerified()) // See (1) |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4035 | && !klass->IsInterface()) { // See (2) |
| 4036 | int32_t iftable_count = klass->GetIfTableCount(); |
| 4037 | MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 4038 | // Loop through all interfaces this class has defined. It doesn't matter the order. |
| 4039 | for (int32_t i = 0; i < iftable_count; i++) { |
| 4040 | iface.Assign(klass->GetIfTable()->GetInterface(i)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4041 | DCHECK(iface != nullptr); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4042 | // We only care if we have default interfaces and can skip if we are already verified... |
| 4043 | if (LIKELY(!iface->HasDefaultMethods() || iface->IsVerified())) { |
| 4044 | continue; |
| 4045 | } else if (UNLIKELY(!AttemptSupertypeVerification(self, klass, iface))) { |
| 4046 | // We had a hard failure while verifying this interface. Just return immediately. |
| 4047 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 4048 | return verifier::MethodVerifier::kHardFailure; |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4049 | } else if (UNLIKELY(!iface->IsVerified())) { |
| 4050 | // We softly failed to verify the iface. Stop checking and clean up. |
| 4051 | // Put the iface into the supertype handle so we know what caused us to fail. |
| 4052 | supertype.Assign(iface.Get()); |
| 4053 | break; |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4054 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4055 | } |
| 4056 | } |
| 4057 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4058 | // At this point if verification failed, then supertype is the "first" supertype that failed |
| 4059 | // verification (without a specific order). If verification succeeded, then supertype is either |
| 4060 | // null or the original superclass of klass and is verified. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4061 | DCHECK(supertype == nullptr || |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4062 | supertype.Get() == klass->GetSuperClass() || |
| 4063 | !supertype->IsVerified()); |
| 4064 | |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 4065 | // 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] | 4066 | const DexFile& dex_file = *klass->GetDexCache()->GetDexFile(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4067 | mirror::Class::Status oat_file_class_status(mirror::Class::kStatusNotReady); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4068 | bool preverified = VerifyClassUsingOatFile(dex_file, klass.Get(), oat_file_class_status); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4069 | // If the oat file says the class had an error, re-run the verifier. That way we will get a |
| 4070 | // precise error message. To ensure a rerun, test: |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4071 | // mirror::Class::IsErroneous(oat_file_class_status) => !preverified |
| 4072 | DCHECK(!mirror::Class::IsErroneous(oat_file_class_status) || !preverified); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4073 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4074 | std::string error_msg; |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 4075 | verifier::MethodVerifier::FailureKind verifier_failure = verifier::MethodVerifier::kNoFailure; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4076 | if (!preverified) { |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 4077 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4078 | verifier_failure = verifier::MethodVerifier::VerifyClass(self, |
| 4079 | klass.Get(), |
Andreas Gampe | 53e32d1 | 2015-12-09 21:03:23 -0800 | [diff] [blame] | 4080 | runtime->GetCompilerCallbacks(), |
| 4081 | runtime->IsAotCompiler(), |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 4082 | log_level, |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4083 | &error_msg); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4084 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4085 | |
| 4086 | // Verification is done, grab the lock again. |
| 4087 | ObjectLock<mirror::Class> lock(self, klass); |
| 4088 | |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4089 | if (preverified || verifier_failure != verifier::MethodVerifier::kHardFailure) { |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 4090 | if (!preverified && verifier_failure != verifier::MethodVerifier::kNoFailure) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4091 | VLOG(class_linker) << "Soft verification failure in class " |
| 4092 | << klass->PrettyDescriptor() |
| 4093 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4094 | << " because: " << error_msg; |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 4095 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 4096 | self->AssertNoPendingException(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4097 | // Make sure all classes referenced by catch blocks are resolved. |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4098 | ResolveClassExceptionHandlerTypes(klass); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4099 | if (verifier_failure == verifier::MethodVerifier::kNoFailure) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4100 | // Even though there were no verifier failures we need to respect whether the super-class and |
| 4101 | // super-default-interfaces were verified or requiring runtime reverification. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4102 | if (supertype == nullptr || supertype->IsVerified()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4103 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4104 | } else { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4105 | CHECK_EQ(supertype->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4106 | mirror::Class::SetStatus(klass, mirror::Class::kStatusRetryVerificationAtRuntime, self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4107 | // 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] | 4108 | verifier_failure = verifier::MethodVerifier::kSoftFailure; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4109 | } |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4110 | } else { |
| 4111 | CHECK_EQ(verifier_failure, verifier::MethodVerifier::kSoftFailure); |
| 4112 | // Soft failures at compile time should be retried at runtime. Soft |
| 4113 | // failures at runtime will be handled by slow paths in the generated |
| 4114 | // code. Set status accordingly. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4115 | if (Runtime::Current()->IsAotCompiler()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4116 | mirror::Class::SetStatus(klass, mirror::Class::kStatusRetryVerificationAtRuntime, self); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4117 | } else { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4118 | mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, self); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4119 | // As this is a fake verified status, make sure the methods are _not_ marked |
| 4120 | // kAccSkipAccessChecks later. |
| 4121 | klass->SetVerificationAttempted(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4122 | } |
| 4123 | } |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 4124 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4125 | VLOG(verifier) << "Verification failed on class " << klass->PrettyDescriptor() |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4126 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4127 | << " because: " << error_msg; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4128 | self->AssertNoPendingException(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4129 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4130 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 4131 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 4132 | if (preverified || verifier_failure == verifier::MethodVerifier::kNoFailure) { |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 4133 | // 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] | 4134 | // Let the interpreter know it by setting the kAccSkipAccessChecks flag onto each |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 4135 | // method. |
| 4136 | // 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] | 4137 | // kAccSkipAccessChecks flag when compiling image classes, the flag is recorded |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 4138 | // in the image and is set when loading the image. |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4139 | |
| 4140 | if (UNLIKELY(Runtime::Current()->IsVerificationSoftFail())) { |
| 4141 | // Never skip access checks if the verification soft fail is forced. |
| 4142 | // Mark the class as having a verification attempt to avoid re-running the verifier. |
| 4143 | klass->SetVerificationAttempted(); |
| 4144 | } else { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4145 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4146 | } |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4147 | } |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 4148 | return verifier_failure; |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4149 | } |
| 4150 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4151 | bool ClassLinker::VerifyClassUsingOatFile(const DexFile& dex_file, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4152 | ObjPtr<mirror::Class> klass, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4153 | mirror::Class::Status& oat_file_class_status) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4154 | // If we're compiling, we can only verify the class using the oat file if |
| 4155 | // we are not compiling the image or if the class we're verifying is not part of |
| 4156 | // the app. In other words, we will only check for preverification of bootclasspath |
| 4157 | // classes. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4158 | if (Runtime::Current()->IsAotCompiler()) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4159 | // Are we compiling the bootclasspath? |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 4160 | if (Runtime::Current()->GetCompilerCallbacks()->IsBootImage()) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4161 | return false; |
| 4162 | } |
| 4163 | // We are compiling an app (not the image). |
| 4164 | |
| 4165 | // Is this an app class? (I.e. not a bootclasspath class) |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4166 | if (klass->GetClassLoader() != nullptr) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4167 | return false; |
| 4168 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4169 | } |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4170 | |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 4171 | const OatFile::OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4172 | // In case we run without an image there won't be a backing oat file. |
Mathieu Chartier | 1b86849 | 2016-11-16 16:22:37 -0800 | [diff] [blame] | 4173 | if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) { |
Anwar Ghuloum | ad256bb | 2013-07-18 14:58:55 -0700 | [diff] [blame] | 4174 | return false; |
| 4175 | } |
| 4176 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4177 | uint16_t class_def_index = klass->GetDexClassDefIndex(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 4178 | oat_file_class_status = oat_dex_file->GetOatClass(class_def_index).GetStatus(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4179 | if (oat_file_class_status == mirror::Class::kStatusVerified || |
| 4180 | oat_file_class_status == mirror::Class::kStatusInitialized) { |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4181 | return true; |
| 4182 | } |
| 4183 | // If we only verified a subset of the classes at compile time, we can end up with classes that |
| 4184 | // were resolved by the verifier. |
| 4185 | if (oat_file_class_status == mirror::Class::kStatusResolved) { |
| 4186 | return false; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4187 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4188 | if (oat_file_class_status == mirror::Class::kStatusRetryVerificationAtRuntime) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 4189 | // Compile time verification failed with a soft error. Compile time verification can fail |
| 4190 | // because we have incomplete type information. Consider the following: |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 4191 | // class ... { |
| 4192 | // Foo x; |
| 4193 | // .... () { |
| 4194 | // if (...) { |
| 4195 | // v1 gets assigned a type of resolved class Foo |
| 4196 | // } else { |
| 4197 | // v1 gets assigned a type of unresolved class Bar |
| 4198 | // } |
| 4199 | // iput x = v1 |
| 4200 | // } } |
| 4201 | // when we merge v1 following the if-the-else it results in Conflict |
| 4202 | // (see verifier::RegType::Merge) as we can't know the type of Bar and we could possibly be |
| 4203 | // allowing an unsafe assignment to the field x in the iput (javac may have compiled this as |
| 4204 | // it knew Bar was a sub-class of Foo, but for us this may have been moved into a separate apk |
| 4205 | // at compile time). |
| 4206 | return false; |
| 4207 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4208 | if (mirror::Class::IsErroneous(oat_file_class_status)) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 4209 | // Compile time verification failed with a hard error. This is caused by invalid instructions |
| 4210 | // in the class. These errors are unrecoverable. |
| 4211 | return false; |
| 4212 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4213 | if (oat_file_class_status == mirror::Class::kStatusNotReady) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 4214 | // Status is uninitialized if we couldn't determine the status at compile time, for example, |
| 4215 | // not loading the class. |
| 4216 | // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy |
| 4217 | // isn't a problem and this case shouldn't occur |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4218 | return false; |
| 4219 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4220 | std::string temp; |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 4221 | LOG(FATAL) << "Unexpected class status: " << oat_file_class_status |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4222 | << " " << dex_file.GetLocation() << " " << klass->PrettyClass() << " " |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4223 | << klass->GetDescriptor(&temp); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4224 | UNREACHABLE(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4225 | } |
| 4226 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4227 | void ClassLinker::ResolveClassExceptionHandlerTypes(Handle<mirror::Class> klass) { |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 4228 | for (ArtMethod& method : klass->GetMethods(image_pointer_size_)) { |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4229 | ResolveMethodExceptionHandlerTypes(&method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4230 | } |
| 4231 | } |
| 4232 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4233 | void ClassLinker::ResolveMethodExceptionHandlerTypes(ArtMethod* method) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4234 | // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod. |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4235 | const DexFile::CodeItem* code_item = |
| 4236 | method->GetDexFile()->GetCodeItem(method->GetCodeItemOffset()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4237 | if (code_item == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4238 | return; // native or abstract method |
| 4239 | } |
| 4240 | if (code_item->tries_size_ == 0) { |
| 4241 | return; // nothing to process |
| 4242 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 4243 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item, 0); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4244 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4245 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 4246 | CatchHandlerIterator iterator(handlers_ptr); |
| 4247 | for (; iterator.HasNext(); iterator.Next()) { |
| 4248 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 4249 | // unresolved exception types will be ignored by exception delivery |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4250 | if (iterator.GetHandlerTypeIndex().IsValid()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4251 | ObjPtr<mirror::Class> exception_type = ResolveType(iterator.GetHandlerTypeIndex(), method); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4252 | if (exception_type == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4253 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4254 | Thread::Current()->ClearException(); |
| 4255 | } |
| 4256 | } |
| 4257 | } |
| 4258 | handlers_ptr = iterator.EndDataPointer(); |
| 4259 | } |
| 4260 | } |
| 4261 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4262 | mirror::Class* ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, |
| 4263 | jstring name, |
| 4264 | jobjectArray interfaces, |
| 4265 | jobject loader, |
| 4266 | jobjectArray methods, |
| 4267 | jobjectArray throws) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4268 | Thread* self = soa.Self(); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4269 | StackHandleScope<10> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4270 | MutableHandle<mirror::Class> klass(hs.NewHandle( |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4271 | AllocClass(self, GetClassRoot(kJavaLangClass), sizeof(mirror::Class)))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4272 | if (klass == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4273 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4274 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4275 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4276 | DCHECK(klass->GetClass() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4277 | klass->SetObjectSize(sizeof(mirror::Proxy)); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4278 | // Set the class access flags incl. VerificationAttempted, so we do not try to set the flag on |
| 4279 | // the methods. |
| 4280 | klass->SetAccessFlags(kAccClassIsProxy | kAccPublic | kAccFinal | kAccVerificationAttempted); |
Mathieu Chartier | 1a5337f | 2016-10-13 13:48:23 -0700 | [diff] [blame] | 4281 | klass->SetClassLoader(soa.Decode<mirror::ClassLoader>(loader)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4282 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Mathieu Chartier | 1a5337f | 2016-10-13 13:48:23 -0700 | [diff] [blame] | 4283 | klass->SetName(soa.Decode<mirror::String>(name)); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4284 | klass->SetDexCache(GetClassRoot(kJavaLangReflectProxy)->GetDexCache()); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4285 | // Object has an empty iftable, copy it for that reason. |
| 4286 | klass->SetIfTable(GetClassRoot(kJavaLangObject)->GetIfTable()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4287 | mirror::Class::SetStatus(klass, mirror::Class::kStatusIdx, self); |
Nicolas Geoffray | a7a4759 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 4288 | std::string descriptor(GetDescriptorForProxy(klass.Get())); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 4289 | const size_t hash = ComputeModifiedUtf8Hash(descriptor.c_str()); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4290 | |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 4291 | // Needs to be before we insert the class so that the allocator field is set. |
| 4292 | LinearAlloc* const allocator = GetOrCreateAllocatorForClassLoader(klass->GetClassLoader()); |
| 4293 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4294 | // Insert the class before loading the fields as the field roots |
| 4295 | // (ArtField::declaring_class_) are only visited from the class |
| 4296 | // table. There can't be any suspend points between inserting the |
| 4297 | // class and setting the field arrays below. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4298 | ObjPtr<mirror::Class> existing = InsertClass(descriptor.c_str(), klass.Get(), hash); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4299 | CHECK(existing == nullptr); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4300 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4301 | // Instance fields are inherited, but we add a couple of static fields... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4302 | const size_t num_fields = 2; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 4303 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, allocator, num_fields); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4304 | klass->SetSFieldsPtr(sfields); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4305 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4306 | // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by |
| 4307 | // our proxy, so Class.getInterfaces doesn't return the flattened set. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4308 | ArtField& interfaces_sfield = sfields->At(0); |
| 4309 | interfaces_sfield.SetDexFieldIndex(0); |
| 4310 | interfaces_sfield.SetDeclaringClass(klass.Get()); |
| 4311 | interfaces_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4312 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4313 | // 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] | 4314 | ArtField& throws_sfield = sfields->At(1); |
| 4315 | throws_sfield.SetDexFieldIndex(1); |
| 4316 | throws_sfield.SetDeclaringClass(klass.Get()); |
| 4317 | throws_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4318 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4319 | // Proxies have 1 direct method, the constructor |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4320 | const size_t num_direct_methods = 1; |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4321 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4322 | // They have as many virtual methods as the array |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4323 | auto h_methods = hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Method>>(methods)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 4324 | DCHECK_EQ(h_methods->GetClass(), mirror::Method::ArrayClass()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4325 | << mirror::Class::PrettyClass(h_methods->GetClass()); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 4326 | const size_t num_virtual_methods = h_methods->GetLength(); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4327 | |
| 4328 | // Create the methods array. |
| 4329 | LengthPrefixedArray<ArtMethod>* proxy_class_methods = AllocArtMethodArray( |
| 4330 | self, allocator, num_direct_methods + num_virtual_methods); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4331 | // Currently AllocArtMethodArray cannot return null, but the OOM logic is left there in case we |
| 4332 | // want to throw OOM in the future. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4333 | if (UNLIKELY(proxy_class_methods == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4334 | self->AssertPendingOOMException(); |
| 4335 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4336 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4337 | klass->SetMethodsPtr(proxy_class_methods, num_direct_methods, num_virtual_methods); |
| 4338 | |
| 4339 | // Create the single direct method. |
| 4340 | CreateProxyConstructor(klass, klass->GetDirectMethodUnchecked(0, image_pointer_size_)); |
| 4341 | |
| 4342 | // Create virtual method using specified prototypes. |
| 4343 | // TODO These should really use the iterators. |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4344 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4345 | auto* virtual_method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
| 4346 | auto* prototype = h_methods->Get(i)->GetArtMethod(); |
| 4347 | CreateProxyMethod(klass, prototype, virtual_method); |
| 4348 | DCHECK(virtual_method->GetDeclaringClass() != nullptr); |
| 4349 | DCHECK(prototype->GetDeclaringClass() != nullptr); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4350 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4351 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4352 | // The super class is java.lang.reflect.Proxy |
| 4353 | klass->SetSuperClass(GetClassRoot(kJavaLangReflectProxy)); |
| 4354 | // Now effectively in the loaded state. |
| 4355 | mirror::Class::SetStatus(klass, mirror::Class::kStatusLoaded, self); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4356 | self->AssertNoPendingException(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4357 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4358 | MutableHandle<mirror::Class> new_class = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 4359 | { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4360 | // Must hold lock on object when resolved. |
| 4361 | ObjectLock<mirror::Class> resolution_lock(self, klass); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4362 | // Link the fields and virtual methods, creating vtable and iftables. |
| 4363 | // The new class will replace the old one in the class table. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4364 | Handle<mirror::ObjectArray<mirror::Class>> h_interfaces( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4365 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces))); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4366 | if (!LinkClass(self, descriptor.c_str(), klass, h_interfaces, &new_class)) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4367 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorUnresolved, self); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4368 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4369 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4370 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4371 | CHECK(klass->IsRetired()); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4372 | CHECK_NE(klass.Get(), new_class.Get()); |
| 4373 | klass.Assign(new_class.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4374 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4375 | CHECK_EQ(interfaces_sfield.GetDeclaringClass(), klass.Get()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4376 | interfaces_sfield.SetObject<false>( |
| 4377 | klass.Get(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4378 | soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4379 | CHECK_EQ(throws_sfield.GetDeclaringClass(), klass.Get()); |
| 4380 | throws_sfield.SetObject<false>( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4381 | klass.Get(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4382 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(throws)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4383 | |
| 4384 | { |
| 4385 | // Lock on klass is released. Lock new class object. |
| 4386 | ObjectLock<mirror::Class> initialization_lock(self, klass); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4387 | mirror::Class::SetStatus(klass, mirror::Class::kStatusInitialized, self); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 4388 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4389 | |
| 4390 | // sanity checks |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 4391 | if (kIsDebugBuild) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4392 | CHECK(klass->GetIFieldsPtr() == nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4393 | CheckProxyConstructor(klass->GetDirectMethod(0, image_pointer_size_)); |
| 4394 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4395 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4396 | auto* virtual_method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
| 4397 | auto* prototype = h_methods->Get(i++)->GetArtMethod(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4398 | CheckProxyMethod(virtual_method, prototype); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4399 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4400 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4401 | StackHandleScope<1> hs2(self); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4402 | Handle<mirror::String> decoded_name = hs2.NewHandle(soa.Decode<mirror::String>(name)); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4403 | std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4404 | decoded_name->ToModifiedUtf8().c_str())); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4405 | CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(0)), interfaces_field_name); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4406 | |
| 4407 | std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4408 | decoded_name->ToModifiedUtf8().c_str())); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4409 | CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(1)), throws_field_name); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4410 | |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 4411 | CHECK_EQ(klass.Get()->GetProxyInterfaces(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4412 | soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)); |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 4413 | CHECK_EQ(klass.Get()->GetProxyThrows(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4414 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(throws)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4415 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4416 | return klass.Get(); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4417 | } |
| 4418 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4419 | std::string ClassLinker::GetDescriptorForProxy(ObjPtr<mirror::Class> proxy_class) { |
Nicolas Geoffray | a7a4759 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 4420 | DCHECK(proxy_class->IsProxyClass()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4421 | ObjPtr<mirror::String> name = proxy_class->GetName(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4422 | DCHECK(name != nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4423 | return DotToDescriptor(name->ToModifiedUtf8().c_str()); |
| 4424 | } |
| 4425 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4426 | void ClassLinker::CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out) { |
| 4427 | // Create constructor for Proxy that must initialize the method. |
Przemyslaw Szczepaniak | f11cd29 | 2016-08-17 17:46:38 +0100 | [diff] [blame] | 4428 | CHECK_EQ(GetClassRoot(kJavaLangReflectProxy)->NumDirectMethods(), 23u); |
| 4429 | |
Igor Murashkin | 9d3d752 | 2017-02-27 10:39:49 -0800 | [diff] [blame] | 4430 | // Find the <init>(InvocationHandler)V method. The exact method offset varies depending |
| 4431 | // on which front-end compiler was used to build the libcore DEX files. |
| 4432 | ArtMethod* proxy_constructor = GetClassRoot(kJavaLangReflectProxy)-> |
| 4433 | FindDeclaredDirectMethod("<init>", |
| 4434 | "(Ljava/lang/reflect/InvocationHandler;)V", |
| 4435 | image_pointer_size_); |
| 4436 | DCHECK(proxy_constructor != nullptr) |
| 4437 | << "Could not find <init> method in java.lang.reflect.Proxy"; |
| 4438 | |
Sebastien Hertz | ae94e35 | 2014-08-27 15:32:56 +0200 | [diff] [blame] | 4439 | // Ensure constructor is in dex cache so that we can use the dex cache to look up the overridden |
| 4440 | // constructor method. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4441 | GetClassRoot(kJavaLangReflectProxy)->GetDexCache()->SetResolvedMethod( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4442 | proxy_constructor->GetDexMethodIndex(), proxy_constructor, image_pointer_size_); |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 4443 | // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its |
| 4444 | // code_ too) |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4445 | DCHECK(out != nullptr); |
| 4446 | out->CopyFrom(proxy_constructor, image_pointer_size_); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4447 | // 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] | 4448 | out->SetAccessFlags((out->GetAccessFlags() & ~kAccProtected) | kAccPublic); |
| 4449 | out->SetDeclaringClass(klass.Get()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4450 | } |
| 4451 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4452 | void ClassLinker::CheckProxyConstructor(ArtMethod* constructor) const { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4453 | CHECK(constructor->IsConstructor()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4454 | auto* np = constructor->GetInterfaceMethodIfProxy(image_pointer_size_); |
| 4455 | CHECK_STREQ(np->GetName(), "<init>"); |
| 4456 | CHECK_STREQ(np->GetSignature().ToString().c_str(), "(Ljava/lang/reflect/InvocationHandler;)V"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4457 | DCHECK(constructor->IsPublic()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4458 | } |
| 4459 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4460 | void ClassLinker::CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4461 | ArtMethod* out) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4462 | // Ensure prototype is in dex cache so that we can use the dex cache to look up the overridden |
| 4463 | // prototype method |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 4464 | auto* dex_cache = prototype->GetDeclaringClass()->GetDexCache(); |
| 4465 | // Avoid dirtying the dex cache unless we need to. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4466 | if (dex_cache->GetResolvedMethod(prototype->GetDexMethodIndex(), image_pointer_size_) != |
| 4467 | prototype) { |
| 4468 | dex_cache->SetResolvedMethod( |
| 4469 | prototype->GetDexMethodIndex(), prototype, image_pointer_size_); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 4470 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4471 | // 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] | 4472 | // as necessary |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4473 | DCHECK(out != nullptr); |
| 4474 | out->CopyFrom(prototype, image_pointer_size_); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4475 | |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 4476 | // Set class to be the concrete proxy class. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4477 | out->SetDeclaringClass(klass.Get()); |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 4478 | // Clear the abstract, default and conflict flags to ensure that defaults aren't picked in |
| 4479 | // preference to the invocation handler. |
| 4480 | const uint32_t kRemoveFlags = kAccAbstract | kAccDefault | kAccDefaultConflict; |
| 4481 | // Make the method final. |
| 4482 | const uint32_t kAddFlags = kAccFinal; |
| 4483 | out->SetAccessFlags((out->GetAccessFlags() & ~kRemoveFlags) | kAddFlags); |
| 4484 | |
| 4485 | // Clear the dex_code_item_offset_. It needs to be 0 since proxy methods have no CodeItems but the |
| 4486 | // method they copy might (if it's a default method). |
| 4487 | out->SetCodeItemOffset(0); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4488 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4489 | // At runtime the method looks like a reference and argument saving method, clone the code |
| 4490 | // related parameters from this method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4491 | out->SetEntryPointFromQuickCompiledCode(GetQuickProxyInvokeHandler()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4492 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4493 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4494 | void ClassLinker::CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4495 | // Basic sanity |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4496 | CHECK(!prototype->IsFinal()); |
| 4497 | CHECK(method->IsFinal()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 4498 | CHECK(method->IsInvokable()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 4499 | |
| 4500 | // The proxy method doesn't have its own dex cache or dex file and so it steals those of its |
| 4501 | // 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] | 4502 | CHECK(prototype->HasSameDexCacheResolvedMethods(method, image_pointer_size_)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4503 | auto* np = method->GetInterfaceMethodIfProxy(image_pointer_size_); |
| 4504 | CHECK_EQ(prototype->GetDeclaringClass()->GetDexCache(), np->GetDexCache()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 4505 | CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex()); |
| 4506 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4507 | CHECK_STREQ(np->GetName(), prototype->GetName()); |
| 4508 | CHECK_STREQ(np->GetShorty(), prototype->GetShorty()); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4509 | // More complex sanity - via dex cache |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 4510 | CHECK_EQ(np->GetReturnType(true /* resolve */), prototype->GetReturnType(true /* resolve */)); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4511 | } |
| 4512 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4513 | bool ClassLinker::CanWeInitializeClass(ObjPtr<mirror::Class> klass, bool can_init_statics, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4514 | bool can_init_parents) { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 4515 | if (can_init_statics && can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4516 | return true; |
| 4517 | } |
| 4518 | if (!can_init_statics) { |
| 4519 | // Check if there's a class initializer. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4520 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4521 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4522 | return false; |
| 4523 | } |
| 4524 | // Check if there are encoded static values needing initialization. |
| 4525 | if (klass->NumStaticFields() != 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4526 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4527 | DCHECK(dex_class_def != nullptr); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4528 | if (dex_class_def->static_values_off_ != 0) { |
| 4529 | return false; |
| 4530 | } |
| 4531 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4532 | // If we are a class we need to initialize all interfaces with default methods when we are |
| 4533 | // initialized. Check all of them. |
| 4534 | if (!klass->IsInterface()) { |
| 4535 | size_t num_interfaces = klass->GetIfTableCount(); |
| 4536 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4537 | ObjPtr<mirror::Class> iface = klass->GetIfTable()->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4538 | if (iface->HasDefaultMethods() && |
| 4539 | !CanWeInitializeClass(iface, can_init_statics, can_init_parents)) { |
| 4540 | return false; |
| 4541 | } |
| 4542 | } |
| 4543 | } |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4544 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4545 | if (klass->IsInterface() || !klass->HasSuperClass()) { |
| 4546 | return true; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4547 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4548 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4549 | if (!can_init_parents && !super_class->IsInitialized()) { |
| 4550 | return false; |
| 4551 | } |
| 4552 | return CanWeInitializeClass(super_class, can_init_statics, can_init_parents); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4553 | } |
| 4554 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4555 | bool ClassLinker::InitializeClass(Thread* self, Handle<mirror::Class> klass, |
| 4556 | bool can_init_statics, bool can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4557 | // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol |
| 4558 | |
| 4559 | // Are we already initialized and therefore done? |
| 4560 | // Note: we differ from the JLS here as we don't do this under the lock, this is benign as |
| 4561 | // an initialized class will never change its state. |
| 4562 | if (klass->IsInitialized()) { |
| 4563 | return true; |
| 4564 | } |
| 4565 | |
| 4566 | // 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] | 4567 | if (!CanWeInitializeClass(klass.Get(), can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4568 | return false; |
| 4569 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4570 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4571 | self->AllowThreadSuspension(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4572 | uint64_t t0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4573 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4574 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4575 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4576 | // Re-check under the lock in case another thread initialized ahead of us. |
| 4577 | if (klass->IsInitialized()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4578 | return true; |
| 4579 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4580 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4581 | // 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] | 4582 | if (klass->IsErroneous()) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 4583 | ThrowEarlierClassFailure(klass.Get(), true); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4584 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4585 | return false; |
| 4586 | } |
| 4587 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4588 | CHECK(klass->IsResolved() && !klass->IsErroneousResolved()) |
| 4589 | << klass->PrettyClass() << ": state=" << klass->GetStatus(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4590 | |
| 4591 | if (!klass->IsVerified()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4592 | VerifyClass(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4593 | if (!klass->IsVerified()) { |
| 4594 | // We failed to verify, expect either the klass to be erroneous or verification failed at |
| 4595 | // compile time. |
| 4596 | if (klass->IsErroneous()) { |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 4597 | // The class is erroneous. This may be a verifier error, or another thread attempted |
| 4598 | // verification and/or initialization and failed. We can distinguish those cases by |
| 4599 | // whether an exception is already pending. |
| 4600 | if (self->IsExceptionPending()) { |
| 4601 | // Check that it's a VerifyError. |
| 4602 | DCHECK_EQ("java.lang.Class<java.lang.VerifyError>", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4603 | mirror::Class::PrettyClass(self->GetException()->GetClass())); |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 4604 | } else { |
| 4605 | // Check that another thread attempted initialization. |
| 4606 | DCHECK_NE(0, klass->GetClinitThreadId()); |
| 4607 | DCHECK_NE(self->GetTid(), klass->GetClinitThreadId()); |
| 4608 | // Need to rethrow the previous failure now. |
| 4609 | ThrowEarlierClassFailure(klass.Get(), true); |
| 4610 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4611 | VlogClassInitializationFailure(klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4612 | } else { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4613 | CHECK(Runtime::Current()->IsAotCompiler()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4614 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 4615 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4616 | return false; |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4617 | } else { |
| 4618 | self->AssertNoPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4619 | } |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 4620 | |
| 4621 | // A separate thread could have moved us all the way to initialized. A "simple" example |
| 4622 | // involves a subclass of the current class being initialized at the same time (which |
| 4623 | // will implicitly initialize the superclass, if scheduled that way). b/28254258 |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4624 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 4625 | if (klass->IsInitialized()) { |
| 4626 | return true; |
| 4627 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4628 | } |
| 4629 | |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4630 | // If the class is kStatusInitializing, either this thread is |
| 4631 | // initializing higher up the stack or another thread has beat us |
| 4632 | // to initializing and we need to wait. Either way, this |
| 4633 | // invocation of InitializeClass will not be responsible for |
| 4634 | // running <clinit> and will return. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4635 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4636 | // Could have got an exception during verification. |
| 4637 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4638 | VlogClassInitializationFailure(klass); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4639 | return false; |
| 4640 | } |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 4641 | // We caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 4642 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4643 | // Yes. That's fine. Return so we can continue initializing. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4644 | return true; |
| 4645 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4646 | // No. That's fine. Wait for another thread to finish initializing. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4647 | return WaitForInitializeClass(klass, self, lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4648 | } |
| 4649 | |
| 4650 | if (!ValidateSuperClassDescriptors(klass)) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4651 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4652 | return false; |
| 4653 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4654 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4655 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4656 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusVerified) << klass->PrettyClass() |
Andreas Gampe | 9510ccd | 2016-04-20 09:55:25 -0700 | [diff] [blame] | 4657 | << " self.tid=" << self->GetTid() << " clinit.tid=" << klass->GetClinitThreadId(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4658 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4659 | // From here out other threads may observe that we're initializing and so changes of state |
| 4660 | // require the a notification. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 4661 | klass->SetClinitThreadId(self->GetTid()); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4662 | mirror::Class::SetStatus(klass, mirror::Class::kStatusInitializing, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4663 | |
| 4664 | t0 = NanoTime(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4665 | } |
| 4666 | |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 4667 | // Initialize super classes, must be done while initializing for the JLS. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4668 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4669 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4670 | if (!super_class->IsInitialized()) { |
| 4671 | CHECK(!super_class->IsInterface()); |
| 4672 | CHECK(can_init_parents); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4673 | StackHandleScope<1> hs(self); |
| 4674 | Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4675 | bool super_initialized = InitializeClass(self, handle_scope_super, can_init_statics, true); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4676 | if (!super_initialized) { |
| 4677 | // The super class was verified ahead of entering initializing, we should only be here if |
| 4678 | // the super class became erroneous due to initialization. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4679 | CHECK(handle_scope_super->IsErroneous() && self->IsExceptionPending()) |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4680 | << "Super class initialization failed for " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4681 | << handle_scope_super->PrettyDescriptor() |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4682 | << " that has unexpected status " << handle_scope_super->GetStatus() |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4683 | << "\nPending exception:\n" |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 4684 | << (self->GetException() != nullptr ? self->GetException()->Dump() : ""); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4685 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4686 | // Initialization failed because the super-class is erroneous. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4687 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4688 | return false; |
| 4689 | } |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 4690 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4691 | } |
| 4692 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4693 | if (!klass->IsInterface()) { |
| 4694 | // Initialize interfaces with default methods for the JLS. |
| 4695 | size_t num_direct_interfaces = klass->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4696 | // Only setup the (expensive) handle scope if we actually need to. |
| 4697 | if (UNLIKELY(num_direct_interfaces > 0)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4698 | StackHandleScope<1> hs_iface(self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4699 | MutableHandle<mirror::Class> handle_scope_iface(hs_iface.NewHandle<mirror::Class>(nullptr)); |
| 4700 | for (size_t i = 0; i < num_direct_interfaces; i++) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 4701 | handle_scope_iface.Assign(mirror::Class::GetDirectInterface(self, klass.Get(), i)); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 4702 | CHECK(handle_scope_iface != nullptr) << klass->PrettyDescriptor() << " iface #" << i; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4703 | CHECK(handle_scope_iface->IsInterface()); |
| 4704 | if (handle_scope_iface->HasBeenRecursivelyInitialized()) { |
| 4705 | // We have already done this for this interface. Skip it. |
| 4706 | continue; |
| 4707 | } |
| 4708 | // We cannot just call initialize class directly because we need to ensure that ALL |
| 4709 | // interfaces with default methods are initialized. Non-default interface initialization |
| 4710 | // will not affect other non-default super-interfaces. |
| 4711 | bool iface_initialized = InitializeDefaultInterfaceRecursive(self, |
| 4712 | handle_scope_iface, |
| 4713 | can_init_statics, |
| 4714 | can_init_parents); |
| 4715 | if (!iface_initialized) { |
| 4716 | ObjectLock<mirror::Class> lock(self, klass); |
| 4717 | // Initialization failed because one of our interfaces with default methods is erroneous. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4718 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4719 | return false; |
| 4720 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4721 | } |
| 4722 | } |
| 4723 | } |
| 4724 | |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4725 | const size_t num_static_fields = klass->NumStaticFields(); |
| 4726 | if (num_static_fields > 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4727 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4728 | CHECK(dex_class_def != nullptr); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4729 | const DexFile& dex_file = klass->GetDexFile(); |
Hiroshi Yamauchi | 67ef46a | 2014-08-21 15:59:43 -0700 | [diff] [blame] | 4730 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4731 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4732 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4733 | |
| 4734 | // Eagerly fill in static fields so that the we don't have to do as many expensive |
| 4735 | // Class::FindStaticField in ResolveField. |
| 4736 | for (size_t i = 0; i < num_static_fields; ++i) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4737 | ArtField* field = klass->GetStaticField(i); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4738 | const uint32_t field_idx = field->GetDexFieldIndex(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4739 | ArtField* resolved_field = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4740 | if (resolved_field == nullptr) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4741 | dex_cache->SetResolvedField(field_idx, field, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4742 | } else { |
| 4743 | DCHECK_EQ(field, resolved_field); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4744 | } |
| 4745 | } |
| 4746 | |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 4747 | annotations::RuntimeEncodedStaticFieldValueIterator value_it(dex_file, |
| 4748 | &dex_cache, |
| 4749 | &class_loader, |
| 4750 | this, |
| 4751 | *dex_class_def); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 4752 | const uint8_t* class_data = dex_file.GetClassData(*dex_class_def); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4753 | ClassDataItemIterator field_it(dex_file, class_data); |
| 4754 | if (value_it.HasNext()) { |
| 4755 | DCHECK(field_it.HasNextStaticField()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4756 | CHECK(can_init_statics); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4757 | for ( ; value_it.HasNext(); value_it.Next(), field_it.Next()) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4758 | ArtField* field = ResolveField( |
| 4759 | dex_file, field_it.GetMemberIndex(), dex_cache, class_loader, true); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4760 | if (Runtime::Current()->IsActiveTransaction()) { |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4761 | value_it.ReadValueToField<true>(field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4762 | } else { |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4763 | value_it.ReadValueToField<false>(field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4764 | } |
Mathieu Chartier | da595be | 2016-08-10 13:57:39 -0700 | [diff] [blame] | 4765 | if (self->IsExceptionPending()) { |
| 4766 | break; |
| 4767 | } |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4768 | DCHECK(!value_it.HasNext() || field_it.HasNextStaticField()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4769 | } |
| 4770 | } |
| 4771 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4772 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4773 | |
Mathieu Chartier | da595be | 2016-08-10 13:57:39 -0700 | [diff] [blame] | 4774 | if (!self->IsExceptionPending()) { |
| 4775 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
| 4776 | if (clinit != nullptr) { |
| 4777 | CHECK(can_init_statics); |
| 4778 | JValue result; |
| 4779 | clinit->Invoke(self, nullptr, 0, &result, "V"); |
| 4780 | } |
| 4781 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4782 | self->AllowThreadSuspension(); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 4783 | uint64_t t1 = NanoTime(); |
| 4784 | |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4785 | bool success = true; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4786 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4787 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4788 | |
| 4789 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4790 | WrapExceptionInInitializer(klass); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4791 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4792 | success = false; |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 4793 | } else if (Runtime::Current()->IsTransactionAborted()) { |
| 4794 | // The exception thrown when the transaction aborted has been caught and cleared |
| 4795 | // so we need to throw it again now. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4796 | VLOG(compiler) << "Return from class initializer of " |
| 4797 | << mirror::Class::PrettyDescriptor(klass.Get()) |
Sebastien Hertz | bd9cf9f | 2015-03-03 12:16:13 +0100 | [diff] [blame] | 4798 | << " without exception while transaction was aborted: re-throw it now."; |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 4799 | Runtime::Current()->ThrowTransactionAbortError(self); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4800 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 4801 | success = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4802 | } else { |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 4803 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 4804 | RuntimeStats* thread_stats = self->GetStats(); |
| 4805 | ++global_stats->class_init_count; |
| 4806 | ++thread_stats->class_init_count; |
| 4807 | global_stats->class_init_time_ns += (t1 - t0); |
| 4808 | thread_stats->class_init_time_ns += (t1 - t0); |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 4809 | // Set the class as initialized except if failed to initialize static fields. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 4810 | mirror::Class::SetStatus(klass, mirror::Class::kStatusInitialized, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4811 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4812 | std::string temp; |
| 4813 | LOG(INFO) << "Initialized class " << klass->GetDescriptor(&temp) << " from " << |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4814 | klass->GetLocation(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 4815 | } |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 4816 | // Opportunistically set static method trampolines to their destination. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4817 | FixupStaticTrampolines(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4818 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4819 | } |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4820 | return success; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4821 | } |
| 4822 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4823 | // We recursively run down the tree of interfaces. We need to do this in the order they are declared |
| 4824 | // and perform the initialization only on those interfaces that contain default methods. |
| 4825 | bool ClassLinker::InitializeDefaultInterfaceRecursive(Thread* self, |
| 4826 | Handle<mirror::Class> iface, |
| 4827 | bool can_init_statics, |
| 4828 | bool can_init_parents) { |
| 4829 | CHECK(iface->IsInterface()); |
| 4830 | size_t num_direct_ifaces = iface->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4831 | // Only create the (expensive) handle scope if we need it. |
| 4832 | if (UNLIKELY(num_direct_ifaces > 0)) { |
| 4833 | StackHandleScope<1> hs(self); |
| 4834 | MutableHandle<mirror::Class> handle_super_iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 4835 | // First we initialize all of iface's super-interfaces recursively. |
| 4836 | for (size_t i = 0; i < num_direct_ifaces; i++) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 4837 | ObjPtr<mirror::Class> super_iface = mirror::Class::GetDirectInterface(self, iface.Get(), i); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 4838 | CHECK(super_iface != nullptr) << iface->PrettyDescriptor() << " iface #" << i; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4839 | if (!super_iface->HasBeenRecursivelyInitialized()) { |
| 4840 | // Recursive step |
| 4841 | handle_super_iface.Assign(super_iface); |
| 4842 | if (!InitializeDefaultInterfaceRecursive(self, |
| 4843 | handle_super_iface, |
| 4844 | can_init_statics, |
| 4845 | can_init_parents)) { |
| 4846 | return false; |
| 4847 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4848 | } |
| 4849 | } |
| 4850 | } |
| 4851 | |
| 4852 | bool result = true; |
| 4853 | // Then we initialize 'iface' if it has default methods. We do not need to (and in fact must not) |
| 4854 | // initialize if we don't have default methods. |
| 4855 | if (iface->HasDefaultMethods()) { |
| 4856 | result = EnsureInitialized(self, iface, can_init_statics, can_init_parents); |
| 4857 | } |
| 4858 | |
| 4859 | // Mark that this interface has undergone recursive default interface initialization so we know we |
| 4860 | // can skip it on any later class initializations. We do this even if we are not a default |
| 4861 | // interface since we can still avoid the traversal. This is purely a performance optimization. |
| 4862 | if (result) { |
| 4863 | // TODO This should be done in a better way |
| 4864 | ObjectLock<mirror::Class> lock(self, iface); |
| 4865 | iface->SetRecursivelyInitialized(); |
| 4866 | } |
| 4867 | return result; |
| 4868 | } |
| 4869 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4870 | bool ClassLinker::WaitForInitializeClass(Handle<mirror::Class> klass, |
| 4871 | Thread* self, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4872 | ObjectLock<mirror::Class>& lock) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4873 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4874 | while (true) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4875 | self->AssertNoPendingException(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4876 | CHECK(!klass->IsInitialized()); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4877 | lock.WaitIgnoringInterrupts(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4878 | |
| 4879 | // When we wake up, repeat the test for init-in-progress. If |
| 4880 | // there's an exception pending (only possible if |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4881 | // we were not using WaitIgnoringInterrupts), bail out. |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4882 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4883 | WrapExceptionInInitializer(klass); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4884 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorResolved, self); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4885 | return false; |
| 4886 | } |
| 4887 | // Spurious wakeup? Go back to waiting. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4888 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4889 | continue; |
| 4890 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4891 | if (klass->GetStatus() == mirror::Class::kStatusVerified && |
| 4892 | Runtime::Current()->IsAotCompiler()) { |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 4893 | // Compile time initialization failed. |
| 4894 | return false; |
| 4895 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4896 | if (klass->IsErroneous()) { |
| 4897 | // The caller wants an exception, but it was thrown in a |
| 4898 | // different thread. Synthesize one here. |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 4899 | ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4900 | klass->PrettyDescriptor().c_str()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4901 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4902 | return false; |
| 4903 | } |
| 4904 | if (klass->IsInitialized()) { |
| 4905 | return true; |
| 4906 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4907 | LOG(FATAL) << "Unexpected class status. " << klass->PrettyClass() << " is " |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4908 | << klass->GetStatus(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4909 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 4910 | UNREACHABLE(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4911 | } |
| 4912 | |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4913 | static void ThrowSignatureCheckResolveReturnTypeException(Handle<mirror::Class> klass, |
| 4914 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4915 | ArtMethod* method, |
| 4916 | ArtMethod* m) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4917 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4918 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4919 | DCHECK(!m->IsProxyMethod()); |
| 4920 | const DexFile* dex_file = m->GetDexFile(); |
| 4921 | const DexFile::MethodId& method_id = dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 4922 | const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4923 | dex::TypeIndex return_type_idx = proto_id.return_type_idx_; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4924 | std::string return_type = dex_file->PrettyType(return_type_idx); |
| 4925 | std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4926 | ThrowWrappedLinkageError(klass.Get(), |
| 4927 | "While checking class %s method %s signature against %s %s: " |
| 4928 | "Failed to resolve return type %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4929 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 4930 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4931 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4932 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4933 | return_type.c_str(), class_loader.c_str()); |
| 4934 | } |
| 4935 | |
| 4936 | static void ThrowSignatureCheckResolveArgException(Handle<mirror::Class> klass, |
| 4937 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4938 | ArtMethod* method, |
| 4939 | ArtMethod* m, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4940 | uint32_t index, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4941 | dex::TypeIndex arg_type_idx) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4942 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4943 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4944 | DCHECK(!m->IsProxyMethod()); |
| 4945 | const DexFile* dex_file = m->GetDexFile(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4946 | std::string arg_type = dex_file->PrettyType(arg_type_idx); |
| 4947 | std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4948 | ThrowWrappedLinkageError(klass.Get(), |
| 4949 | "While checking class %s method %s signature against %s %s: " |
| 4950 | "Failed to resolve arg %u type %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4951 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 4952 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4953 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4954 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4955 | index, arg_type.c_str(), class_loader.c_str()); |
| 4956 | } |
| 4957 | |
| 4958 | static void ThrowSignatureMismatch(Handle<mirror::Class> klass, |
| 4959 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4960 | ArtMethod* method, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4961 | const std::string& error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4962 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4963 | ThrowLinkageError(klass.Get(), |
| 4964 | "Class %s method %s resolves differently in %s %s: %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4965 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 4966 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4967 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4968 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4969 | error_msg.c_str()); |
| 4970 | } |
| 4971 | |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4972 | static bool HasSameSignatureWithDifferentClassLoaders(Thread* self, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4973 | Handle<mirror::Class> klass, |
| 4974 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4975 | ArtMethod* method1, |
| 4976 | ArtMethod* method2) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4977 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4978 | { |
| 4979 | StackHandleScope<1> hs(self); |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 4980 | Handle<mirror::Class> return_type(hs.NewHandle(method1->GetReturnType(true /* resolve */))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4981 | if (UNLIKELY(return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4982 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method1); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4983 | return false; |
| 4984 | } |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 4985 | ObjPtr<mirror::Class> other_return_type = method2->GetReturnType(true /* resolve */); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4986 | if (UNLIKELY(other_return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4987 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method2); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4988 | return false; |
| 4989 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 4990 | if (UNLIKELY(other_return_type != return_type.Get())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4991 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 4992 | StringPrintf("Return types mismatch: %s(%p) vs %s(%p)", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4993 | return_type->PrettyClassAndClassLoader().c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 4994 | return_type.Get(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4995 | other_return_type->PrettyClassAndClassLoader().c_str(), |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4996 | other_return_type.Ptr())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 4997 | return false; |
| 4998 | } |
| 4999 | } |
| 5000 | const DexFile::TypeList* types1 = method1->GetParameterTypeList(); |
| 5001 | const DexFile::TypeList* types2 = method2->GetParameterTypeList(); |
| 5002 | if (types1 == nullptr) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5003 | if (types2 != nullptr && types2->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5004 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5005 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5006 | method2->PrettyMethod(true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5007 | return false; |
| 5008 | } |
| 5009 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5010 | } else if (UNLIKELY(types2 == nullptr)) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5011 | if (types1->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5012 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5013 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5014 | method2->PrettyMethod(true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5015 | return false; |
| 5016 | } |
| 5017 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5018 | } |
| 5019 | uint32_t num_types = types1->Size(); |
| 5020 | if (UNLIKELY(num_types != types2->Size())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5021 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5022 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5023 | method2->PrettyMethod(true).c_str())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5024 | return false; |
| 5025 | } |
| 5026 | for (uint32_t i = 0; i < num_types; ++i) { |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5027 | StackHandleScope<1> hs(self); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5028 | dex::TypeIndex param_type_idx = types1->GetTypeItem(i).type_idx_; |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5029 | Handle<mirror::Class> param_type(hs.NewHandle( |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5030 | method1->GetClassFromTypeIndex(param_type_idx, true /* resolve */))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5031 | if (UNLIKELY(param_type == nullptr)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5032 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5033 | method1, i, param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5034 | return false; |
| 5035 | } |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5036 | dex::TypeIndex other_param_type_idx = types2->GetTypeItem(i).type_idx_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5037 | ObjPtr<mirror::Class> other_param_type = |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5038 | method2->GetClassFromTypeIndex(other_param_type_idx, true /* resolve */); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5039 | if (UNLIKELY(other_param_type == nullptr)) { |
| 5040 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5041 | method2, i, other_param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5042 | return false; |
| 5043 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5044 | if (UNLIKELY(param_type.Get() != other_param_type)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5045 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5046 | StringPrintf("Parameter %u type mismatch: %s(%p) vs %s(%p)", |
| 5047 | i, |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5048 | param_type->PrettyClassAndClassLoader().c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5049 | param_type.Get(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5050 | other_param_type->PrettyClassAndClassLoader().c_str(), |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5051 | other_param_type.Ptr())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5052 | return false; |
| 5053 | } |
| 5054 | } |
| 5055 | return true; |
| 5056 | } |
| 5057 | |
| 5058 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5059 | bool ClassLinker::ValidateSuperClassDescriptors(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5060 | if (klass->IsInterface()) { |
| 5061 | return true; |
| 5062 | } |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 5063 | // Begin with the methods local to the superclass. |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 5064 | Thread* self = Thread::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5065 | StackHandleScope<1> hs(self); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5066 | MutableHandle<mirror::Class> super_klass(hs.NewHandle<mirror::Class>(nullptr)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5067 | if (klass->HasSuperClass() && |
| 5068 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5069 | super_klass.Assign(klass->GetSuperClass()); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5070 | for (int i = klass->GetSuperClass()->GetVTableLength() - 1; i >= 0; --i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5071 | auto* m = klass->GetVTableEntry(i, image_pointer_size_); |
| 5072 | auto* super_m = klass->GetSuperClass()->GetVTableEntry(i, image_pointer_size_); |
| 5073 | if (m != super_m) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5074 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, |
| 5075 | klass, |
| 5076 | super_klass, |
| 5077 | m, |
| 5078 | super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5079 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5080 | return false; |
| 5081 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5082 | } |
| 5083 | } |
| 5084 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 5085 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5086 | super_klass.Assign(klass->GetIfTable()->GetInterface(i)); |
| 5087 | if (klass->GetClassLoader() != super_klass->GetClassLoader()) { |
| 5088 | uint32_t num_methods = super_klass->NumVirtualMethods(); |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 5089 | for (uint32_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5090 | auto* m = klass->GetIfTable()->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>( |
| 5091 | j, image_pointer_size_); |
| 5092 | auto* super_m = super_klass->GetVirtualMethod(j, image_pointer_size_); |
| 5093 | if (m != super_m) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5094 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, |
| 5095 | klass, |
| 5096 | super_klass, |
| 5097 | m, |
| 5098 | super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5099 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5100 | return false; |
| 5101 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5102 | } |
| 5103 | } |
| 5104 | } |
| 5105 | } |
| 5106 | return true; |
| 5107 | } |
| 5108 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5109 | bool ClassLinker::EnsureInitialized(Thread* self, |
| 5110 | Handle<mirror::Class> c, |
| 5111 | bool can_init_fields, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5112 | bool can_init_parents) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5113 | DCHECK(c != nullptr); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5114 | if (c->IsInitialized()) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 5115 | EnsureSkipAccessChecksMethods(c, image_pointer_size_); |
Mathieu Chartier | 8502f72 | 2016-06-08 15:09:08 -0700 | [diff] [blame] | 5116 | self->AssertNoPendingException(); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5117 | return true; |
| 5118 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5119 | const bool success = InitializeClass(self, c, can_init_fields, can_init_parents); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5120 | if (!success) { |
| 5121 | if (can_init_fields && can_init_parents) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5122 | CHECK(self->IsExceptionPending()) << c->PrettyClass(); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5123 | } |
| 5124 | } else { |
| 5125 | self->AssertNoPendingException(); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 5126 | } |
| 5127 | return success; |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 5128 | } |
| 5129 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5130 | void ClassLinker::FixupTemporaryDeclaringClass(ObjPtr<mirror::Class> temp_class, |
| 5131 | ObjPtr<mirror::Class> new_class) { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5132 | DCHECK_EQ(temp_class->NumInstanceFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5133 | for (ArtField& field : new_class->GetIFields()) { |
| 5134 | if (field.GetDeclaringClass() == temp_class) { |
| 5135 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5136 | } |
| 5137 | } |
| 5138 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5139 | DCHECK_EQ(temp_class->NumStaticFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5140 | for (ArtField& field : new_class->GetSFields()) { |
| 5141 | if (field.GetDeclaringClass() == temp_class) { |
| 5142 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5143 | } |
| 5144 | } |
| 5145 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5146 | DCHECK_EQ(temp_class->NumDirectMethods(), 0u); |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5147 | DCHECK_EQ(temp_class->NumVirtualMethods(), 0u); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5148 | for (auto& method : new_class->GetMethods(image_pointer_size_)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5149 | if (method.GetDeclaringClass() == temp_class) { |
| 5150 | method.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5151 | } |
| 5152 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5153 | |
| 5154 | // Make sure the remembered set and mod-union tables know that we updated some of the native |
| 5155 | // roots. |
| 5156 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5157 | } |
| 5158 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5159 | void ClassLinker::RegisterClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5160 | CHECK(class_loader->GetAllocator() == nullptr); |
| 5161 | CHECK(class_loader->GetClassTable() == nullptr); |
| 5162 | Thread* const self = Thread::Current(); |
| 5163 | ClassLoaderData data; |
| 5164 | data.weak_root = self->GetJniEnv()->vm->AddWeakGlobalRef(self, class_loader); |
| 5165 | // Create and set the class table. |
| 5166 | data.class_table = new ClassTable; |
| 5167 | class_loader->SetClassTable(data.class_table); |
| 5168 | // Create and set the linear allocator. |
| 5169 | data.allocator = Runtime::Current()->CreateLinearAlloc(); |
| 5170 | class_loader->SetAllocator(data.allocator); |
| 5171 | // Add to the list so that we know to free the data later. |
| 5172 | class_loaders_.push_back(data); |
| 5173 | } |
| 5174 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5175 | ClassTable* ClassLinker::InsertClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5176 | if (class_loader == nullptr) { |
| 5177 | return &boot_class_table_; |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5178 | } |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5179 | ClassTable* class_table = class_loader->GetClassTable(); |
| 5180 | if (class_table == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5181 | RegisterClassLoader(class_loader); |
| 5182 | class_table = class_loader->GetClassTable(); |
| 5183 | DCHECK(class_table != nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5184 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5185 | return class_table; |
| 5186 | } |
| 5187 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5188 | ClassTable* ClassLinker::ClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5189 | return class_loader == nullptr ? &boot_class_table_ : class_loader->GetClassTable(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5190 | } |
| 5191 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5192 | static ImTable* FindSuperImt(ObjPtr<mirror::Class> klass, PointerSize pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5193 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5194 | while (klass->HasSuperClass()) { |
| 5195 | klass = klass->GetSuperClass(); |
| 5196 | if (klass->ShouldHaveImt()) { |
| 5197 | return klass->GetImt(pointer_size); |
| 5198 | } |
| 5199 | } |
| 5200 | return nullptr; |
| 5201 | } |
| 5202 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5203 | bool ClassLinker::LinkClass(Thread* self, |
| 5204 | const char* descriptor, |
| 5205 | Handle<mirror::Class> klass, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5206 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5207 | MutableHandle<mirror::Class>* h_new_class_out) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5208 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5209 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5210 | if (!LinkSuperClass(klass)) { |
| 5211 | return false; |
| 5212 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5213 | ArtMethod* imt_data[ImTable::kSize]; |
| 5214 | // If there are any new conflicts compared to super class. |
| 5215 | bool new_conflict = false; |
| 5216 | std::fill_n(imt_data, arraysize(imt_data), Runtime::Current()->GetImtUnimplementedMethod()); |
| 5217 | if (!LinkMethods(self, klass, interfaces, &new_conflict, imt_data)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5218 | return false; |
| 5219 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5220 | if (!LinkInstanceFields(self, klass)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5221 | return false; |
| 5222 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5223 | size_t class_size; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5224 | if (!LinkStaticFields(self, klass, &class_size)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5225 | return false; |
| 5226 | } |
| 5227 | CreateReferenceInstanceOffsets(klass); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5228 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5229 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5230 | ImTable* imt = nullptr; |
| 5231 | if (klass->ShouldHaveImt()) { |
| 5232 | // If there are any new conflicts compared to the super class we can not make a copy. There |
| 5233 | // can be cases where both will have a conflict method at the same slot without having the same |
| 5234 | // set of conflicts. In this case, we can not share the IMT since the conflict table slow path |
| 5235 | // will possibly create a table that is incorrect for either of the classes. |
| 5236 | // Same IMT with new_conflict does not happen very often. |
| 5237 | if (!new_conflict) { |
| 5238 | ImTable* super_imt = FindSuperImt(klass.Get(), image_pointer_size_); |
| 5239 | if (super_imt != nullptr) { |
| 5240 | bool imt_equals = true; |
| 5241 | for (size_t i = 0; i < ImTable::kSize && imt_equals; ++i) { |
| 5242 | imt_equals = imt_equals && (super_imt->Get(i, image_pointer_size_) == imt_data[i]); |
| 5243 | } |
| 5244 | if (imt_equals) { |
| 5245 | imt = super_imt; |
| 5246 | } |
| 5247 | } |
| 5248 | } |
| 5249 | if (imt == nullptr) { |
| 5250 | LinearAlloc* allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
| 5251 | imt = reinterpret_cast<ImTable*>( |
| 5252 | allocator->Alloc(self, ImTable::SizeInBytes(image_pointer_size_))); |
| 5253 | if (imt == nullptr) { |
| 5254 | return false; |
| 5255 | } |
| 5256 | imt->Populate(imt_data, image_pointer_size_); |
| 5257 | } |
| 5258 | } |
| 5259 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5260 | if (!klass->IsTemp() || (!init_done_ && klass->GetClassSize() == class_size)) { |
| 5261 | // We don't need to retire this class as it has no embedded tables or it was created the |
| 5262 | // correct size during class linker initialization. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5263 | CHECK_EQ(klass->GetClassSize(), class_size) << klass->PrettyDescriptor(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5264 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5265 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 5266 | klass->PopulateEmbeddedVTable(image_pointer_size_); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5267 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5268 | if (klass->ShouldHaveImt()) { |
| 5269 | klass->SetImt(imt, image_pointer_size_); |
| 5270 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 5271 | |
| 5272 | // Update CHA info based on whether we override methods. |
| 5273 | // Have to do this before setting the class as resolved which allows |
| 5274 | // instantiation of klass. |
| 5275 | Runtime::Current()->GetClassHierarchyAnalysis()->UpdateAfterLoadingOf(klass); |
| 5276 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5277 | // This will notify waiters on klass that saw the not yet resolved |
| 5278 | // class in the class_table_ during EnsureResolved. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 5279 | mirror::Class::SetStatus(klass, mirror::Class::kStatusResolved, self); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5280 | h_new_class_out->Assign(klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5281 | } else { |
| 5282 | CHECK(!klass->IsResolved()); |
| 5283 | // Retire the temporary class and create the correctly sized resolved class. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5284 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5285 | 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] | 5286 | // Set arrays to null since we don't want to have multiple classes with the same ArtField or |
| 5287 | // ArtMethod array pointers. If this occurs, it causes bugs in remembered sets since the GC |
| 5288 | // may not see any references to the target space and clean the card for a class if another |
| 5289 | // class had the same array pointer. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5290 | klass->SetMethodsPtrUnchecked(nullptr, 0, 0); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5291 | klass->SetSFieldsPtrUnchecked(nullptr); |
| 5292 | klass->SetIFieldsPtrUnchecked(nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5293 | if (UNLIKELY(h_new_class == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5294 | self->AssertPendingOOMException(); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 5295 | mirror::Class::SetStatus(klass, mirror::Class::kStatusErrorUnresolved, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5296 | return false; |
| 5297 | } |
| 5298 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5299 | CHECK_EQ(h_new_class->GetClassSize(), class_size); |
| 5300 | ObjectLock<mirror::Class> lock(self, h_new_class); |
| 5301 | FixupTemporaryDeclaringClass(klass.Get(), h_new_class.Get()); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5302 | |
| 5303 | { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5304 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5305 | ObjPtr<mirror::ClassLoader> const class_loader = h_new_class.Get()->GetClassLoader(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5306 | ClassTable* const table = InsertClassTableForClassLoader(class_loader); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5307 | ObjPtr<mirror::Class> existing = table->UpdateClass(descriptor, h_new_class.Get(), |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5308 | ComputeModifiedUtf8Hash(descriptor)); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 5309 | if (class_loader != nullptr) { |
| 5310 | // We updated the class in the class table, perform the write barrier so that the GC knows |
| 5311 | // about the change. |
| 5312 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader); |
| 5313 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5314 | CHECK_EQ(existing, klass.Get()); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 5315 | if (log_new_roots_) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5316 | new_class_roots_.push_back(GcRoot<mirror::Class>(h_new_class.Get())); |
| 5317 | } |
| 5318 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5319 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 5320 | // Update CHA info based on whether we override methods. |
| 5321 | // Have to do this before setting the class as resolved which allows |
| 5322 | // instantiation of klass. |
| 5323 | Runtime::Current()->GetClassHierarchyAnalysis()->UpdateAfterLoadingOf(h_new_class); |
| 5324 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5325 | // This will notify waiters on temp class that saw the not yet resolved class in the |
| 5326 | // class_table_ during EnsureResolved. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 5327 | mirror::Class::SetStatus(klass, mirror::Class::kStatusRetired, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5328 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5329 | CHECK_EQ(h_new_class->GetStatus(), mirror::Class::kStatusResolving); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5330 | // This will notify waiters on new_class that saw the not yet resolved |
| 5331 | // class in the class_table_ during EnsureResolved. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5332 | mirror::Class::SetStatus(h_new_class, mirror::Class::kStatusResolved, self); |
| 5333 | // Return the new class. |
| 5334 | h_new_class_out->Assign(h_new_class.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5335 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5336 | return true; |
| 5337 | } |
| 5338 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5339 | static void CountMethodsAndFields(ClassDataItemIterator& dex_data, |
| 5340 | size_t* virtual_methods, |
| 5341 | size_t* direct_methods, |
| 5342 | size_t* static_fields, |
| 5343 | size_t* instance_fields) { |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5344 | *virtual_methods = *direct_methods = *static_fields = *instance_fields = 0; |
| 5345 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5346 | while (dex_data.HasNextStaticField()) { |
| 5347 | dex_data.Next(); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5348 | (*static_fields)++; |
| 5349 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5350 | while (dex_data.HasNextInstanceField()) { |
| 5351 | dex_data.Next(); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5352 | (*instance_fields)++; |
| 5353 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5354 | while (dex_data.HasNextDirectMethod()) { |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5355 | (*direct_methods)++; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5356 | dex_data.Next(); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5357 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5358 | while (dex_data.HasNextVirtualMethod()) { |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5359 | (*virtual_methods)++; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5360 | dex_data.Next(); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5361 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5362 | DCHECK(!dex_data.HasNext()); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5363 | } |
| 5364 | |
| 5365 | static void DumpClass(std::ostream& os, |
| 5366 | const DexFile& dex_file, const DexFile::ClassDef& dex_class_def, |
| 5367 | const char* suffix) { |
| 5368 | ClassDataItemIterator dex_data(dex_file, dex_file.GetClassData(dex_class_def)); |
| 5369 | os << dex_file.GetClassDescriptor(dex_class_def) << suffix << ":\n"; |
| 5370 | os << " Static fields:\n"; |
| 5371 | while (dex_data.HasNextStaticField()) { |
| 5372 | const DexFile::FieldId& id = dex_file.GetFieldId(dex_data.GetMemberIndex()); |
| 5373 | os << " " << dex_file.GetFieldTypeDescriptor(id) << " " << dex_file.GetFieldName(id) << "\n"; |
| 5374 | dex_data.Next(); |
| 5375 | } |
| 5376 | os << " Instance fields:\n"; |
| 5377 | while (dex_data.HasNextInstanceField()) { |
| 5378 | const DexFile::FieldId& id = dex_file.GetFieldId(dex_data.GetMemberIndex()); |
| 5379 | os << " " << dex_file.GetFieldTypeDescriptor(id) << " " << dex_file.GetFieldName(id) << "\n"; |
| 5380 | dex_data.Next(); |
| 5381 | } |
| 5382 | os << " Direct methods:\n"; |
| 5383 | while (dex_data.HasNextDirectMethod()) { |
| 5384 | const DexFile::MethodId& id = dex_file.GetMethodId(dex_data.GetMemberIndex()); |
| 5385 | os << " " << dex_file.GetMethodName(id) << dex_file.GetMethodSignature(id).ToString() << "\n"; |
| 5386 | dex_data.Next(); |
| 5387 | } |
| 5388 | os << " Virtual methods:\n"; |
| 5389 | while (dex_data.HasNextVirtualMethod()) { |
| 5390 | const DexFile::MethodId& id = dex_file.GetMethodId(dex_data.GetMemberIndex()); |
| 5391 | os << " " << dex_file.GetMethodName(id) << dex_file.GetMethodSignature(id).ToString() << "\n"; |
| 5392 | dex_data.Next(); |
| 5393 | } |
| 5394 | } |
| 5395 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5396 | static std::string DumpClasses(const DexFile& dex_file1, |
| 5397 | const DexFile::ClassDef& dex_class_def1, |
| 5398 | const DexFile& dex_file2, |
| 5399 | const DexFile::ClassDef& dex_class_def2) { |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5400 | std::ostringstream os; |
| 5401 | DumpClass(os, dex_file1, dex_class_def1, " (Compile time)"); |
| 5402 | DumpClass(os, dex_file2, dex_class_def2, " (Runtime)"); |
| 5403 | return os.str(); |
| 5404 | } |
| 5405 | |
| 5406 | |
| 5407 | // Very simple structural check on whether the classes match. Only compares the number of |
| 5408 | // methods and fields. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5409 | static bool SimpleStructuralCheck(const DexFile& dex_file1, |
| 5410 | const DexFile::ClassDef& dex_class_def1, |
| 5411 | const DexFile& dex_file2, |
| 5412 | const DexFile::ClassDef& dex_class_def2, |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5413 | std::string* error_msg) { |
| 5414 | ClassDataItemIterator dex_data1(dex_file1, dex_file1.GetClassData(dex_class_def1)); |
| 5415 | ClassDataItemIterator dex_data2(dex_file2, dex_file2.GetClassData(dex_class_def2)); |
| 5416 | |
| 5417 | // Counters for current dex file. |
| 5418 | 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] | 5419 | CountMethodsAndFields(dex_data1, |
| 5420 | &dex_virtual_methods1, |
| 5421 | &dex_direct_methods1, |
| 5422 | &dex_static_fields1, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5423 | &dex_instance_fields1); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5424 | // Counters for compile-time dex file. |
| 5425 | 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] | 5426 | CountMethodsAndFields(dex_data2, |
| 5427 | &dex_virtual_methods2, |
| 5428 | &dex_direct_methods2, |
| 5429 | &dex_static_fields2, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5430 | &dex_instance_fields2); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5431 | |
| 5432 | if (dex_virtual_methods1 != dex_virtual_methods2) { |
| 5433 | 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] | 5434 | *error_msg = StringPrintf("Virtual method count off: %zu vs %zu\n%s", |
| 5435 | dex_virtual_methods1, |
| 5436 | dex_virtual_methods2, |
| 5437 | class_dump.c_str()); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5438 | return false; |
| 5439 | } |
| 5440 | if (dex_direct_methods1 != dex_direct_methods2) { |
| 5441 | 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] | 5442 | *error_msg = StringPrintf("Direct method count off: %zu vs %zu\n%s", |
| 5443 | dex_direct_methods1, |
| 5444 | dex_direct_methods2, |
| 5445 | class_dump.c_str()); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5446 | return false; |
| 5447 | } |
| 5448 | if (dex_static_fields1 != dex_static_fields2) { |
| 5449 | 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] | 5450 | *error_msg = StringPrintf("Static field count off: %zu vs %zu\n%s", |
| 5451 | dex_static_fields1, |
| 5452 | dex_static_fields2, |
| 5453 | class_dump.c_str()); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5454 | return false; |
| 5455 | } |
| 5456 | if (dex_instance_fields1 != dex_instance_fields2) { |
| 5457 | 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] | 5458 | *error_msg = StringPrintf("Instance field count off: %zu vs %zu\n%s", |
| 5459 | dex_instance_fields1, |
| 5460 | dex_instance_fields2, |
| 5461 | class_dump.c_str()); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5462 | return false; |
| 5463 | } |
| 5464 | |
| 5465 | return true; |
| 5466 | } |
| 5467 | |
| 5468 | // Checks whether a the super-class changed from what we had at compile-time. This would |
| 5469 | // invalidate quickening. |
| 5470 | static bool CheckSuperClassChange(Handle<mirror::Class> klass, |
| 5471 | const DexFile& dex_file, |
| 5472 | const DexFile::ClassDef& class_def, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5473 | ObjPtr<mirror::Class> super_class) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5474 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5475 | // Check for unexpected changes in the superclass. |
| 5476 | // Quick check 1) is the super_class class-loader the boot class loader? This always has |
| 5477 | // precedence. |
| 5478 | if (super_class->GetClassLoader() != nullptr && |
| 5479 | // Quick check 2) different dex cache? Breaks can only occur for different dex files, |
| 5480 | // which is implied by different dex cache. |
| 5481 | klass->GetDexCache() != super_class->GetDexCache()) { |
| 5482 | // Now comes the expensive part: things can be broken if (a) the klass' dex file has a |
| 5483 | // definition for the super-class, and (b) the files are in separate oat files. The oat files |
| 5484 | // 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] | 5485 | const OatDexFile* class_oat_dex_file = dex_file.GetOatDexFile(); |
| 5486 | const OatFile* class_oat_file = nullptr; |
| 5487 | if (class_oat_dex_file != nullptr) { |
| 5488 | class_oat_file = class_oat_dex_file->GetOatFile(); |
| 5489 | } |
| 5490 | |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5491 | if (class_oat_file != nullptr) { |
Richard Uhler | 07b3c23 | 2015-03-31 15:57:54 -0700 | [diff] [blame] | 5492 | const OatDexFile* loaded_super_oat_dex_file = super_class->GetDexFile().GetOatDexFile(); |
| 5493 | const OatFile* loaded_super_oat_file = nullptr; |
| 5494 | if (loaded_super_oat_dex_file != nullptr) { |
| 5495 | loaded_super_oat_file = loaded_super_oat_dex_file->GetOatFile(); |
| 5496 | } |
| 5497 | |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5498 | if (loaded_super_oat_file != nullptr && class_oat_file != loaded_super_oat_file) { |
| 5499 | // Now check (a). |
| 5500 | const DexFile::ClassDef* super_class_def = dex_file.FindClassDef(class_def.superclass_idx_); |
| 5501 | if (super_class_def != nullptr) { |
| 5502 | // Uh-oh, we found something. Do our check. |
| 5503 | std::string error_msg; |
| 5504 | if (!SimpleStructuralCheck(dex_file, *super_class_def, |
| 5505 | super_class->GetDexFile(), *super_class->GetClassDef(), |
| 5506 | &error_msg)) { |
| 5507 | // Print a warning to the log. This exception might be caught, e.g., as common in test |
| 5508 | // drivers. When the class is later tried to be used, we re-throw a new instance, as we |
| 5509 | // only save the type of the exception. |
| 5510 | LOG(WARNING) << "Incompatible structural change detected: " << |
| 5511 | StringPrintf( |
| 5512 | "Structural change of %s is hazardous (%s at compile time, %s at runtime): %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5513 | dex_file.PrettyType(super_class_def->class_idx_).c_str(), |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5514 | class_oat_file->GetLocation().c_str(), |
| 5515 | loaded_super_oat_file->GetLocation().c_str(), |
| 5516 | error_msg.c_str()); |
| 5517 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 5518 | "Structural change of %s is hazardous (%s at compile time, %s at runtime): %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5519 | dex_file.PrettyType(super_class_def->class_idx_).c_str(), |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5520 | class_oat_file->GetLocation().c_str(), |
| 5521 | loaded_super_oat_file->GetLocation().c_str(), |
| 5522 | error_msg.c_str()); |
| 5523 | return false; |
| 5524 | } |
| 5525 | } |
| 5526 | } |
| 5527 | } |
| 5528 | } |
| 5529 | return true; |
| 5530 | } |
| 5531 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5532 | bool ClassLinker::LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5533 | CHECK_EQ(mirror::Class::kStatusIdx, klass->GetStatus()); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 5534 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5535 | dex::TypeIndex super_class_idx = class_def.superclass_idx_; |
| 5536 | if (super_class_idx.IsValid()) { |
Roland Levillain | 90328ac | 2016-05-18 12:25:38 +0100 | [diff] [blame] | 5537 | // Check that a class does not inherit from itself directly. |
| 5538 | // |
| 5539 | // TODO: This is a cheap check to detect the straightforward case |
| 5540 | // of a class extending itself (b/28685551), but we should do a |
| 5541 | // proper cycle detection on loaded classes, to detect all cases |
| 5542 | // of class circularity errors (b/28830038). |
| 5543 | if (super_class_idx == class_def.class_idx_) { |
| 5544 | ThrowClassCircularityError(klass.Get(), |
| 5545 | "Class %s extends itself", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5546 | klass->PrettyDescriptor().c_str()); |
Roland Levillain | 90328ac | 2016-05-18 12:25:38 +0100 | [diff] [blame] | 5547 | return false; |
| 5548 | } |
| 5549 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5550 | ObjPtr<mirror::Class> super_class = ResolveType(dex_file, super_class_idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5551 | if (super_class == nullptr) { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 5552 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5553 | return false; |
| 5554 | } |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 5555 | // Verify |
| 5556 | if (!klass->CanAccess(super_class)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5557 | ThrowIllegalAccessError(klass.Get(), "Class %s extended by class %s is inaccessible", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5558 | super_class->PrettyDescriptor().c_str(), |
| 5559 | klass->PrettyDescriptor().c_str()); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 5560 | return false; |
| 5561 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5562 | CHECK(super_class->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5563 | klass->SetSuperClass(super_class); |
Andreas Gampe | fd9eb39 | 2014-11-06 16:52:58 -0800 | [diff] [blame] | 5564 | |
| 5565 | if (!CheckSuperClassChange(klass, dex_file, class_def, super_class)) { |
| 5566 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5567 | return false; |
| 5568 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5569 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 5570 | const DexFile::TypeList* interfaces = dex_file.GetInterfacesList(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5571 | if (interfaces != nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5572 | for (size_t i = 0; i < interfaces->Size(); i++) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5573 | dex::TypeIndex idx = interfaces->GetTypeItem(i).type_idx_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5574 | ObjPtr<mirror::Class> interface = ResolveType(dex_file, idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5575 | if (interface == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5576 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5577 | return false; |
| 5578 | } |
| 5579 | // Verify |
| 5580 | if (!klass->CanAccess(interface)) { |
| 5581 | // TODO: the RI seemed to ignore this in my testing. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5582 | ThrowIllegalAccessError(klass.Get(), |
| 5583 | "Interface %s implemented by class %s is inaccessible", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5584 | interface->PrettyDescriptor().c_str(), |
| 5585 | klass->PrettyDescriptor().c_str()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5586 | return false; |
| 5587 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5588 | } |
| 5589 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 5590 | // Mark the class as loaded. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 5591 | mirror::Class::SetStatus(klass, mirror::Class::kStatusLoaded, nullptr); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5592 | return true; |
| 5593 | } |
| 5594 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5595 | bool ClassLinker::LinkSuperClass(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5596 | CHECK(!klass->IsPrimitive()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5597 | ObjPtr<mirror::Class> super = klass->GetSuperClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5598 | if (klass.Get() == GetClassRoot(kJavaLangObject)) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5599 | if (super != nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5600 | ThrowClassFormatError(klass.Get(), "java.lang.Object must not have a superclass"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5601 | return false; |
| 5602 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5603 | return true; |
| 5604 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5605 | if (super == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5606 | ThrowLinkageError(klass.Get(), "No superclass defined for class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5607 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5608 | return false; |
| 5609 | } |
| 5610 | // Verify |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 5611 | if (super->IsFinal() || super->IsInterface()) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5612 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 5613 | "Superclass %s of %s is %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5614 | super->PrettyDescriptor().c_str(), |
| 5615 | klass->PrettyDescriptor().c_str(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5616 | super->IsFinal() ? "declared final" : "an interface"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5617 | return false; |
| 5618 | } |
| 5619 | if (!klass->CanAccess(super)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5620 | ThrowIllegalAccessError(klass.Get(), "Superclass %s is inaccessible to class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5621 | super->PrettyDescriptor().c_str(), |
| 5622 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5623 | return false; |
| 5624 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 5625 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5626 | // Inherit kAccClassIsFinalizable from the superclass in case this |
| 5627 | // class doesn't override finalize. |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 5628 | if (super->IsFinalizable()) { |
| 5629 | klass->SetFinalizable(); |
| 5630 | } |
| 5631 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 5632 | // Inherit class loader flag form super class. |
| 5633 | if (super->IsClassLoaderClass()) { |
| 5634 | klass->SetClassLoaderClass(); |
| 5635 | } |
| 5636 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5637 | // Inherit reference flags (if any) from the superclass. |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 5638 | uint32_t reference_flags = (super->GetClassFlags() & mirror::kClassFlagReference); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5639 | if (reference_flags != 0) { |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 5640 | CHECK_EQ(klass->GetClassFlags(), 0u); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 5641 | klass->SetClassFlags(klass->GetClassFlags() | reference_flags); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5642 | } |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 5643 | // Disallow custom direct subclasses of java.lang.ref.Reference. |
Elliott Hughes | bf61ba3 | 2011-10-11 10:53:09 -0700 | [diff] [blame] | 5644 | if (init_done_ && super == GetClassRoot(kJavaLangRefReference)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5645 | ThrowLinkageError(klass.Get(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5646 | "Class %s attempts to subclass java.lang.ref.Reference, which is not allowed", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5647 | klass->PrettyDescriptor().c_str()); |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 5648 | return false; |
| 5649 | } |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5650 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5651 | if (kIsDebugBuild) { |
| 5652 | // Ensure super classes are fully resolved prior to resolving fields.. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5653 | while (super != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5654 | CHECK(super->IsResolved()); |
| 5655 | super = super->GetSuperClass(); |
| 5656 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5657 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5658 | return true; |
| 5659 | } |
| 5660 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5661 | // Populate the class vtable and itable. Compute return type indices. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5662 | bool ClassLinker::LinkMethods(Thread* self, |
| 5663 | Handle<mirror::Class> klass, |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5664 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5665 | bool* out_new_conflict, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5666 | ArtMethod** out_imt) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5667 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5668 | // A map from vtable indexes to the method they need to be updated to point to. Used because we |
| 5669 | // need to have default methods be in the virtuals array of each class but we don't set that up |
| 5670 | // until LinkInterfaceMethods. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5671 | std::unordered_map<size_t, ClassLinker::MethodTranslation> default_translations; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5672 | // Link virtual methods then interface methods. |
| 5673 | // We set up the interface lookup table first because we need it to determine if we need to update |
| 5674 | // any vtable entries with new default method implementations. |
| 5675 | return SetupInterfaceLookupTable(self, klass, interfaces) |
| 5676 | && LinkVirtualMethods(self, klass, /*out*/ &default_translations) |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5677 | && LinkInterfaceMethods(self, klass, default_translations, out_new_conflict, out_imt); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5678 | } |
| 5679 | |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5680 | // Comparator for name and signature of a method, used in finding overriding methods. Implementation |
| 5681 | // avoids the use of handles, if it didn't then rather than compare dex files we could compare dex |
| 5682 | // caches in the implementation below. |
| 5683 | class MethodNameAndSignatureComparator FINAL : public ValueObject { |
| 5684 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5685 | explicit MethodNameAndSignatureComparator(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5686 | REQUIRES_SHARED(Locks::mutator_lock_) : |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5687 | dex_file_(method->GetDexFile()), mid_(&dex_file_->GetMethodId(method->GetDexMethodIndex())), |
| 5688 | name_(nullptr), name_len_(0) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5689 | DCHECK(!method->IsProxyMethod()) << method->PrettyMethod(); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5690 | } |
| 5691 | |
| 5692 | const char* GetName() { |
| 5693 | if (name_ == nullptr) { |
| 5694 | name_ = dex_file_->StringDataAndUtf16LengthByIdx(mid_->name_idx_, &name_len_); |
| 5695 | } |
| 5696 | return name_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5697 | } |
| 5698 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5699 | bool HasSameNameAndSignature(ArtMethod* other) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5700 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5701 | DCHECK(!other->IsProxyMethod()) << other->PrettyMethod(); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5702 | const DexFile* other_dex_file = other->GetDexFile(); |
| 5703 | const DexFile::MethodId& other_mid = other_dex_file->GetMethodId(other->GetDexMethodIndex()); |
| 5704 | if (dex_file_ == other_dex_file) { |
| 5705 | return mid_->name_idx_ == other_mid.name_idx_ && mid_->proto_idx_ == other_mid.proto_idx_; |
| 5706 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5707 | GetName(); // Only used to make sure its calculated. |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5708 | uint32_t other_name_len; |
| 5709 | const char* other_name = other_dex_file->StringDataAndUtf16LengthByIdx(other_mid.name_idx_, |
| 5710 | &other_name_len); |
| 5711 | if (name_len_ != other_name_len || strcmp(name_, other_name) != 0) { |
| 5712 | return false; |
| 5713 | } |
| 5714 | return dex_file_->GetMethodSignature(*mid_) == other_dex_file->GetMethodSignature(other_mid); |
| 5715 | } |
| 5716 | |
| 5717 | private: |
| 5718 | // Dex file for the method to compare against. |
| 5719 | const DexFile* const dex_file_; |
| 5720 | // MethodId for the method to compare against. |
| 5721 | const DexFile::MethodId* const mid_; |
| 5722 | // Lazily computed name from the dex file's strings. |
| 5723 | const char* name_; |
| 5724 | // Lazily computed name length. |
| 5725 | uint32_t name_len_; |
| 5726 | }; |
| 5727 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5728 | class LinkVirtualHashTable { |
| 5729 | public: |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5730 | LinkVirtualHashTable(Handle<mirror::Class> klass, |
| 5731 | size_t hash_size, |
| 5732 | uint32_t* hash_table, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5733 | PointerSize image_pointer_size) |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5734 | : klass_(klass), |
| 5735 | hash_size_(hash_size), |
| 5736 | hash_table_(hash_table), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5737 | image_pointer_size_(image_pointer_size) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5738 | std::fill(hash_table_, hash_table_ + hash_size_, invalid_index_); |
| 5739 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5740 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5741 | void Add(uint32_t virtual_method_index) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5742 | ArtMethod* local_method = klass_->GetVirtualMethodDuringLinking( |
| 5743 | virtual_method_index, image_pointer_size_); |
| 5744 | const char* name = local_method->GetInterfaceMethodIfProxy(image_pointer_size_)->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 5745 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5746 | uint32_t index = hash % hash_size_; |
| 5747 | // Linear probe until we have an empty slot. |
| 5748 | while (hash_table_[index] != invalid_index_) { |
| 5749 | if (++index == hash_size_) { |
| 5750 | index = 0; |
| 5751 | } |
| 5752 | } |
| 5753 | hash_table_[index] = virtual_method_index; |
| 5754 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5755 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5756 | uint32_t FindAndRemove(MethodNameAndSignatureComparator* comparator) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5757 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5758 | const char* name = comparator->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 5759 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5760 | size_t index = hash % hash_size_; |
| 5761 | while (true) { |
| 5762 | const uint32_t value = hash_table_[index]; |
| 5763 | // Since linear probe makes continuous blocks, hitting an invalid index means we are done |
| 5764 | // the block and can safely assume not found. |
| 5765 | if (value == invalid_index_) { |
| 5766 | break; |
| 5767 | } |
| 5768 | if (value != removed_index_) { // This signifies not already overriden. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5769 | ArtMethod* virtual_method = |
| 5770 | klass_->GetVirtualMethodDuringLinking(value, image_pointer_size_); |
| 5771 | if (comparator->HasSameNameAndSignature( |
| 5772 | virtual_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5773 | hash_table_[index] = removed_index_; |
| 5774 | return value; |
| 5775 | } |
| 5776 | } |
| 5777 | if (++index == hash_size_) { |
| 5778 | index = 0; |
| 5779 | } |
| 5780 | } |
| 5781 | return GetNotFoundIndex(); |
| 5782 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5783 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5784 | static uint32_t GetNotFoundIndex() { |
| 5785 | return invalid_index_; |
| 5786 | } |
| 5787 | |
| 5788 | private: |
| 5789 | static const uint32_t invalid_index_; |
| 5790 | static const uint32_t removed_index_; |
| 5791 | |
| 5792 | Handle<mirror::Class> klass_; |
| 5793 | const size_t hash_size_; |
| 5794 | uint32_t* const hash_table_; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5795 | const PointerSize image_pointer_size_; |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5796 | }; |
| 5797 | |
| 5798 | const uint32_t LinkVirtualHashTable::invalid_index_ = std::numeric_limits<uint32_t>::max(); |
| 5799 | const uint32_t LinkVirtualHashTable::removed_index_ = std::numeric_limits<uint32_t>::max() - 1; |
| 5800 | |
Stephen Hines | 1ddd913 | 2017-02-08 01:51:18 -0800 | [diff] [blame] | 5801 | bool ClassLinker::LinkVirtualMethods( |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5802 | Thread* self, |
| 5803 | Handle<mirror::Class> klass, |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5804 | /*out*/std::unordered_map<size_t, ClassLinker::MethodTranslation>* default_translations) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5805 | const size_t num_virtual_methods = klass->NumVirtualMethods(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5806 | if (klass->IsInterface()) { |
| 5807 | // No vtable. |
| 5808 | if (!IsUint<16>(num_virtual_methods)) { |
| 5809 | ThrowClassFormatError(klass.Get(), "Too many methods on interface: %zu", num_virtual_methods); |
| 5810 | return false; |
| 5811 | } |
| 5812 | bool has_defaults = false; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5813 | // Assign each method an IMT index and set the default flag. |
| 5814 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
| 5815 | ArtMethod* m = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
| 5816 | m->SetMethodIndex(i); |
| 5817 | if (!m->IsAbstract()) { |
| 5818 | m->SetAccessFlags(m->GetAccessFlags() | kAccDefault); |
| 5819 | has_defaults = true; |
| 5820 | } |
| 5821 | } |
| 5822 | // Mark that we have default methods so that we won't need to scan the virtual_methods_ array |
| 5823 | // during initialization. This is a performance optimization. We could simply traverse the |
| 5824 | // virtual_methods_ array again during initialization. |
| 5825 | if (has_defaults) { |
| 5826 | klass->SetHasDefaultMethods(); |
| 5827 | } |
| 5828 | return true; |
| 5829 | } else if (klass->HasSuperClass()) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5830 | const size_t super_vtable_length = klass->GetSuperClass()->GetVTableLength(); |
| 5831 | const size_t max_count = num_virtual_methods + super_vtable_length; |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5832 | StackHandleScope<2> hs(self); |
Mingyao Yang | 38eecb0 | 2014-08-13 14:51:03 -0700 | [diff] [blame] | 5833 | Handle<mirror::Class> super_class(hs.NewHandle(klass->GetSuperClass())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5834 | MutableHandle<mirror::PointerArray> vtable; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5835 | if (super_class->ShouldHaveEmbeddedVTable()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5836 | vtable = hs.NewHandle(AllocPointerArray(self, max_count)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5837 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5838 | self->AssertPendingOOMException(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5839 | return false; |
| 5840 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5841 | for (size_t i = 0; i < super_vtable_length; i++) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5842 | vtable->SetElementPtrSize( |
| 5843 | i, super_class->GetEmbeddedVTableEntry(i, image_pointer_size_), image_pointer_size_); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5844 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5845 | // We might need to change vtable if we have new virtual methods or new interfaces (since that |
| 5846 | // might give us new default methods). If no new interfaces then we can skip the rest since |
| 5847 | // the class cannot override any of the super-class's methods. This is required for |
| 5848 | // correctness since without it we might not update overridden default method vtable entries |
| 5849 | // correctly. |
| 5850 | if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5851 | klass->SetVTable(vtable.Get()); |
| 5852 | return true; |
| 5853 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5854 | } else { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5855 | DCHECK(super_class->IsAbstract() && !super_class->IsArrayClass()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5856 | auto* super_vtable = super_class->GetVTable(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5857 | CHECK(super_vtable != nullptr) << super_class->PrettyClass(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5858 | // We might need to change vtable if we have new virtual methods or new interfaces (since that |
| 5859 | // might give us new default methods). See comment above. |
| 5860 | if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5861 | klass->SetVTable(super_vtable); |
| 5862 | return true; |
| 5863 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5864 | vtable = hs.NewHandle(down_cast<mirror::PointerArray*>( |
| 5865 | super_vtable->CopyOf(self, max_count))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5866 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5867 | self->AssertPendingOOMException(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5868 | return false; |
| 5869 | } |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5870 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5871 | // How the algorithm works: |
| 5872 | // 1. Populate hash table by adding num_virtual_methods from klass. The values in the hash |
| 5873 | // table are: invalid_index for unused slots, index super_vtable_length + i for a virtual |
| 5874 | // method which has not been matched to a vtable method, and j if the virtual method at the |
| 5875 | // index overrode the super virtual method at index j. |
| 5876 | // 2. Loop through super virtual methods, if they overwrite, update hash table to j |
| 5877 | // (j < super_vtable_length) to avoid redundant checks. (TODO maybe use this info for reducing |
| 5878 | // the need for the initial vtable which we later shrink back down). |
| 5879 | // 3. Add non overridden methods to the end of the vtable. |
| 5880 | static constexpr size_t kMaxStackHash = 250; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5881 | // + 1 so that even if we only have new default methods we will still be able to use this hash |
| 5882 | // table (i.e. it will never have 0 size). |
| 5883 | const size_t hash_table_size = num_virtual_methods * 3 + 1; |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5884 | uint32_t* hash_table_ptr; |
| 5885 | std::unique_ptr<uint32_t[]> hash_heap_storage; |
| 5886 | if (hash_table_size <= kMaxStackHash) { |
| 5887 | hash_table_ptr = reinterpret_cast<uint32_t*>( |
| 5888 | alloca(hash_table_size * sizeof(*hash_table_ptr))); |
| 5889 | } else { |
| 5890 | hash_heap_storage.reset(new uint32_t[hash_table_size]); |
| 5891 | hash_table_ptr = hash_heap_storage.get(); |
| 5892 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5893 | 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] | 5894 | // Add virtual methods to the hash table. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5895 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5896 | DCHECK(klass->GetVirtualMethodDuringLinking( |
| 5897 | i, image_pointer_size_)->GetDeclaringClass() != nullptr); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5898 | hash_table.Add(i); |
| 5899 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5900 | // 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] | 5901 | // the hash table. |
| 5902 | for (size_t j = 0; j < super_vtable_length; ++j) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5903 | // 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] | 5904 | ArtMethod* super_method = vtable->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 5905 | if (!klass->CanAccessMember(super_method->GetDeclaringClass(), |
| 5906 | super_method->GetAccessFlags())) { |
| 5907 | // Continue on to the next method since this one is package private and canot be overridden. |
| 5908 | // Before Android 4.1, the package-private method super_method might have been incorrectly |
| 5909 | // overridden. |
| 5910 | continue; |
| 5911 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5912 | MethodNameAndSignatureComparator super_method_name_comparator( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5913 | super_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 5914 | // We remove the method so that subsequent lookups will be faster by making the hash-map |
| 5915 | // smaller as we go on. |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5916 | uint32_t hash_index = hash_table.FindAndRemove(&super_method_name_comparator); |
| 5917 | if (hash_index != hash_table.GetNotFoundIndex()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5918 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking( |
| 5919 | hash_index, image_pointer_size_); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 5920 | if (super_method->IsFinal()) { |
| 5921 | ThrowLinkageError(klass.Get(), "Method %s overrides final method in class %s", |
| 5922 | virtual_method->PrettyMethod().c_str(), |
| 5923 | super_method->GetDeclaringClassDescriptor()); |
| 5924 | return false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5925 | } |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 5926 | vtable->SetElementPtrSize(j, virtual_method, image_pointer_size_); |
| 5927 | virtual_method->SetMethodIndex(j); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5928 | } else if (super_method->IsOverridableByDefaultMethod()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5929 | // We didn't directly override this method but we might through default methods... |
| 5930 | // Check for default method update. |
| 5931 | ArtMethod* default_method = nullptr; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5932 | switch (FindDefaultMethodImplementation(self, |
| 5933 | super_method, |
| 5934 | klass, |
| 5935 | /*out*/&default_method)) { |
| 5936 | case DefaultMethodSearchResult::kDefaultConflict: { |
| 5937 | // A conflict was found looking for default methods. Note this (assuming it wasn't |
| 5938 | // pre-existing) in the translations map. |
| 5939 | if (UNLIKELY(!super_method->IsDefaultConflicting())) { |
| 5940 | // Don't generate another conflict method to reduce memory use as an optimization. |
| 5941 | default_translations->insert( |
| 5942 | {j, ClassLinker::MethodTranslation::CreateConflictingMethod()}); |
| 5943 | } |
| 5944 | break; |
| 5945 | } |
| 5946 | case DefaultMethodSearchResult::kAbstractFound: { |
| 5947 | // No conflict but method is abstract. |
| 5948 | // We note that this vtable entry must be made abstract. |
| 5949 | if (UNLIKELY(!super_method->IsAbstract())) { |
| 5950 | default_translations->insert( |
| 5951 | {j, ClassLinker::MethodTranslation::CreateAbstractMethod()}); |
| 5952 | } |
| 5953 | break; |
| 5954 | } |
| 5955 | case DefaultMethodSearchResult::kDefaultFound: { |
| 5956 | if (UNLIKELY(super_method->IsDefaultConflicting() || |
| 5957 | default_method->GetDeclaringClass() != super_method->GetDeclaringClass())) { |
| 5958 | // Found a default method implementation that is new. |
| 5959 | // TODO Refactor this add default methods to virtuals here and not in |
| 5960 | // LinkInterfaceMethods maybe. |
| 5961 | // The problem is default methods might override previously present |
| 5962 | // default-method or miranda-method vtable entries from the superclass. |
| 5963 | // Unfortunately we need these to be entries in this class's virtuals. We do not |
| 5964 | // give these entries there until LinkInterfaceMethods so we pass this map around |
| 5965 | // to let it know which vtable entries need to be updated. |
| 5966 | // Make a note that vtable entry j must be updated, store what it needs to be updated |
| 5967 | // to. We will allocate a virtual method slot in LinkInterfaceMethods and fix it up |
| 5968 | // then. |
| 5969 | default_translations->insert( |
| 5970 | {j, ClassLinker::MethodTranslation::CreateTranslatedMethod(default_method)}); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5971 | VLOG(class_linker) << "Method " << super_method->PrettyMethod() |
| 5972 | << " overridden by default " |
| 5973 | << default_method->PrettyMethod() |
| 5974 | << " in " << mirror::Class::PrettyClass(klass.Get()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5975 | } |
| 5976 | break; |
| 5977 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5978 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5979 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5980 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5981 | size_t actual_count = super_vtable_length; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5982 | // Add the non-overridden methods at the end. |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5983 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5984 | ArtMethod* local_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5985 | size_t method_idx = local_method->GetMethodIndexDuringLinking(); |
| 5986 | if (method_idx < super_vtable_length && |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5987 | local_method == vtable->GetElementPtrSize<ArtMethod*>(method_idx, image_pointer_size_)) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5988 | continue; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5989 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5990 | vtable->SetElementPtrSize(actual_count, local_method, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5991 | local_method->SetMethodIndex(actual_count); |
| 5992 | ++actual_count; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5993 | } |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 5994 | if (!IsUint<16>(actual_count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5995 | ThrowClassFormatError(klass.Get(), "Too many methods defined on class: %zd", actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5996 | return false; |
| 5997 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5998 | // Shrink vtable if possible |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5999 | CHECK_LE(actual_count, max_count); |
| 6000 | if (actual_count < max_count) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6001 | vtable.Assign(down_cast<mirror::PointerArray*>(vtable->CopyOf(self, actual_count))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6002 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6003 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6004 | return false; |
| 6005 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6006 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6007 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6008 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 6009 | CHECK_EQ(klass.Get(), GetClassRoot(kJavaLangObject)); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 6010 | if (!IsUint<16>(num_virtual_methods)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6011 | ThrowClassFormatError(klass.Get(), "Too many methods: %d", |
| 6012 | static_cast<int>(num_virtual_methods)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6013 | return false; |
| 6014 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6015 | auto* vtable = AllocPointerArray(self, num_virtual_methods); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6016 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6017 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6018 | return false; |
| 6019 | } |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 6020 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6021 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
| 6022 | vtable->SetElementPtrSize(i, virtual_method, image_pointer_size_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6023 | virtual_method->SetMethodIndex(i & 0xFFFF); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6024 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6025 | klass->SetVTable(vtable); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6026 | } |
| 6027 | return true; |
| 6028 | } |
| 6029 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6030 | // Determine if the given iface has any subinterface in the given list that declares the method |
| 6031 | // specified by 'target'. |
| 6032 | // |
| 6033 | // Arguments |
| 6034 | // - self: The thread we are running on |
| 6035 | // - target: A comparator that will match any method that overrides the method we are checking for |
| 6036 | // - iftable: The iftable we are searching for an overriding method on. |
| 6037 | // - ifstart: The index of the interface we are checking to see if anything overrides |
| 6038 | // - iface: The interface we are checking to see if anything overrides. |
| 6039 | // - image_pointer_size: |
| 6040 | // The image pointer size. |
| 6041 | // |
| 6042 | // Returns |
| 6043 | // - True: There is some method that matches the target comparator defined in an interface that |
| 6044 | // is a subtype of iface. |
| 6045 | // - False: There is no method that matches the target comparator in any interface that is a subtype |
| 6046 | // of iface. |
| 6047 | static bool ContainsOverridingMethodOf(Thread* self, |
| 6048 | MethodNameAndSignatureComparator& target, |
| 6049 | Handle<mirror::IfTable> iftable, |
| 6050 | size_t ifstart, |
| 6051 | Handle<mirror::Class> iface, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6052 | PointerSize image_pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6053 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6054 | DCHECK(self != nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6055 | DCHECK(iface != nullptr); |
| 6056 | DCHECK(iftable != nullptr); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6057 | DCHECK_GE(ifstart, 0u); |
| 6058 | DCHECK_LT(ifstart, iftable->Count()); |
| 6059 | DCHECK_EQ(iface.Get(), iftable->GetInterface(ifstart)); |
| 6060 | DCHECK(iface->IsInterface()); |
| 6061 | |
| 6062 | size_t iftable_count = iftable->Count(); |
| 6063 | StackHandleScope<1> hs(self); |
| 6064 | MutableHandle<mirror::Class> current_iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 6065 | for (size_t k = ifstart + 1; k < iftable_count; k++) { |
| 6066 | // Skip ifstart since our current interface obviously cannot override itself. |
| 6067 | current_iface.Assign(iftable->GetInterface(k)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6068 | // Iterate through every method on this interface. The order does not matter. |
| 6069 | for (ArtMethod& current_method : current_iface->GetDeclaredVirtualMethods(image_pointer_size)) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6070 | if (UNLIKELY(target.HasSameNameAndSignature( |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6071 | current_method.GetInterfaceMethodIfProxy(image_pointer_size)))) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6072 | // Check if the i'th interface is a subtype of this one. |
| 6073 | if (iface->IsAssignableFrom(current_iface.Get())) { |
| 6074 | return true; |
| 6075 | } |
| 6076 | break; |
| 6077 | } |
| 6078 | } |
| 6079 | } |
| 6080 | return false; |
| 6081 | } |
| 6082 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6083 | // 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] | 6084 | // out_default_method and returns kDefaultFound on success. If no default method was found return |
| 6085 | // kAbstractFound and store nullptr into out_default_method. If an error occurs (such as a |
| 6086 | // default_method conflict) it will return kDefaultConflict. |
| 6087 | ClassLinker::DefaultMethodSearchResult ClassLinker::FindDefaultMethodImplementation( |
| 6088 | Thread* self, |
| 6089 | ArtMethod* target_method, |
| 6090 | Handle<mirror::Class> klass, |
| 6091 | /*out*/ArtMethod** out_default_method) const { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6092 | DCHECK(self != nullptr); |
| 6093 | DCHECK(target_method != nullptr); |
| 6094 | DCHECK(out_default_method != nullptr); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6095 | |
| 6096 | *out_default_method = nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6097 | |
| 6098 | // We organize the interface table so that, for interface I any subinterfaces J follow it in the |
| 6099 | // table. This lets us walk the table backwards when searching for default methods. The first one |
| 6100 | // we encounter is the best candidate since it is the most specific. Once we have found it we keep |
| 6101 | // track of it and then continue checking all other interfaces, since we need to throw an error if |
| 6102 | // we encounter conflicting default method implementations (one is not a subtype of the other). |
| 6103 | // |
| 6104 | // The order of unrelated interfaces does not matter and is not defined. |
| 6105 | size_t iftable_count = klass->GetIfTableCount(); |
| 6106 | if (iftable_count == 0) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6107 | // No interfaces. We have already reset out to null so just return kAbstractFound. |
| 6108 | return DefaultMethodSearchResult::kAbstractFound; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6109 | } |
| 6110 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6111 | StackHandleScope<3> hs(self); |
| 6112 | MutableHandle<mirror::Class> chosen_iface(hs.NewHandle<mirror::Class>(nullptr)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6113 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable())); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6114 | MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6115 | MethodNameAndSignatureComparator target_name_comparator( |
| 6116 | target_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
| 6117 | // Iterates over the klass's iftable in reverse |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6118 | for (size_t k = iftable_count; k != 0; ) { |
| 6119 | --k; |
| 6120 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6121 | DCHECK_LT(k, iftable->Count()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6122 | |
| 6123 | iface.Assign(iftable->GetInterface(k)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6124 | // Iterate through every declared method on this interface. The order does not matter. |
| 6125 | for (auto& method_iter : iface->GetDeclaredVirtualMethods(image_pointer_size_)) { |
| 6126 | ArtMethod* current_method = &method_iter; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6127 | // Skip abstract methods and methods with different names. |
| 6128 | if (current_method->IsAbstract() || |
| 6129 | !target_name_comparator.HasSameNameAndSignature( |
| 6130 | current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
| 6131 | continue; |
Alex Light | d7c10c2 | 2016-03-31 10:03:07 -0700 | [diff] [blame] | 6132 | } else if (!current_method->IsPublic()) { |
| 6133 | // The verifier should have caught the non-public method for dex version 37. Just warn and |
| 6134 | // skip it since this is from before default-methods so we don't really need to care that it |
| 6135 | // has code. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6136 | LOG(WARNING) << "Interface method " << current_method->PrettyMethod() |
| 6137 | << " is not public! " |
Alex Light | d7c10c2 | 2016-03-31 10:03:07 -0700 | [diff] [blame] | 6138 | << "This will be a fatal error in subsequent versions of android. " |
| 6139 | << "Continuing anyway."; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6140 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6141 | if (UNLIKELY(chosen_iface != nullptr)) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6142 | // We have multiple default impls of the same method. This is a potential default conflict. |
| 6143 | // We need to check if this possibly conflicting method is either a superclass of the chosen |
| 6144 | // default implementation or is overridden by a non-default interface method. In either case |
| 6145 | // there is no conflict. |
| 6146 | if (!iface->IsAssignableFrom(chosen_iface.Get()) && |
| 6147 | !ContainsOverridingMethodOf(self, |
| 6148 | target_name_comparator, |
| 6149 | iftable, |
| 6150 | k, |
| 6151 | iface, |
| 6152 | image_pointer_size_)) { |
Nicolas Geoffray | 7f3e0db | 2016-01-28 09:29:31 +0000 | [diff] [blame] | 6153 | VLOG(class_linker) << "Conflicting default method implementations found: " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6154 | << current_method->PrettyMethod() << " and " |
| 6155 | << ArtMethod::PrettyMethod(*out_default_method) << " in class " |
| 6156 | << klass->PrettyClass() << " conflict."; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6157 | *out_default_method = nullptr; |
| 6158 | return DefaultMethodSearchResult::kDefaultConflict; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6159 | } else { |
| 6160 | break; // Continue checking at the next interface. |
| 6161 | } |
| 6162 | } else { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6163 | // chosen_iface == null |
| 6164 | if (!ContainsOverridingMethodOf(self, |
| 6165 | target_name_comparator, |
| 6166 | iftable, |
| 6167 | k, |
| 6168 | iface, |
| 6169 | image_pointer_size_)) { |
| 6170 | // Don't set this as the chosen interface if something else is overriding it (because that |
| 6171 | // other interface would be potentially chosen instead if it was default). If the other |
| 6172 | // interface was abstract then we wouldn't select this interface as chosen anyway since |
| 6173 | // the abstract method masks it. |
| 6174 | *out_default_method = current_method; |
| 6175 | chosen_iface.Assign(iface.Get()); |
| 6176 | // We should now finish traversing the graph to find if we have default methods that |
| 6177 | // conflict. |
| 6178 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6179 | VLOG(class_linker) << "A default method '" << current_method->PrettyMethod() |
| 6180 | << "' was " |
| 6181 | << "skipped because it was overridden by an abstract method in a " |
| 6182 | << "subinterface on class '" << klass->PrettyClass() << "'"; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6183 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6184 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6185 | break; |
| 6186 | } |
| 6187 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6188 | if (*out_default_method != nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6189 | VLOG(class_linker) << "Default method '" << (*out_default_method)->PrettyMethod() |
| 6190 | << "' selected " |
| 6191 | << "as the implementation for '" << target_method->PrettyMethod() |
| 6192 | << "' in '" << klass->PrettyClass() << "'"; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6193 | return DefaultMethodSearchResult::kDefaultFound; |
| 6194 | } else { |
| 6195 | return DefaultMethodSearchResult::kAbstractFound; |
| 6196 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6197 | } |
| 6198 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6199 | ArtMethod* ClassLinker::AddMethodToConflictTable(ObjPtr<mirror::Class> klass, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6200 | ArtMethod* conflict_method, |
| 6201 | ArtMethod* interface_method, |
| 6202 | ArtMethod* method, |
| 6203 | bool force_new_conflict_method) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6204 | ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6205 | Runtime* const runtime = Runtime::Current(); |
| 6206 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
| 6207 | bool new_entry = conflict_method == runtime->GetImtConflictMethod() || force_new_conflict_method; |
| 6208 | |
| 6209 | // Create a new entry if the existing one is the shared conflict method. |
| 6210 | ArtMethod* new_conflict_method = new_entry |
| 6211 | ? runtime->CreateImtConflictMethod(linear_alloc) |
| 6212 | : conflict_method; |
| 6213 | |
| 6214 | // Allocate a new table. Note that we will leak this table at the next conflict, |
| 6215 | // but that's a tradeoff compared to making the table fixed size. |
| 6216 | void* data = linear_alloc->Alloc( |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6217 | Thread::Current(), ImtConflictTable::ComputeSizeWithOneMoreEntry(current_table, |
| 6218 | image_pointer_size_)); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6219 | if (data == nullptr) { |
| 6220 | LOG(ERROR) << "Failed to allocate conflict table"; |
| 6221 | return conflict_method; |
| 6222 | } |
| 6223 | ImtConflictTable* new_table = new (data) ImtConflictTable(current_table, |
| 6224 | interface_method, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6225 | method, |
| 6226 | image_pointer_size_); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6227 | |
| 6228 | // Do a fence to ensure threads see the data in the table before it is assigned |
| 6229 | // to the conflict method. |
| 6230 | // Note that there is a race in the presence of multiple threads and we may leak |
| 6231 | // memory from the LinearAlloc, but that's a tradeoff compared to using |
| 6232 | // atomic operations. |
| 6233 | QuasiAtomic::ThreadFenceRelease(); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6234 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6235 | return new_conflict_method; |
| 6236 | } |
| 6237 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6238 | bool ClassLinker::AllocateIfTableMethodArrays(Thread* self, |
| 6239 | Handle<mirror::Class> klass, |
| 6240 | Handle<mirror::IfTable> iftable) { |
| 6241 | DCHECK(!klass->IsInterface()); |
| 6242 | const bool has_superclass = klass->HasSuperClass(); |
| 6243 | const bool extend_super_iftable = has_superclass; |
| 6244 | const size_t ifcount = klass->GetIfTableCount(); |
| 6245 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
| 6246 | for (size_t i = 0; i < ifcount; ++i) { |
| 6247 | size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods(); |
| 6248 | if (num_methods > 0) { |
| 6249 | const bool is_super = i < super_ifcount; |
| 6250 | // This is an interface implemented by a super-class. Therefore we can just copy the method |
| 6251 | // array from the superclass. |
| 6252 | const bool super_interface = is_super && extend_super_iftable; |
| 6253 | ObjPtr<mirror::PointerArray> method_array; |
| 6254 | if (super_interface) { |
| 6255 | ObjPtr<mirror::IfTable> if_table = klass->GetSuperClass()->GetIfTable(); |
| 6256 | DCHECK(if_table != nullptr); |
| 6257 | DCHECK(if_table->GetMethodArray(i) != nullptr); |
| 6258 | // If we are working on a super interface, try extending the existing method array. |
| 6259 | method_array = down_cast<mirror::PointerArray*>(if_table->GetMethodArray(i)->Clone(self)); |
| 6260 | } else { |
| 6261 | method_array = AllocPointerArray(self, num_methods); |
| 6262 | } |
| 6263 | if (UNLIKELY(method_array == nullptr)) { |
| 6264 | self->AssertPendingOOMException(); |
| 6265 | return false; |
| 6266 | } |
| 6267 | iftable->SetMethodArray(i, method_array); |
| 6268 | } |
| 6269 | } |
| 6270 | return true; |
| 6271 | } |
| 6272 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6273 | void ClassLinker::SetIMTRef(ArtMethod* unimplemented_method, |
| 6274 | ArtMethod* imt_conflict_method, |
| 6275 | ArtMethod* current_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6276 | /*out*/bool* new_conflict, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6277 | /*out*/ArtMethod** imt_ref) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6278 | // Place method in imt if entry is empty, place conflict otherwise. |
| 6279 | if (*imt_ref == unimplemented_method) { |
| 6280 | *imt_ref = current_method; |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6281 | } else if (!(*imt_ref)->IsRuntimeMethod()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6282 | // If we are not a conflict and we have the same signature and name as the imt |
| 6283 | // entry, it must be that we overwrote a superclass vtable entry. |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6284 | // Note that we have checked IsRuntimeMethod, as there may be multiple different |
| 6285 | // conflict methods. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6286 | MethodNameAndSignatureComparator imt_comparator( |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6287 | (*imt_ref)->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6288 | if (imt_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6289 | current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6290 | *imt_ref = current_method; |
| 6291 | } else { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6292 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6293 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6294 | } |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6295 | } else { |
| 6296 | // Place the default conflict method. Note that there may be an existing conflict |
| 6297 | // method in the IMT, but it could be one tailored to the super class, with a |
| 6298 | // specific ImtConflictTable. |
| 6299 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6300 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6301 | } |
| 6302 | } |
| 6303 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6304 | void ClassLinker::FillIMTAndConflictTables(ObjPtr<mirror::Class> klass) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6305 | DCHECK(klass->ShouldHaveImt()) << klass->PrettyClass(); |
| 6306 | DCHECK(!klass->IsTemp()) << klass->PrettyClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6307 | ArtMethod* imt_data[ImTable::kSize]; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6308 | Runtime* const runtime = Runtime::Current(); |
| 6309 | ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod(); |
| 6310 | ArtMethod* const conflict_method = runtime->GetImtConflictMethod(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6311 | std::fill_n(imt_data, arraysize(imt_data), unimplemented_method); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6312 | if (klass->GetIfTable() != nullptr) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6313 | bool new_conflict = false; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6314 | FillIMTFromIfTable(klass->GetIfTable(), |
| 6315 | unimplemented_method, |
| 6316 | conflict_method, |
| 6317 | klass, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6318 | /*create_conflict_tables*/true, |
| 6319 | /*ignore_copied_methods*/false, |
| 6320 | &new_conflict, |
| 6321 | &imt_data[0]); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6322 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6323 | if (!klass->ShouldHaveImt()) { |
| 6324 | return; |
| 6325 | } |
| 6326 | // Compare the IMT with the super class including the conflict methods. If they are equivalent, |
| 6327 | // we can just use the same pointer. |
| 6328 | ImTable* imt = nullptr; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6329 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6330 | if (super_class != nullptr && super_class->ShouldHaveImt()) { |
| 6331 | ImTable* super_imt = super_class->GetImt(image_pointer_size_); |
| 6332 | bool same = true; |
| 6333 | for (size_t i = 0; same && i < ImTable::kSize; ++i) { |
| 6334 | ArtMethod* method = imt_data[i]; |
| 6335 | ArtMethod* super_method = super_imt->Get(i, image_pointer_size_); |
| 6336 | if (method != super_method) { |
| 6337 | bool is_conflict_table = method->IsRuntimeMethod() && |
| 6338 | method != unimplemented_method && |
| 6339 | method != conflict_method; |
| 6340 | // Verify conflict contents. |
| 6341 | bool super_conflict_table = super_method->IsRuntimeMethod() && |
| 6342 | super_method != unimplemented_method && |
| 6343 | super_method != conflict_method; |
| 6344 | if (!is_conflict_table || !super_conflict_table) { |
| 6345 | same = false; |
| 6346 | } else { |
| 6347 | ImtConflictTable* table1 = method->GetImtConflictTable(image_pointer_size_); |
| 6348 | ImtConflictTable* table2 = super_method->GetImtConflictTable(image_pointer_size_); |
| 6349 | same = same && table1->Equals(table2, image_pointer_size_); |
| 6350 | } |
| 6351 | } |
| 6352 | } |
| 6353 | if (same) { |
| 6354 | imt = super_imt; |
| 6355 | } |
| 6356 | } |
| 6357 | if (imt == nullptr) { |
| 6358 | imt = klass->GetImt(image_pointer_size_); |
| 6359 | DCHECK(imt != nullptr); |
| 6360 | imt->Populate(imt_data, image_pointer_size_); |
| 6361 | } else { |
| 6362 | klass->SetImt(imt, image_pointer_size_); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6363 | } |
| 6364 | } |
| 6365 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6366 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, |
| 6367 | LinearAlloc* linear_alloc, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6368 | PointerSize image_pointer_size) { |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6369 | void* data = linear_alloc->Alloc(Thread::Current(), |
| 6370 | ImtConflictTable::ComputeSize(count, |
| 6371 | image_pointer_size)); |
| 6372 | return (data != nullptr) ? new (data) ImtConflictTable(count, image_pointer_size) : nullptr; |
| 6373 | } |
| 6374 | |
| 6375 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, LinearAlloc* linear_alloc) { |
| 6376 | return CreateImtConflictTable(count, linear_alloc, image_pointer_size_); |
| 6377 | } |
| 6378 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6379 | void ClassLinker::FillIMTFromIfTable(ObjPtr<mirror::IfTable> if_table, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6380 | ArtMethod* unimplemented_method, |
| 6381 | ArtMethod* imt_conflict_method, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6382 | ObjPtr<mirror::Class> klass, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6383 | bool create_conflict_tables, |
| 6384 | bool ignore_copied_methods, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6385 | /*out*/bool* new_conflict, |
| 6386 | /*out*/ArtMethod** imt) { |
| 6387 | uint32_t conflict_counts[ImTable::kSize] = {}; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6388 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6389 | ObjPtr<mirror::Class> interface = if_table->GetInterface(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6390 | const size_t num_virtuals = interface->NumVirtualMethods(); |
| 6391 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 6392 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 6393 | DCHECK_GE(num_virtuals, method_array_count); |
| 6394 | if (kIsDebugBuild) { |
| 6395 | if (klass->IsInterface()) { |
| 6396 | DCHECK_EQ(method_array_count, 0u); |
| 6397 | } else { |
| 6398 | DCHECK_EQ(interface->NumDeclaredVirtualMethods(), method_array_count); |
| 6399 | } |
| 6400 | } |
| 6401 | if (method_array_count == 0) { |
| 6402 | continue; |
| 6403 | } |
| 6404 | auto* method_array = if_table->GetMethodArray(i); |
| 6405 | for (size_t j = 0; j < method_array_count; ++j) { |
| 6406 | ArtMethod* implementation_method = |
| 6407 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 6408 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 6409 | continue; |
| 6410 | } |
| 6411 | DCHECK(implementation_method != nullptr); |
| 6412 | // Miranda methods cannot be used to implement an interface method, but they are safe to put |
| 6413 | // in the IMT since their entrypoint is the interface trampoline. If we put any copied methods |
| 6414 | // or interface methods in the IMT here they will not create extra conflicts since we compare |
| 6415 | // names and signatures in SetIMTRef. |
| 6416 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 6417 | const uint32_t imt_index = ImTable::GetImtIndex(interface_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6418 | |
| 6419 | // There is only any conflicts if all of the interface methods for an IMT slot don't have |
| 6420 | // the same implementation method, keep track of this to avoid creating a conflict table in |
| 6421 | // this case. |
| 6422 | |
| 6423 | // Conflict table size for each IMT slot. |
| 6424 | ++conflict_counts[imt_index]; |
| 6425 | |
| 6426 | SetIMTRef(unimplemented_method, |
| 6427 | imt_conflict_method, |
| 6428 | implementation_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6429 | /*out*/new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6430 | /*out*/&imt[imt_index]); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6431 | } |
| 6432 | } |
| 6433 | |
| 6434 | if (create_conflict_tables) { |
| 6435 | // Create the conflict tables. |
| 6436 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6437 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6438 | size_t conflicts = conflict_counts[i]; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6439 | if (imt[i] == imt_conflict_method) { |
| 6440 | ImtConflictTable* new_table = CreateImtConflictTable(conflicts, linear_alloc); |
| 6441 | if (new_table != nullptr) { |
| 6442 | ArtMethod* new_conflict_method = |
| 6443 | Runtime::Current()->CreateImtConflictMethod(linear_alloc); |
| 6444 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
| 6445 | imt[i] = new_conflict_method; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6446 | } else { |
| 6447 | LOG(ERROR) << "Failed to allocate conflict table"; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6448 | imt[i] = imt_conflict_method; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6449 | } |
| 6450 | } else { |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6451 | DCHECK_NE(imt[i], imt_conflict_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6452 | } |
| 6453 | } |
| 6454 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6455 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6456 | ObjPtr<mirror::Class> interface = if_table->GetInterface(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6457 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 6458 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 6459 | if (method_array_count == 0) { |
| 6460 | continue; |
| 6461 | } |
| 6462 | auto* method_array = if_table->GetMethodArray(i); |
| 6463 | for (size_t j = 0; j < method_array_count; ++j) { |
| 6464 | ArtMethod* implementation_method = |
| 6465 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 6466 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 6467 | continue; |
| 6468 | } |
| 6469 | DCHECK(implementation_method != nullptr); |
| 6470 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 6471 | const uint32_t imt_index = ImTable::GetImtIndex(interface_method); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6472 | if (!imt[imt_index]->IsRuntimeMethod() || |
| 6473 | imt[imt_index] == unimplemented_method || |
| 6474 | imt[imt_index] == imt_conflict_method) { |
| 6475 | continue; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6476 | } |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6477 | ImtConflictTable* table = imt[imt_index]->GetImtConflictTable(image_pointer_size_); |
| 6478 | const size_t num_entries = table->NumEntries(image_pointer_size_); |
| 6479 | table->SetInterfaceMethod(num_entries, image_pointer_size_, interface_method); |
| 6480 | table->SetImplementationMethod(num_entries, image_pointer_size_, implementation_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6481 | } |
| 6482 | } |
| 6483 | } |
| 6484 | } |
| 6485 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6486 | // Simple helper function that checks that no subtypes of 'val' are contained within the 'classes' |
| 6487 | // set. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6488 | static bool NotSubinterfaceOfAny( |
| 6489 | const std::unordered_set<ObjPtr<mirror::Class>, HashObjPtr>& classes, |
| 6490 | ObjPtr<mirror::Class> val) |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6491 | REQUIRES(Roles::uninterruptible_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6492 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6493 | DCHECK(val != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6494 | for (ObjPtr<mirror::Class> c : classes) { |
| 6495 | if (val->IsAssignableFrom(c)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6496 | return false; |
| 6497 | } |
| 6498 | } |
| 6499 | return true; |
| 6500 | } |
| 6501 | |
| 6502 | // Fills in and flattens the interface inheritance hierarchy. |
| 6503 | // |
| 6504 | // By the end of this function all interfaces in the transitive closure of to_process are added to |
| 6505 | // the iftable and every interface precedes all of its sub-interfaces in this list. |
| 6506 | // |
| 6507 | // all I, J: Interface | I <: J implies J precedes I |
| 6508 | // |
| 6509 | // (note A <: B means that A is a subtype of B) |
| 6510 | // |
| 6511 | // This returns the total number of items in the iftable. The iftable might be resized down after |
| 6512 | // this call. |
| 6513 | // |
| 6514 | // We order this backwards so that we do not need to reorder superclass interfaces when new |
| 6515 | // interfaces are added in subclass's interface tables. |
| 6516 | // |
| 6517 | // Upon entry into this function iftable is a copy of the superclass's iftable with the first |
| 6518 | // super_ifcount entries filled in with the transitive closure of the interfaces of the superclass. |
| 6519 | // The other entries are uninitialized. We will fill in the remaining entries in this function. The |
| 6520 | // iftable must be large enough to hold all interfaces without changing its size. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6521 | static size_t FillIfTable(ObjPtr<mirror::IfTable> iftable, |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6522 | size_t super_ifcount, |
| 6523 | std::vector<mirror::Class*> to_process) |
| 6524 | REQUIRES(Roles::uninterruptible_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6525 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6526 | // This is the set of all class's already in the iftable. Used to make checking if a class has |
| 6527 | // already been added quicker. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6528 | std::unordered_set<ObjPtr<mirror::Class>, HashObjPtr> classes_in_iftable; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6529 | // The first super_ifcount elements are from the superclass. We note that they are already added. |
| 6530 | for (size_t i = 0; i < super_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6531 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6532 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, iface)) << "Bad ordering."; |
| 6533 | classes_in_iftable.insert(iface); |
| 6534 | } |
| 6535 | size_t filled_ifcount = super_ifcount; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6536 | for (ObjPtr<mirror::Class> interface : to_process) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6537 | // Let us call the first filled_ifcount elements of iftable the current-iface-list. |
| 6538 | // At this point in the loop current-iface-list has the invariant that: |
| 6539 | // for every pair of interfaces I,J within it: |
| 6540 | // if index_of(I) < index_of(J) then I is not a subtype of J |
| 6541 | |
| 6542 | // If we have already seen this element then all of its super-interfaces must already be in the |
| 6543 | // current-iface-list so we can skip adding it. |
| 6544 | if (!ContainsElement(classes_in_iftable, interface)) { |
| 6545 | // We haven't seen this interface so add all of its super-interfaces onto the |
| 6546 | // current-iface-list, skipping those already on it. |
| 6547 | int32_t ifcount = interface->GetIfTableCount(); |
| 6548 | for (int32_t j = 0; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6549 | ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6550 | if (!ContainsElement(classes_in_iftable, super_interface)) { |
| 6551 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, super_interface)) << "Bad ordering."; |
| 6552 | classes_in_iftable.insert(super_interface); |
| 6553 | iftable->SetInterface(filled_ifcount, super_interface); |
| 6554 | filled_ifcount++; |
| 6555 | } |
| 6556 | } |
| 6557 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, interface)) << "Bad ordering"; |
| 6558 | // Place this interface onto the current-iface-list after all of its super-interfaces. |
| 6559 | classes_in_iftable.insert(interface); |
| 6560 | iftable->SetInterface(filled_ifcount, interface); |
| 6561 | filled_ifcount++; |
| 6562 | } else if (kIsDebugBuild) { |
| 6563 | // Check all super-interfaces are already in the list. |
| 6564 | int32_t ifcount = interface->GetIfTableCount(); |
| 6565 | for (int32_t j = 0; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6566 | ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6567 | DCHECK(ContainsElement(classes_in_iftable, super_interface)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6568 | << "Iftable does not contain " << mirror::Class::PrettyClass(super_interface) |
| 6569 | << ", a superinterface of " << interface->PrettyClass(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6570 | } |
| 6571 | } |
| 6572 | } |
| 6573 | if (kIsDebugBuild) { |
| 6574 | // Check that the iftable is ordered correctly. |
| 6575 | for (size_t i = 0; i < filled_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6576 | ObjPtr<mirror::Class> if_a = iftable->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6577 | for (size_t j = i + 1; j < filled_ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6578 | ObjPtr<mirror::Class> if_b = iftable->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6579 | // !(if_a <: if_b) |
| 6580 | CHECK(!if_b->IsAssignableFrom(if_a)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6581 | << "Bad interface order: " << mirror::Class::PrettyClass(if_a) << " (index " << i |
| 6582 | << ") extends " |
| 6583 | << if_b->PrettyClass() << " (index " << j << ") and so should be after it in the " |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6584 | << "interface list."; |
| 6585 | } |
| 6586 | } |
| 6587 | } |
| 6588 | return filled_ifcount; |
| 6589 | } |
| 6590 | |
| 6591 | bool ClassLinker::SetupInterfaceLookupTable(Thread* self, Handle<mirror::Class> klass, |
| 6592 | Handle<mirror::ObjectArray<mirror::Class>> interfaces) { |
| 6593 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 6594 | const bool has_superclass = klass->HasSuperClass(); |
| 6595 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6596 | const bool have_interfaces = interfaces != nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6597 | const size_t num_interfaces = |
| 6598 | have_interfaces ? interfaces->GetLength() : klass->NumDirectInterfaces(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6599 | if (num_interfaces == 0) { |
| 6600 | if (super_ifcount == 0) { |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 6601 | if (LIKELY(has_superclass)) { |
| 6602 | klass->SetIfTable(klass->GetSuperClass()->GetIfTable()); |
| 6603 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6604 | // Class implements no interfaces. |
| 6605 | DCHECK_EQ(klass->GetIfTableCount(), 0); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6606 | return true; |
| 6607 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6608 | // Class implements same interfaces as parent, are any of these not marker interfaces? |
| 6609 | bool has_non_marker_interface = false; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6610 | ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6611 | for (size_t i = 0; i < super_ifcount; ++i) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6612 | if (super_iftable->GetMethodArrayCount(i) > 0) { |
| 6613 | has_non_marker_interface = true; |
| 6614 | break; |
| 6615 | } |
| 6616 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6617 | // Class just inherits marker interfaces from parent so recycle parent's iftable. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6618 | if (!has_non_marker_interface) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6619 | klass->SetIfTable(super_iftable); |
| 6620 | return true; |
| 6621 | } |
| 6622 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6623 | size_t ifcount = super_ifcount + num_interfaces; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6624 | // Check that every class being implemented is an interface. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6625 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 6626 | ObjPtr<mirror::Class> interface = have_interfaces |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6627 | ? interfaces->GetWithoutChecks(i) |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 6628 | : mirror::Class::GetDirectInterface(self, klass.Get(), i); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6629 | DCHECK(interface != nullptr); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6630 | if (UNLIKELY(!interface->IsInterface())) { |
| 6631 | std::string temp; |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6632 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 6633 | "Class %s implements non-interface class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6634 | klass->PrettyDescriptor().c_str(), |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6635 | PrettyDescriptor(interface->GetDescriptor(&temp)).c_str()); |
| 6636 | return false; |
| 6637 | } |
| 6638 | ifcount += interface->GetIfTableCount(); |
| 6639 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6640 | // Create the interface function table. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 6641 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(AllocIfTable(self, ifcount))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6642 | if (UNLIKELY(iftable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6643 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6644 | return false; |
| 6645 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6646 | // Fill in table with superclass's iftable. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6647 | if (super_ifcount != 0) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6648 | ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable(); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 6649 | for (size_t i = 0; i < super_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6650 | ObjPtr<mirror::Class> super_interface = super_iftable->GetInterface(i); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6651 | iftable->SetInterface(i, super_interface); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 6652 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6653 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6654 | |
| 6655 | // Note that AllowThreadSuspension is to thread suspension as pthread_testcancel is to pthread |
| 6656 | // cancellation. That is it will suspend if one has a pending suspend request but otherwise |
| 6657 | // doesn't really do anything. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 6658 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6659 | |
| 6660 | size_t new_ifcount; |
| 6661 | { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 6662 | ScopedAssertNoThreadSuspension nts("Copying mirror::Class*'s for FillIfTable"); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6663 | std::vector<mirror::Class*> to_add; |
| 6664 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 6665 | ObjPtr<mirror::Class> interface = have_interfaces ? interfaces->Get(i) : |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 6666 | mirror::Class::GetDirectInterface(self, klass.Get(), i); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 6667 | to_add.push_back(interface.Ptr()); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6668 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6669 | |
| 6670 | new_ifcount = FillIfTable(iftable.Get(), super_ifcount, std::move(to_add)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6671 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6672 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 6673 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6674 | |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6675 | // Shrink iftable in case duplicates were found |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6676 | if (new_ifcount < ifcount) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6677 | DCHECK_NE(num_interfaces, 0U); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6678 | iftable.Assign(down_cast<mirror::IfTable*>( |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6679 | iftable->CopyOf(self, new_ifcount * mirror::IfTable::kMax))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6680 | if (UNLIKELY(iftable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6681 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6682 | return false; |
| 6683 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6684 | ifcount = new_ifcount; |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6685 | } else { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6686 | DCHECK_EQ(new_ifcount, ifcount); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6687 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6688 | klass->SetIfTable(iftable.Get()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6689 | return true; |
| 6690 | } |
| 6691 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6692 | // Finds the method with a name/signature that matches cmp in the given lists of methods. The list |
| 6693 | // of methods must be unique. |
| 6694 | static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp ATTRIBUTE_UNUSED) { |
| 6695 | return nullptr; |
| 6696 | } |
| 6697 | |
| 6698 | template <typename ... Types> |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6699 | static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp, |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6700 | const ScopedArenaVector<ArtMethod*>& list, |
| 6701 | const Types& ... rest) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6702 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6703 | for (ArtMethod* method : list) { |
| 6704 | if (cmp.HasSameNameAndSignature(method)) { |
| 6705 | return method; |
| 6706 | } |
| 6707 | } |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6708 | return FindSameNameAndSignature(cmp, rest...); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6709 | } |
| 6710 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6711 | // Check that all vtable entries are present in this class's virtuals or are the same as a |
| 6712 | // superclasses vtable entry. |
| 6713 | static void CheckClassOwnsVTableEntries(Thread* self, |
| 6714 | Handle<mirror::Class> klass, |
| 6715 | PointerSize pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6716 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6717 | StackHandleScope<2> hs(self); |
| 6718 | Handle<mirror::PointerArray> check_vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6719 | ObjPtr<mirror::Class> super_temp = (klass->HasSuperClass()) ? klass->GetSuperClass() : nullptr; |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6720 | Handle<mirror::Class> superclass(hs.NewHandle(super_temp)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6721 | int32_t super_vtable_length = (superclass != nullptr) ? superclass->GetVTableLength() : 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6722 | for (int32_t i = 0; i < check_vtable->GetLength(); ++i) { |
| 6723 | ArtMethod* m = check_vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 6724 | CHECK(m != nullptr); |
| 6725 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6726 | CHECK_EQ(m->GetMethodIndexDuringLinking(), i) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6727 | << m->PrettyMethod() |
| 6728 | << " has an unexpected method index for its spot in the vtable for class" |
| 6729 | << klass->PrettyClass(); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6730 | ArraySlice<ArtMethod> virtuals = klass->GetVirtualMethodsSliceUnchecked(pointer_size); |
| 6731 | auto is_same_method = [m] (const ArtMethod& meth) { |
| 6732 | return &meth == m; |
| 6733 | }; |
Alex Light | 3f98053 | 2017-03-17 15:10:32 -0700 | [diff] [blame] | 6734 | if (!((super_vtable_length > i && superclass->GetVTableEntry(i, pointer_size) == m) || |
| 6735 | std::find_if(virtuals.begin(), virtuals.end(), is_same_method) != virtuals.end())) { |
| 6736 | LOG(WARNING) << m->PrettyMethod() << " does not seem to be owned by current class " |
| 6737 | << klass->PrettyClass() << " or any of its superclasses!"; |
| 6738 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6739 | } |
| 6740 | } |
| 6741 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6742 | // Check to make sure the vtable does not have duplicates. Duplicates could cause problems when a |
| 6743 | // method is overridden in a subclass. |
| 6744 | static void CheckVTableHasNoDuplicates(Thread* self, |
| 6745 | Handle<mirror::Class> klass, |
| 6746 | PointerSize pointer_size) |
| 6747 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6748 | StackHandleScope<1> hs(self); |
| 6749 | Handle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
| 6750 | int32_t num_entries = vtable->GetLength(); |
| 6751 | for (int32_t i = 0; i < num_entries; i++) { |
| 6752 | ArtMethod* vtable_entry = vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 6753 | // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member maybe). |
| 6754 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 6755 | vtable_entry->GetAccessFlags())) { |
| 6756 | continue; |
| 6757 | } |
| 6758 | MethodNameAndSignatureComparator name_comparator( |
| 6759 | vtable_entry->GetInterfaceMethodIfProxy(pointer_size)); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 6760 | for (int32_t j = i + 1; j < num_entries; j++) { |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6761 | ArtMethod* other_entry = vtable->GetElementPtrSize<ArtMethod*>(j, pointer_size); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 6762 | if (!klass->CanAccessMember(other_entry->GetDeclaringClass(), |
| 6763 | other_entry->GetAccessFlags())) { |
| 6764 | continue; |
| 6765 | } |
Alex Light | 3f98053 | 2017-03-17 15:10:32 -0700 | [diff] [blame] | 6766 | if (vtable_entry == other_entry || |
| 6767 | name_comparator.HasSameNameAndSignature( |
| 6768 | other_entry->GetInterfaceMethodIfProxy(pointer_size))) { |
| 6769 | LOG(WARNING) << "vtable entries " << i << " and " << j << " are identical for " |
| 6770 | << klass->PrettyClass() << " in method " << vtable_entry->PrettyMethod() |
| 6771 | << " (0x" << std::hex << reinterpret_cast<uintptr_t>(vtable_entry) << ") and " |
| 6772 | << other_entry->PrettyMethod() << " (0x" << std::hex |
| 6773 | << reinterpret_cast<uintptr_t>(other_entry) << ")"; |
| 6774 | } |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6775 | } |
| 6776 | } |
| 6777 | } |
| 6778 | |
| 6779 | static void SanityCheckVTable(Thread* self, Handle<mirror::Class> klass, PointerSize pointer_size) |
| 6780 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6781 | CheckClassOwnsVTableEntries(self, klass, pointer_size); |
| 6782 | CheckVTableHasNoDuplicates(self, klass, pointer_size); |
| 6783 | } |
| 6784 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6785 | void ClassLinker::FillImtFromSuperClass(Handle<mirror::Class> klass, |
| 6786 | ArtMethod* unimplemented_method, |
| 6787 | ArtMethod* imt_conflict_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6788 | bool* new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6789 | ArtMethod** imt) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6790 | DCHECK(klass->HasSuperClass()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6791 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6792 | if (super_class->ShouldHaveImt()) { |
| 6793 | ImTable* super_imt = super_class->GetImt(image_pointer_size_); |
| 6794 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 6795 | imt[i] = super_imt->Get(i, image_pointer_size_); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6796 | } |
| 6797 | } else { |
| 6798 | // No imt in the super class, need to reconstruct from the iftable. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6799 | ObjPtr<mirror::IfTable> if_table = super_class->GetIfTable(); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 6800 | if (if_table->Count() != 0) { |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6801 | // Ignore copied methods since we will handle these in LinkInterfaceMethods. |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6802 | FillIMTFromIfTable(if_table, |
| 6803 | unimplemented_method, |
| 6804 | imt_conflict_method, |
| 6805 | klass.Get(), |
| 6806 | /*create_conflict_table*/false, |
| 6807 | /*ignore_copied_methods*/true, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6808 | /*out*/new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6809 | /*out*/imt); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6810 | } |
| 6811 | } |
| 6812 | } |
| 6813 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6814 | class ClassLinker::LinkInterfaceMethodsHelper { |
| 6815 | public: |
| 6816 | LinkInterfaceMethodsHelper(ClassLinker* class_linker, |
| 6817 | Handle<mirror::Class> klass, |
| 6818 | Thread* self, |
| 6819 | Runtime* runtime) |
| 6820 | : class_linker_(class_linker), |
| 6821 | klass_(klass), |
| 6822 | method_alignment_(ArtMethod::Alignment(class_linker->GetImagePointerSize())), |
| 6823 | method_size_(ArtMethod::Size(class_linker->GetImagePointerSize())), |
| 6824 | self_(self), |
| 6825 | stack_(runtime->GetLinearAlloc()->GetArenaPool()), |
| 6826 | allocator_(&stack_), |
| 6827 | default_conflict_methods_(allocator_.Adapter()), |
| 6828 | overriding_default_conflict_methods_(allocator_.Adapter()), |
| 6829 | miranda_methods_(allocator_.Adapter()), |
| 6830 | default_methods_(allocator_.Adapter()), |
| 6831 | overriding_default_methods_(allocator_.Adapter()), |
| 6832 | move_table_(allocator_.Adapter()) { |
| 6833 | } |
| 6834 | |
| 6835 | ArtMethod* FindMethod(ArtMethod* interface_method, |
| 6836 | MethodNameAndSignatureComparator& interface_name_comparator, |
| 6837 | ArtMethod* vtable_impl) |
| 6838 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 6839 | |
| 6840 | ArtMethod* GetOrCreateMirandaMethod(ArtMethod* interface_method, |
| 6841 | MethodNameAndSignatureComparator& interface_name_comparator) |
| 6842 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 6843 | |
| 6844 | bool HasNewVirtuals() const { |
| 6845 | return !(miranda_methods_.empty() && |
| 6846 | default_methods_.empty() && |
| 6847 | overriding_default_methods_.empty() && |
| 6848 | overriding_default_conflict_methods_.empty() && |
| 6849 | default_conflict_methods_.empty()); |
| 6850 | } |
| 6851 | |
| 6852 | void ReallocMethods() REQUIRES_SHARED(Locks::mutator_lock_); |
| 6853 | |
| 6854 | ObjPtr<mirror::PointerArray> UpdateVtable( |
| 6855 | const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations, |
| 6856 | ObjPtr<mirror::PointerArray> old_vtable) REQUIRES_SHARED(Locks::mutator_lock_); |
| 6857 | |
| 6858 | void UpdateIfTable(Handle<mirror::IfTable> iftable) REQUIRES_SHARED(Locks::mutator_lock_); |
| 6859 | |
| 6860 | void UpdateIMT(ArtMethod** out_imt); |
| 6861 | |
| 6862 | void CheckNoStaleMethodsInDexCache() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6863 | if (kIsDebugBuild) { |
| 6864 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 6865 | // Check that there are no stale methods are in the dex cache array. |
| 6866 | auto* resolved_methods = klass_->GetDexCache()->GetResolvedMethods(); |
| 6867 | for (size_t i = 0, count = klass_->GetDexCache()->NumResolvedMethods(); i < count; ++i) { |
| 6868 | auto* m = mirror::DexCache::GetElementPtrSize(resolved_methods, i, pointer_size); |
| 6869 | CHECK(move_table_.find(m) == move_table_.end() || |
| 6870 | // The original versions of copied methods will still be present so allow those too. |
| 6871 | // Note that if the first check passes this might fail to GetDeclaringClass(). |
| 6872 | std::find_if(m->GetDeclaringClass()->GetMethods(pointer_size).begin(), |
| 6873 | m->GetDeclaringClass()->GetMethods(pointer_size).end(), |
| 6874 | [m] (ArtMethod& meth) { |
| 6875 | return &meth == m; |
| 6876 | }) != m->GetDeclaringClass()->GetMethods(pointer_size).end()) |
| 6877 | << "Obsolete method " << m->PrettyMethod() << " is in dex cache!"; |
| 6878 | } |
| 6879 | } |
| 6880 | } |
| 6881 | |
| 6882 | void ClobberOldMethods(LengthPrefixedArray<ArtMethod>* old_methods, |
| 6883 | LengthPrefixedArray<ArtMethod>* methods) { |
| 6884 | if (kIsDebugBuild) { |
| 6885 | CHECK(methods != nullptr); |
| 6886 | // Put some random garbage in old methods to help find stale pointers. |
| 6887 | if (methods != old_methods && old_methods != nullptr) { |
| 6888 | // Need to make sure the GC is not running since it could be scanning the methods we are |
| 6889 | // about to overwrite. |
| 6890 | ScopedThreadStateChange tsc(self_, kSuspended); |
| 6891 | gc::ScopedGCCriticalSection gcs(self_, |
| 6892 | gc::kGcCauseClassLinker, |
| 6893 | gc::kCollectorTypeClassLinker); |
| 6894 | const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_methods->size(), |
| 6895 | method_size_, |
| 6896 | method_alignment_); |
| 6897 | memset(old_methods, 0xFEu, old_size); |
| 6898 | } |
| 6899 | } |
| 6900 | } |
| 6901 | |
| 6902 | private: |
| 6903 | size_t NumberOfNewVirtuals() const { |
| 6904 | return miranda_methods_.size() + |
| 6905 | default_methods_.size() + |
| 6906 | overriding_default_conflict_methods_.size() + |
| 6907 | overriding_default_methods_.size() + |
| 6908 | default_conflict_methods_.size(); |
| 6909 | } |
| 6910 | |
| 6911 | bool FillTables() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6912 | return !klass_->IsInterface(); |
| 6913 | } |
| 6914 | |
| 6915 | void LogNewVirtuals() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6916 | DCHECK(!klass_->IsInterface() || (default_methods_.empty() && miranda_methods_.empty())) |
| 6917 | << "Interfaces should only have default-conflict methods appended to them."; |
| 6918 | VLOG(class_linker) << mirror::Class::PrettyClass(klass_.Get()) << ": miranda_methods=" |
| 6919 | << miranda_methods_.size() |
| 6920 | << " default_methods=" << default_methods_.size() |
| 6921 | << " overriding_default_methods=" << overriding_default_methods_.size() |
| 6922 | << " default_conflict_methods=" << default_conflict_methods_.size() |
| 6923 | << " overriding_default_conflict_methods=" |
| 6924 | << overriding_default_conflict_methods_.size(); |
| 6925 | } |
| 6926 | |
| 6927 | ClassLinker* class_linker_; |
| 6928 | Handle<mirror::Class> klass_; |
| 6929 | size_t method_alignment_; |
| 6930 | size_t method_size_; |
| 6931 | Thread* const self_; |
| 6932 | |
| 6933 | // These are allocated on the heap to begin, we then transfer to linear alloc when we re-create |
| 6934 | // the virtual methods array. |
| 6935 | // Need to use low 4GB arenas for compiler or else the pointers wont fit in 32 bit method array |
| 6936 | // during cross compilation. |
| 6937 | // Use the linear alloc pool since this one is in the low 4gb for the compiler. |
| 6938 | ArenaStack stack_; |
| 6939 | ScopedArenaAllocator allocator_; |
| 6940 | |
| 6941 | ScopedArenaVector<ArtMethod*> default_conflict_methods_; |
| 6942 | ScopedArenaVector<ArtMethod*> overriding_default_conflict_methods_; |
| 6943 | ScopedArenaVector<ArtMethod*> miranda_methods_; |
| 6944 | ScopedArenaVector<ArtMethod*> default_methods_; |
| 6945 | ScopedArenaVector<ArtMethod*> overriding_default_methods_; |
| 6946 | |
| 6947 | ScopedArenaUnorderedMap<ArtMethod*, ArtMethod*> move_table_; |
| 6948 | }; |
| 6949 | |
| 6950 | ArtMethod* ClassLinker::LinkInterfaceMethodsHelper::FindMethod( |
| 6951 | ArtMethod* interface_method, |
| 6952 | MethodNameAndSignatureComparator& interface_name_comparator, |
| 6953 | ArtMethod* vtable_impl) { |
| 6954 | ArtMethod* current_method = nullptr; |
| 6955 | switch (class_linker_->FindDefaultMethodImplementation(self_, |
| 6956 | interface_method, |
| 6957 | klass_, |
| 6958 | /*out*/¤t_method)) { |
| 6959 | case DefaultMethodSearchResult::kDefaultConflict: { |
| 6960 | // Default method conflict. |
| 6961 | DCHECK(current_method == nullptr); |
| 6962 | ArtMethod* default_conflict_method = nullptr; |
| 6963 | if (vtable_impl != nullptr && vtable_impl->IsDefaultConflicting()) { |
| 6964 | // We can reuse the method from the superclass, don't bother adding it to virtuals. |
| 6965 | default_conflict_method = vtable_impl; |
| 6966 | } else { |
| 6967 | // See if we already have a conflict method for this method. |
| 6968 | ArtMethod* preexisting_conflict = FindSameNameAndSignature( |
| 6969 | interface_name_comparator, |
| 6970 | default_conflict_methods_, |
| 6971 | overriding_default_conflict_methods_); |
| 6972 | if (LIKELY(preexisting_conflict != nullptr)) { |
| 6973 | // We already have another conflict we can reuse. |
| 6974 | default_conflict_method = preexisting_conflict; |
| 6975 | } else { |
| 6976 | // Note that we do this even if we are an interface since we need to create this and |
| 6977 | // cannot reuse another classes. |
| 6978 | // Create a new conflict method for this to use. |
| 6979 | default_conflict_method = reinterpret_cast<ArtMethod*>(allocator_.Alloc(method_size_)); |
| 6980 | new(default_conflict_method) ArtMethod(interface_method, |
| 6981 | class_linker_->GetImagePointerSize()); |
| 6982 | if (vtable_impl == nullptr) { |
| 6983 | // Save the conflict method. We need to add it to the vtable. |
| 6984 | default_conflict_methods_.push_back(default_conflict_method); |
| 6985 | } else { |
| 6986 | // Save the conflict method but it is already in the vtable. |
| 6987 | overriding_default_conflict_methods_.push_back(default_conflict_method); |
| 6988 | } |
| 6989 | } |
| 6990 | } |
| 6991 | current_method = default_conflict_method; |
| 6992 | break; |
| 6993 | } // case kDefaultConflict |
| 6994 | case DefaultMethodSearchResult::kDefaultFound: { |
| 6995 | DCHECK(current_method != nullptr); |
| 6996 | // Found a default method. |
| 6997 | if (vtable_impl != nullptr && |
| 6998 | current_method->GetDeclaringClass() == vtable_impl->GetDeclaringClass()) { |
| 6999 | // We found a default method but it was the same one we already have from our |
| 7000 | // superclass. Don't bother adding it to our vtable again. |
| 7001 | current_method = vtable_impl; |
| 7002 | } else if (LIKELY(FillTables())) { |
| 7003 | // Interfaces don't need to copy default methods since they don't have vtables. |
| 7004 | // Only record this default method if it is new to save space. |
| 7005 | // TODO It might be worthwhile to copy default methods on interfaces anyway since it |
| 7006 | // would make lookup for interface super much faster. (We would only need to scan |
| 7007 | // the iftable to find if there is a NSME or AME.) |
| 7008 | ArtMethod* old = FindSameNameAndSignature(interface_name_comparator, |
| 7009 | default_methods_, |
| 7010 | overriding_default_methods_); |
| 7011 | if (old == nullptr) { |
| 7012 | // We found a default method implementation and there were no conflicts. |
| 7013 | if (vtable_impl == nullptr) { |
| 7014 | // Save the default method. We need to add it to the vtable. |
| 7015 | default_methods_.push_back(current_method); |
| 7016 | } else { |
| 7017 | // Save the default method but it is already in the vtable. |
| 7018 | overriding_default_methods_.push_back(current_method); |
| 7019 | } |
| 7020 | } else { |
| 7021 | CHECK(old == current_method) << "Multiple default implementations selected!"; |
| 7022 | } |
| 7023 | } |
| 7024 | break; |
| 7025 | } // case kDefaultFound |
| 7026 | case DefaultMethodSearchResult::kAbstractFound: { |
| 7027 | DCHECK(current_method == nullptr); |
| 7028 | // Abstract method masks all defaults. |
| 7029 | if (vtable_impl != nullptr && |
| 7030 | vtable_impl->IsAbstract() && |
| 7031 | !vtable_impl->IsDefaultConflicting()) { |
| 7032 | // We need to make this an abstract method but the version in the vtable already is so |
| 7033 | // don't do anything. |
| 7034 | current_method = vtable_impl; |
| 7035 | } |
| 7036 | break; |
| 7037 | } // case kAbstractFound |
| 7038 | } |
| 7039 | return current_method; |
| 7040 | } |
| 7041 | |
| 7042 | ArtMethod* ClassLinker::LinkInterfaceMethodsHelper::GetOrCreateMirandaMethod( |
| 7043 | ArtMethod* interface_method, |
| 7044 | MethodNameAndSignatureComparator& interface_name_comparator) { |
| 7045 | // Find out if there is already a miranda method we can use. |
| 7046 | ArtMethod* miranda_method = FindSameNameAndSignature(interface_name_comparator, |
| 7047 | miranda_methods_); |
| 7048 | if (miranda_method == nullptr) { |
| 7049 | DCHECK(interface_method->IsAbstract()) << interface_method->PrettyMethod(); |
| 7050 | miranda_method = reinterpret_cast<ArtMethod*>(allocator_.Alloc(method_size_)); |
| 7051 | CHECK(miranda_method != nullptr); |
| 7052 | // Point the interface table at a phantom slot. |
| 7053 | new(miranda_method) ArtMethod(interface_method, class_linker_->GetImagePointerSize()); |
| 7054 | miranda_methods_.push_back(miranda_method); |
| 7055 | } |
| 7056 | return miranda_method; |
| 7057 | } |
| 7058 | |
| 7059 | void ClassLinker::LinkInterfaceMethodsHelper::ReallocMethods() { |
| 7060 | LogNewVirtuals(); |
| 7061 | |
| 7062 | const size_t old_method_count = klass_->NumMethods(); |
| 7063 | const size_t new_method_count = old_method_count + NumberOfNewVirtuals(); |
| 7064 | DCHECK_NE(old_method_count, new_method_count); |
| 7065 | |
| 7066 | // Attempt to realloc to save RAM if possible. |
| 7067 | LengthPrefixedArray<ArtMethod>* old_methods = klass_->GetMethodsPtr(); |
| 7068 | // The Realloced virtual methods aren't visible from the class roots, so there is no issue |
| 7069 | // where GCs could attempt to mark stale pointers due to memcpy. And since we overwrite the |
| 7070 | // realloced memory with out->CopyFrom, we are guaranteed to have objects in the to space since |
| 7071 | // CopyFrom has internal read barriers. |
| 7072 | // |
| 7073 | // TODO We should maybe move some of this into mirror::Class or at least into another method. |
| 7074 | const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_method_count, |
| 7075 | method_size_, |
| 7076 | method_alignment_); |
| 7077 | const size_t new_size = LengthPrefixedArray<ArtMethod>::ComputeSize(new_method_count, |
| 7078 | method_size_, |
| 7079 | method_alignment_); |
| 7080 | const size_t old_methods_ptr_size = (old_methods != nullptr) ? old_size : 0; |
| 7081 | auto* methods = reinterpret_cast<LengthPrefixedArray<ArtMethod>*>( |
| 7082 | Runtime::Current()->GetLinearAlloc()->Realloc( |
| 7083 | self_, old_methods, old_methods_ptr_size, new_size)); |
| 7084 | CHECK(methods != nullptr); // Native allocation failure aborts. |
| 7085 | |
| 7086 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 7087 | if (methods != old_methods) { |
| 7088 | // Maps from heap allocated miranda method to linear alloc miranda method. |
| 7089 | StrideIterator<ArtMethod> out = methods->begin(method_size_, method_alignment_); |
| 7090 | // Copy over the old methods. |
| 7091 | for (auto& m : klass_->GetMethods(pointer_size)) { |
| 7092 | move_table_.emplace(&m, &*out); |
| 7093 | // The CopyFrom is only necessary to not miss read barriers since Realloc won't do read |
| 7094 | // barriers when it copies. |
| 7095 | out->CopyFrom(&m, pointer_size); |
| 7096 | ++out; |
| 7097 | } |
| 7098 | } |
| 7099 | StrideIterator<ArtMethod> out(methods->begin(method_size_, method_alignment_) + old_method_count); |
| 7100 | // Copy over miranda methods before copying vtable since CopyOf may cause thread suspension and |
| 7101 | // we want the roots of the miranda methods to get visited. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7102 | for (size_t i = 0; i < miranda_methods_.size(); ++i) { |
| 7103 | ArtMethod* mir_method = miranda_methods_[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7104 | ArtMethod& new_method = *out; |
| 7105 | new_method.CopyFrom(mir_method, pointer_size); |
| 7106 | new_method.SetAccessFlags(new_method.GetAccessFlags() | kAccMiranda | kAccCopied); |
| 7107 | DCHECK_NE(new_method.GetAccessFlags() & kAccAbstract, 0u) |
| 7108 | << "Miranda method should be abstract!"; |
| 7109 | move_table_.emplace(mir_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7110 | // Update the entry in the method array, as the array will be used for future lookups, |
| 7111 | // where thread suspension is allowed. |
| 7112 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 7113 | // would not see them. |
| 7114 | miranda_methods_[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7115 | ++out; |
| 7116 | } |
| 7117 | // We need to copy the default methods into our own method table since the runtime requires that |
| 7118 | // every method on a class's vtable be in that respective class's virtual method table. |
| 7119 | // NOTE This means that two classes might have the same implementation of a method from the same |
| 7120 | // interface but will have different ArtMethod*s for them. This also means we cannot compare a |
| 7121 | // default method found on a class with one found on the declaring interface directly and must |
| 7122 | // look at the declaring class to determine if they are the same. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7123 | for (ScopedArenaVector<ArtMethod*>* methods_vec : {&default_methods_, |
| 7124 | &overriding_default_methods_}) { |
| 7125 | for (size_t i = 0; i < methods_vec->size(); ++i) { |
| 7126 | ArtMethod* def_method = (*methods_vec)[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7127 | ArtMethod& new_method = *out; |
| 7128 | new_method.CopyFrom(def_method, pointer_size); |
| 7129 | // Clear the kAccSkipAccessChecks flag if it is present. Since this class hasn't been |
| 7130 | // verified yet it shouldn't have methods that are skipping access checks. |
| 7131 | // TODO This is rather arbitrary. We should maybe support classes where only some of its |
| 7132 | // methods are skip_access_checks. |
| 7133 | constexpr uint32_t kSetFlags = kAccDefault | kAccCopied; |
| 7134 | constexpr uint32_t kMaskFlags = ~kAccSkipAccessChecks; |
| 7135 | new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags); |
| 7136 | move_table_.emplace(def_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7137 | // Update the entry in the method array, as the array will be used for future lookups, |
| 7138 | // where thread suspension is allowed. |
| 7139 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 7140 | // would not see them. |
| 7141 | (*methods_vec)[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7142 | ++out; |
| 7143 | } |
| 7144 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7145 | for (ScopedArenaVector<ArtMethod*>* methods_vec : {&default_conflict_methods_, |
| 7146 | &overriding_default_conflict_methods_}) { |
| 7147 | for (size_t i = 0; i < methods_vec->size(); ++i) { |
| 7148 | ArtMethod* conf_method = (*methods_vec)[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7149 | ArtMethod& new_method = *out; |
| 7150 | new_method.CopyFrom(conf_method, pointer_size); |
| 7151 | // This is a type of default method (there are default method impls, just a conflict) so |
| 7152 | // mark this as a default, non-abstract method, since thats what it is. Also clear the |
| 7153 | // kAccSkipAccessChecks bit since this class hasn't been verified yet it shouldn't have |
| 7154 | // methods that are skipping access checks. |
| 7155 | constexpr uint32_t kSetFlags = kAccDefault | kAccDefaultConflict | kAccCopied; |
| 7156 | constexpr uint32_t kMaskFlags = ~(kAccAbstract | kAccSkipAccessChecks); |
| 7157 | new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags); |
| 7158 | DCHECK(new_method.IsDefaultConflicting()); |
| 7159 | // The actual method might or might not be marked abstract since we just copied it from a |
| 7160 | // (possibly default) interface method. We need to set it entry point to be the bridge so |
| 7161 | // that the compiler will not invoke the implementation of whatever method we copied from. |
| 7162 | EnsureThrowsInvocationError(class_linker_, &new_method); |
| 7163 | move_table_.emplace(conf_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7164 | // Update the entry in the method array, as the array will be used for future lookups, |
| 7165 | // where thread suspension is allowed. |
| 7166 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 7167 | // would not see them. |
| 7168 | (*methods_vec)[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7169 | ++out; |
| 7170 | } |
| 7171 | } |
| 7172 | methods->SetSize(new_method_count); |
| 7173 | class_linker_->UpdateClassMethods(klass_.Get(), methods); |
| 7174 | } |
| 7175 | |
| 7176 | ObjPtr<mirror::PointerArray> ClassLinker::LinkInterfaceMethodsHelper::UpdateVtable( |
| 7177 | const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations, |
| 7178 | ObjPtr<mirror::PointerArray> old_vtable) { |
| 7179 | // Update the vtable to the new method structures. We can skip this for interfaces since they |
| 7180 | // do not have vtables. |
| 7181 | const size_t old_vtable_count = old_vtable->GetLength(); |
| 7182 | const size_t new_vtable_count = old_vtable_count + |
| 7183 | miranda_methods_.size() + |
| 7184 | default_methods_.size() + |
| 7185 | default_conflict_methods_.size(); |
| 7186 | |
| 7187 | ObjPtr<mirror::PointerArray> vtable = |
| 7188 | down_cast<mirror::PointerArray*>(old_vtable->CopyOf(self_, new_vtable_count)); |
| 7189 | if (UNLIKELY(vtable == nullptr)) { |
| 7190 | self_->AssertPendingOOMException(); |
| 7191 | return nullptr; |
| 7192 | } |
| 7193 | |
| 7194 | size_t vtable_pos = old_vtable_count; |
| 7195 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 7196 | // Update all the newly copied method's indexes so they denote their placement in the vtable. |
| 7197 | for (const ScopedArenaVector<ArtMethod*>& methods_vec : {default_methods_, |
| 7198 | default_conflict_methods_, |
| 7199 | miranda_methods_}) { |
| 7200 | // These are the functions that are not already in the vtable! |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7201 | for (ArtMethod* new_vtable_method : methods_vec) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7202 | // Leave the declaring class alone the method's dex_code_item_offset_ and dex_method_index_ |
| 7203 | // fields are references into the dex file the method was defined in. Since the ArtMethod |
| 7204 | // does not store that information it uses declaring_class_->dex_cache_. |
| 7205 | new_vtable_method->SetMethodIndex(0xFFFF & vtable_pos); |
| 7206 | vtable->SetElementPtrSize(vtable_pos, new_vtable_method, pointer_size); |
| 7207 | ++vtable_pos; |
| 7208 | } |
| 7209 | } |
| 7210 | DCHECK_EQ(vtable_pos, new_vtable_count); |
| 7211 | |
| 7212 | // Update old vtable methods. We use the default_translations map to figure out what each |
| 7213 | // vtable entry should be updated to, if they need to be at all. |
| 7214 | for (size_t i = 0; i < old_vtable_count; ++i) { |
| 7215 | ArtMethod* translated_method = vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 7216 | // Try and find what we need to change this method to. |
| 7217 | auto translation_it = default_translations.find(i); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7218 | if (translation_it != default_translations.end()) { |
| 7219 | if (translation_it->second.IsInConflict()) { |
| 7220 | // Find which conflict method we are to use for this method. |
| 7221 | MethodNameAndSignatureComparator old_method_comparator( |
| 7222 | translated_method->GetInterfaceMethodIfProxy(pointer_size)); |
| 7223 | // We only need to look through overriding_default_conflict_methods since this is an |
| 7224 | // overridden method we are fixing up here. |
| 7225 | ArtMethod* new_conflict_method = FindSameNameAndSignature( |
| 7226 | old_method_comparator, overriding_default_conflict_methods_); |
| 7227 | CHECK(new_conflict_method != nullptr) << "Expected a conflict method!"; |
| 7228 | translated_method = new_conflict_method; |
| 7229 | } else if (translation_it->second.IsAbstract()) { |
| 7230 | // Find which miranda method we are to use for this method. |
| 7231 | MethodNameAndSignatureComparator old_method_comparator( |
| 7232 | translated_method->GetInterfaceMethodIfProxy(pointer_size)); |
| 7233 | ArtMethod* miranda_method = FindSameNameAndSignature(old_method_comparator, |
| 7234 | miranda_methods_); |
| 7235 | DCHECK(miranda_method != nullptr); |
| 7236 | translated_method = miranda_method; |
| 7237 | } else { |
| 7238 | // Normal default method (changed from an older default or abstract interface method). |
| 7239 | DCHECK(translation_it->second.IsTranslation()); |
| 7240 | translated_method = translation_it->second.GetTranslation(); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7241 | auto it = move_table_.find(translated_method); |
| 7242 | DCHECK(it != move_table_.end()); |
| 7243 | translated_method = it->second; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7244 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7245 | } else { |
| 7246 | auto it = move_table_.find(translated_method); |
| 7247 | translated_method = (it != move_table_.end()) ? it->second : nullptr; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7248 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7249 | |
| 7250 | if (translated_method != nullptr) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7251 | // Make sure the new_methods index is set. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7252 | if (translated_method->GetMethodIndexDuringLinking() != i) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7253 | if (kIsDebugBuild) { |
| 7254 | auto* methods = klass_->GetMethodsPtr(); |
| 7255 | CHECK_LE(reinterpret_cast<uintptr_t>(&*methods->begin(method_size_, method_alignment_)), |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7256 | reinterpret_cast<uintptr_t>(translated_method)); |
| 7257 | CHECK_LT(reinterpret_cast<uintptr_t>(translated_method), |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7258 | reinterpret_cast<uintptr_t>(&*methods->end(method_size_, method_alignment_))); |
| 7259 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7260 | translated_method->SetMethodIndex(0xFFFF & i); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7261 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7262 | vtable->SetElementPtrSize(i, translated_method, pointer_size); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7263 | } |
| 7264 | } |
| 7265 | klass_->SetVTable(vtable.Ptr()); |
| 7266 | return vtable; |
| 7267 | } |
| 7268 | |
| 7269 | void ClassLinker::LinkInterfaceMethodsHelper::UpdateIfTable(Handle<mirror::IfTable> iftable) { |
| 7270 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 7271 | const size_t ifcount = klass_->GetIfTableCount(); |
| 7272 | // Go fix up all the stale iftable pointers. |
| 7273 | for (size_t i = 0; i < ifcount; ++i) { |
| 7274 | for (size_t j = 0, count = iftable->GetMethodArrayCount(i); j < count; ++j) { |
| 7275 | auto* method_array = iftable->GetMethodArray(i); |
| 7276 | auto* m = method_array->GetElementPtrSize<ArtMethod*>(j, pointer_size); |
| 7277 | DCHECK(m != nullptr) << klass_->PrettyClass(); |
| 7278 | auto it = move_table_.find(m); |
| 7279 | if (it != move_table_.end()) { |
| 7280 | auto* new_m = it->second; |
| 7281 | DCHECK(new_m != nullptr) << klass_->PrettyClass(); |
| 7282 | method_array->SetElementPtrSize(j, new_m, pointer_size); |
| 7283 | } |
| 7284 | } |
| 7285 | } |
| 7286 | } |
| 7287 | |
| 7288 | void ClassLinker::LinkInterfaceMethodsHelper::UpdateIMT(ArtMethod** out_imt) { |
| 7289 | // Fix up IMT next. |
| 7290 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 7291 | auto it = move_table_.find(out_imt[i]); |
| 7292 | if (it != move_table_.end()) { |
| 7293 | out_imt[i] = it->second; |
| 7294 | } |
| 7295 | } |
| 7296 | } |
| 7297 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7298 | // TODO This method needs to be split up into several smaller methods. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7299 | bool ClassLinker::LinkInterfaceMethods( |
| 7300 | Thread* self, |
| 7301 | Handle<mirror::Class> klass, |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7302 | const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7303 | bool* out_new_conflict, |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7304 | ArtMethod** out_imt) { |
| 7305 | StackHandleScope<3> hs(self); |
| 7306 | Runtime* const runtime = Runtime::Current(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7307 | |
| 7308 | const bool is_interface = klass->IsInterface(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7309 | const bool has_superclass = klass->HasSuperClass(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7310 | const bool fill_tables = !is_interface; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7311 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7312 | const size_t ifcount = klass->GetIfTableCount(); |
| 7313 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7314 | Handle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7315 | |
| 7316 | MutableHandle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
| 7317 | ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod(); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7318 | ArtMethod* const imt_conflict_method = runtime->GetImtConflictMethod(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7319 | // Copy the IMT from the super class if possible. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7320 | const bool extend_super_iftable = has_superclass; |
| 7321 | if (has_superclass && fill_tables) { |
| 7322 | FillImtFromSuperClass(klass, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7323 | unimplemented_method, |
| 7324 | imt_conflict_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7325 | out_new_conflict, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7326 | out_imt); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7327 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7328 | // Allocate method arrays before since we don't want miss visiting miranda method roots due to |
| 7329 | // thread suspension. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7330 | if (fill_tables) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7331 | if (!AllocateIfTableMethodArrays(self, klass, iftable)) { |
| 7332 | return false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7333 | } |
| 7334 | } |
| 7335 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7336 | LinkInterfaceMethodsHelper helper(this, klass, self, runtime); |
| 7337 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7338 | auto* old_cause = self->StartAssertNoThreadSuspension( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7339 | "Copying ArtMethods for LinkInterfaceMethods"); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7340 | // Going in reverse to ensure that we will hit abstract methods that override defaults before the |
| 7341 | // defaults. This means we don't need to do any trickery when creating the Miranda methods, since |
| 7342 | // they will already be null. This has the additional benefit that the declarer of a miranda |
| 7343 | // method will actually declare an abstract method. |
| 7344 | for (size_t i = ifcount; i != 0; ) { |
| 7345 | --i; |
| 7346 | |
| 7347 | DCHECK_GE(i, 0u); |
| 7348 | DCHECK_LT(i, ifcount); |
| 7349 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7350 | size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7351 | if (num_methods > 0) { |
| 7352 | StackHandleScope<2> hs2(self); |
| 7353 | const bool is_super = i < super_ifcount; |
| 7354 | const bool super_interface = is_super && extend_super_iftable; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7355 | // We don't actually create or fill these tables for interfaces, we just copy some methods for |
| 7356 | // conflict methods. Just set this as nullptr in those cases. |
| 7357 | Handle<mirror::PointerArray> method_array(fill_tables |
| 7358 | ? hs2.NewHandle(iftable->GetMethodArray(i)) |
| 7359 | : hs2.NewHandle<mirror::PointerArray>(nullptr)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7360 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7361 | ArraySlice<ArtMethod> input_virtual_methods; |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 7362 | ScopedNullHandle<mirror::PointerArray> null_handle; |
| 7363 | Handle<mirror::PointerArray> input_vtable_array(null_handle); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7364 | int32_t input_array_length = 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7365 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7366 | // TODO Cleanup Needed: In the presence of default methods this optimization is rather dirty |
| 7367 | // and confusing. Default methods should always look through all the superclasses |
| 7368 | // because they are the last choice of an implementation. We get around this by looking |
| 7369 | // at the super-classes iftable methods (copied into method_array previously) when we are |
| 7370 | // 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] | 7371 | bool using_virtuals; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7372 | if (super_interface || is_interface) { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7373 | // 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] | 7374 | // whole vtable. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7375 | using_virtuals = true; |
| 7376 | input_virtual_methods = klass->GetDeclaredMethodsSlice(image_pointer_size_); |
| 7377 | input_array_length = input_virtual_methods.size(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7378 | } else { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7379 | // For a new interface, however, we need the whole vtable in case a new |
| 7380 | // interface method is implemented in the whole superclass. |
| 7381 | using_virtuals = false; |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7382 | DCHECK(vtable != nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7383 | input_vtable_array = vtable; |
| 7384 | input_array_length = input_vtable_array->GetLength(); |
| 7385 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7386 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7387 | // For each method in interface |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 7388 | for (size_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7389 | auto* interface_method = iftable->GetInterface(i)->GetVirtualMethod(j, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 7390 | MethodNameAndSignatureComparator interface_name_comparator( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7391 | interface_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 7392 | uint32_t imt_index = ImTable::GetImtIndex(interface_method); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7393 | ArtMethod** imt_ptr = &out_imt[imt_index]; |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7394 | // For each method listed in the interface's method list, find the |
| 7395 | // matching method in our class's method list. We want to favor the |
| 7396 | // subclass over the superclass, which just requires walking |
| 7397 | // back from the end of the vtable. (This only matters if the |
| 7398 | // superclass defines a private method and this class redefines |
| 7399 | // it -- otherwise it would use the same vtable slot. In .dex files |
| 7400 | // those don't end up in the virtual method table, so it shouldn't |
| 7401 | // matter which direction we go. We walk it backward anyway.) |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7402 | // |
| 7403 | // To find defaults we need to do the same but also go over interfaces. |
| 7404 | bool found_impl = false; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7405 | ArtMethod* vtable_impl = nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7406 | for (int32_t k = input_array_length - 1; k >= 0; --k) { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7407 | ArtMethod* vtable_method = using_virtuals ? |
| 7408 | &input_virtual_methods[k] : |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7409 | input_vtable_array->GetElementPtrSize<ArtMethod*>(k, image_pointer_size_); |
| 7410 | ArtMethod* vtable_method_for_name_comparison = |
| 7411 | vtable_method->GetInterfaceMethodIfProxy(image_pointer_size_); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 7412 | if (interface_name_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 7413 | vtable_method_for_name_comparison)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7414 | if (!vtable_method->IsAbstract() && !vtable_method->IsPublic()) { |
Mathieu Chartier | 4d122c1 | 2015-06-17 14:14:36 -0700 | [diff] [blame] | 7415 | // Must do EndAssertNoThreadSuspension before throw since the throw can cause |
| 7416 | // allocations. |
| 7417 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7418 | ThrowIllegalAccessError(klass.Get(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 7419 | "Method '%s' implementing interface method '%s' is not public", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7420 | vtable_method->PrettyMethod().c_str(), |
| 7421 | interface_method->PrettyMethod().c_str()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7422 | return false; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7423 | } else if (UNLIKELY(vtable_method->IsOverridableByDefaultMethod())) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7424 | // We might have a newer, better, default method for this, so we just skip it. If we |
| 7425 | // are still using this we will select it again when scanning for default methods. To |
| 7426 | // obviate the need to copy the method again we will make a note that we already found |
| 7427 | // a default here. |
| 7428 | // TODO This should be much cleaner. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7429 | vtable_impl = vtable_method; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7430 | break; |
| 7431 | } else { |
| 7432 | found_impl = true; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7433 | if (LIKELY(fill_tables)) { |
| 7434 | method_array->SetElementPtrSize(j, vtable_method, image_pointer_size_); |
| 7435 | // Place method in imt if entry is empty, place conflict otherwise. |
| 7436 | SetIMTRef(unimplemented_method, |
| 7437 | imt_conflict_method, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7438 | vtable_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7439 | /*out*/out_new_conflict, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7440 | /*out*/imt_ptr); |
| 7441 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7442 | break; |
| 7443 | } |
| 7444 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7445 | } |
| 7446 | // Continue on to the next method if we are done. |
| 7447 | if (LIKELY(found_impl)) { |
| 7448 | continue; |
| 7449 | } else if (LIKELY(super_interface)) { |
| 7450 | // Don't look for a default implementation when the super-method is implemented directly |
| 7451 | // by the class. |
| 7452 | // |
| 7453 | // See if we can use the superclasses method and skip searching everything else. |
| 7454 | // Note: !found_impl && super_interface |
| 7455 | CHECK(extend_super_iftable); |
| 7456 | // If this is a super_interface method it is possible we shouldn't override it because a |
| 7457 | // superclass could have implemented it directly. We get the method the superclass used |
| 7458 | // to implement this to know if we can override it with a default method. Doing this is |
| 7459 | // safe since we know that the super_iftable is filled in so we can simply pull it from |
| 7460 | // there. We don't bother if this is not a super-classes interface since in that case we |
| 7461 | // have scanned the entire vtable anyway and would have found it. |
| 7462 | // TODO This is rather dirty but it is faster than searching through the entire vtable |
| 7463 | // every time. |
| 7464 | ArtMethod* supers_method = |
| 7465 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 7466 | DCHECK(supers_method != nullptr); |
| 7467 | DCHECK(interface_name_comparator.HasSameNameAndSignature(supers_method)); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7468 | if (LIKELY(!supers_method->IsOverridableByDefaultMethod())) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7469 | // The method is not overridable by a default method (i.e. it is directly implemented |
| 7470 | // in some class). Therefore move onto the next interface method. |
| 7471 | continue; |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 7472 | } else { |
| 7473 | // If the super-classes method is override-able by a default method we need to keep |
| 7474 | // track of it since though it is override-able it is not guaranteed to be 'overridden'. |
| 7475 | // 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] | 7476 | // to the vtable twice, causing corruption (vtable entries having inconsistent and |
| 7477 | // illegal states, incorrect vtable size, and incorrect or inconsistent iftable entries) |
| 7478 | // in this class and any subclasses. |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 7479 | DCHECK(vtable_impl == nullptr || vtable_impl == supers_method) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7480 | << "vtable_impl was " << ArtMethod::PrettyMethod(vtable_impl) |
| 7481 | << " and not 'nullptr' or " |
| 7482 | << supers_method->PrettyMethod() |
| 7483 | << " as expected. IFTable appears to be corrupt!"; |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 7484 | vtable_impl = supers_method; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7485 | } |
| 7486 | } |
| 7487 | // If we haven't found it yet we should search through the interfaces for default methods. |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7488 | ArtMethod* current_method = helper.FindMethod(interface_method, |
| 7489 | interface_name_comparator, |
| 7490 | vtable_impl); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7491 | if (LIKELY(fill_tables)) { |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 7492 | if (current_method == nullptr && !super_interface) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7493 | // We could not find an implementation for this method and since it is a brand new |
| 7494 | // interface we searched the entire vtable (and all default methods) for an |
| 7495 | // implementation but couldn't find one. We therefore need to make a miranda method. |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7496 | current_method = helper.GetOrCreateMirandaMethod(interface_method, |
| 7497 | interface_name_comparator); |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 7498 | } |
| 7499 | |
| 7500 | if (current_method != nullptr) { |
| 7501 | // We found a default method implementation. Record it in the iftable and IMT. |
| 7502 | method_array->SetElementPtrSize(j, current_method, image_pointer_size_); |
| 7503 | SetIMTRef(unimplemented_method, |
| 7504 | imt_conflict_method, |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 7505 | current_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7506 | /*out*/out_new_conflict, |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 7507 | /*out*/imt_ptr); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7508 | } |
| 7509 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7510 | } // For each method in interface end. |
| 7511 | } // if (num_methods > 0) |
| 7512 | } // For each interface. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7513 | // TODO don't extend virtuals of interface unless necessary (when is it?). |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7514 | if (helper.HasNewVirtuals()) { |
| 7515 | LengthPrefixedArray<ArtMethod>* old_methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr; |
| 7516 | helper.ReallocMethods(); // No return value to check. Native allocation failure aborts. |
| 7517 | LengthPrefixedArray<ArtMethod>* methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr; |
| 7518 | |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 7519 | // 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] | 7520 | // suspension assert. |
| 7521 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 7522 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7523 | if (fill_tables) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7524 | vtable.Assign(helper.UpdateVtable(default_translations, vtable.Get())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7525 | if (UNLIKELY(vtable == nullptr)) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7526 | // The helper has already called self->AssertPendingOOMException(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7527 | return false; |
| 7528 | } |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7529 | helper.UpdateIfTable(iftable); |
| 7530 | helper.UpdateIMT(out_imt); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7531 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7532 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7533 | helper.CheckNoStaleMethodsInDexCache(); |
| 7534 | helper.ClobberOldMethods(old_methods, methods); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7535 | } else { |
| 7536 | self->EndAssertNoThreadSuspension(old_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7537 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7538 | if (kIsDebugBuild && !is_interface) { |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7539 | SanityCheckVTable(self, klass, image_pointer_size_); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 7540 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7541 | return true; |
| 7542 | } |
| 7543 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7544 | bool ClassLinker::LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7545 | CHECK(klass != nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7546 | return LinkFields(self, klass, false, nullptr); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 7547 | } |
| 7548 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7549 | bool ClassLinker::LinkStaticFields(Thread* self, Handle<mirror::Class> klass, size_t* class_size) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7550 | CHECK(klass != nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7551 | return LinkFields(self, klass, true, class_size); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 7552 | } |
| 7553 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7554 | struct LinkFieldsComparator { |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 7555 | explicit LinkFieldsComparator() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 7556 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 7557 | // 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] | 7558 | bool operator()(ArtField* field1, ArtField* field2) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 7559 | NO_THREAD_SAFETY_ANALYSIS { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 7560 | // 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] | 7561 | Primitive::Type type1 = field1->GetTypeAsPrimitiveType(); |
| 7562 | Primitive::Type type2 = field2->GetTypeAsPrimitiveType(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 7563 | if (type1 != type2) { |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 7564 | if (type1 == Primitive::kPrimNot) { |
| 7565 | // Reference always goes first. |
| 7566 | return true; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 7567 | } |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 7568 | if (type2 == Primitive::kPrimNot) { |
| 7569 | // Reference always goes first. |
| 7570 | return false; |
| 7571 | } |
| 7572 | size_t size1 = Primitive::ComponentSize(type1); |
| 7573 | size_t size2 = Primitive::ComponentSize(type2); |
| 7574 | if (size1 != size2) { |
| 7575 | // Larger primitive types go first. |
| 7576 | return size1 > size2; |
| 7577 | } |
| 7578 | // Primitive types differ but sizes match. Arbitrarily order by primitive type. |
| 7579 | return type1 < type2; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7580 | } |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 7581 | // Same basic group? Then sort by dex field index. This is guaranteed to be sorted |
| 7582 | // by name and for equal names by type id index. |
| 7583 | // NOTE: This works also for proxies. Their static fields are assigned appropriate indexes. |
| 7584 | return field1->GetDexFieldIndex() < field2->GetDexFieldIndex(); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7585 | } |
| 7586 | }; |
| 7587 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7588 | bool ClassLinker::LinkFields(Thread* self, |
| 7589 | Handle<mirror::Class> klass, |
| 7590 | bool is_static, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7591 | size_t* class_size) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 7592 | self->AllowThreadSuspension(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7593 | const size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7594 | LengthPrefixedArray<ArtField>* const fields = is_static ? klass->GetSFieldsPtr() : |
| 7595 | klass->GetIFieldsPtr(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7596 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 7597 | // Initialize field_offset |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 7598 | MemberOffset field_offset(0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7599 | if (is_static) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7600 | field_offset = klass->GetFirstReferenceStaticFieldOffsetDuringLinking(image_pointer_size_); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7601 | } else { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7602 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7603 | if (super_class != nullptr) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 7604 | CHECK(super_class->IsResolved()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7605 | << klass->PrettyClass() << " " << super_class->PrettyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7606 | field_offset = MemberOffset(super_class->GetObjectSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7607 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7608 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7609 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7610 | CHECK_EQ(num_fields == 0, fields == nullptr) << klass->PrettyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7611 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7612 | // we want a relatively stable order so that adding new fields |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7613 | // minimizes disruption of C++ version such as Class and Method. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7614 | // |
| 7615 | // The overall sort order order is: |
| 7616 | // 1) All object reference fields, sorted alphabetically. |
| 7617 | // 2) All java long (64-bit) integer fields, sorted alphabetically. |
| 7618 | // 3) All java double (64-bit) floating point fields, sorted alphabetically. |
| 7619 | // 4) All java int (32-bit) integer fields, sorted alphabetically. |
| 7620 | // 5) All java float (32-bit) floating point fields, sorted alphabetically. |
| 7621 | // 6) All java char (16-bit) integer fields, sorted alphabetically. |
| 7622 | // 7) All java short (16-bit) integer fields, sorted alphabetically. |
| 7623 | // 8) All java boolean (8-bit) integer fields, sorted alphabetically. |
| 7624 | // 9) All java byte (8-bit) integer fields, sorted alphabetically. |
| 7625 | // |
| 7626 | // Once the fields are sorted in this order we will attempt to fill any gaps that might be present |
| 7627 | // 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] | 7628 | std::deque<ArtField*> grouped_and_sorted_fields; |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 7629 | const char* old_no_suspend_cause = self->StartAssertNoThreadSuspension( |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 7630 | "Naked ArtField references in deque"); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7631 | for (size_t i = 0; i < num_fields; i++) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7632 | grouped_and_sorted_fields.push_back(&fields->At(i)); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7633 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 7634 | std::sort(grouped_and_sorted_fields.begin(), grouped_and_sorted_fields.end(), |
| 7635 | LinkFieldsComparator()); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7636 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7637 | // References should be at the front. |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7638 | size_t current_field = 0; |
| 7639 | size_t num_reference_fields = 0; |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7640 | FieldGaps gaps; |
| 7641 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7642 | for (; current_field < num_fields; current_field++) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7643 | ArtField* field = grouped_and_sorted_fields.front(); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 7644 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 7645 | bool isPrimitive = type != Primitive::kPrimNot; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7646 | if (isPrimitive) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 7647 | break; // past last reference, move on to the next phase |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7648 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7649 | if (UNLIKELY(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>( |
| 7650 | field_offset.Uint32Value()))) { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7651 | MemberOffset old_offset = field_offset; |
| 7652 | field_offset = MemberOffset(RoundUp(field_offset.Uint32Value(), 4)); |
| 7653 | AddFieldGap(old_offset.Uint32Value(), field_offset.Uint32Value(), &gaps); |
| 7654 | } |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 7655 | DCHECK_ALIGNED(field_offset.Uint32Value(), sizeof(mirror::HeapReference<mirror::Object>)); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7656 | grouped_and_sorted_fields.pop_front(); |
| 7657 | num_reference_fields++; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7658 | field->SetOffset(field_offset); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7659 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 7660 | sizeof(mirror::HeapReference<mirror::Object>)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7661 | } |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7662 | // Gaps are stored as a max heap which means that we must shuffle from largest to smallest |
| 7663 | // otherwise we could end up with suboptimal gap fills. |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7664 | ShuffleForward<8>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 7665 | ShuffleForward<4>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 7666 | ShuffleForward<2>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 7667 | ShuffleForward<1>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 7668 | CHECK(grouped_and_sorted_fields.empty()) << "Missed " << grouped_and_sorted_fields.size() << |
| 7669 | " fields."; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 7670 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7671 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7672 | // 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] | 7673 | if (!is_static && klass->DescriptorEquals("Ljava/lang/ref/Reference;")) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7674 | // We know there are no non-reference fields in the Reference classes, and we know |
| 7675 | // that 'referent' is alphabetically last, so this is easy... |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7676 | CHECK_EQ(num_reference_fields, num_fields) << klass->PrettyClass(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7677 | CHECK_STREQ(fields->At(num_fields - 1).GetName(), "referent") |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7678 | << klass->PrettyClass(); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7679 | --num_reference_fields; |
| 7680 | } |
| 7681 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 7682 | size_t size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7683 | // Update klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7684 | if (is_static) { |
| 7685 | klass->SetNumReferenceStaticFields(num_reference_fields); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 7686 | *class_size = size; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7687 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7688 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7689 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 7690 | if (num_reference_fields == 0 || super_class == nullptr) { |
| 7691 | // 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] | 7692 | // super_class is null iff the class is java.lang.Object. |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 7693 | if (super_class == nullptr || |
| 7694 | (super_class->GetClassFlags() & mirror::kClassFlagNoReferenceFields) != 0) { |
| 7695 | klass->SetClassFlags(klass->GetClassFlags() | mirror::kClassFlagNoReferenceFields); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 7696 | } |
| 7697 | } |
| 7698 | if (kIsDebugBuild) { |
| 7699 | DCHECK_EQ(super_class == nullptr, klass->DescriptorEquals("Ljava/lang/Object;")); |
| 7700 | size_t total_reference_instance_fields = 0; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7701 | ObjPtr<mirror::Class> cur_super = klass.Get(); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 7702 | while (cur_super != nullptr) { |
| 7703 | total_reference_instance_fields += cur_super->NumReferenceInstanceFieldsDuringLinking(); |
| 7704 | cur_super = cur_super->GetSuperClass(); |
| 7705 | } |
| 7706 | if (super_class == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7707 | CHECK_EQ(total_reference_instance_fields, 1u) << klass->PrettyDescriptor(); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 7708 | } else { |
| 7709 | // Check that there is at least num_reference_fields other than Object.class. |
| 7710 | CHECK_GE(total_reference_instance_fields, 1u + num_reference_fields) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7711 | << klass->PrettyClass(); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 7712 | } |
| 7713 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7714 | if (!klass->IsVariableSize()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7715 | std::string temp; |
| 7716 | DCHECK_GE(size, sizeof(mirror::Object)) << klass->GetDescriptor(&temp); |
| 7717 | size_t previous_size = klass->GetObjectSize(); |
| 7718 | if (previous_size != 0) { |
| 7719 | // Make sure that we didn't originally have an incorrect size. |
| 7720 | CHECK_EQ(previous_size, size) << klass->GetDescriptor(&temp); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 7721 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7722 | klass->SetObjectSize(size); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7723 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7724 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7725 | |
| 7726 | if (kIsDebugBuild) { |
| 7727 | // Make sure that the fields array is ordered by name but all reference |
| 7728 | // offsets are at the beginning as far as alignment allows. |
| 7729 | MemberOffset start_ref_offset = is_static |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7730 | ? klass->GetFirstReferenceStaticFieldOffsetDuringLinking(image_pointer_size_) |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7731 | : klass->GetFirstReferenceInstanceFieldOffset(); |
| 7732 | MemberOffset end_ref_offset(start_ref_offset.Uint32Value() + |
| 7733 | num_reference_fields * |
| 7734 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 7735 | MemberOffset current_ref_offset = start_ref_offset; |
| 7736 | for (size_t i = 0; i < num_fields; i++) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7737 | ArtField* field = &fields->At(i); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7738 | VLOG(class_linker) << "LinkFields: " << (is_static ? "static" : "instance") |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7739 | << " class=" << klass->PrettyClass() << " field=" << field->PrettyField() |
| 7740 | << " offset=" << field->GetOffsetDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7741 | if (i != 0) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7742 | ArtField* const prev_field = &fields->At(i - 1); |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 7743 | // NOTE: The field names can be the same. This is not possible in the Java language |
| 7744 | // 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] | 7745 | DCHECK_LE(strcmp(prev_field->GetName(), field->GetName()), 0); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7746 | } |
| 7747 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 7748 | bool is_primitive = type != Primitive::kPrimNot; |
| 7749 | if (klass->DescriptorEquals("Ljava/lang/ref/Reference;") && |
| 7750 | strcmp("referent", field->GetName()) == 0) { |
| 7751 | is_primitive = true; // We lied above, so we have to expect a lie here. |
| 7752 | } |
| 7753 | MemberOffset offset = field->GetOffsetDuringLinking(); |
| 7754 | if (is_primitive) { |
| 7755 | if (offset.Uint32Value() < end_ref_offset.Uint32Value()) { |
| 7756 | // Shuffled before references. |
| 7757 | size_t type_size = Primitive::ComponentSize(type); |
| 7758 | CHECK_LT(type_size, sizeof(mirror::HeapReference<mirror::Object>)); |
| 7759 | CHECK_LT(offset.Uint32Value(), start_ref_offset.Uint32Value()); |
| 7760 | CHECK_LE(offset.Uint32Value() + type_size, start_ref_offset.Uint32Value()); |
| 7761 | CHECK(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(offset.Uint32Value())); |
| 7762 | } |
| 7763 | } else { |
| 7764 | CHECK_EQ(current_ref_offset.Uint32Value(), offset.Uint32Value()); |
| 7765 | current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() + |
| 7766 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 7767 | } |
| 7768 | } |
| 7769 | CHECK_EQ(current_ref_offset.Uint32Value(), end_ref_offset.Uint32Value()); |
| 7770 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7771 | return true; |
| 7772 | } |
| 7773 | |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7774 | // Set the bitmap of reference instance field offsets. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7775 | void ClassLinker::CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7776 | uint32_t reference_offsets = 0; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7777 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7778 | // 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] | 7779 | if (super_class != nullptr) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7780 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7781 | // Compute reference offsets unless our superclass overflowed. |
| 7782 | if (reference_offsets != mirror::Class::kClassWalkSuper) { |
| 7783 | size_t num_reference_fields = klass->NumReferenceInstanceFieldsDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7784 | if (num_reference_fields != 0u) { |
| 7785 | // All of the fields that contain object references are guaranteed be grouped in memory |
| 7786 | // starting at an appropriately aligned address after super class object data. |
| 7787 | uint32_t start_offset = RoundUp(super_class->GetObjectSize(), |
| 7788 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 7789 | uint32_t start_bit = (start_offset - mirror::kObjectHeaderSize) / |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7790 | sizeof(mirror::HeapReference<mirror::Object>); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7791 | if (start_bit + num_reference_fields > 32) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7792 | reference_offsets = mirror::Class::kClassWalkSuper; |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7793 | } else { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7794 | reference_offsets |= (0xffffffffu << start_bit) & |
| 7795 | (0xffffffffu >> (32 - (start_bit + num_reference_fields))); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7796 | } |
| 7797 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 7798 | } |
| 7799 | } |
Mingyao Yang | faff0f0 | 2014-09-10 12:03:22 -0700 | [diff] [blame] | 7800 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7801 | } |
| 7802 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7803 | mirror::String* ClassLinker::ResolveString(const DexFile& dex_file, |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 7804 | dex::StringIndex string_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7805 | Handle<mirror::DexCache> dex_cache) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7806 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 7807 | Thread::PoisonObjectPointersIfDebug(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7808 | ObjPtr<mirror::String> resolved = dex_cache->GetResolvedString(string_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7809 | if (resolved != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7810 | return resolved.Ptr(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7811 | } |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 7812 | uint32_t utf16_length; |
| 7813 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7814 | ObjPtr<mirror::String> string = intern_table_->InternStrong(utf16_length, utf8_data); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 7815 | if (string != nullptr) { |
| 7816 | dex_cache->SetResolvedString(string_idx, string); |
| 7817 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7818 | return string.Ptr(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7819 | } |
| 7820 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7821 | mirror::String* ClassLinker::LookupString(const DexFile& dex_file, |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 7822 | dex::StringIndex string_idx, |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 7823 | ObjPtr<mirror::DexCache> dex_cache) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7824 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7825 | ObjPtr<mirror::String> resolved = dex_cache->GetResolvedString(string_idx); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7826 | if (resolved != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7827 | return resolved.Ptr(); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7828 | } |
| 7829 | uint32_t utf16_length; |
| 7830 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 7831 | ObjPtr<mirror::String> string = |
| 7832 | intern_table_->LookupStrong(Thread::Current(), utf16_length, utf8_data); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7833 | if (string != nullptr) { |
| 7834 | dex_cache->SetResolvedString(string_idx, string); |
| 7835 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7836 | return string.Ptr(); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7837 | } |
| 7838 | |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7839 | ObjPtr<mirror::Class> ClassLinker::LookupResolvedType(const DexFile& dex_file, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 7840 | dex::TypeIndex type_idx, |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7841 | ObjPtr<mirror::DexCache> dex_cache, |
| 7842 | ObjPtr<mirror::ClassLoader> class_loader) { |
| 7843 | ObjPtr<mirror::Class> type = dex_cache->GetResolvedType(type_idx); |
| 7844 | if (type == nullptr) { |
| 7845 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
| 7846 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
| 7847 | if (descriptor[1] == '\0') { |
| 7848 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 7849 | // for primitive classes that aren't backed by dex files. |
| 7850 | type = FindPrimitiveClass(descriptor[0]); |
| 7851 | } else { |
| 7852 | Thread* const self = Thread::Current(); |
| 7853 | DCHECK(self != nullptr); |
| 7854 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
| 7855 | // Find the class in the loaded classes table. |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 7856 | type = LookupClass(self, descriptor, hash, class_loader.Ptr()); |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7857 | } |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 7858 | if (type != nullptr) { |
| 7859 | if (type->IsResolved()) { |
| 7860 | dex_cache->SetResolvedType(type_idx, type); |
| 7861 | } else { |
| 7862 | type = nullptr; |
| 7863 | } |
| 7864 | } |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7865 | } |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 7866 | DCHECK(type == nullptr || type->IsResolved()); |
| 7867 | return type; |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7868 | } |
| 7869 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7870 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 7871 | dex::TypeIndex type_idx, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7872 | ObjPtr<mirror::Class> referrer) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 7873 | StackHandleScope<2> hs(Thread::Current()); |
| 7874 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 7875 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 7876 | return ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 7877 | } |
| 7878 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7879 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 7880 | dex::TypeIndex type_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7881 | Handle<mirror::DexCache> dex_cache, |
| 7882 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7883 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 7884 | Thread::PoisonObjectPointersIfDebug(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7885 | ObjPtr<mirror::Class> resolved = dex_cache->GetResolvedType(type_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7886 | if (resolved == nullptr) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 7887 | // TODO: Avoid this lookup as it duplicates work done in FindClass(). It is here |
| 7888 | // as a workaround for FastNative JNI to avoid AssertNoPendingException() when |
| 7889 | // trying to resolve annotations while an exception may be pending. Bug: 34659969 |
| 7890 | resolved = LookupResolvedType(dex_file, type_idx, dex_cache.Get(), class_loader.Get()); |
| 7891 | } |
| 7892 | if (resolved == nullptr) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 7893 | Thread* self = Thread::Current(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 7894 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 7895 | resolved = FindClass(self, descriptor, class_loader); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7896 | if (resolved != nullptr) { |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 7897 | // TODO: we used to throw here if resolved's class loader was not the |
| 7898 | // boot class loader. This was to permit different classes with the |
| 7899 | // same name to be loaded simultaneously by different loaders |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7900 | dex_cache->SetResolvedType(type_idx, resolved); |
| 7901 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 7902 | CHECK(self->IsExceptionPending()) |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 7903 | << "Expected pending exception for failed resolution of: " << descriptor; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 7904 | // Convert a ClassNotFoundException to a NoClassDefFoundError. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 7905 | StackHandleScope<1> hs(self); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 7906 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 7907 | if (cause->InstanceOf(GetClassRoot(kJavaLangClassNotFoundException))) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7908 | DCHECK(resolved == nullptr); // No Handle needed to preserve resolved. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 7909 | self->ClearException(); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 7910 | ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 7911 | self->GetException()->SetCause(cause.Get()); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 7912 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7913 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7914 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 7915 | DCHECK((resolved == nullptr) || resolved->IsResolved()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7916 | << resolved->PrettyDescriptor() << " " << resolved->GetStatus(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7917 | return resolved.Ptr(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7918 | } |
| 7919 | |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 7920 | template <ClassLinker::ResolveMode kResolveMode> |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7921 | ArtMethod* ClassLinker::ResolveMethod(const DexFile& dex_file, |
| 7922 | uint32_t method_idx, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7923 | Handle<mirror::DexCache> dex_cache, |
| 7924 | Handle<mirror::ClassLoader> class_loader, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7925 | ArtMethod* referrer, |
| 7926 | InvokeType type) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7927 | DCHECK(dex_cache != nullptr); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7928 | // Check for hit in the dex cache. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7929 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 7930 | Thread::PoisonObjectPointersIfDebug(); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 7931 | if (resolved != nullptr && !resolved->IsRuntimeMethod()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7932 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 7933 | if (kResolveMode == ClassLinker::kForceICCECheck) { |
| 7934 | if (resolved->CheckIncompatibleClassChange(type)) { |
| 7935 | ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer); |
| 7936 | return nullptr; |
| 7937 | } |
| 7938 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7939 | return resolved; |
| 7940 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7941 | // Fail, get the declaring class. |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7942 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7943 | ObjPtr<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] | 7944 | if (klass == nullptr) { |
Elliott Hughes | cc5f9a9 | 2011-09-28 19:17:29 -0700 | [diff] [blame] | 7945 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7946 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7947 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7948 | // Scan using method_idx, this saves string compares but will only hit for matching dex |
| 7949 | // caches/files. |
| 7950 | switch (type) { |
| 7951 | case kDirect: // Fall-through. |
| 7952 | case kStatic: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7953 | resolved = klass->FindDirectMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 7954 | DCHECK(resolved == nullptr || resolved->GetDeclaringClassUnchecked() != nullptr); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7955 | break; |
| 7956 | case kInterface: |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 7957 | // We have to check whether the method id really belongs to an interface (dex static bytecode |
| 7958 | // constraint A15). Otherwise you must not invoke-interface on it. |
| 7959 | // |
| 7960 | // This is not symmetric to A12-A14 (direct, static, virtual), as using FindInterfaceMethod |
| 7961 | // assumes that the given type is an interface, and will check the interface table if the |
| 7962 | // method isn't declared in the class. So it may find an interface method (usually by name |
| 7963 | // in the handling below, but we do the constraint check early). In that case, |
| 7964 | // CheckIncompatibleClassChange will succeed (as it is called on an interface method) |
| 7965 | // unexpectedly. |
| 7966 | // Example: |
| 7967 | // interface I { |
| 7968 | // foo() |
| 7969 | // } |
| 7970 | // class A implements I { |
| 7971 | // ... |
| 7972 | // } |
| 7973 | // class B extends A { |
| 7974 | // ... |
| 7975 | // } |
| 7976 | // invoke-interface B.foo |
| 7977 | // -> FindInterfaceMethod finds I.foo (interface method), not A.foo (miranda method) |
| 7978 | if (UNLIKELY(!klass->IsInterface())) { |
| 7979 | ThrowIncompatibleClassChangeError(klass, |
| 7980 | "Found class %s, but interface was expected", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7981 | klass->PrettyDescriptor().c_str()); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 7982 | return nullptr; |
| 7983 | } else { |
| 7984 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 7985 | DCHECK(resolved == nullptr || resolved->GetDeclaringClass()->IsInterface()); |
| 7986 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7987 | break; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7988 | case kSuper: |
| 7989 | if (klass->IsInterface()) { |
| 7990 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 7991 | } else { |
| 7992 | resolved = klass->FindVirtualMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 7993 | } |
| 7994 | break; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7995 | case kVirtual: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7996 | resolved = klass->FindVirtualMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7997 | break; |
| 7998 | default: |
| 7999 | LOG(FATAL) << "Unreachable - invocation type: " << type; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 8000 | UNREACHABLE(); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 8001 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8002 | if (resolved == nullptr) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8003 | // Search by name, which works across dex files. |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 8004 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 8005 | const Signature signature = dex_file.GetMethodSignature(method_id); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8006 | switch (type) { |
| 8007 | case kDirect: // Fall-through. |
| 8008 | case kStatic: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8009 | resolved = klass->FindDirectMethod(name, signature, image_pointer_size_); |
| 8010 | DCHECK(resolved == nullptr || resolved->GetDeclaringClassUnchecked() != nullptr); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8011 | break; |
| 8012 | case kInterface: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8013 | resolved = klass->FindInterfaceMethod(name, signature, image_pointer_size_); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8014 | DCHECK(resolved == nullptr || resolved->GetDeclaringClass()->IsInterface()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8015 | break; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8016 | case kSuper: |
| 8017 | if (klass->IsInterface()) { |
| 8018 | resolved = klass->FindInterfaceMethod(name, signature, image_pointer_size_); |
| 8019 | } else { |
| 8020 | resolved = klass->FindVirtualMethod(name, signature, image_pointer_size_); |
| 8021 | } |
| 8022 | break; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8023 | case kVirtual: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8024 | resolved = klass->FindVirtualMethod(name, signature, image_pointer_size_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8025 | break; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 8026 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8027 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8028 | // If we found a method, check for incompatible class changes. |
| 8029 | if (LIKELY(resolved != nullptr && !resolved->CheckIncompatibleClassChange(type))) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8030 | // 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] | 8031 | dex_cache->SetResolvedMethod(method_idx, resolved, image_pointer_size_); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8032 | return resolved; |
| 8033 | } else { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8034 | // If we had a method, it's an incompatible-class-change error. |
| 8035 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8036 | ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8037 | } else { |
| 8038 | // We failed to find the method which means either an access error, an incompatible class |
| 8039 | // change, or no such method. First try to find the method among direct and virtual methods. |
| 8040 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 8041 | const Signature signature = dex_file.GetMethodSignature(method_id); |
| 8042 | switch (type) { |
| 8043 | case kDirect: |
| 8044 | case kStatic: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8045 | resolved = klass->FindVirtualMethod(name, signature, image_pointer_size_); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8046 | // Note: kDirect and kStatic are also mutually exclusive, but in that case we would |
| 8047 | // have had a resolved method before, which triggers the "true" branch above. |
| 8048 | break; |
| 8049 | case kInterface: |
| 8050 | case kVirtual: |
| 8051 | case kSuper: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8052 | resolved = klass->FindDirectMethod(name, signature, image_pointer_size_); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8053 | break; |
| 8054 | } |
| 8055 | |
| 8056 | // 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] | 8057 | bool exception_generated = false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8058 | if (resolved != nullptr && referrer != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8059 | ObjPtr<mirror::Class> methods_class = resolved->GetDeclaringClass(); |
| 8060 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8061 | if (!referring_class->CanAccess(methods_class)) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 8062 | ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, |
| 8063 | methods_class, |
| 8064 | resolved, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8065 | type); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8066 | exception_generated = true; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8067 | } else if (!referring_class->CanAccessMember(methods_class, resolved->GetAccessFlags())) { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8068 | ThrowIllegalAccessErrorMethod(referring_class, resolved); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8069 | exception_generated = true; |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8070 | } |
| 8071 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8072 | if (!exception_generated) { |
| 8073 | // Otherwise, throw an IncompatibleClassChangeError if we found something, and check |
| 8074 | // interface methods and throw if we find the method there. If we find nothing, throw a |
| 8075 | // NoSuchMethodError. |
| 8076 | switch (type) { |
| 8077 | case kDirect: |
| 8078 | case kStatic: |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8079 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8080 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8081 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8082 | resolved = klass->FindInterfaceMethod(name, signature, image_pointer_size_); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8083 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8084 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8085 | } else { |
| 8086 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 8087 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8088 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8089 | break; |
| 8090 | case kInterface: |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8091 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8092 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8093 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8094 | resolved = klass->FindVirtualMethod(name, signature, image_pointer_size_); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8095 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8096 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8097 | } else { |
| 8098 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 8099 | } |
| 8100 | } |
| 8101 | break; |
| 8102 | case kSuper: |
| 8103 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8104 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8105 | } else { |
| 8106 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 8107 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8108 | break; |
| 8109 | case kVirtual: |
| 8110 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8111 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8112 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8113 | resolved = klass->FindInterfaceMethod(name, signature, image_pointer_size_); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8114 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8115 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8116 | } else { |
| 8117 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 8118 | } |
| 8119 | } |
| 8120 | break; |
| 8121 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8122 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8123 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8124 | Thread::Current()->AssertPendingException(); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8125 | return nullptr; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8126 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8127 | } |
| 8128 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 8129 | ArtMethod* ClassLinker::ResolveMethodWithoutInvokeType(const DexFile& dex_file, |
| 8130 | uint32_t method_idx, |
| 8131 | Handle<mirror::DexCache> dex_cache, |
| 8132 | Handle<mirror::ClassLoader> class_loader) { |
| 8133 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 8134 | Thread::PoisonObjectPointersIfDebug(); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 8135 | if (resolved != nullptr && !resolved->IsRuntimeMethod()) { |
| 8136 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
| 8137 | return resolved; |
| 8138 | } |
| 8139 | // Fail, get the declaring class. |
| 8140 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8141 | ObjPtr<mirror::Class> klass = ResolveType(dex_file, method_id.class_idx_, dex_cache, class_loader); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 8142 | if (klass == nullptr) { |
| 8143 | Thread::Current()->AssertPendingException(); |
| 8144 | return nullptr; |
| 8145 | } |
| 8146 | if (klass->IsInterface()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8147 | LOG(FATAL) << "ResolveAmbiguousMethod: unexpected method in interface: " |
| 8148 | << klass->PrettyClass(); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 8149 | return nullptr; |
| 8150 | } |
| 8151 | |
| 8152 | // Search both direct and virtual methods |
| 8153 | resolved = klass->FindDirectMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 8154 | if (resolved == nullptr) { |
| 8155 | resolved = klass->FindVirtualMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 8156 | } |
| 8157 | |
| 8158 | return resolved; |
| 8159 | } |
| 8160 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 8161 | ArtField* ClassLinker::LookupResolvedField(uint32_t field_idx, |
| 8162 | ObjPtr<mirror::DexCache> dex_cache, |
| 8163 | ObjPtr<mirror::ClassLoader> class_loader, |
| 8164 | bool is_static) { |
| 8165 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 8166 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
| 8167 | ObjPtr<mirror::Class> klass = dex_cache->GetResolvedType(field_id.class_idx_); |
| 8168 | if (klass == nullptr) { |
| 8169 | klass = LookupResolvedType(dex_file, field_id.class_idx_, dex_cache, class_loader); |
| 8170 | } |
| 8171 | if (klass == nullptr) { |
| 8172 | // The class has not been resolved yet, so the field is also unresolved. |
| 8173 | return nullptr; |
| 8174 | } |
| 8175 | DCHECK(klass->IsResolved()); |
| 8176 | Thread* self = is_static ? Thread::Current() : nullptr; |
| 8177 | |
| 8178 | // First try to find a field declared directly by `klass` by the field index. |
| 8179 | ArtField* resolved_field = is_static |
| 8180 | ? mirror::Class::FindStaticField(self, klass, dex_cache, field_idx) |
| 8181 | : klass->FindInstanceField(dex_cache, field_idx); |
| 8182 | |
| 8183 | if (resolved_field == nullptr) { |
| 8184 | // If not found in `klass` by field index, search the class hierarchy using the name and type. |
| 8185 | const char* name = dex_file.GetFieldName(field_id); |
| 8186 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 8187 | resolved_field = is_static |
| 8188 | ? mirror::Class::FindStaticField(self, klass, name, type) |
| 8189 | : klass->FindInstanceField(name, type); |
| 8190 | } |
| 8191 | |
| 8192 | if (resolved_field != nullptr) { |
| 8193 | dex_cache->SetResolvedField(field_idx, resolved_field, image_pointer_size_); |
| 8194 | } |
| 8195 | return resolved_field; |
| 8196 | } |
| 8197 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 8198 | ArtField* ClassLinker::ResolveField(const DexFile& dex_file, |
| 8199 | uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8200 | Handle<mirror::DexCache> dex_cache, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 8201 | Handle<mirror::ClassLoader> class_loader, |
| 8202 | bool is_static) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8203 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8204 | ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 8205 | Thread::PoisonObjectPointersIfDebug(); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 8206 | if (resolved != nullptr) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8207 | return resolved; |
| 8208 | } |
| 8209 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 8210 | Thread* const self = Thread::Current(); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8211 | ObjPtr<mirror::Class> klass = ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader); |
| 8212 | if (klass == nullptr) { |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 8213 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 8214 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8215 | } |
| 8216 | |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 8217 | if (is_static) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8218 | resolved = mirror::Class::FindStaticField(self, klass, dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 8219 | } else { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 8220 | resolved = klass->FindInstanceField(dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 8221 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 8222 | |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 8223 | if (resolved == nullptr) { |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 8224 | const char* name = dex_file.GetFieldName(field_id); |
| 8225 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 8226 | if (is_static) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 8227 | resolved = mirror::Class::FindStaticField(self, klass, name, type); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 8228 | } else { |
| 8229 | resolved = klass->FindInstanceField(name, type); |
| 8230 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 8231 | if (resolved == nullptr) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8232 | ThrowNoSuchFieldError(is_static ? "static " : "instance ", klass, type, name); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8233 | return nullptr; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 8234 | } |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8235 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8236 | dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8237 | return resolved; |
| 8238 | } |
| 8239 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 8240 | ArtField* ClassLinker::ResolveFieldJLS(const DexFile& dex_file, |
| 8241 | uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8242 | Handle<mirror::DexCache> dex_cache, |
| 8243 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8244 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8245 | ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 8246 | Thread::PoisonObjectPointersIfDebug(); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 8247 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8248 | return resolved; |
| 8249 | } |
| 8250 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 8251 | Thread* self = Thread::Current(); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8252 | ObjPtr<mirror::Class> klass(ResolveType(dex_file, field_id.class_idx_, dex_cache, class_loader)); |
| 8253 | if (klass == nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8254 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8255 | return nullptr; |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8256 | } |
| 8257 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 8258 | StringPiece name(dex_file.GetFieldName(field_id)); |
| 8259 | StringPiece type(dex_file.GetFieldTypeDescriptor(field_id)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 8260 | resolved = mirror::Class::FindField(self, klass, name, type); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8261 | if (resolved != nullptr) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8262 | dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8263 | } else { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8264 | ThrowNoSuchFieldError("", klass, type, name); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8265 | } |
| 8266 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 8267 | } |
| 8268 | |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8269 | mirror::MethodType* ClassLinker::ResolveMethodType(const DexFile& dex_file, |
| 8270 | uint32_t proto_idx, |
| 8271 | Handle<mirror::DexCache> dex_cache, |
| 8272 | Handle<mirror::ClassLoader> class_loader) { |
| 8273 | DCHECK(Runtime::Current()->IsMethodHandlesEnabled()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8274 | DCHECK(dex_cache != nullptr); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8275 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8276 | ObjPtr<mirror::MethodType> resolved = dex_cache->GetResolvedMethodType(proto_idx); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8277 | if (resolved != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8278 | return resolved.Ptr(); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8279 | } |
| 8280 | |
| 8281 | Thread* const self = Thread::Current(); |
| 8282 | StackHandleScope<4> hs(self); |
| 8283 | |
| 8284 | // First resolve the return type. |
| 8285 | const DexFile::ProtoId& proto_id = dex_file.GetProtoId(proto_idx); |
| 8286 | Handle<mirror::Class> return_type(hs.NewHandle( |
| 8287 | ResolveType(dex_file, proto_id.return_type_idx_, dex_cache, class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8288 | if (return_type == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8289 | DCHECK(self->IsExceptionPending()); |
| 8290 | return nullptr; |
| 8291 | } |
| 8292 | |
| 8293 | // Then resolve the argument types. |
| 8294 | // |
| 8295 | // TODO: Is there a better way to figure out the number of method arguments |
| 8296 | // other than by looking at the shorty ? |
| 8297 | const size_t num_method_args = strlen(dex_file.StringDataByIdx(proto_id.shorty_idx_)) - 1; |
| 8298 | |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 8299 | ObjPtr<mirror::Class> class_type = mirror::Class::GetJavaLangClass(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8300 | ObjPtr<mirror::Class> array_of_class = FindArrayClass(self, &class_type); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8301 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 8302 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_method_args))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8303 | if (method_params == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8304 | DCHECK(self->IsExceptionPending()); |
| 8305 | return nullptr; |
| 8306 | } |
| 8307 | |
| 8308 | DexFileParameterIterator it(dex_file, proto_id); |
| 8309 | int32_t i = 0; |
| 8310 | MutableHandle<mirror::Class> param_class = hs.NewHandle<mirror::Class>(nullptr); |
| 8311 | for (; it.HasNext(); it.Next()) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 8312 | const dex::TypeIndex type_idx = it.GetTypeIdx(); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8313 | param_class.Assign(ResolveType(dex_file, type_idx, dex_cache, class_loader)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8314 | if (param_class == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8315 | DCHECK(self->IsExceptionPending()); |
| 8316 | return nullptr; |
| 8317 | } |
| 8318 | |
| 8319 | method_params->Set(i++, param_class.Get()); |
| 8320 | } |
| 8321 | |
| 8322 | DCHECK(!it.HasNext()); |
| 8323 | |
| 8324 | Handle<mirror::MethodType> type = hs.NewHandle( |
| 8325 | mirror::MethodType::Create(self, return_type, method_params)); |
| 8326 | dex_cache->SetResolvedMethodType(proto_idx, type.Get()); |
| 8327 | |
| 8328 | return type.Get(); |
| 8329 | } |
| 8330 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8331 | mirror::MethodHandle* ClassLinker::ResolveMethodHandle(uint32_t method_handle_idx, |
| 8332 | ArtMethod* referrer) |
| 8333 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8334 | Thread* const self = Thread::Current(); |
| 8335 | const DexFile* const dex_file = referrer->GetDexFile(); |
| 8336 | const DexFile::MethodHandleItem& mh = dex_file->GetMethodHandle(method_handle_idx); |
| 8337 | |
| 8338 | union { |
| 8339 | ArtField* field; |
| 8340 | ArtMethod* method; |
| 8341 | uintptr_t field_or_method; |
| 8342 | } target; |
| 8343 | uint32_t num_params; |
| 8344 | mirror::MethodHandle::Kind kind; |
| 8345 | DexFile::MethodHandleType handle_type = |
| 8346 | static_cast<DexFile::MethodHandleType>(mh.method_handle_type_); |
| 8347 | switch (handle_type) { |
| 8348 | case DexFile::MethodHandleType::kStaticPut: { |
| 8349 | kind = mirror::MethodHandle::Kind::kStaticPut; |
| 8350 | target.field = ResolveField(mh.field_or_method_idx_, referrer, true /* is_static */); |
| 8351 | num_params = 1; |
| 8352 | break; |
| 8353 | } |
| 8354 | case DexFile::MethodHandleType::kStaticGet: { |
| 8355 | kind = mirror::MethodHandle::Kind::kStaticGet; |
| 8356 | target.field = ResolveField(mh.field_or_method_idx_, referrer, true /* is_static */); |
| 8357 | num_params = 0; |
| 8358 | break; |
| 8359 | } |
| 8360 | case DexFile::MethodHandleType::kInstancePut: { |
| 8361 | kind = mirror::MethodHandle::Kind::kInstancePut; |
| 8362 | target.field = ResolveField(mh.field_or_method_idx_, referrer, false /* is_static */); |
| 8363 | num_params = 2; |
| 8364 | break; |
| 8365 | } |
| 8366 | case DexFile::MethodHandleType::kInstanceGet: { |
| 8367 | kind = mirror::MethodHandle::Kind::kInstanceGet; |
| 8368 | target.field = ResolveField(mh.field_or_method_idx_, referrer, false /* is_static */); |
| 8369 | num_params = 1; |
| 8370 | break; |
| 8371 | } |
| 8372 | case DexFile::MethodHandleType::kInvokeStatic: { |
| 8373 | kind = mirror::MethodHandle::Kind::kInvokeStatic; |
| 8374 | target.method = ResolveMethod<kNoICCECheckForCache>(self, |
| 8375 | mh.field_or_method_idx_, |
| 8376 | referrer, |
| 8377 | InvokeType::kStatic); |
| 8378 | uint32_t shorty_length; |
| 8379 | target.method->GetShorty(&shorty_length); |
| 8380 | num_params = shorty_length - 1; // Remove 1 for return value. |
| 8381 | break; |
| 8382 | } |
| 8383 | case DexFile::MethodHandleType::kInvokeInstance: { |
| 8384 | kind = mirror::MethodHandle::Kind::kInvokeVirtual; |
| 8385 | target.method = ResolveMethod<kNoICCECheckForCache>(self, |
| 8386 | mh.field_or_method_idx_, |
| 8387 | referrer, |
| 8388 | InvokeType::kVirtual); |
| 8389 | uint32_t shorty_length; |
| 8390 | target.method->GetShorty(&shorty_length); |
| 8391 | num_params = shorty_length - 1; // Remove 1 for return value. |
| 8392 | break; |
| 8393 | } |
| 8394 | case DexFile::MethodHandleType::kInvokeConstructor: { |
| 8395 | UNIMPLEMENTED(FATAL) << "Invoke constructor is implemented as a transform."; |
| 8396 | num_params = 0; |
| 8397 | } |
| 8398 | } |
| 8399 | |
| 8400 | StackHandleScope<5> hs(self); |
| 8401 | ObjPtr<mirror::Class> class_type = mirror::Class::GetJavaLangClass(); |
| 8402 | ObjPtr<mirror::Class> array_of_class = FindArrayClass(self, &class_type); |
| 8403 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 8404 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params))); |
| 8405 | if (method_params.Get() == nullptr) { |
| 8406 | DCHECK(self->IsExceptionPending()); |
| 8407 | return nullptr; |
| 8408 | } |
| 8409 | |
| 8410 | Handle<mirror::Class> return_type; |
| 8411 | switch (handle_type) { |
| 8412 | case DexFile::MethodHandleType::kStaticPut: { |
| 8413 | method_params->Set(0, target.field->GetType<true>()); |
| 8414 | return_type = hs.NewHandle(FindPrimitiveClass('V')); |
| 8415 | break; |
| 8416 | } |
| 8417 | case DexFile::MethodHandleType::kStaticGet: { |
| 8418 | return_type = hs.NewHandle(target.field->GetType<true>()); |
| 8419 | break; |
| 8420 | } |
| 8421 | case DexFile::MethodHandleType::kInstancePut: { |
| 8422 | method_params->Set(0, target.field->GetDeclaringClass()); |
| 8423 | method_params->Set(1, target.field->GetType<true>()); |
| 8424 | return_type = hs.NewHandle(FindPrimitiveClass('V')); |
| 8425 | break; |
| 8426 | } |
| 8427 | case DexFile::MethodHandleType::kInstanceGet: { |
| 8428 | method_params->Set(0, target.field->GetDeclaringClass()); |
| 8429 | return_type = hs.NewHandle(target.field->GetType<true>()); |
| 8430 | break; |
| 8431 | } |
| 8432 | case DexFile::MethodHandleType::kInvokeStatic: |
| 8433 | case DexFile::MethodHandleType::kInvokeInstance: { |
| 8434 | // TODO(oth): This will not work for varargs methods as this |
| 8435 | // requires instantiating a Transformer. This resolution step |
| 8436 | // would be best done in managed code rather than in the run |
| 8437 | // time (b/35235705) |
| 8438 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 8439 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
| 8440 | DexFileParameterIterator it(*dex_file, target.method->GetPrototype()); |
| 8441 | for (int32_t i = 0; it.HasNext(); i++, it.Next()) { |
| 8442 | const dex::TypeIndex type_idx = it.GetTypeIdx(); |
| 8443 | mirror::Class* klass = ResolveType(*dex_file, type_idx, dex_cache, class_loader); |
| 8444 | if (nullptr == klass) { |
| 8445 | DCHECK(self->IsExceptionPending()); |
| 8446 | return nullptr; |
| 8447 | } |
| 8448 | method_params->Set(i, klass); |
| 8449 | } |
| 8450 | return_type = hs.NewHandle(target.method->GetReturnType(true)); |
| 8451 | break; |
| 8452 | } |
| 8453 | case DexFile::MethodHandleType::kInvokeConstructor: { |
| 8454 | // TODO(oth): b/35235705 |
| 8455 | UNIMPLEMENTED(FATAL) << "Invoke constructor is implemented as a transform."; |
| 8456 | } |
| 8457 | } |
| 8458 | |
| 8459 | if (return_type.IsNull()) { |
| 8460 | DCHECK(self->IsExceptionPending()); |
| 8461 | return nullptr; |
| 8462 | } |
| 8463 | |
| 8464 | Handle<mirror::MethodType> |
| 8465 | mt(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params))); |
| 8466 | if (mt.IsNull()) { |
| 8467 | DCHECK(self->IsExceptionPending()); |
| 8468 | return nullptr; |
| 8469 | } |
| 8470 | return mirror::MethodHandleImpl::Create(self, target.field_or_method, kind, mt); |
| 8471 | } |
| 8472 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8473 | bool ClassLinker::IsQuickResolutionStub(const void* entry_point) const { |
| 8474 | return (entry_point == GetQuickResolutionStub()) || |
| 8475 | (quick_resolution_trampoline_ == entry_point); |
| 8476 | } |
| 8477 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8478 | bool ClassLinker::IsQuickToInterpreterBridge(const void* entry_point) const { |
| 8479 | return (entry_point == GetQuickToInterpreterBridge()) || |
| 8480 | (quick_to_interpreter_bridge_trampoline_ == entry_point); |
| 8481 | } |
| 8482 | |
| 8483 | bool ClassLinker::IsQuickGenericJniStub(const void* entry_point) const { |
| 8484 | return (entry_point == GetQuickGenericJniStub()) || |
| 8485 | (quick_generic_jni_trampoline_ == entry_point); |
| 8486 | } |
| 8487 | |
| 8488 | const void* ClassLinker::GetRuntimeQuickGenericJniStub() const { |
| 8489 | return GetQuickGenericJniStub(); |
| 8490 | } |
| 8491 | |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 8492 | void ClassLinker::SetEntryPointsToCompiledCode(ArtMethod* method, const void* code) const { |
| 8493 | CHECK(code != nullptr); |
| 8494 | const uint8_t* base = reinterpret_cast<const uint8_t*>(code); // Base of data points at code. |
| 8495 | base -= sizeof(void*); // Move backward so that code_offset != 0. |
| 8496 | const uint32_t code_offset = sizeof(void*); |
| 8497 | OatFile::OatMethod oat_method(base, code_offset); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8498 | oat_method.LinkMethod(method); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8499 | } |
| 8500 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8501 | void ClassLinker::SetEntryPointsToInterpreter(ArtMethod* method) const { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8502 | if (!method->IsNative()) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8503 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 8504 | } else { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 8505 | SetEntryPointsToCompiledCode(method, GetQuickGenericJniStub()); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8506 | } |
| 8507 | } |
| 8508 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 8509 | void ClassLinker::DumpForSigQuit(std::ostream& os) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 8510 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 8511 | ReaderMutexLock mu(soa.Self(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 8512 | os << "Zygote loaded classes=" << NumZygoteClasses() << " post zygote classes=" |
| 8513 | << NumNonZygoteClasses() << "\n"; |
| 8514 | } |
| 8515 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8516 | class CountClassesVisitor : public ClassLoaderVisitor { |
| 8517 | public: |
| 8518 | CountClassesVisitor() : num_zygote_classes(0), num_non_zygote_classes(0) {} |
| 8519 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8520 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 8521 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) OVERRIDE { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8522 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 8523 | if (class_table != nullptr) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 8524 | num_zygote_classes += class_table->NumZygoteClasses(class_loader); |
| 8525 | num_non_zygote_classes += class_table->NumNonZygoteClasses(class_loader); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 8526 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 8527 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8528 | |
| 8529 | size_t num_zygote_classes; |
| 8530 | size_t num_non_zygote_classes; |
| 8531 | }; |
| 8532 | |
| 8533 | size_t ClassLinker::NumZygoteClasses() const { |
| 8534 | CountClassesVisitor visitor; |
| 8535 | VisitClassLoaders(&visitor); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 8536 | return visitor.num_zygote_classes + boot_class_table_.NumZygoteClasses(nullptr); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 8537 | } |
| 8538 | |
| 8539 | size_t ClassLinker::NumNonZygoteClasses() const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8540 | CountClassesVisitor visitor; |
| 8541 | VisitClassLoaders(&visitor); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 8542 | return visitor.num_non_zygote_classes + boot_class_table_.NumNonZygoteClasses(nullptr); |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 8543 | } |
| 8544 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 8545 | size_t ClassLinker::NumLoadedClasses() { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 8546 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 8547 | // 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] | 8548 | return NumNonZygoteClasses(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 8549 | } |
| 8550 | |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 8551 | pid_t ClassLinker::GetClassesLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 8552 | return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 8553 | } |
| 8554 | |
| 8555 | pid_t ClassLinker::GetDexLockOwner() { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 8556 | return Locks::dex_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 8557 | } |
| 8558 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8559 | void ClassLinker::SetClassRoot(ClassRoot class_root, ObjPtr<mirror::Class> klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 8560 | DCHECK(!init_done_); |
| 8561 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8562 | DCHECK(klass != nullptr); |
| 8563 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 8564 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 8565 | mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8566 | DCHECK(class_roots != nullptr); |
| 8567 | DCHECK(class_roots->Get(class_root) == nullptr); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 8568 | class_roots->Set<false>(class_root, klass); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 8569 | } |
| 8570 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8571 | const char* ClassLinker::GetClassRootDescriptor(ClassRoot class_root) { |
| 8572 | static const char* class_roots_descriptors[] = { |
| 8573 | "Ljava/lang/Class;", |
| 8574 | "Ljava/lang/Object;", |
| 8575 | "[Ljava/lang/Class;", |
| 8576 | "[Ljava/lang/Object;", |
| 8577 | "Ljava/lang/String;", |
| 8578 | "Ljava/lang/DexCache;", |
| 8579 | "Ljava/lang/ref/Reference;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 8580 | "Ljava/lang/reflect/Constructor;", |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 8581 | "Ljava/lang/reflect/Field;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 8582 | "Ljava/lang/reflect/Method;", |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8583 | "Ljava/lang/reflect/Proxy;", |
| 8584 | "[Ljava/lang/String;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 8585 | "[Ljava/lang/reflect/Constructor;", |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 8586 | "[Ljava/lang/reflect/Field;", |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 8587 | "[Ljava/lang/reflect/Method;", |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8588 | "Ljava/lang/invoke/CallSite;", |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8589 | "Ljava/lang/invoke/MethodHandleImpl;", |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8590 | "Ljava/lang/invoke/MethodHandles$Lookup;", |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8591 | "Ljava/lang/invoke/MethodType;", |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8592 | "Ljava/lang/ClassLoader;", |
| 8593 | "Ljava/lang/Throwable;", |
| 8594 | "Ljava/lang/ClassNotFoundException;", |
| 8595 | "Ljava/lang/StackTraceElement;", |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 8596 | "Ldalvik/system/EmulatedStackFrame;", |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8597 | "Z", |
| 8598 | "B", |
| 8599 | "C", |
| 8600 | "D", |
| 8601 | "F", |
| 8602 | "I", |
| 8603 | "J", |
| 8604 | "S", |
| 8605 | "V", |
| 8606 | "[Z", |
| 8607 | "[B", |
| 8608 | "[C", |
| 8609 | "[D", |
| 8610 | "[F", |
| 8611 | "[I", |
| 8612 | "[J", |
| 8613 | "[S", |
| 8614 | "[Ljava/lang/StackTraceElement;", |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 8615 | "Ldalvik/system/ClassExt;", |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8616 | }; |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 8617 | static_assert(arraysize(class_roots_descriptors) == size_t(kClassRootsMax), |
| 8618 | "Mismatch between class descriptors and class-root enum"); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8619 | |
| 8620 | const char* descriptor = class_roots_descriptors[class_root]; |
| 8621 | CHECK(descriptor != nullptr); |
| 8622 | return descriptor; |
| 8623 | } |
| 8624 | |
Jeff Hao | 5872d7c | 2016-04-27 11:07:41 -0700 | [diff] [blame] | 8625 | jobject ClassLinker::CreatePathClassLoader(Thread* self, |
| 8626 | const std::vector<const DexFile*>& dex_files) { |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8627 | // SOAAlreadyRunnable is protected, and we need something to add a global reference. |
| 8628 | // We could move the jobject to the callers, but all call-sites do this... |
| 8629 | ScopedObjectAccessUnchecked soa(self); |
| 8630 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8631 | // For now, create a libcore-level DexFile for each ART DexFile. This "explodes" multidex. |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8632 | StackHandleScope<6> hs(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8633 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8634 | ArtField* dex_elements_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8635 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8636 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 8637 | Handle<mirror::Class> dex_elements_class(hs.NewHandle(dex_elements_field->GetType<true>())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8638 | DCHECK(dex_elements_class != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8639 | DCHECK(dex_elements_class->IsArrayClass()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 8640 | Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements(hs.NewHandle( |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 8641 | mirror::ObjectArray<mirror::Object>::Alloc(self, |
| 8642 | dex_elements_class.Get(), |
| 8643 | dex_files.size()))); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8644 | Handle<mirror::Class> h_dex_element_class = |
| 8645 | hs.NewHandle(dex_elements_class->GetComponentType()); |
| 8646 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8647 | ArtField* element_file_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8648 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8649 | DCHECK_EQ(h_dex_element_class.Get(), element_file_field->GetDeclaringClass()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8650 | |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8651 | ArtField* cookie_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8652 | DCHECK_EQ(cookie_field->GetDeclaringClass(), element_file_field->GetType<false>()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8653 | |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8654 | ArtField* file_name_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_fileName); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 8655 | DCHECK_EQ(file_name_field->GetDeclaringClass(), element_file_field->GetType<false>()); |
| 8656 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8657 | // Fill the elements array. |
| 8658 | int32_t index = 0; |
| 8659 | for (const DexFile* dex_file : dex_files) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 8660 | StackHandleScope<4> hs2(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8661 | |
Mathieu Chartier | 966878d | 2016-01-14 14:33:29 -0800 | [diff] [blame] | 8662 | // CreatePathClassLoader is only used by gtests. Index 0 of h_long_array is supposed to be the |
| 8663 | // oat file but we can leave it null. |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 8664 | Handle<mirror::LongArray> h_long_array = hs2.NewHandle(mirror::LongArray::Alloc( |
| 8665 | self, |
| 8666 | kDexFileIndexStart + 1)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8667 | DCHECK(h_long_array != nullptr); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 8668 | h_long_array->Set(kDexFileIndexStart, reinterpret_cast<intptr_t>(dex_file)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8669 | |
| 8670 | Handle<mirror::Object> h_dex_file = hs2.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8671 | cookie_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8672 | DCHECK(h_dex_file != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8673 | cookie_field->SetObject<false>(h_dex_file.Get(), h_long_array.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8674 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 8675 | Handle<mirror::String> h_file_name = hs2.NewHandle( |
| 8676 | mirror::String::AllocFromModifiedUtf8(self, dex_file->GetLocation().c_str())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8677 | DCHECK(h_file_name != nullptr); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 8678 | file_name_field->SetObject<false>(h_dex_file.Get(), h_file_name.Get()); |
| 8679 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8680 | Handle<mirror::Object> h_element = hs2.NewHandle(h_dex_element_class->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8681 | DCHECK(h_element != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8682 | element_file_field->SetObject<false>(h_element.Get(), h_dex_file.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8683 | |
| 8684 | h_dex_elements->Set(index, h_element.Get()); |
| 8685 | index++; |
| 8686 | } |
| 8687 | DCHECK_EQ(index, h_dex_elements->GetLength()); |
| 8688 | |
| 8689 | // Create DexPathList. |
| 8690 | Handle<mirror::Object> h_dex_path_list = hs.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8691 | dex_elements_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8692 | DCHECK(h_dex_path_list != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8693 | // Set elements. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8694 | 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] | 8695 | |
| 8696 | // Create PathClassLoader. |
| 8697 | Handle<mirror::Class> h_path_class_class = hs.NewHandle( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 8698 | soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8699 | Handle<mirror::Object> h_path_class_loader = hs.NewHandle( |
| 8700 | h_path_class_class->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8701 | DCHECK(h_path_class_loader != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8702 | // Set DexPathList. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8703 | ArtField* path_list_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8704 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8705 | DCHECK(path_list_field != nullptr); |
| 8706 | 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] | 8707 | |
| 8708 | // Make a pretend boot-classpath. |
| 8709 | // TODO: Should we scan the image? |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8710 | ArtField* const parent_field = |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8711 | mirror::Class::FindField(self, |
| 8712 | h_path_class_loader->GetClass(), |
| 8713 | "parent", |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8714 | "Ljava/lang/ClassLoader;"); |
Roland Levillain | f39c9eb | 2015-05-26 15:02:07 +0100 | [diff] [blame] | 8715 | DCHECK(parent_field != nullptr); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 8716 | ObjPtr<mirror::Object> boot_cl = |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 8717 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader)->AllocObject(self); |
Mathieu Chartier | 966878d | 2016-01-14 14:33:29 -0800 | [diff] [blame] | 8718 | parent_field->SetObject<false>(h_path_class_loader.Get(), boot_cl); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8719 | |
| 8720 | // Make it a global ref and return. |
| 8721 | ScopedLocalRef<jobject> local_ref( |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 8722 | soa.Env(), soa.Env()->AddLocalReference<jobject>(h_path_class_loader.Get())); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8723 | return soa.Env()->NewGlobalRef(local_ref.get()); |
| 8724 | } |
| 8725 | |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 8726 | void ClassLinker::DropFindArrayClassCache() { |
| 8727 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
| 8728 | find_array_class_cache_next_victim_ = 0; |
| 8729 | } |
| 8730 | |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 8731 | void ClassLinker::ClearClassTableStrongRoots() const { |
| 8732 | Thread* const self = Thread::Current(); |
| 8733 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 8734 | for (const ClassLoaderData& data : class_loaders_) { |
| 8735 | if (data.class_table != nullptr) { |
| 8736 | data.class_table->ClearStrongRoots(); |
| 8737 | } |
| 8738 | } |
| 8739 | } |
| 8740 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 8741 | void ClassLinker::VisitClassLoaders(ClassLoaderVisitor* visitor) const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8742 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 8743 | for (const ClassLoaderData& data : class_loaders_) { |
Mathieu Chartier | 4843bd5 | 2015-10-01 17:08:44 -0700 | [diff] [blame] | 8744 | // Need to use DecodeJObject so that we get null for cleared JNI weak globals. |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 8745 | ObjPtr<mirror::ClassLoader> class_loader = ObjPtr<mirror::ClassLoader>::DownCast( |
| 8746 | self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8747 | if (class_loader != nullptr) { |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 8748 | visitor->Visit(class_loader.Ptr()); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8749 | } |
| 8750 | } |
| 8751 | } |
| 8752 | |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 8753 | void ClassLinker::InsertDexFileInToClassLoader(ObjPtr<mirror::Object> dex_file, |
| 8754 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8755 | DCHECK(dex_file != nullptr); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8756 | Thread* const self = Thread::Current(); |
| 8757 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 8758 | ClassTable* const table = ClassTableForClassLoader(class_loader.Ptr()); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8759 | DCHECK(table != nullptr); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 8760 | if (table->InsertStrongRoot(dex_file) && class_loader != nullptr) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8761 | // It was not already inserted, perform the write barrier to let the GC know the class loader's |
| 8762 | // class table was modified. |
| 8763 | Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader); |
| 8764 | } |
| 8765 | } |
| 8766 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 8767 | void ClassLinker::CleanupClassLoaders() { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8768 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8769 | std::vector<ClassLoaderData> to_delete; |
| 8770 | // Do the delete outside the lock to avoid lock violation in jit code cache. |
| 8771 | { |
| 8772 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 8773 | for (auto it = class_loaders_.begin(); it != class_loaders_.end(); ) { |
| 8774 | const ClassLoaderData& data = *it; |
| 8775 | // Need to use DecodeJObject so that we get null for cleared JNI weak globals. |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 8776 | ObjPtr<mirror::ClassLoader> class_loader = |
| 8777 | ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8778 | if (class_loader != nullptr) { |
| 8779 | ++it; |
| 8780 | } else { |
| 8781 | VLOG(class_linker) << "Freeing class loader"; |
| 8782 | to_delete.push_back(data); |
| 8783 | it = class_loaders_.erase(it); |
| 8784 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8785 | } |
| 8786 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8787 | for (ClassLoaderData& data : to_delete) { |
| 8788 | DeleteClassLoader(self, data); |
| 8789 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8790 | } |
| 8791 | |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 8792 | class GetResolvedClassesVisitor : public ClassVisitor { |
| 8793 | public: |
| 8794 | GetResolvedClassesVisitor(std::set<DexCacheResolvedClasses>* result, bool ignore_boot_classes) |
| 8795 | : result_(result), |
| 8796 | ignore_boot_classes_(ignore_boot_classes), |
| 8797 | last_resolved_classes_(result->end()), |
| 8798 | last_dex_file_(nullptr), |
| 8799 | vlog_is_on_(VLOG_IS_ON(class_linker)), |
| 8800 | extra_stats_(), |
| 8801 | last_extra_stats_(extra_stats_.end()) { } |
| 8802 | |
| 8803 | bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8804 | if (!klass->IsProxyClass() && |
| 8805 | !klass->IsArrayClass() && |
| 8806 | klass->IsResolved() && |
| 8807 | !klass->IsErroneousResolved() && |
| 8808 | (!ignore_boot_classes_ || klass->GetClassLoader() != nullptr)) { |
| 8809 | const DexFile& dex_file = klass->GetDexFile(); |
| 8810 | if (&dex_file != last_dex_file_) { |
| 8811 | last_dex_file_ = &dex_file; |
| 8812 | DexCacheResolvedClasses resolved_classes(dex_file.GetLocation(), |
| 8813 | dex_file.GetBaseLocation(), |
| 8814 | dex_file.GetLocationChecksum()); |
| 8815 | last_resolved_classes_ = result_->find(resolved_classes); |
| 8816 | if (last_resolved_classes_ == result_->end()) { |
| 8817 | last_resolved_classes_ = result_->insert(resolved_classes).first; |
| 8818 | } |
| 8819 | } |
| 8820 | bool added = last_resolved_classes_->AddClass(klass->GetDexTypeIndex()); |
| 8821 | if (UNLIKELY(vlog_is_on_) && added) { |
| 8822 | const DexCacheResolvedClasses* resolved_classes = std::addressof(*last_resolved_classes_); |
| 8823 | if (last_extra_stats_ == extra_stats_.end() || |
| 8824 | last_extra_stats_->first != resolved_classes) { |
| 8825 | last_extra_stats_ = extra_stats_.find(resolved_classes); |
| 8826 | if (last_extra_stats_ == extra_stats_.end()) { |
| 8827 | last_extra_stats_ = |
| 8828 | extra_stats_.emplace(resolved_classes, ExtraStats(dex_file.NumClassDefs())).first; |
| 8829 | } |
| 8830 | } |
| 8831 | } |
| 8832 | } |
| 8833 | return true; |
| 8834 | } |
| 8835 | |
| 8836 | void PrintStatistics() const { |
| 8837 | if (vlog_is_on_) { |
| 8838 | for (const DexCacheResolvedClasses& resolved_classes : *result_) { |
| 8839 | auto it = extra_stats_.find(std::addressof(resolved_classes)); |
| 8840 | DCHECK(it != extra_stats_.end()); |
| 8841 | const ExtraStats& extra_stats = it->second; |
| 8842 | LOG(INFO) << "Dex location " << resolved_classes.GetDexLocation() |
| 8843 | << " has " << resolved_classes.GetClasses().size() << " / " |
| 8844 | << extra_stats.number_of_class_defs_ << " resolved classes"; |
| 8845 | } |
| 8846 | } |
| 8847 | } |
| 8848 | |
| 8849 | private: |
| 8850 | struct ExtraStats { |
| 8851 | explicit ExtraStats(uint32_t number_of_class_defs) |
| 8852 | : number_of_class_defs_(number_of_class_defs) {} |
| 8853 | uint32_t number_of_class_defs_; |
| 8854 | }; |
| 8855 | |
| 8856 | std::set<DexCacheResolvedClasses>* result_; |
| 8857 | bool ignore_boot_classes_; |
| 8858 | std::set<DexCacheResolvedClasses>::iterator last_resolved_classes_; |
| 8859 | const DexFile* last_dex_file_; |
| 8860 | |
| 8861 | // Statistics. |
| 8862 | bool vlog_is_on_; |
| 8863 | std::map<const DexCacheResolvedClasses*, ExtraStats> extra_stats_; |
| 8864 | std::map<const DexCacheResolvedClasses*, ExtraStats>::iterator last_extra_stats_; |
| 8865 | }; |
| 8866 | |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8867 | std::set<DexCacheResolvedClasses> ClassLinker::GetResolvedClasses(bool ignore_boot_classes) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 8868 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8869 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 8870 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8871 | std::set<DexCacheResolvedClasses> ret; |
| 8872 | VLOG(class_linker) << "Collecting resolved classes"; |
| 8873 | const uint64_t start_time = NanoTime(); |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 8874 | GetResolvedClassesVisitor visitor(&ret, ignore_boot_classes); |
| 8875 | VisitClasses(&visitor); |
| 8876 | if (VLOG_IS_ON(class_linker)) { |
| 8877 | visitor.PrintStatistics(); |
| 8878 | LOG(INFO) << "Collecting class profile took " << PrettyDuration(NanoTime() - start_time); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8879 | } |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8880 | return ret; |
| 8881 | } |
| 8882 | |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 8883 | std::unordered_set<std::string> ClassLinker::GetClassDescriptorsForResolvedClasses( |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8884 | const std::set<DexCacheResolvedClasses>& classes) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 8885 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8886 | std::unordered_set<std::string> ret; |
| 8887 | Thread* const self = Thread::Current(); |
| 8888 | std::unordered_map<std::string, const DexFile*> location_to_dex_file; |
| 8889 | ScopedObjectAccess soa(self); |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 8890 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 8891 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8892 | for (const ClassLinker::DexCacheData& data : GetDexCachesData()) { |
| 8893 | if (!self->IsJWeakCleared(data.weak_root)) { |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 8894 | ObjPtr<mirror::DexCache> dex_cache = soa.Decode<mirror::DexCache>(data.weak_root); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8895 | if (dex_cache != nullptr) { |
| 8896 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 8897 | // There could be duplicates if two dex files with the same location are mapped. |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 8898 | location_to_dex_file.emplace(dex_file->GetLocation(), dex_file); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8899 | } |
| 8900 | } |
| 8901 | } |
| 8902 | for (const DexCacheResolvedClasses& info : classes) { |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 8903 | const std::string& location = info.GetDexLocation(); |
| 8904 | auto found = location_to_dex_file.find(location); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8905 | if (found != location_to_dex_file.end()) { |
| 8906 | const DexFile* dex_file = found->second; |
| 8907 | VLOG(profiler) << "Found opened dex file for " << dex_file->GetLocation() << " with " |
| 8908 | << info.GetClasses().size() << " classes"; |
| 8909 | DCHECK_EQ(dex_file->GetLocationChecksum(), info.GetLocationChecksum()); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 8910 | for (dex::TypeIndex type_idx : info.GetClasses()) { |
Jeff Hao | 54b5855 | 2016-11-16 15:15:04 -0800 | [diff] [blame] | 8911 | const DexFile::TypeId& type_id = dex_file->GetTypeId(type_idx); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8912 | const char* descriptor = dex_file->GetTypeDescriptor(type_id); |
| 8913 | ret.insert(descriptor); |
| 8914 | } |
| 8915 | } else { |
Mathieu Chartier | 046854b | 2017-03-01 17:16:22 -0800 | [diff] [blame] | 8916 | VLOG(class_linker) << "Failed to find opened dex file for location " << location; |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8917 | } |
| 8918 | } |
| 8919 | return ret; |
| 8920 | } |
| 8921 | |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8922 | class ClassLinker::FindVirtualMethodHolderVisitor : public ClassVisitor { |
| 8923 | public: |
| 8924 | FindVirtualMethodHolderVisitor(const ArtMethod* method, PointerSize pointer_size) |
| 8925 | : method_(method), |
| 8926 | pointer_size_(pointer_size) {} |
| 8927 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8928 | bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8929 | if (klass->GetVirtualMethodsSliceUnchecked(pointer_size_).Contains(method_)) { |
| 8930 | holder_ = klass; |
| 8931 | } |
| 8932 | // Return false to stop searching if holder_ is not null. |
| 8933 | return holder_ == nullptr; |
| 8934 | } |
| 8935 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8936 | ObjPtr<mirror::Class> holder_ = nullptr; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8937 | const ArtMethod* const method_; |
| 8938 | const PointerSize pointer_size_; |
| 8939 | }; |
| 8940 | |
| 8941 | mirror::Class* ClassLinker::GetHoldingClassOfCopiedMethod(ArtMethod* method) { |
| 8942 | ScopedTrace trace(__FUNCTION__); // Since this function is slow, have a trace to notify people. |
| 8943 | CHECK(method->IsCopied()); |
| 8944 | FindVirtualMethodHolderVisitor visitor(method, image_pointer_size_); |
| 8945 | VisitClasses(&visitor); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8946 | return visitor.holder_.Ptr(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8947 | } |
| 8948 | |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 8949 | // Instantiate ResolveMethod. |
| 8950 | template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::kForceICCECheck>( |
| 8951 | const DexFile& dex_file, |
| 8952 | uint32_t method_idx, |
| 8953 | Handle<mirror::DexCache> dex_cache, |
| 8954 | Handle<mirror::ClassLoader> class_loader, |
| 8955 | ArtMethod* referrer, |
| 8956 | InvokeType type); |
| 8957 | template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::kNoICCECheckForCache>( |
| 8958 | const DexFile& dex_file, |
| 8959 | uint32_t method_idx, |
| 8960 | Handle<mirror::DexCache> dex_cache, |
| 8961 | Handle<mirror::ClassLoader> class_loader, |
| 8962 | ArtMethod* referrer, |
| 8963 | InvokeType type); |
| 8964 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8965 | } // namespace art |